llm_config.py Source File

llm_config.py Source File#

Mobilint SDK qb Compiler: llm_config.py Source File
Mobilint SDK qb Compiler v1.0
MCS002-EN
llm_config.py
1from typing import List, Optional
2from typeguard import typechecked
3from . import ConfigABC
4
5
6
12class LlmConfig(ConfigABC):
13 """
14 @brief Configuration for Large Language Model (LLM) compilation.
15 """
16
17 optional = True
18 DEFAULTS = {
19 "llm_config_apply": False,
20 "max_data_length": 4096,
21 "max_sequence_length": 4096,
22 "max_cache_length": 4096,
23 "max_core_data_length": 128,
24 "random_seq_length": 80,
25 "use_full_seq_length": False,
26 "use_global_core": False,
27 "batch_size": 1,
28 "npu_core_ids": [0],
29 "dynamic_rope": False,
30 }
31
32 py_to_cpp_name = {
33 "llm_config_apply": "apply",
34 "max_data_length": "maxDataLength",
35 "max_sequence_length": "maxSequenceLength",
36 "max_cache_length": "maxCacheLength",
37 "max_core_data_length": "maxCoreDataLength",
38 "random_seq_length": "randomSeqLength",
39 "use_full_seq_length": "useFullSeqLength",
40 "use_global_core": "useGlobalCore",
41 "batch_size": "batchSize",
42 "dynamic_rope": "dynamicRope",
43 }
44
45 min_max_check = {
46 "max_data_length": [0, None],
47 "max_sequence_length": [0, None],
48 "max_cache_length": [0, None],
49 "max_core_data_length": [0, None],
50 "random_seq_length": [0, None],
51 "batch_size": [0, None],
52 }
53
55 self,
56 llm_config_apply: bool,
57 max_data_length: int,
58 max_sequence_length: int,
59 max_cache_length: int,
60 max_core_data_length: int,
61 random_seq_length: int,
62 use_full_seq_length: bool,
63 use_global_core: bool,
64 batch_size: int,
65 npu_core_ids: List[int],
66 dynamic_rope: bool,
67 optional: bool = optional,
68 ):
69 """
70 @brief Initialize the LlmConfig:
71 @param llm_config_apply bool. If True, apply LLM-specific configurations.
72 @param max_data_length int. Maximum data length.
73 @param max_sequence_length int. Maximum sequence length.
74 @param max_cache_length int. Maximum cache length.
75 @param max_core_data_length int. Maximum core data length.
76 @param random_seq_length int. Random sequence length used for calibration.
77 @param use_full_seq_length bool. If True, use the full sequence length for calibration.
78 @param use_global_core bool. If True, use a global core.
79 @param batch_size int. Batch size.
80 @param npu_core_ids List[int]. List of NPU core IDs.
81 @param dynamic_rope bool. If True, enable dynamic RoPE.
82 @param optional bool. Indicates whether this config is optional. Use the default value defined as a class variable.
83 """
84 super().__init__(optional)
85 self.llm_config_apply = llm_config_apply
86 self.max_data_length = max_data_length
87 self.max_sequence_length = max_sequence_length
88 self.max_cache_length = max_cache_length
89 self.max_core_data_length = max_core_data_length
90 self.random_seq_length = random_seq_length
91 self.use_full_seq_length = use_full_seq_length
92 self.use_global_core = use_global_core
93 self.batch_size = batch_size
94 self.npu_core_ids = npu_core_ids
95 self.dynamic_rope = dynamic_rope
96 self.check_valid(except_list=["optional", "npu_core_ids"])
97
98 @classmethod
99 def default_config(cls):
100 return cls(**cls.DEFAULTS, optional=cls.optional)
101
102 @classmethod
103 def from_kwargs(cls, **kwargs):
104 params = cls.DEFAULTS.copy()
105 updated_kwargs = dict()
106 for key in cls.DEFAULTS.keys():
107 if key in kwargs:
108 updated_kwargs[key] = kwargs[key]
109 params[key] = kwargs[key]
110
111 try:
112 return cls(**params, optional=cls.optional)
113 except:
114 raise ValueError(
115 f"Please review the configuration kwargs passed to {cls.__name__}. These kwargs were updated and will be used: {updated_kwargs}"
116 )
117
118 @classmethod
119 def from_dict(cls, config_dict: dict):
120 """Create LlmConfig from dictionary (JSON structure)"""
121 params = cls.DEFAULTS.copy()
122
123 if "llm_config_apply" in config_dict:
124 params["llm_config_apply"] = config_dict["apply"]
125
126 if "attributes" in config_dict:
127 attributes = config_dict["attributes"]
128
129 if "maxDataLength" in attributes:
130 params["max_data_length"] = attributes["maxDataLength"]
131 if "maxSequenceLength" in attributes:
132 params["max_sequence_length"] = attributes["maxSequenceLength"]
133 if "maxCacheLength" in attributes:
134 params["max_cache_length"] = attributes["maxCacheLength"]
135 if "maxCoreDataLength" in attributes:
136 params["max_core_data_length"] = attributes["maxCoreDataLength"]
137
138 if "calibration" in attributes:
139 calibration = attributes["calibration"]
140 if "randomSeqLength" in calibration:
141 params["random_seq_length"] = calibration["randomSeqLength"]
142 if "useFullSeqLength" in calibration:
143 params["use_full_seq_length"] = calibration["useFullSeqLength"]
144
145 if "runtime" in attributes:
146 runtime = attributes["runtime"]
147 if "useGlobalCore" in runtime:
148 params["use_global_core"] = runtime["useGlobalCore"]
149 if "batchSize" in runtime:
150 params["batch_size"] = runtime["batchSize"]
151 if "npuCoreIds" in runtime:
152 params["npu_core_ids"] = runtime["npuCoreIds"]
153 if "dynamicRope" in runtime:
154 params["dynamic_rope"] = runtime["dynamicRope"]
155
156 return cls(**params)
157
158 def to_dict(self):
159 return {
160 "llmConfig": {
161 "apply": self.llm_config_apply,
162 "attributes": {
163 "maxDataLength": self.max_data_length,
164 "maxSequenceLength": self.max_sequence_length,
165 "maxCacheLength": self.max_cache_length,
166 "maxCoreDataLength": self.max_core_data_length,
167 "calibration": {
168 "randomSeqLength": self.random_seq_length,
169 "useFullSeqLength": self.use_full_seq_length,
170 },
171 "runtime": {
172 "useGlobalCore": self.use_global_core,
173 "batchSize": self.batch_size,
174 "npuCoreIds": self.npu_core_ids,
175 "dynamicRope": self.dynamic_rope,
176 },
177 "debug": {
178 "apply": False,
179 "batchDebugBundleSize": 0,
180 },
181 },
182 }
183 }
184
185
186def get_llm_config(**kwargs) -> LlmConfig:
187 """
188 @brief Create LlmConfig with partial parameters merged with defaults.
189 """
190 params = LlmConfig.DEFAULTS.copy()
191 params.update(kwargs)
192 return LlmConfig(**params, optional=LlmConfig.optional)
193
194
195# @}
Configuration for Large Language Model (LLM) compilation.
Definition llm_config.py:12
__init__(self, bool llm_config_apply, int max_data_length, int max_sequence_length, int max_cache_length, int max_core_data_length, int random_seq_length, bool use_full_seq_length, bool use_global_core, int batch_size, List[int] npu_core_ids, bool dynamic_rope, bool optional=optional)
Initialize the LlmConfig:
Definition llm_config.py:68
from_dict(cls, dict config_dict)
Create LlmConfig from dictionary (JSON structure)
LlmConfig get_llm_config(**kwargs)
Create LlmConfig with partial parameters merged with defaults.