-
Notifications
You must be signed in to change notification settings - Fork 129
Fix Incorrect Hidden State Extraction with Right Padding #26
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -310,8 +310,19 @@ def generate_latent_batch( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| past = outputs.past_key_values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| e_t = outputs.hidden_states[0][:, -1, :] # [B, D] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| last_hidden = outputs.hidden_states[-1][:, -1, :] # [B, D] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # If past_key_values is None, we might have padding. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if past_key_values is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Identify last token index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # attention_mask (at this point, if past is None, it is just original mask) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| last_token_indices = attention_mask.sum(1) - 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| batch_indices = torch.arange(input_ids.shape[0], device=self.device) |
Copilot
AI
Dec 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation inside this if block appears to use 5 spaces instead of the standard 4 spaces used throughout the rest of the codebase. Lines 315-320 should be indented with 12 spaces (8 base + 4 for the if block) rather than 13 spaces.
| # Identify last token index | |
| # attention_mask (at this point, if past is None, it is just original mask) | |
| last_token_indices = attention_mask.sum(1) - 1 | |
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| e_t = outputs.hidden_states[0][batch_indices, last_token_indices, :] | |
| last_hidden = outputs.hidden_states[-1][batch_indices, last_token_indices, :] | |
| else: | |
| # Assume no padding in incremental decoding steps | |
| e_t = outputs.hidden_states[0][:, -1, :] | |
| last_hidden = outputs.hidden_states[-1][:, -1, :] | |
| # Identify last token index | |
| # attention_mask (at this point, if past is None, it is just original mask) | |
| last_token_indices = attention_mask.sum(1) - 1 | |
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| e_t = outputs.hidden_states[0][batch_indices, last_token_indices, :] | |
| last_hidden = outputs.hidden_states[-1][batch_indices, last_token_indices, :] | |
| else: | |
| # Assume no padding in incremental decoding steps | |
| e_t = outputs.hidden_states[0][:, -1, :] | |
| last_hidden = outputs.hidden_states[-1][:, -1, :] |
Copilot
AI
Dec 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation inside this if block appears to use 5 spaces instead of the standard 4 spaces used throughout the rest of the codebase. Lines 323-324 should be indented with 12 spaces (8 base + 4 for the else block) rather than 13 spaces.
| # Identify last token index | |
| # attention_mask (at this point, if past is None, it is just original mask) | |
| last_token_indices = attention_mask.sum(1) - 1 | |
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| e_t = outputs.hidden_states[0][batch_indices, last_token_indices, :] | |
| last_hidden = outputs.hidden_states[-1][batch_indices, last_token_indices, :] | |
| else: | |
| # Assume no padding in incremental decoding steps | |
| e_t = outputs.hidden_states[0][:, -1, :] | |
| last_hidden = outputs.hidden_states[-1][:, -1, :] | |
| # Identify last token index | |
| # attention_mask (at this point, if past is None, it is just original mask) | |
| last_token_indices = attention_mask.sum(1) - 1 | |
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| e_t = outputs.hidden_states[0][batch_indices, last_token_indices, :] | |
| last_hidden = outputs.hidden_states[-1][batch_indices, last_token_indices, :] | |
| else: | |
| # Assume no padding in incremental decoding steps | |
| e_t = outputs.hidden_states[0][:, -1, :] | |
| last_hidden = outputs.hidden_states[-1][:, -1, :] |
Copilot
AI
Dec 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For clarity and consistency with how attention_mask is handled, batch_indices should be created using self.HF_device instead of input_ids.device. While input_ids.device should be the same as self.HF_device (otherwise the model call would fail), using self.HF_device explicitly makes the device management more clear and matches the pattern used for attention_mask at line 375.
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| batch_indices = torch.arange(input_ids.shape[0], device=self.HF_device) |
Copilot
AI
Dec 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The attention_mask sum could include padding tokens (zeros) in the mask. If a sequence is completely padded (all zeros in attention_mask), then attention_mask.sum(1) would be 0, resulting in last_token_indices being -1. While this is technically a valid Python index (referring to the last element), it would give incorrect behavior for completely padded sequences. Consider adding validation or handling for this edge case.
| last_token_indices = attention_mask.sum(1) - 1 | |
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| last_hidden = outputs.hidden_states[-1][batch_indices, last_token_indices, :] | |
| seq_lengths = attention_mask.sum(1) | |
| # Handle fully padded sequences (sum == 0) to avoid negative indices (-1) | |
| if torch.any(seq_lengths == 0): | |
| seq_lengths = seq_lengths.clone() | |
| seq_lengths[seq_lengths == 0] = 1 | |
| last_token_indices = seq_lengths - 1 | |
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| last_hidden = outputs.hidden_states[-1][batch_indices, last_token_indices, :] |
Copilot
AI
Dec 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation inside this else block appears to use 5 spaces instead of the standard 4 spaces used throughout the rest of the codebase. Line 402 should be indented with 12 spaces (8 base + 4 for the else block) rather than 13 spaces.
| last_token_indices = attention_mask.sum(1) - 1 | |
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| last_hidden = outputs.hidden_states[-1][batch_indices, last_token_indices, :] | |
| else: | |
| last_hidden = outputs.hidden_states[-1][:, -1, :] | |
| last_token_indices = attention_mask.sum(1) - 1 | |
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| last_hidden = outputs.hidden_states[-1][batch_indices, last_token_indices, :] | |
| else: | |
| last_hidden = outputs.hidden_states[-1][:, -1, :] |
Copilot
AI
Dec 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation inside this if block appears to use 5 spaces instead of the standard 4 spaces used throughout the rest of the codebase. Lines 398-400 should be indented with 12 spaces (8 base + 4 for the if block) rather than 13 spaces.
| last_token_indices = attention_mask.sum(1) - 1 | |
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| last_hidden = outputs.hidden_states[-1][batch_indices, last_token_indices, :] | |
| else: | |
| last_hidden = outputs.hidden_states[-1][:, -1, :] | |
| last_token_indices = attention_mask.sum(1) - 1 | |
| batch_indices = torch.arange(input_ids.shape[0], device=input_ids.device) | |
| last_hidden = outputs.hidden_states[-1][batch_indices, last_token_indices, :] | |
| else: | |
| last_hidden = outputs.hidden_states[-1][:, -1, :] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The attention_mask sum could include padding tokens (zeros) in the mask. If a sequence is completely padded (all zeros in attention_mask), then attention_mask.sum(1) would be 0, resulting in last_token_indices being -1. While this is technically a valid Python index (referring to the last element), it would give incorrect behavior for completely padded sequences. Consider adding validation or handling for this edge case.