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

Inconsistent hidden_states in inference #29428

Closed
xxyyzztlb opened this issue Mar 4, 2024 · 2 comments
Closed

Inconsistent hidden_states in inference #29428

xxyyzztlb opened this issue Mar 4, 2024 · 2 comments

Comments

@xxyyzztlb
Copy link

I want to get the tensor of the last layer in the inference stage. The batch_size is 1. I use the following two methods:
1:
model = LlamaForCausalLM.from_pretrained(args.base_model, torch_dtype=torch.bfloat16, low_cpu_mem_usage=True, device_map=device_map, )

output = model.module.generate(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"], max_new_tokens=10, prefix_allowed_tokens_fn=prefix_allowed_tokens, num_beams=20, num_return_sequences=20, output_scores=True, return_dict_in_generate=True, early_stopping=True, output_hidden_states=True )

last_output = output['hidden_states'][0][-1][0, -1, :]

2:
model = LlamaForCausalLM.from_pretrained(args.base_model, torch_dtype=torch.bfloat16, low_cpu_mem_usage=True, device_map=device_map, )

outputs = model(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"], output_hidden_states=True)

last_output = outputs.hidden_states[-1][0, -1, :]

I found that the tensor values ​​obtained are different. I'd like to know what are the potential reasons.

@zucchini-nlp
Copy link
Member

Hey @xxyyzztlb. Please take a look at this comment. TL;DR: we cannot guarantee exactly same values for hidden states, due to numerical precision errors. Given that your code snippet is using float16, the error might be even higher than float32

Copy link

github-actions bot commented Apr 4, 2024

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants