-
Notifications
You must be signed in to change notification settings - Fork 27.9k
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
RoPE: Add numerical tests ✨ #32380
Merged
Merged
RoPE: Add numerical tests ✨ #32380
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,7 +150,7 @@ def _compute_dynamic_ntk_parameters( | |
attention_factor = 1.0 # Unused in this type of RoPE | ||
|
||
# seq_len: default to max_position_embeddings, e.g. at init time | ||
seq_len = seq_len if seq_len is not None else max_position_embeddings | ||
seq_len = seq_len if seq_len is not None and seq_len > max_position_embeddings else max_position_embeddings | ||
|
||
# Compute the inverse frequencies | ||
base = base * ((factor * seq_len / max_position_embeddings) - (factor - 1)) ** (dim / (dim - 2)) | ||
|
@@ -210,23 +210,28 @@ def find_correction_range(low_rot, high_rot, dim, base, max_position_embeddings) | |
high = math.ceil(find_correction_dim(high_rot, dim, base, max_position_embeddings)) | ||
return max(low, 0), min(high, dim - 1) | ||
|
||
def linear_ramp_mask(min, max, dim): | ||
def linear_ramp_factor(min, max, dim): | ||
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. "mask" is a bad name for this function and related variables -- the results are not a mask (i.e. binary array), but an array with |
||
if min == max: | ||
max += 0.001 # Prevent singularity | ||
|
||
linear_func = (torch.arange(dim, dtype=torch.float32) - min) / (max - min) | ||
ramp_func = torch.clamp(linear_func, 0, 1) | ||
return ramp_func | ||
|
||
# Note on variable naming: "interpolation" comes from the original technique, where we interpolate the position IDs | ||
# to expand the possible context length. In other words, interpolation = apply scaling factor. | ||
pos_freqs = base ** (torch.arange(0, dim, 2).float().to(device) / dim) | ||
inv_freq_extrapolation = 1.0 / pos_freqs | ||
inv_freq_interpolation = 1.0 / (factor * pos_freqs) | ||
|
||
low, high = find_correction_range(beta_fast, beta_slow, dim, base, max_position_embeddings) | ||
|
||
# Get n-dimensional rotational scaling corrected for extrapolation | ||
inv_freq_mask = 1 - linear_ramp_mask(low, high, dim // 2).float().to(device) | ||
inv_freq = inv_freq_interpolation * (1 - inv_freq_mask) + inv_freq_extrapolation * inv_freq_mask | ||
inv_freq_extrapolation_factor = 1 - linear_ramp_factor(low, high, dim // 2).float().to(device) | ||
inv_freq = ( | ||
inv_freq_interpolation * (1 - inv_freq_extrapolation_factor) | ||
+ inv_freq_extrapolation * inv_freq_extrapolation_factor | ||
) | ||
|
||
return inv_freq, attention_factor | ||
|
||
|
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
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.
missing corner case: we don't want to scale down from the original context length (we would lose modeling performance with nothing to gain in exchange)