-
Notifications
You must be signed in to change notification settings - Fork 39
Refactor attention mask and bias handling for efficiency #177
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
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -241,16 +241,12 @@ def forward( | |||||||||||
| dt_states = self.dt_proj( | ||||||||||||
| value_states.transpose(1, 2).reshape(value_states.shape[0], value_states.shape[-2], -1) | ||||||||||||
| ) | ||||||||||||
| dt_states = torch.exp(self.A * F.softplus(dt_states)).transpose(-1, -2) | ||||||||||||
| attn_bias = dt_states[:, :, None, :].expand( | ||||||||||||
| -1, -1, hidden_states.shape[1], -1 | ||||||||||||
| ).to(hidden_states.dtype) # [batch_size, num_heads, query_len, key_len] | ||||||||||||
| attn_bias = torch.exp(self.A * F.softplus(dt_states)).transpose(-1, -2).to(hidden_states.dtype) | ||||||||||||
|
|
||||||||||||
| attention_interface: Callable = eager_attention_forward | ||||||||||||
| if flash_dynamic_mask_attention_forward is not None: | ||||||||||||
| attention_interface = flash_dynamic_mask_attention_forward | ||||||||||||
|
|
||||||||||||
| attention_mask = attention_mask.expand(-1, attn_bias.shape[1], -1, -1) if attention_mask is not None else None # attention_mask: batch, num_kv_heads, query_len, key_len | ||||||||||||
| attn_output, attn_weights = attention_interface( | ||||||||||||
| self, | ||||||||||||
| query_states, | ||||||||||||
|
|
@@ -414,7 +410,7 @@ def _init_weights(self, module): | |||||||||||
| super()._init_weights(module) | ||||||||||||
| if isinstance(module, DogeAttention): | ||||||||||||
| if hasattr(module, "A"): | ||||||||||||
|
||||||||||||
| if hasattr(module, "A"): | |
| if hasattr(module, "A"): | |
| # Initialize module.A with a normal distribution for better convergence. | |
| # Zero initialization was considered, but normal initialization empirically improves stability and performance in this attention mechanism. | |
| # See: [Add reference or empirical result if available] |
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 removal of the tensor expansion operation may cause shape mismatch issues. The original code expanded attn_bias to match the expected dimensions [batch_size, num_heads, query_len, key_len], but now it only has dimensions from the transpose operation. This could lead to broadcasting errors in subsequent attention computations.