-
Notifications
You must be signed in to change notification settings - Fork 121
issue/1180 refactor(nn): decouple RoPE scaling logic with polymorphic interfaces #1181
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
Merged
Merged
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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| #pragma once | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| namespace infinicore::nn { | ||
|
|
||
| /** | ||
| * @brief Abstract base class for RoPE scaling strategies. | ||
| * Uses polymorphism to eliminate type checking (if-else) in the core RoPE loop. | ||
| */ | ||
| class RopeScalingConfig { | ||
| public: | ||
| virtual ~RopeScalingConfig() = default; | ||
|
|
||
| /** | ||
| * @brief Calculate the frequency scaling factor for a specific position and dimension. | ||
| * | ||
| * @param pos Current sequence position | ||
| * @param dim_idx Current dimension index (0 to head_dim/2 - 1) | ||
| * @param base_inv_freq Pre-computed base inverse frequency for this dimension (1.0 / theta^(2j/head_dim)) | ||
| * @return Frequency scaling factor (default 1.0) | ||
| */ | ||
| virtual float get_freq_scale(size_t pos, size_t dim_idx, float base_inv_freq) const { | ||
| return 1.0f; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Calculate the magnitude scaling factor for a specific position and dimension. | ||
| * | ||
| * @param pos Current sequence position | ||
| * @param dim_idx Current dimension index (0 to head_dim/2 - 1) | ||
| * @param base_inv_freq Pre-computed base inverse frequency for this dimension | ||
| * @return Magnitude scaling factor (default 1.0) | ||
| */ | ||
| virtual float get_magnitude_scale(size_t pos, size_t dim_idx, float base_inv_freq) const { | ||
| return 1.0f; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @brief LongRoPE scaling configuration. | ||
| */ | ||
| class LongRopeScalingConfig : public RopeScalingConfig { | ||
| public: | ||
| LongRopeScalingConfig( | ||
| std::vector<float> short_factor, | ||
| std::vector<float> long_factor, | ||
| size_t original_max_position_embeddings, | ||
| float factor = 1.0f); | ||
|
|
||
| float get_freq_scale(size_t pos, size_t dim_idx, float base_inv_freq) const override; | ||
| float get_magnitude_scale(size_t pos, size_t dim_idx, float base_inv_freq) const override; | ||
|
|
||
| size_t original_max_position_embeddings() const { return original_max_position_embeddings_; } | ||
| const std::vector<float> &short_factor() const { return short_factor_; } | ||
| const std::vector<float> &long_factor() const { return long_factor_; } | ||
| float factor() const { return factor_; } | ||
|
|
||
| private: | ||
| std::vector<float> short_factor_; | ||
| std::vector<float> long_factor_; | ||
| size_t original_max_position_embeddings_; | ||
| float factor_; | ||
| }; | ||
|
|
||
| // TODO(rubik) implement in cpp | ||
| /** | ||
| * @brief Llama3 frequency-aware RoPE scaling configuration. | ||
| * Native support for Llama 3.1 RoPE scaling (smooth interpolation based on wavelength). | ||
| */ | ||
| class Llama3RopeScalingConfig : public RopeScalingConfig { | ||
| public: | ||
| Llama3RopeScalingConfig( | ||
| float factor, | ||
| float low_freq_factor, | ||
| float high_freq_factor, | ||
| size_t original_max_position_embeddings); | ||
|
|
||
| float get_freq_scale(size_t pos, size_t dim_idx, float base_inv_freq) const override; | ||
|
|
||
| // Llama3 does not use magnitude scaling, so it inherits the default get_magnitude_scale() returning 1.0f | ||
|
|
||
| private: | ||
| float factor_; | ||
| float low_freq_factor_; | ||
| float high_freq_factor_; | ||
| size_t original_max_position_embeddings_; | ||
| }; | ||
|
|
||
| } // namespace infinicore::nn |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #include "infinicore/nn/rope_scaling_configs.hpp" | ||
| #include <cmath> | ||
| #include <stdexcept> | ||
|
|
||
| namespace infinicore::nn { | ||
|
|
||
| // LongRopeScalingConfig Implementation | ||
| LongRopeScalingConfig::LongRopeScalingConfig( | ||
| std::vector<float> short_factor, | ||
| std::vector<float> long_factor, | ||
| size_t original_max_position_embeddings, | ||
| float factor) | ||
| : short_factor_(std::move(short_factor)), | ||
| long_factor_(std::move(long_factor)), | ||
| original_max_position_embeddings_(original_max_position_embeddings), | ||
| factor_(factor == 1.0f ? 1.0f : std::sqrt(1 + std::log(factor) / std::log(original_max_position_embeddings))) {} | ||
|
|
||
| float LongRopeScalingConfig::get_freq_scale(size_t pos, size_t dim_idx, float base_inv_freq) const { | ||
| float _ext = (pos < original_max_position_embeddings_) ? short_factor_[dim_idx] : long_factor_[dim_idx]; | ||
| // The base inv_freq is multiplied by this scale. | ||
| // Original: inv_freq = 1.0f / (_ext * pow(theta, 2j/head_dim)) | ||
| // New: inv_freq = base_inv_freq * (1.0f / _ext) | ||
| return 1.0f / _ext; | ||
| } | ||
|
|
||
| float LongRopeScalingConfig::get_magnitude_scale(size_t pos, size_t dim_idx, float base_inv_freq) const { | ||
| return factor_; | ||
| } | ||
|
|
||
| // TODO(rubik) llama3 implement here | ||
| // Llama3RopeScalingConfig Implementation | ||
| Llama3RopeScalingConfig::Llama3RopeScalingConfig( | ||
| float factor, | ||
| float low_freq_factor, | ||
| float high_freq_factor, | ||
| size_t original_max_position_embeddings) | ||
| : factor_(factor), | ||
| low_freq_factor_(low_freq_factor), | ||
| high_freq_factor_(high_freq_factor), | ||
| original_max_position_embeddings_(original_max_position_embeddings) {} | ||
|
|
||
| float Llama3RopeScalingConfig::get_freq_scale(size_t pos, size_t dim_idx, float base_inv_freq) const { | ||
| return 1.0f; | ||
| } | ||
|
|
||
| } // namespace infinicore::nn |
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.
Uh oh!
There was an error while loading. Please reload this page.