Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stateless llama fixes #95

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions examples/llama2_inference/stateless_llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ def run_initialize(
token, *state = self.initialize(x, constraints=init_const)
updates = []
self.global_seq_step = IREE.tensor_dim(
state[0], 3
) # 3rd dimension of arbitrarily 0th kv tensor
state[0], 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh crap. Yeah. Good catch

) # 2nd dimension of arbitrarily 0th kv tensor
for i in range(HEADS * 2):
slice_of_state = IREE.tensor_reshape(
state[i], 1, 1, HEADS, self.global_seq_step, HIDDEN_DIM
Expand All @@ -305,16 +305,23 @@ def run_forward(self, x=AbstractTensor(1, None, dtype=torch.int64)):
token, *state_update = self.forward(
x, *state_arg, constraints=forw_const
)
self.global_seq_step = self.global_seq_step + 1
res = update_state(
self.global_state,
state_update,
self.global_seq_step,
HEADS,
HIDDEN_DIM,
)
self.global_seq_step = self.global_seq_step + 1

return token

def get_global_state(self):
return self.global_state

def get_seq_step(self):
return self.global_seq_step

@jittable
def initialize(input_ids):
result = mod.forward(input_ids)
Expand Down Expand Up @@ -391,24 +398,40 @@ def run_vmfb_comparison(args):
initial_input = tokenizer(prompt, return_tensors="pt")
example_input_id = initial_input.input_ids
device_inputs = [ireert.asdevicearray(config.device, example_input_id)]

step0 = ModuleCompiled["get_seq_step"]()
print("step0 :"+str(step0))
results = ModuleCompiled["run_initialize"](*device_inputs)

pkv = ModuleCompiled["get_global_state"]().to_host()
step = ModuleCompiled["get_seq_step"]()
print(f"step: {step}")
sliced = pkv[0,:,:,:step,:]

def format_out(results):
return torch.tensor(results.to_host()[0][0])

print(tokenizer.decode(format_out(results)))
for i in range(100):
# print(tokenizer.decode(format_out(results)))
for i in range(10):
results = ModuleCompiled["run_forward"](results)
step = ModuleCompiled["get_seq_step"]()
print(f"step: {step}")
print(tokenizer.decode(format_out(results)))
model = InferenceModel(args)


def get_token_from_logits(logits):
return torch.argmax(logits[:, -1, :], dim=1)

base_model_results = model.base_model.forward(example_input_id)
base_model_token = get_token_from_logits(base_model_results.logits)
print(tokenizer.decode(base_model_token))
# print(tokenizer.decode(base_model_token))

matcher = base_model_results.past_key_values[0][0]
print(sliced)
print(matcher)
print(sliced.shape)
print(matcher.shape)

if __name__ == "__main__":
args = parser.parse_args()
Expand Down
Loading