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

Fix Training Error with AdapterDrop and Prefix Tuning #673

Merged
merged 3 commits into from
Apr 12, 2024
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
10 changes: 8 additions & 2 deletions src/adapters/models/t5/modeling_t5.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def forward(
if past_key_value is not None:
assert (
len(past_key_value) == 2
), f"past_key_value should have 2 past states: keys and values. Got { len(past_key_value)} past states"
), f"past_key_value should have 2 past states: keys and values. Got {len(past_key_value)} past states"
real_seq_length += past_key_value[0].shape[2] if query_length is None else query_length

key_length = real_seq_length if key_value_states is None else key_value_states.shape[1]
Expand Down Expand Up @@ -144,7 +144,13 @@ def project(hidden_states, proj_layer, key_value_states, past_key_value):
query_states, key_states.transpose(3, 2)
) # equivalent of torch.einsum("bnqd,bnkd->bnqk", query_states, key_states), compatible with onnx op>9

if position_bias is None:
# For Prefix Tuning, when training with AdapterDrop, we must additionally check that the sequence lengths of
# both positional encoding and the scores account for the prefix tokens.
# This is because the positional encoding is calculated only once in the beginning and then used for all layers.
# However, if the encoding was calculated without the prefix tokens due to AdapterDrop having dropped an
# adapter layer in the beginning, the positional encoding will be shorter than the scores, resulting in a
# dimension mismatch when adding the positional encoding to the scores.
if position_bias is None or position_bias.shape[3] != scores.shape[3]:
if not self.has_relative_attention_bias:
position_bias = torch.zeros(
(1, self.n_heads, real_seq_length, key_length), device=scores.device, dtype=scores.dtype
Expand Down
Loading