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

Allow MPT models to return attention weights #599

Merged
merged 15 commits into from
Sep 21, 2023
Merged
2 changes: 2 additions & 0 deletions llmfoundry/models/layers/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def forward(
attn_bias: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.ByteTensor] = None,
is_causal: bool = True,
output_attentions: bool = False,
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[
torch.Tensor, torch.Tensor]]]:
a = self.norm_1(x)
Expand All @@ -100,6 +101,7 @@ def forward(
attn_bias=attn_bias,
attention_mask=attention_mask,
is_causal=is_causal,
needs_weights=output_attentions,
)
x = x + self.resid_attn_dropout(b)
m = x
Expand Down
1 change: 1 addition & 0 deletions llmfoundry/models/mpt/modeling_mpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ def forward(
attn_bias=attn_bias,
attention_mask=attention_mask,
is_causal=self.is_causal,
output_attentions=bool(output_attentions),
)
if past_key_values is not None:
past_key_values[b_idx] = past_key_value
Expand Down
1 change: 1 addition & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,7 @@ def test_forward_with_output_attentions_and_output_hidden_states(

if output_attentions:
assert len(outputs.attentions) == n_layers
assert all(attn.shape == (1, 4, 2048, 2048) for attn in outputs.attentions)
dakinggg marked this conversation as resolved.
Show resolved Hide resolved
if output_hidden_states:
assert len(outputs.hidden_states) == n_layers + 1

Expand Down