equivalent_transformation_config.py Source File

equivalent_transformation_config.py Source File#

Mobilint SDK qb Compiler: equivalent_transformation_config.py Source File
Mobilint SDK qb Compiler v1.0
MCS002-KR
equivalent_transformation_config.py
1from typing import Dict, List, Optional
2from typeguard import typechecked
3from . import ConfigABC
4from .config_utils import value_range_check
5
6
12
13
15 """
16 @brief Configuration for equivalent transformation techniques.
17
18 @details This includes various transformation methods like NormConv, QK, UD, VO,
19 FeedForwardMultiLUT, SpinR1, HeadOutChRotation, SpinR2, QKRotation, and FlattenQuant.
20 """
21
22 optional = False
23 py_to_cpp_name = {}
24 # Empty py_to_cpp_name since this config uses nested structure in to_dict
25
26 # Default values shared between __init__ and from_dict
27 DEFAULTS = {
28 "seed": 0,
29 "apply_hadamard_rotation_matrix": True,
30 # NormConv
31 "norm_conv_apply": False,
32 "norm_conv_learn": False,
33 "norm_conv_smoothing_factor": 0.5,
34 "norm_conv_min_gamma": 0.0001,
35 "norm_conv_max_gamma": 10000.0,
36 # QK
37 "qk_apply": False,
38 "qk_smoothing_factor": 0.5,
39 "qk_min_gamma": 0.0001,
40 "qk_max_gamma": 10000.0,
41 # UD
42 "ud_apply": False,
43 "ud_learn": False,
44 "ud_smoothing_factor": 0.5,
45 "ud_min_gamma": 0.0001,
46 "ud_max_gamma": 10000.0,
47 # VO
48 "vo_apply": False,
49 "vo_smoothing_factor": 0.5,
50 "vo_min_gamma": 0.0001,
51 "vo_max_gamma": 10000.0,
52 # FeedForwardMultiLUT
53 "ff_multi_lut_apply": False,
54 "ff_multi_lut_breakpoints": [-8.0, -4.0, 0],
55 # SpinR1
56 "spin_r1_apply": False,
57 "spin_r1_matrix_path": "",
58 # HeadOutChRotation
59 "head_out_ch_rotation_apply": False,
60 "head_out_ch_rotation_matrix_path": "",
61 # SpinR2
62 "spin_r2_apply": False,
63 "spin_r2_learn": False,
64 "spin_r2_matrix_path": "",
65 # QKRotation
66 "qk_rotation_apply": False,
67 "qk_rotation_matrix_path": "",
68 # FlattenQuant
69 "flatten_quant_apply": False,
70 "flatten_quant_learn": False,
71 "flatten_quant_apply_threshold": 0.33,
72 "flatten_quant_max_overhead": 0.05,
73 # OptimizeFFN
74 "optimize_ffn_apply": False,
75 "optimize_ffn_ch_per_ffn": -1,
76 }
77
78 # Min/max range checks
79 min_max_check = {
80 "norm_conv_smoothing_factor": [0.0, 1.0],
81 "norm_conv_min_gamma": [0.0, None],
82 "norm_conv_max_gamma": [0.0, None],
83 "qk_smoothing_factor": [0.0, 1.0],
84 "qk_min_gamma": [0.0, None],
85 "qk_max_gamma": [0.0, None],
86 "ud_smoothing_factor": [0.0, 1.0],
87 "ud_min_gamma": [0.0, None],
88 "ud_max_gamma": [0.0, None],
89 "vo_smoothing_factor": [0.0, 1.0],
90 "vo_min_gamma": [0.0, None],
91 "vo_max_gamma": [0.0, None],
92 "flatten_quant_apply_threshold": [0.0, 1.0],
93 "flatten_quant_max_overhead": [0.0, None],
94 }
95
97 self,
98 seed: int,
99 apply_hadamard_rotation_matrix: bool,
100 # NormConv
101 norm_conv_apply: bool,
102 norm_conv_learn: bool,
103 norm_conv_smoothing_factor: float,
104 norm_conv_min_gamma: float,
105 norm_conv_max_gamma: float,
106 # QK
107 qk_apply: bool,
108 qk_smoothing_factor: float,
109 qk_min_gamma: float,
110 qk_max_gamma: float,
111 # UD
112 ud_apply: bool,
113 ud_learn: bool,
114 ud_smoothing_factor: float,
115 ud_min_gamma: float,
116 ud_max_gamma: float,
117 # VO
118 vo_apply: bool,
119 vo_smoothing_factor: float,
120 vo_min_gamma: float,
121 vo_max_gamma: float,
122 # FeedForwardMultiLUT
123 ff_multi_lut_apply: bool,
124 ff_multi_lut_breakpoints: List[float],
125 # SpinR1
126 spin_r1_apply: bool,
127 spin_r1_matrix_path: str,
128 # HeadOutChRotation
129 head_out_ch_rotation_apply: bool,
130 head_out_ch_rotation_matrix_path: str,
131 # SpinR2
132 spin_r2_apply: bool,
133 spin_r2_learn: bool,
134 spin_r2_matrix_path: str,
135 # QKRotation
136 qk_rotation_apply: bool,
137 qk_rotation_matrix_path: str,
138 # FlattenQuant
139 flatten_quant_apply: bool,
140 flatten_quant_learn: bool,
141 flatten_quant_apply_threshold: float,
142 flatten_quant_max_overhead: float,
143 # OptimizeFFN
144 optimize_ffn_apply: bool,
145 optimize_ffn_ch_per_ffn: int,
146 optional: bool = optional,
147 ):
148 """
149 @brief Initialize the EquivalentTransformationConfig.
150 @param seed int. Random seed used for equivalent transformation operations. Default is 0.
151 @param apply_hadamard_rotation_matrix bool. If True, apply the Hadamard rotation matrix where applicable. Default is True.
152
153 # NormConv
154 @param norm_conv_apply bool. If True, enable NormConv transformation. Default is False.
155 @param norm_conv_learn bool. If True, learn NormConv parameters during calibration/optimization. Default is False.
156 @param norm_conv_smoothing_factor float. Smoothing factor used in NormConv. Default is 0.5.
157 @param norm_conv_min_gamma float. Minimum gamma value (lower bound clamp) for NormConv. Default is 0.0001.
158 @param norm_conv_max_gamma float. Maximum gamma value (upper bound clamp) for NormConv. Default is 10000.0.
159
160 # QK
161 @param qk_apply bool. If True, enable QK transformation. Default is False.
162 @param qk_smoothing_factor float. Smoothing factor used in QK. Default is 0.5.
163 @param qk_min_gamma float. Minimum gamma value (lower bound clamp) for QK. Default is 0.0001.
164 @param qk_max_gamma float. Maximum gamma value (upper bound clamp) for QK. Default is 10000.0.
165
166 # UD
167 @param ud_apply bool. If True, enable UD transformation. Default is False.
168 @param ud_learn bool. If True, learn UD parameters during calibration/optimization. Default is False.
169 @param ud_smoothing_factor float. Smoothing factor used in UD. Default is 0.5.
170 @param ud_min_gamma float. Minimum gamma value (lower bound clamp) for UD. Default is 0.0001.
171 @param ud_max_gamma float. Maximum gamma value (upper bound clamp) for UD. Default is 10000.0.
172
173 # VO
174 @param vo_apply bool. If True, enable VO transformation. Default is False.
175 @param vo_smoothing_factor float. Smoothing factor used in VO. Default is 0.5.
176 @param vo_min_gamma float. Minimum gamma value (lower bound clamp) for VO. Default is 0.0001.
177 @param vo_max_gamma float. Maximum gamma value (upper bound clamp) for VO. Default is 10000.0.
178
179
180 # FeedForwardMultiLUT
181 @param ff_multi_lut_apply bool. If True, enable FeedForward Multi-LUT. Default is False.
182 @param ff_multi_lut_breakpoints List[float]. Breakpoints used for FeedForward Multi-LUT piecewise regions. Default is [-8.0, -4.0, 0].
183
184 # SpinR1
185 @param spin_r1_apply bool. If True, enable SpinR1 rotation. Default is False.
186 @param spin_r1_matrix_path str. Path to the SpinR1 rotation matrix file. Default is "".
187
188 # HeadOutChRotation
189 @param head_out_ch_rotation_apply bool. If True, enable HeadOutChRotation. Default is False.
190 @param head_out_ch_rotation_matrix_path str. Path to the HeadOutChRotation matrix file. Default is "".
191
192 # SpinR2
193 @param spin_r2_apply bool. If True, enable SpinR2 rotation. Default is False.
194 @param spin_r2_learn bool. If True, learn the SpinR2 rotation matrix/parameters during calibration/optimization. Default is False.
195 @param spin_r2_matrix_path str. Path to the SpinR2 rotation matrix file. Default is "".
196
197 # QKRotation
198 @param qk_rotation_apply bool. If True, enable QKRotation. Default is False.
199 @param qk_rotation_matrix_path str. Path to the QKRotation matrix file. Default is "".
200
201 # FlattenQuant
202 @param flatten_quant_apply bool. If True, enable FlattenQuant. Default is False.
203 @param flatten_quant_learn bool. If True, learn FlattenQuant parameters during calibration/optimization. Default is False.
204 @param flatten_quant_apply_threshold float. Threshold for applying FlattenQuant. Default is 0.33.
205 @param flatten_quant_max_overhead float. Maximum allowed overhead when applying FlattenQuant. Default is 0.05.
206
207 # OptimizeFFN
208 @param optimize_ffn_apply bool. If True, enable OptimizeFFN. Default is False.
209 @param optimize_ffn_ch_per_ffn int. Number of channels per FFN used when OptimizeFFN is enabled. Default is -1.
210
211 @param optional bool. Indicates whether this config is optional. Use the default value defined as a class variable.
212 """
213 super().__init__(optional)
214
215 self.seed = seed
216 self.apply_hadamard_rotation_matrix = apply_hadamard_rotation_matrix
217
218 # NormConv
219 self.norm_conv_apply = norm_conv_apply
220 self.norm_conv_learn = norm_conv_learn
221 self.norm_conv_smoothing_factor = norm_conv_smoothing_factor
222 self.norm_conv_min_gamma = norm_conv_min_gamma
223 self.norm_conv_max_gamma = norm_conv_max_gamma
224
225 # QK
226 self.qk_apply = qk_apply
227 self.qk_smoothing_factor = qk_smoothing_factor
228 self.qk_min_gamma = qk_min_gamma
229 self.qk_max_gamma = qk_max_gamma
230
231 # UD
232 self.ud_apply = ud_apply
233 self.ud_learn = ud_learn
234 self.ud_smoothing_factor = ud_smoothing_factor
235 self.ud_min_gamma = ud_min_gamma
236 self.ud_max_gamma = ud_max_gamma
237
238 # VO
239 self.vo_apply = vo_apply
240 self.vo_smoothing_factor = vo_smoothing_factor
241 self.vo_min_gamma = vo_min_gamma
242 self.vo_max_gamma = vo_max_gamma
243
244 # FeedForwardMultiLUT
245 self.ff_multi_lut_apply = ff_multi_lut_apply
246 self.ff_multi_lut_breakpoints = ff_multi_lut_breakpoints
247
248 # SpinR1
249 self.spin_r1_apply = spin_r1_apply
250 self.spin_r1_matrix_path = spin_r1_matrix_path
251
252 # HeadOutChRotation
253 self.head_out_ch_rotation_apply = head_out_ch_rotation_apply
254 self.head_out_ch_rotation_matrix_path = head_out_ch_rotation_matrix_path
255
256 # SpinR2
257 self.spin_r2_apply = spin_r2_apply
258 self.spin_r2_learn = spin_r2_learn
259 self.spin_r2_matrix_path = spin_r2_matrix_path
260
261 # QKRotation
262 self.qk_rotation_apply = qk_rotation_apply
263 self.qk_rotation_matrix_path = qk_rotation_matrix_path
264
265 # FlattenQuant
266 self.flatten_quant_apply = flatten_quant_apply
267 self.flatten_quant_learn = flatten_quant_learn
268 self.flatten_quant_apply_threshold = flatten_quant_apply_threshold
269 self.flatten_quant_max_overhead = flatten_quant_max_overhead
270
271 # OptimizeFFN
272 self.optimize_ffn_apply = optimize_ffn_apply
273 self.optimize_ffn_ch_per_ffn = optimize_ffn_ch_per_ffn
274
275 self.check_valid()
276
277 def check_valid(self, except_list=["optional"]):
278 """Override to skip py_to_cpp_name check (uses nested structure in to_dict)"""
279 self.check_min_max_range()
280 self.check_valid_str_configs()
281
282 @classmethod
283 def default_config(cls):
284 return cls(**cls.DEFAULTS, optional=cls.optional)
285
286 @classmethod
287 def from_dict(cls, config_dict: dict):
288 """Create EquivalentTransformationConfig from dictionary (JSON structure)"""
289 # Start with defaults and update with provided values
290 params = cls.DEFAULTS.copy()
291
292 # Top-level parameters
293 if "seed" in config_dict:
294 params["seed"] = config_dict["seed"]
295 if "applyHadamardRotationMatrix" in config_dict:
296 params["apply_hadamard_rotation_matrix"] = config_dict[
297 "applyHadamardRotationMatrix"
298 ]
299
300 # NormConv
301 if "NormConv" in config_dict:
302 norm_conv = config_dict["NormConv"]
303 if "apply" in norm_conv:
304 params["norm_conv_apply"] = norm_conv["apply"]
305 if "learn" in norm_conv:
306 params["norm_conv_learn"] = norm_conv["learn"]
307 if "smoothingFactor" in norm_conv:
308 params["norm_conv_smoothing_factor"] = norm_conv["smoothingFactor"]
309 if "minGamma" in norm_conv:
310 params["norm_conv_min_gamma"] = norm_conv["minGamma"]
311 if "maxGamma" in norm_conv:
312 params["norm_conv_max_gamma"] = norm_conv["maxGamma"]
313
314 # QK
315 if "QK" in config_dict:
316 qk = config_dict["QK"]
317 if "apply" in qk:
318 params["qk_apply"] = qk["apply"]
319 if "smoothingFactor" in qk:
320 params["qk_smoothing_factor"] = qk["smoothingFactor"]
321 if "minGamma" in qk:
322 params["qk_min_gamma"] = qk["minGamma"]
323 if "maxGamma" in qk:
324 params["qk_max_gamma"] = qk["maxGamma"]
325
326 # UD
327 if "UD" in config_dict:
328 ud = config_dict["UD"]
329 if "apply" in ud:
330 params["ud_apply"] = ud["apply"]
331 if "learn" in ud:
332 params["ud_learn"] = ud["learn"]
333 if "smoothingFactor" in ud:
334 params["ud_smoothing_factor"] = ud["smoothingFactor"]
335 if "minGamma" in ud:
336 params["ud_min_gamma"] = ud["minGamma"]
337 if "maxGamma" in ud:
338 params["ud_max_gamma"] = ud["maxGamma"]
339
340 # VO
341 if "VO" in config_dict:
342 vo = config_dict["VO"]
343 if "apply" in vo:
344 params["vo_apply"] = vo["apply"]
345 if "smoothingFactor" in vo:
346 params["vo_smoothing_factor"] = vo["smoothingFactor"]
347 if "minGamma" in vo:
348 params["vo_min_gamma"] = vo["minGamma"]
349 if "maxGamma" in vo:
350 params["vo_max_gamma"] = vo["maxGamma"]
351
352 # FeedForwardMultiLUT
353 if "FeedForwardMultiLUT" in config_dict:
354 ff_multi_lut = config_dict["FeedForwardMultiLUT"]
355 if "apply" in ff_multi_lut:
356 params["ff_multi_lut_apply"] = ff_multi_lut["apply"]
357 if "breakpoints" in ff_multi_lut:
358 params["ff_multi_lut_breakpoints"] = ff_multi_lut["breakpoints"]
359
360 # SpinR1
361 if "SpinR1" in config_dict:
362 spin_r1 = config_dict["SpinR1"]
363 if "apply" in spin_r1:
364 params["spin_r1_apply"] = spin_r1["apply"]
365 if "matrixPath" in spin_r1:
366 params["spin_r1_matrix_path"] = spin_r1["matrixPath"]
367
368 # HeadOutChRotation
369 if "HeadOutChRotation" in config_dict:
370 head_out_ch = config_dict["HeadOutChRotation"]
371 if "apply" in head_out_ch:
372 params["head_out_ch_rotation_apply"] = head_out_ch["apply"]
373 if "matrixPath" in head_out_ch:
374 params["head_out_ch_rotation_matrix_path"] = head_out_ch["matrixPath"]
375
376 # SpinR2
377 if "SpinR2" in config_dict:
378 spin_r2 = config_dict["SpinR2"]
379 if "apply" in spin_r2:
380 params["spin_r2_apply"] = spin_r2["apply"]
381 if "learn" in spin_r2:
382 params["spin_r2_learn"] = spin_r2["learn"]
383 if "matrixPath" in spin_r2:
384 params["spin_r2_matrix_path"] = spin_r2["matrixPath"]
385
386 # QKRotation
387 if "QKRotation" in config_dict:
388 qk_rotation = config_dict["QKRotation"]
389 if "apply" in qk_rotation:
390 params["qk_rotation_apply"] = qk_rotation["apply"]
391 if "matrixPath" in qk_rotation:
392 params["qk_rotation_matrix_path"] = qk_rotation["matrixPath"]
393
394 # FlattenQuant
395 if "FlattenQuant" in config_dict:
396 flatten_quant = config_dict["FlattenQuant"]
397 if "apply" in flatten_quant:
398 params["flatten_quant_apply"] = flatten_quant["apply"]
399 if "learn" in flatten_quant:
400 params["flatten_quant_learn"] = flatten_quant["learn"]
401 if "applyThreshold" in flatten_quant:
402 params["flatten_quant_apply_threshold"] = flatten_quant[
403 "applyThreshold"
404 ]
405 if "maxOverhead" in flatten_quant:
406 params["flatten_quant_max_overhead"] = flatten_quant["maxOverhead"]
407
408 # OptimizeFFN
409 if "OptimizeFFN" in config_dict:
410 optimize_ffn = config_dict["OptimizeFFN"]
411 if "apply" in optimize_ffn:
412 params["optimize_ffn_apply"] = optimize_ffn["apply"]
413 if "chPerFFN" in optimize_ffn:
414 params["optimize_ffn_ch_per_ffn"] = optimize_ffn["chPerFFN"]
415
416 return cls(**params)
417
418 def to_dict(self):
419 return {
420 "seed": self.seed,
421 "applyHadamardRotationMatrix": self.apply_hadamard_rotation_matrix,
422 "NormConv": {
423 "apply": self.norm_conv_apply,
424 "learn": self.norm_conv_learn,
425 "smoothingFactor": self.norm_conv_smoothing_factor,
426 "minGamma": self.norm_conv_min_gamma,
427 "maxGamma": self.norm_conv_max_gamma,
428 },
429 "QK": {
430 "apply": self.qk_apply,
431 "smoothingFactor": self.qk_smoothing_factor,
432 "minGamma": self.qk_min_gamma,
433 "maxGamma": self.qk_max_gamma,
434 },
435 "UD": {
436 "apply": self.ud_apply,
437 "learn": self.ud_learn,
438 "smoothingFactor": self.ud_smoothing_factor,
439 "minGamma": self.ud_min_gamma,
440 "maxGamma": self.ud_max_gamma,
441 },
442 "VO": {
443 "apply": self.vo_apply,
444 "smoothingFactor": self.vo_smoothing_factor,
445 "minGamma": self.vo_min_gamma,
446 "maxGamma": self.vo_max_gamma,
447 },
448 "FeedForwardMultiLUT": {
449 "apply": self.ff_multi_lut_apply,
450 "breakpoints": self.ff_multi_lut_breakpoints,
451 },
452 "SpinR1": {
453 "apply": self.spin_r1_apply,
454 "matrixPath": self.spin_r1_matrix_path,
455 },
456 "HeadOutChRotation": {
457 "apply": self.head_out_ch_rotation_apply,
458 "matrixPath": self.head_out_ch_rotation_matrix_path,
459 },
460 "SpinR2": {
461 "apply": self.spin_r2_apply,
462 "learn": self.spin_r2_learn,
463 "matrixPath": self.spin_r2_matrix_path,
464 },
465 "QKRotation": {
466 "apply": self.qk_rotation_apply,
467 "matrixPath": self.qk_rotation_matrix_path,
468 },
469 "FlattenQuant": {
470 "apply": self.flatten_quant_apply,
471 "learn": self.flatten_quant_learn,
472 "applyThreshold": self.flatten_quant_apply_threshold,
473 "maxOverhead": self.flatten_quant_max_overhead,
474 },
475 "OptimizeFFN": {
476 "apply": self.optimize_ffn_apply,
477 "chPerFFN": self.optimize_ffn_ch_per_ffn,
478 },
479 }
480
481
482def get_equivalent_transformation_config(**kwargs) -> EquivalentTransformationConfig:
483 """
484 @brief Create EquivalentTransformationConfig with partial parameters merged with defaults.
485 """
486 params = EquivalentTransformationConfig.DEFAULTS.copy()
487 params.update(kwargs)
489 **params, optional=EquivalentTransformationConfig.optional
490 )
491
492
493# }@
from_dict(cls, dict config_dict)
Create EquivalentTransformationConfig from dictionary (JSON structure)
check_valid(self, except_list=["optional"])
Override to skip py_to_cpp_name check (uses nested structure in to_dict)
EquivalentTransformationConfig get_equivalent_transformation_config(**kwargs)
Create EquivalentTransformationConfig with partial parameters merged with defaults.
__init__(self, int seed, bool apply_hadamard_rotation_matrix, bool norm_conv_apply, bool norm_conv_learn, float norm_conv_smoothing_factor, float norm_conv_min_gamma, float norm_conv_max_gamma, bool qk_apply, float qk_smoothing_factor, float qk_min_gamma, float qk_max_gamma, bool ud_apply, bool ud_learn, float ud_smoothing_factor, float ud_min_gamma, float ud_max_gamma, bool vo_apply, float vo_smoothing_factor, float vo_min_gamma, float vo_max_gamma, bool ff_multi_lut_apply, List[float] ff_multi_lut_breakpoints, bool spin_r1_apply, str spin_r1_matrix_path, bool head_out_ch_rotation_apply, str head_out_ch_rotation_matrix_path, bool spin_r2_apply, bool spin_r2_learn, str spin_r2_matrix_path, bool qk_rotation_apply, str qk_rotation_matrix_path, bool flatten_quant_apply, bool flatten_quant_learn, float flatten_quant_apply_threshold, float flatten_quant_max_overhead, bool optimize_ffn_apply, int optimize_ffn_ch_per_ffn, bool optional=optional)
Initialize the EquivalentTransformationConfig.