llm_config.py Source File

llm_config.py Source File#

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