-
Notifications
You must be signed in to change notification settings - Fork 71
issue/401 refactor(rope): add scaling factory and TP-safe RoPE cache #402
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
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,66 @@ | ||
| #include "../../config/model_config.hpp" | ||
| #include "infinicore/nn/rope_scaling_configs.hpp" | ||
| #include "rotary_embedding_factory.hpp" | ||
| #include <vector> | ||
|
|
||
| namespace infinilm::layers::rotary_embedding { | ||
| namespace { | ||
| /** | ||
| * @brief Default creator for types that apply no scaling. | ||
| * Returns nullptr, which the InfiniCore RoPE layer interprets as a 1.0x pass-through. | ||
| */ | ||
| std::shared_ptr<infinicore::nn::RopeScalingConfig> | ||
| create_default_scaling_config(const std::shared_ptr<config::ModelConfig> &) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| // TODO(rubik) create_dynamic_scaling | ||
|
|
||
| /** | ||
| * @brief Creator function for LongRoPE scaling configuration. | ||
| * Extracts 'short_factor', 'long_factor', etc., from the model config. | ||
| */ | ||
| std::shared_ptr<infinicore::nn::RopeScalingConfig> | ||
| create_longrope_config(const std::shared_ptr<config::ModelConfig> &cfg) { | ||
| const auto &rope_scaling = cfg->get_config_json()["rope_scaling"]; | ||
|
|
||
| // Required fields for LongRopeConfig | ||
| if (!rope_scaling.contains("short_factor") || !rope_scaling.contains("long_factor") || !rope_scaling.contains("original_max_position_embeddings")) { | ||
| throw std::runtime_error( | ||
| "LongRopeConfig requires 'short_factor', 'long_factor', and 'original_max_position_embeddings'"); | ||
| } | ||
|
|
||
| auto short_factor = rope_scaling["short_factor"].get<std::vector<float>>(); | ||
| auto long_factor = rope_scaling["long_factor"].get<std::vector<float>>(); | ||
| size_t original_max_position_embeddings = rope_scaling["original_max_position_embeddings"].get<size_t>(); | ||
|
|
||
| float factor = 1.0f; | ||
| if (rope_scaling.contains("factor")) { | ||
| factor = rope_scaling["factor"].get<float>(); | ||
| } | ||
|
|
||
| return std::make_shared<infinicore::nn::LongRopeScalingConfig>( | ||
| std::move(short_factor), | ||
| std::move(long_factor), | ||
| original_max_position_embeddings, | ||
| factor); | ||
| } | ||
|
|
||
| // Future scaling creators go here (e.g., create_llama3, create_linear) | ||
|
|
||
| } // anonymous namespace | ||
|
|
||
| // Static self-registration block | ||
| // Registers creator functions into the factory registry upon program startup. | ||
| static bool _registered = []() { | ||
| auto ®istry = get_scaling_registry(); | ||
| registry["default"] = create_default_scaling_config; | ||
| registry["none"] = create_default_scaling_config; | ||
| registry["dynamic"] = create_default_scaling_config; | ||
| registry["longrope"] = create_longrope_config; | ||
| // add new scaling | ||
| // registry["llama3"] = create_llama3_scaling; | ||
| return true; | ||
| }(); | ||
|
|
||
| } // namespace infinilm::layers::rotary_embedding | ||
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 |
|---|---|---|
| @@ -1,17 +1,23 @@ | ||
| #pragma once | ||
|
|
||
| #include "../../config/model_config.hpp" | ||
| #include "infinicore/nn/rope.hpp" | ||
| #include <memory> | ||
|
|
||
| namespace infinilm::layers::rotary_embedding { | ||
| namespace infinilm::config { | ||
| class ModelConfig; // Forward declaration | ||
| } | ||
|
|
||
| // Compute the actual number of dimensions involved in rotary position embedding. | ||
| // For partial rotation, the dimension is clamped to [2, head_dim] and must be even. | ||
| size_t get_rotary_dim(size_t head_dim, double partial_rotary_factor); | ||
| namespace infinilm::layers::rotary_embedding { | ||
|
|
||
| std::shared_ptr<infinicore::nn::RoPE> get_rope(const std::shared_ptr<infinilm::config::ModelConfig> &model_config, | ||
| const infinicore::Device &device, | ||
| infinicore::nn::RoPE::Algo algo = infinicore::nn::RoPE::Algo::GPT_NEOX); | ||
| /** | ||
| * @brief Public API to assemble and construct a complete RoPE module. | ||
| * | ||
| * @param model_config Model configuration. | ||
| * @param device Device to create the cache on. | ||
| * @param algo RoPE algorithm type (default: Algo::GPT_NEOX). | ||
| */ | ||
| std::shared_ptr<infinicore::nn::RoPE> | ||
| get_rope(const std::shared_ptr<infinilm::config::ModelConfig> &model_config, | ||
| const infinicore::Device &device); | ||
|
|
||
| } // namespace infinilm::layers::rotary_embedding |
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,42 @@ | ||
| #include "rotary_embedding_factory.hpp" | ||
| #include "../../config/model_config.hpp" | ||
| #include <stdexcept> | ||
|
|
||
| namespace infinilm::layers::rotary_embedding { | ||
|
|
||
| std::unordered_map<std::string, ScalingCreator> &get_scaling_registry() { | ||
| static std::unordered_map<std::string, ScalingCreator> registry; | ||
| return registry; | ||
| } | ||
|
|
||
| std::shared_ptr<infinicore::nn::RopeScalingConfig> | ||
| make_scaling_config(const std::shared_ptr<config::ModelConfig> &model_config) { | ||
| if (!model_config || !model_config->get_config_json().contains("rope_scaling") || model_config->get_config_json()["rope_scaling"].is_null()) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| const auto &rope_scaling = model_config->get_config_json()["rope_scaling"]; | ||
| if (!rope_scaling.is_object()) { | ||
| throw std::runtime_error("rope_scaling must be an object"); | ||
| } | ||
|
|
||
| std::string scaling_type; | ||
| if (rope_scaling.contains("type")) { | ||
| scaling_type = rope_scaling["type"].get<std::string>(); | ||
| } else if (rope_scaling.contains("rope_type")) { | ||
| scaling_type = rope_scaling["rope_type"].get<std::string>(); | ||
| } else { | ||
| throw std::runtime_error("rope_scaling must contain 'type' or 'rope_type' field"); | ||
| } | ||
|
|
||
| // Registry routing: delegate construction to the specific creator | ||
| auto ®istry = get_scaling_registry(); | ||
| auto it = registry.find(scaling_type); | ||
| if (it != registry.end()) { | ||
| return it->second(model_config); | ||
| } | ||
|
|
||
| throw std::runtime_error("Unsupported rope_scaling_type: " + scaling_type); | ||
| } | ||
|
|
||
| } // namespace infinilm::layers::rotary_embedding |
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,35 @@ | ||
| #pragma once | ||
|
|
||
| #include "infinicore/nn/rope.hpp" | ||
| #include "infinicore/nn/rope_scaling_configs.hpp" | ||
| #include <functional> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <unordered_map> | ||
|
|
||
| namespace infinilm::config { | ||
| class ModelConfig; // Forward declaration | ||
| } | ||
|
|
||
| namespace infinilm::layers::rotary_embedding { | ||
|
|
||
| /** | ||
| * @brief Function pointer type for creating specific RopeScalingConfig instances. | ||
| * Implementations should extract parameters from ModelConfig and construct the corresponding Config object. | ||
| */ | ||
| using ScalingCreator = std::function<std::shared_ptr<infinicore::nn::RopeScalingConfig>( | ||
| const std::shared_ptr<infinilm::config::ModelConfig> &)>; | ||
|
|
||
| /** | ||
| * @brief Get the singleton registry mapping scaling type strings to their creator functions. | ||
| */ | ||
| std::unordered_map<std::string, ScalingCreator> &get_scaling_registry(); | ||
|
rubik-hua marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * @brief Factory method to create a RopeScalingConfig based on the ModelConfig. | ||
| * Routes the "rope_scaling_type" string to the corresponding registered creator. | ||
| */ | ||
| std::shared_ptr<infinicore::nn::RopeScalingConfig> | ||
| make_scaling_config(const std::shared_ptr<infinilm::config::ModelConfig> &model_config); | ||
|
|
||
| } // namespace infinilm::layers::rotary_embedding | ||
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
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.