|
1 | 1 | #include "rotary_embedding.hpp" |
2 | 2 | #include "../../config/model_config.hpp" |
3 | 3 | #include "rotary_embedding_factory.hpp" |
| 4 | +#include <iomanip> |
4 | 5 | #include <memory> |
| 6 | +#include <sstream> |
5 | 7 | #include <string> |
| 8 | +#include <typeinfo> |
| 9 | +#include <unordered_map> |
6 | 10 |
|
7 | 11 | namespace infinilm::layers::rotary_embedding { |
8 | 12 |
|
| 13 | +namespace { |
9 | 14 | // Cache dictionary to avoid redundant allocations of RoPE instances. |
10 | 15 | // thread_local ensures it is only visible within this compilation unit. |
11 | 16 | thread_local std::unordered_map<std::string, std::shared_ptr<infinicore::nn::RoPE>> _ROPE_DICT; |
12 | 17 |
|
13 | | -std::shared_ptr<infinicore::nn::RoPE> |
14 | | -get_rope(const std::shared_ptr<infinilm::config::ModelConfig> &model_config, |
15 | | - const infinicore::Device &device) { |
| 18 | +std::string make_cache_key(size_t head_dim, |
| 19 | + size_t rotary_dim, |
| 20 | + size_t max_position_embeddings, |
| 21 | + double rope_theta, |
| 22 | + infinicore::nn::RoPE::Algo algo, |
| 23 | + const infinicore::DataType &dtype, |
| 24 | + const infinicore::Device &device, |
| 25 | + const std::shared_ptr<infinicore::nn::RopeScalingConfig> &scaling, |
| 26 | + const std::optional<std::vector<int>> &mrope_section, |
| 27 | + bool mrope_interleaved) { |
| 28 | + std::ostringstream oss; |
| 29 | + oss << "scaling_" << (scaling ? typeid(*scaling).name() : "none") |
| 30 | + << "_head_" << head_dim |
| 31 | + << "_rotary_" << rotary_dim |
| 32 | + << "_max_" << max_position_embeddings |
| 33 | + << "_theta_" << std::setprecision(17) << rope_theta |
| 34 | + << "_algo_" << static_cast<int>(algo) |
| 35 | + << "_dtype_" << static_cast<int>(dtype) |
| 36 | + << "_dev" << device.toString(); |
16 | 37 |
|
17 | | - // 1. Compute the actual rotary dimension |
18 | | - size_t rotary_dim = model_config->get_rotary_dim(); |
19 | | - size_t head_dim = model_config->get_head_dim(); |
20 | | - |
21 | | - // 2. Resolve scaling config via the internal factory |
22 | | - auto scaling = make_scaling_config(model_config); |
23 | | - |
24 | | - // 3. Cache key must include rotary_dim AND the actual scaling type |
25 | | - // to avoid reusing the same RoPE instance across models with different settings |
26 | | - // (Enhancement: dynamically determine scaling_type instead of hardcoding "default") |
27 | | - std::string scaling_type_str = "default"; |
28 | | - if (scaling) { |
29 | | - // Assuming we can get the type string from the JSON for cache key generation, |
30 | | - // or ideally, ScalingConfig should have a virtual std::string type_name() const method. |
31 | | - // Here we read it from JSON for the cache key purpose only, keeping it decoupled from InfiniCore. |
32 | | - const auto &rope_scaling_json = model_config->get_config_json()["rope_scaling"]; |
33 | | - if (rope_scaling_json.contains("type")) { |
34 | | - scaling_type_str = rope_scaling_json["type"].get<std::string>(); |
35 | | - } else if (rope_scaling_json.contains("rope_type")) { |
36 | | - scaling_type_str = rope_scaling_json["rope_type"].get<std::string>(); |
| 38 | + if (mrope_section.has_value()) { |
| 39 | + oss << "_mrope"; |
| 40 | + for (const auto section : mrope_section.value()) { |
| 41 | + oss << '_' << section; |
37 | 42 | } |
| 43 | + oss << "_mrope_interleaved_" << (mrope_interleaved ? 1 : 0); |
38 | 44 | } |
| 45 | + return oss.str(); |
| 46 | +} |
| 47 | + |
| 48 | +} // namespace |
39 | 49 |
|
40 | | - std::string cache_key = scaling_type_str + "_rope_dim_" + std::to_string(rotary_dim) |
41 | | - + "_dev_" + device.toString(); |
| 50 | +std::shared_ptr<infinicore::nn::RoPE> |
| 51 | +get_rope(size_t head_dim, |
| 52 | + size_t rotary_dim, |
| 53 | + size_t max_position_embeddings, |
| 54 | + double rope_theta, |
| 55 | + infinicore::nn::RoPE::Algo algo, |
| 56 | + const infinicore::DataType &dtype, |
| 57 | + const infinicore::Device &device, |
| 58 | + std::shared_ptr<infinicore::nn::RopeScalingConfig> scaling, |
| 59 | + std::optional<std::vector<int>> mrope_section, |
| 60 | + bool mrope_interleaved) { |
| 61 | + const auto cache_key = make_cache_key(head_dim, |
| 62 | + rotary_dim, |
| 63 | + max_position_embeddings, |
| 64 | + rope_theta, |
| 65 | + algo, |
| 66 | + dtype, |
| 67 | + device, |
| 68 | + scaling, |
| 69 | + mrope_section, |
| 70 | + mrope_interleaved); |
42 | 71 | auto it = _ROPE_DICT.find(cache_key); |
43 | 72 | if (it != _ROPE_DICT.end()) { |
44 | 73 | return it->second; |
45 | 74 | } |
46 | 75 |
|
| 76 | + auto rope = std::make_shared<infinicore::nn::RoPE>(head_dim, |
| 77 | + rotary_dim, |
| 78 | + max_position_embeddings, |
| 79 | + rope_theta, |
| 80 | + algo, |
| 81 | + dtype, |
| 82 | + device, |
| 83 | + scaling, |
| 84 | + mrope_section, |
| 85 | + mrope_interleaved); |
| 86 | + _ROPE_DICT.emplace(cache_key, rope); |
| 87 | + return rope; |
| 88 | +} |
| 89 | + |
| 90 | +std::shared_ptr<infinicore::nn::RoPE> |
| 91 | +get_rope(const std::shared_ptr<infinilm::config::ModelConfig> &model_config, |
| 92 | + const infinicore::Device &device) { |
| 93 | + |
| 94 | + size_t rotary_dim = model_config->get_rotary_dim(); |
| 95 | + size_t head_dim = model_config->get_head_dim(); |
| 96 | + auto scaling = make_scaling_config(model_config); |
| 97 | + |
47 | 98 | const auto &dtype = model_config->get_dtype(); |
48 | 99 | size_t max_position_embeddings = model_config->get<size_t>("max_position_embeddings"); |
49 | 100 | double rope_theta = model_config->get<double>("rope_theta"); |
50 | | - |
51 | 101 | infinicore::nn::RoPE::Algo algo = model_config->get_rope_algo(); |
52 | | - auto rope = std::make_shared<infinicore::nn::RoPE>(head_dim, rotary_dim, max_position_embeddings, rope_theta, |
53 | | - algo, dtype, device, scaling); |
54 | 102 |
|
55 | | - _ROPE_DICT.emplace(cache_key, rope); |
56 | | - return rope; |
| 103 | + return get_rope(head_dim, |
| 104 | + rotary_dim, |
| 105 | + max_position_embeddings, |
| 106 | + rope_theta, |
| 107 | + algo, |
| 108 | + dtype, |
| 109 | + device, |
| 110 | + scaling, |
| 111 | + std::nullopt, |
| 112 | + false); |
57 | 113 | } |
58 | 114 |
|
59 | 115 | } // namespace infinilm::layers::rotary_embedding |
0 commit comments