-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
🚨 Llama: update rope scaling to match static cache changes #29143
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,6 @@ def forward(self, hidden_states): | |
class LlamaRotaryEmbedding(nn.Module): | ||
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None): | ||
super().__init__() | ||
|
||
self.dim = dim | ||
self.max_position_embeddings = max_position_embeddings | ||
self.base = base | ||
|
@@ -118,6 +117,9 @@ def cos_cached(self): | |
return self._cos_cached | ||
|
||
def forward(self, x, position_ids, seq_len=None): | ||
if seq_len is not None: | ||
logger.warning_once("The `seq_len` argument is deprecated and unused. It will be removed in v4.40.") | ||
|
||
# x: [bs, num_attention_heads, seq_len, head_size] | ||
inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1) | ||
position_ids_expanded = position_ids[:, None, :].float() | ||
|
@@ -138,16 +140,11 @@ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, s | |
self.scaling_factor = scaling_factor | ||
super().__init__(dim, max_position_embeddings, base, device) | ||
|
||
def _set_cos_sin_cache(self, seq_len, device, dtype): | ||
self.max_seq_len_cached = seq_len | ||
t = torch.arange(self.max_seq_len_cached, device=device, dtype=torch.int64).type_as(self.inv_freq) | ||
t = t / self.scaling_factor | ||
|
||
freqs = torch.outer(t, self.inv_freq) | ||
# Different from paper, but it uses a different permutation in order to obtain the same calculation | ||
emb = torch.cat((freqs, freqs), dim=-1) | ||
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False) | ||
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False) | ||
def forward(self, x, position_ids, seq_len=None): | ||
# difference to the original RoPE: a scaling factor is aplied to the position ids | ||
position_ids = position_ids.float() / self.scaling_factor | ||
cos, sin = super().forward(x, position_ids, seq_len) | ||
return cos, sin | ||
|
||
|
||
class LlamaDynamicNTKScalingRotaryEmbedding(LlamaRotaryEmbedding): | ||
|
@@ -157,23 +154,20 @@ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, s | |
self.scaling_factor = scaling_factor | ||
super().__init__(dim, max_position_embeddings, base, device) | ||
|
||
def _set_cos_sin_cache(self, seq_len, device, dtype): | ||
self.max_seq_len_cached = seq_len | ||
|
||
def forward(self, x, position_ids, seq_len=None): | ||
# difference to the original RoPE: inv_freq is recomputed when the sequence length > original length | ||
seq_len = torch.max(position_ids) + 1 | ||
if seq_len > self.max_position_embeddings: | ||
base = self.base * ( | ||
(self.scaling_factor * seq_len / self.max_position_embeddings) - (self.scaling_factor - 1) | ||
) ** (self.dim / (self.dim - 2)) | ||
inv_freq = 1.0 / (base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(device) / self.dim)) | ||
self.register_buffer("inv_freq", inv_freq, persistent=False) | ||
|
||
t = torch.arange(self.max_seq_len_cached, device=device, dtype=torch.int64).type_as(self.inv_freq) | ||
inv_freq = 1.0 / ( | ||
base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(x.device) / self.dim) | ||
) | ||
self.register_buffer("inv_freq", inv_freq, persistent=False) # TODO joao: this may break with compilation | ||
|
||
freqs = torch.outer(t, self.inv_freq) | ||
# Different from paper, but it uses a different permutation in order to obtain the same calculation | ||
emb = torch.cat((freqs, freqs), dim=-1) | ||
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False) | ||
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False) | ||
cos, sin = super().forward(x, position_ids, seq_len) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a lot cleaner! |
||
return cos, sin | ||
|
||
|
||
def rotate_half(x): | ||
|
@@ -183,17 +177,16 @@ def rotate_half(x): | |
return torch.cat((-x2, x1), dim=-1) | ||
|
||
|
||
def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1): | ||
def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): | ||
"""Applies Rotary Position Embedding to the query and key tensors. | ||
|
||
Args: | ||
q (`torch.Tensor`): The query tensor. | ||
k (`torch.Tensor`): The key tensor. | ||
cos (`torch.Tensor`): The cosine part of the rotary embedding. | ||
sin (`torch.Tensor`): The sine part of the rotary embedding. | ||
position_ids (`torch.Tensor`): | ||
The position indices of the tokens corresponding to the query and key tensors. For example, this can be | ||
used to pass offsetted position ids when working with a KV-cache. | ||
position_ids (`torch.Tensor`, *optional*): | ||
Deprecated and unused. | ||
unsqueeze_dim (`int`, *optional*, defaults to 1): | ||
The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and | ||
sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note | ||
|
@@ -360,8 +353,8 @@ def forward( | |
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) | ||
|
||
past_key_value = getattr(self, "past_key_value", past_key_value) | ||
cos, sin = self.rotary_emb(value_states, position_ids, seq_len=None) | ||
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, None) | ||
cos, sin = self.rotary_emb(value_states, position_ids) | ||
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin) | ||
|
||
if past_key_value is not None: | ||
# sin and cos are specific to RoPE models; position_ids needed for the static cache | ||
|
@@ -447,8 +440,8 @@ def forward( | |
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) | ||
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) | ||
|
||
cos, sin = self.rotary_emb(value_states, position_ids, seq_len=None) | ||
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, None) | ||
cos, sin = self.rotary_emb(value_states, position_ids) | ||
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin) | ||
|
||
past_key_value = getattr(self, "past_key_value", past_key_value) | ||
|
||
|
@@ -645,8 +638,8 @@ def forward( | |
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) | ||
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) | ||
|
||
cos, sin = self.rotary_emb(value_states, position_ids, seq_len=None) | ||
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, None) | ||
cos, sin = self.rotary_emb(value_states, position_ids) | ||
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin) | ||
|
||
past_key_value = getattr(self, "past_key_value", past_key_value) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -362,7 +362,6 @@ def test_save_load_fast_init_from_base(self): | |
pass | ||
|
||
@parameterized.expand([("linear",), ("dynamic",)]) | ||
@unittest.skip("TODO @gante fix this for Llama") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test was fixed as a result of the changes in this PR :) |
||
def test_model_rope_scaling(self, scaling_type): | ||
config, _ = self.model_tester.prepare_config_and_inputs_for_common() | ||
short_input = ids_tensor([1, 10], config.vocab_size) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I am alright with this but it is breaking for any libs that rely on sin cached and cos cached. Same for the static cache PR!
Let's just add a mention that it will be removed next release and still compute cos and sin!
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.
This is the cool part -- it calls super's forward, which in turn caches sin/cos (see here). BC is preserved 🙌
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.
Yes but we need a warning to deprecate !
Follow up is fine
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.
I'm not sure I follow -- the warning is here. Or were you thinking of some other warning?
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.
Perfect! Had not seen this when I checked the diff