models.py Source File

models.py Source File#

Mobilint SDK qb Compiler: models.py Source File
Mobilint SDK qb Compiler v1.4
MCS002-KR
models.py
1"""Auto-generated Pydantic models from config_schema.yaml."""
2
3from __future__ import annotations
4from typing import Any, Dict, List, Optional, Union
5from pydantic import BaseModel, Field, ConfigDict, AliasChoices, model_validator
6import yaml
7import json
8from pathlib import Path
9
10SCHEMA_VERSION = "1.0.0"
11
12
13class Uint8InputConfig(BaseModel):
14 """
15 @brief Configuration for uint8 input handling
16
17 @details Defines whether inputs should be treated as uint8 and which specific inputs to apply this to.
18
19 @param apply bool. If true, treat specified inputs as uint8
20 @param inputs List[str]. List of input names to treat as uint8. If empty and apply is true, applies to all inputs
21 @param division_factor float. Division factor for uint8 to float conversion (e.g., 255.0 for [0,1], 127.5 for [0,2])
22 """
23
24 model_config = ConfigDict(
25 populate_by_name=True,
26 extra="forbid",
27 )
28
29 apply: bool = Field(
30 default=False, description="If true, treat specified inputs as uint8"
31 )
32 inputs: List[str] = Field(
33 default=[],
34 description="List of input names to treat as uint8. If empty and apply is true, applies to all inputs",
35 )
36 division_factor: float = Field(
37 default=255.0,
38 alias="divisionFactor",
39 description="Division factor for uint8 to float conversion (e.g., 255.0 for [0,1], 127.5 for [0,2])",
40 )
41
42 def with_updates(self, **kwargs) -> "Uint8InputConfig":
43 """Return a copy with updated fields."""
44 return self.model_copy(update=kwargs)
45
46
47class PreprocessingConfig(BaseModel):
48 """
49 @brief Configuration for input preprocessing pipeline
50
51 @details Defines preprocessing operations to be applied to model inputs,
52 including operations like resize, normalize, color conversion, etc.
53 Each pipeline entry is {"op": <name>, ...op keys}; unknown keys are rejected.
54 resize/letterbox take "backend": "torch" (default, float interpolate), "pil"
55 (Pillow, bit-exact, bilinear/bicubic) or "opencv" (cv2 INTER_LINEAR, bit-exact),
56 so calibration sees the same pixels as an evaluator built on that library.
57 letterbox "alignType" is 0 (default: center, padding split around the image) or 1
58 (top-left: padding only at the bottom and right, as YOLOX and DAMO-YOLO use).
59 letterbox "roundType" is 0 (default: each backend's rounding of the resized size) or 1
60 (floor: upstream YOLOX and DAMO-YOLO's int(w * r)); a fuseIntoFirstLayer letterbox
61 ignores it, since its exact odd decimation has no fraction to round.
62
63 @param apply bool. If true, apply preprocessing pipeline
64 @param auto_convert_format bool. If true, automatically convert input format
65 @param pipeline List[Dict[str, Any]]. List of preprocessing operations to apply globally
66 @param input_configs Dict[str, Any]. Per-input preprocessing configurations. Keys are input names
67 """
68
69 model_config = ConfigDict(
70 populate_by_name=True,
71 extra="forbid",
72 )
73
74 apply: bool = Field(
75 default=False, description="If true, apply preprocessing pipeline"
76 )
77 auto_convert_format: bool = Field(
78 default=False,
79 alias="autoConvertFormat",
80 description="If true, automatically convert input format",
81 )
82 pipeline: Any = Field(
83 default=[], description="List of preprocessing operations to apply globally"
84 )
85 input_configs: Any = Field(
86 default={},
87 alias="inputConfigs",
88 description="Per-input preprocessing configurations. Keys are input names",
89 )
90
91 def with_updates(self, **kwargs) -> "PreprocessingConfig":
92 """Return a copy with updated fields."""
93 return self.model_copy(update=kwargs)
94
95
96class ResourceManagementConfig(BaseModel):
97 """
98 @brief Configuration for resource management during model compilation
99
100 @details Controls GPU and memory management settings during the compilation process.
101
102 @param weight_dtype str. Weight data type for calibration (e.g., 'float32', 'float16')
103 @param gpu_memory_budget_mb int. Device memory budget for weight quantization in MiB; 0 = unlimited, -1 = auto
104 @param weight_memory WeightMemory. Weight memory management configuration
105 """
106
107 model_config = ConfigDict(
108 populate_by_name=True,
109 extra="forbid",
110 )
111
112 weight_dtype: str = Field(
113 default="float32",
114 alias="weightDtype",
115 description="Weight data type for calibration (e.g., 'float32', 'float16')",
116 )
117 gpu_memory_budget_mb: int = Field(
118 default=-1,
119 alias="gpuMemoryBudgetMB",
120 description="Device memory budget for weight quantization in MiB; 0 = unlimited, -1 = auto",
121 )
122
123 class WeightMemory(BaseModel):
124 """
125 @brief Weight memory management configuration
126
127 @param method int. Weight memory management method index:<br>
128 0: DeleteFloat - Delete float weights after quantization.<br>
129 1: SaveFloat - Save float weights to disk.<br>
130 2: MoveFloat - Move float weights to CPU.<br>
131 3: KeepFloat - Keep float weights in memory.<br>
132 4: KeepAll - Keep all weights in memory.<br>
133 """
134
135 method_list: List[str] = Field(
136 default=["DeleteFloat", "SaveFloat", "MoveFloat", "KeepFloat", "KeepAll"],
137 alias="methodList",
138 )
139 method: int = Field(default=0, alias="method")
140
141 weight_memory: WeightMemory = Field(
142 default_factory=WeightMemory, alias="weightMemory"
143 )
144
145 def with_updates(self, **kwargs) -> "ResourceManagementConfig":
146 """Return a copy with updated fields."""
147 return self.model_copy(update=kwargs)
148
149
150class CalibrationConfig(BaseModel):
151 """
152 @brief Configuration for calibration during quantization
153
154 @details Defines calibration and quantization parameterization used to derive activation/weight scales
155 and related statistics during quantized compilation.
156
157 @param method int. Calibration method index:<br>
158 0: WChALayer - Weight per-channel, Activation per-layer.<br>
159 1: WChAMulti - Weight per-channel, Activation multi-layer.<br>
160 2: WChALayerZeropoint - Weight per-channel, Activation per-layer with zeropoint.<br>
161 3: WChAMultiZeropoint - Weight per-channel, Activation multi-layer with zeropoint.<br>
162 @param output int. Output quantization type index:<br>
163 0: Layer - Per-layer quantization.<br>
164 1: Ch - Per-channel quantization.<br>
165 2: Sigmoid - Sigmoid-based quantization.<br>
166 @param mode int. Quantization mode index:<br>
167 0: Max - Maximum value calibration.<br>
168 1: MaxPercentile - Maximum percentile calibration.<br>
169 2: Histogram - Histogram-based calibration.<br>
170 @param act_scale_min float. Minimum allowed activation scale (lower bound clamp)
171 @param act16_scale_min float. Minimum 16-bit activation scale (actScaleMin / 256)
172 @param weight_scale_min float. Minimum allowed weight scale (lower bound clamp)
173 @param weight16_scale_min float. Minimum 16-bit weight scale (weightScaleMin / 256)
174 @param min_clip_ratio float. Minimum clip ratio constraint applied during calibration
175 @param max_calib_data_size int. Maximum number of calibration samples kept after loading or generation
176 @param max_sample_size_for_quant_scheme int. Maximum number of calibration samples used per quant scheme stage
177 @param max_percentile MaxPercentile. MaxPercentile mode configuration
178 @param fast_dist FastDist. Fast distribution calibration configuration
179 @param histogram Histogram. Histogram-based calibration configuration
180 @param layer_overrides LayerOverrides. Layer-specific override settings for calibration
181 @param statistics Statistics. Statistics save/load configuration with percentile selection
182 @param group_lut GroupLut. LUT grouping algorithm configuration
183 """
184
185 model_config = ConfigDict(
186 populate_by_name=True,
187 extra="forbid",
188 )
189
190 method_list: List[str] = Field(
191 default=["WChALayer", "WChAMulti", "WChALayerZeropoint", "WChAMultiZeropoint"],
192 alias="methodList",
193 )
194 method: int = Field(default=1, alias="method")
195 output_list: List[str] = Field(
196 default=["Layer", "Ch", "Sigmoid"], alias="outputList"
197 )
198 output: int = Field(default=0, alias="output")
199 mode_list: List[str] = Field(
200 default=["Max", "MaxPercentile", "Histogram"], alias="modeList"
201 )
202 mode: int = Field(default=1, alias="mode")
203
204 act_scale_min: float = Field(
205 default=0.0005,
206 alias="actScaleMin",
207 description="Minimum allowed activation scale (lower bound clamp)",
208 ge=0,
209 le=1,
210 )
211 act16_scale_min: float = Field(
212 default=1.953125e-06,
213 alias="act16ScaleMin",
214 description="Minimum 16-bit activation scale (actScaleMin / 256)",
215 )
216 weight_scale_min: float = Field(
217 default=1e-06,
218 alias="weightScaleMin",
219 description="Minimum allowed weight scale (lower bound clamp)",
220 ge=0,
221 le=1,
222 )
223 weight16_scale_min: float = Field(
224 default=3.90625e-09,
225 alias="weight16ScaleMin",
226 description="Minimum 16-bit weight scale (weightScaleMin / 256)",
227 )
228 min_clip_ratio: float = Field(
229 default=-1,
230 alias="minClipRatio",
231 description="Minimum clip ratio constraint applied during calibration",
232 ge=-1,
233 le=1,
234 )
235 max_calib_data_size: int = Field(
236 default=-1,
237 alias="maxCalibDataSize",
238 description="Maximum number of calibration samples kept after loading or generation",
239 ge=-1,
240 )
241 max_sample_size_for_quant_scheme: int = Field(
242 default=16,
243 alias="maxSampleSizeForQuantScheme",
244 description="Maximum number of calibration samples used per quant scheme stage",
245 ge=1,
246 )
247
248 class MaxPercentile(BaseModel):
249 model_config = ConfigDict(populate_by_name=True)
250
251 """
252 @brief MaxPercentile mode configuration
253
254 @param percentile float. Percentile value for maxPercentile mode
255 @param topk_ratio float. Top-k ratio used in maxPercentile mode
256 @param max_each int. Maximum number of samples processed per iteration
257 @param max_total int. Total maximum number of samples
258 @param per_ch_divisor int. Divisor for per-channel buffer capacity (bufferCap = max(maxTotal / perChDivisor, maxEach))
259 """
260 percentile: float = Field(
261 default=0.9999, description="Percentile value for maxPercentile mode"
262 )
263 topk_ratio: float = Field(
264 default=0.01,
265 alias="topKRatio",
266 description="Top-k ratio used in maxPercentile mode",
267 )
268 max_each: int = Field(
269 default=128,
270 alias="maxEach",
271 description="Maximum number of samples processed per iteration",
272 )
273 max_total: int = Field(
274 default=65536,
275 alias="maxTotal",
276 description="Total maximum number of samples",
277 )
278 per_ch_divisor: int = Field(
279 default=16,
280 alias="perChDivisor",
281 description="Divisor for per-channel buffer capacity (bufferCap = max(maxTotal / perChDivisor, maxEach))",
282 ge=1,
283 )
284
285 max_percentile: MaxPercentile = Field(
286 default_factory=MaxPercentile, alias="maxPercentile"
287 )
288
289 class FastDist(BaseModel):
290 model_config = ConfigDict(populate_by_name=True)
291
292 """
293 @brief Fast distribution calibration configuration
294
295 @param size_cali int.
296 @param kernel_size int.
297 @param stack_size int.
298 """
299 size_cali: int = Field(default=100, alias="sizeCali")
300 kernel_size: int = Field(default=9, alias="kernelSize")
301 stack_size: int = Field(default=32768, alias="stackSize")
302
303 fast_dist: FastDist = Field(default_factory=FastDist, alias="fastDist")
304
305 class Histogram(BaseModel):
306 model_config = ConfigDict(populate_by_name=True)
307
308 """
309 @brief Histogram-based calibration configuration
310
311 @param search_type int. Search type for histogram calibration:<br>
312 0: Percentile.<br>
313 1: MSE.<br>
314 2: KL.<br>
315 @param percentile float. Percentile value for histogram calibration
316 @param use_gpu bool. Use GPU for histogram computation
317 @param num_bins int. Number of bins for histogram
318 @param num_samples int. Number of samples for histogram calibration
319 @param buffer_size int. Buffer size for histogram computation (-1 for auto)
320 @param min_bin_width float. Minimum bin width for histogram
321 @param search_percentile_min float. Minimum search percentile
322 @param search_percentile_max float. Maximum search percentile
323 @param num_search int. Number of search iterations
324 """
325 search_type_list: List[str] = Field(
326 default=["Percentile", "MSE", "KL"], alias="searchTypeList"
327 )
328 search_type: int = Field(default=0, alias="searchType")
329 percentile: float = Field(
330 default=0.9999, description="Percentile value for histogram calibration"
331 )
332 use_gpu: bool = Field(
333 default=True,
334 alias="useGPU",
335 description="Use GPU for histogram computation",
336 )
337 num_bins: int = Field(
338 default=256, alias="numBins", description="Number of bins for histogram"
339 )
340 num_samples: int = Field(
341 default=128,
342 alias="numSamples",
343 description="Number of samples for histogram calibration",
344 )
345 buffer_size: int = Field(
346 default=-1,
347 alias="bufferSize",
348 description="Buffer size for histogram computation (-1 for auto)",
349 )
350 min_bin_width: float = Field(
351 default=1e-06,
352 alias="minBinWidth",
353 description="Minimum bin width for histogram",
354 )
355 search_percentile_min: float = Field(
356 default=0.9999,
357 alias="searchPercentileMin",
358 description="Minimum search percentile",
359 )
360 search_percentile_max: float = Field(
361 default=1.0,
362 alias="searchPercentileMax",
363 description="Maximum search percentile",
364 )
365 num_search: int = Field(
366 default=128, alias="numSearch", description="Number of search iterations"
367 )
368
369 histogram: Histogram = Field(default_factory=Histogram, alias="histogram")
370
371 class LayerOverrides(BaseModel):
372 model_config = ConfigDict(populate_by_name=True)
373
374 """
375 @brief Layer-specific override settings for calibration
376
377 @param act_scale_min dict. Per-layer activation scale minimum overrides. Keys are actScaleMin values (e.g. '0.0005'), values are lists of layer names to apply that override. (e.g. {'0.0005' : ['layer1', 'layer2']})
378 @param percentile dict. Per-layer maxPercentile percentile overrides. Keys are layer names, values are percentile floats. (e.g. {'/model/layer0/conv': 0.999, '/model/layer5/attn': 0.99})
379 @param method dict. Per-layer calibration method overrides. Keys are layer names, values are method ints (0=WChALayer, 1=WChAMulti, 2=WChALayerZeropoint, 3=WChAMultiZeropoint). (e.g. {'/model/layer0/conv': 1, '/model/layer5/attn': 3})
380 """
381 act_scale_min: Any = Field(
382 default={},
383 alias="actScaleMin",
384 description="Per-layer activation scale minimum overrides. Keys are actScaleMin values (e.g. '0.0005'), values are lists of layer names to apply that override. (e.g. {'0.0005' : ['layer1', 'layer2']})",
385 )
386 percentile: Any = Field(
387 default={},
388 description="Per-layer maxPercentile percentile overrides. Keys are layer names, values are percentile floats. (e.g. {'/model/layer0/conv': 0.999, '/model/layer5/attn': 0.99})",
389 )
390 method: Any = Field(
391 default={},
392 description="Per-layer calibration method overrides. Keys are layer names, values are method ints (0=WChALayer, 1=WChAMulti, 2=WChALayerZeropoint, 3=WChAMultiZeropoint). (e.g. {'/model/layer0/conv': 1, '/model/layer5/attn': 3})",
393 )
394
395 layer_overrides: LayerOverrides = Field(
396 default_factory=LayerOverrides, alias="layerOverrides"
397 )
398
399 class Statistics(BaseModel):
400 model_config = ConfigDict(populate_by_name=True)
401
402 """
403 @brief Statistics save/load configuration with percentile selection
404
405 @param apply bool. Enable statistics save/load
406 @param save_path str. Path to save statistics. If empty, not saved
407 @param load_path str. Path to load statistics. If empty, not loaded
408 @param percentiles List[float]. List of percentile candidates
409 @param percentile_index int. Index into percentiles list to select active percentile
410 """
411 apply: bool = Field(default=False, description="Enable statistics save/load")
412 save_path: str = Field(
413 default="",
414 alias="savePath",
415 description="Path to save statistics. If empty, not saved",
416 )
417 load_path: str = Field(
418 default="",
419 alias="loadPath",
420 description="Path to load statistics. If empty, not loaded",
421 )
422 percentiles: List[float] = Field(
423 default=[0.9999, 0.999, 0.99, 0.9],
424 description="List of percentile candidates",
425 )
426 percentile_index: int = Field(
427 default=0,
428 alias="percentileIndex",
429 description="Index into percentiles list to select active percentile",
430 )
431
432 statistics: Statistics = Field(default_factory=Statistics, alias="statistics")
433
434 class GroupLut(BaseModel):
435 model_config = ConfigDict(populate_by_name=True)
436
437 """
438 @brief LUT grouping algorithm configuration
439
440 @param irls_iter int. Number of IRLS iterations for optimal scale computation (1 = single WLS)
441 @param dro_eps float. DRO worst-point coefficient; larger = more conservative clipping (0 = disable)
442 @param cover_floor float. LUTAM cluster scale lower bound as fraction of coverage
443 """
444 irls_iter: int = Field(
445 default=3,
446 alias="irlsIter",
447 description="Number of IRLS iterations for optimal scale computation (1 = single WLS)",
448 ge=1,
449 )
450 dro_eps: float = Field(
451 default=0,
452 alias="droEps",
453 description="DRO worst-point coefficient; larger = more conservative clipping (0 = disable)",
454 ge=0,
455 )
456 cover_floor: float = Field(
457 default=0.85,
458 alias="coverFloor",
459 description="LUTAM cluster scale lower bound as fraction of coverage",
460 ge=0,
461 le=1,
462 )
463
464 group_lut: GroupLut = Field(default_factory=GroupLut, alias="groupLut")
465
466 def with_updates(self, **kwargs) -> "CalibrationConfig":
467 """Return a copy with updated fields."""
468 return self.model_copy(update=kwargs)
469
470
471class BitConfig(BaseModel):
472 """
473 @brief Configuration for bit precision
474
475 @details Defines bit-width parameterization for activations and weights used in
476 mixed-precision quantization (e.g., attention and FFN components).
477
478 @param transformer Transformer. Transformer-specific bit-width configuration
479 @param save_info SaveInfo. Bit allocation save/load configuration
480 @param layer_overrides LayerOverrides. Layer-specific bit-width override settings
481 """
482
483 model_config = ConfigDict(
484 populate_by_name=True,
485 extra="forbid",
486 )
487
488 class Transformer(BaseModel):
489 model_config = ConfigDict(populate_by_name=True)
490
491 """
492 @brief Transformer-specific bit-width configuration
493
494 @param activation Activation. Activation bit-widths for transformer components
495 @param weight Weight. Weight bit-widths for transformer components
496 @param mixed_precision MixedPrecision. Mixed precision configuration
497 """
498
499 class Activation(BaseModel):
500 model_config = ConfigDict(populate_by_name=True)
501
502 """
503 @brief Activation bit-widths for transformer components
504
505 @param query int. Query activation bit-width
506 @param key int. Key activation bit-width
507 @param value int. Value activation bit-width
508 @param output int. Output activation bit-width
509 @param head int. Head activation bit-width
510 @param router int. MoE router gate activation bit-width
511 @param ffn Ffn. FFN activation bit-widths (int shorthand sets all sublayers)
512 """
513 query: int = Field(default=8, description="Query activation bit-width")
514 key: int = Field(default=8, description="Key activation bit-width")
515 value: int = Field(default=8, description="Value activation bit-width")
516 output: int = Field(default=16, description="Output activation bit-width")
517 head: int = Field(default=8, description="Head activation bit-width")
518 router: int = Field(
519 default=8, description="MoE router gate activation bit-width"
520 )
521
522 class Ffn(BaseModel):
523 """
524 @brief FFN activation bit-widths (int shorthand sets all sublayers)
525
526 @param up int. FFN up-projection activation bit-width
527 @param gate int. FFN gate (SwiGLU) activation bit-width
528 @param down int. FFN down-projection activation bit-width
529 """
530
531 @model_validator(mode="before")
532 @classmethod
533 def expand_int_shorthand(cls, v):
534 if isinstance(v, bool):
535 raise ValueError("bool is not a valid bit-width")
536 if isinstance(v, int):
537 return {"up": int(v), "gate": int(v), "down": int(v)}
538 return v
539
540 up: int = Field(
541 default=16, description="FFN up-projection activation bit-width"
542 )
543 gate: int = Field(
544 default=16, description="FFN gate (SwiGLU) activation bit-width"
545 )
546 down: int = Field(
547 default=16, description="FFN down-projection activation bit-width"
548 )
549
550 ffn: Ffn = Field(default_factory=Ffn, alias="ffn")
551
552 activation: Activation = Field(default_factory=Activation, alias="activation")
553
554 class Weight(BaseModel):
555 model_config = ConfigDict(populate_by_name=True)
556
557 """
558 @brief Weight bit-widths for transformer components
559
560 @param query int. Query weight bit-width
561 @param key int. Key weight bit-width
562 @param value int. Value weight bit-width
563 @param output int. Output weight bit-width
564 @param head int. Head weight bit-width
565 @param router int. MoE router gate weight bit-width
566 @param ffn Ffn. FFN weight bit-widths (int shorthand sets all sublayers)
567 """
568 query: int = Field(default=8, description="Query weight bit-width")
569 key: int = Field(default=8, description="Key weight bit-width")
570 value: int = Field(default=8, description="Value weight bit-width")
571 output: int = Field(default=8, description="Output weight bit-width")
572 head: int = Field(default=8, description="Head weight bit-width")
573 router: int = Field(
574 default=8, description="MoE router gate weight bit-width"
575 )
576
577 class Ffn(BaseModel):
578 """
579 @brief FFN weight bit-widths (int shorthand sets all sublayers)
580
581 @param up int. FFN up-projection weight bit-width
582 @param gate int. FFN gate (SwiGLU) weight bit-width
583 @param down int. FFN down-projection weight bit-width
584 """
585
586 @model_validator(mode="before")
587 @classmethod
588 def expand_int_shorthand(cls, v):
589 if isinstance(v, bool):
590 raise ValueError("bool is not a valid bit-width")
591 if isinstance(v, int):
592 return {"up": int(v), "gate": int(v), "down": int(v)}
593 return v
594
595 up: int = Field(
596 default=8, description="FFN up-projection weight bit-width"
597 )
598 gate: int = Field(
599 default=8, description="FFN gate (SwiGLU) weight bit-width"
600 )
601 down: int = Field(
602 default=8, description="FFN down-projection weight bit-width"
603 )
604
605 ffn: Ffn = Field(default_factory=Ffn, alias="ffn")
606
607 weight: Weight = Field(default_factory=Weight, alias="weight")
608
609 class MixedPrecision(BaseModel):
610 model_config = ConfigDict(populate_by_name=True)
611
612 """
613 @brief Mixed precision configuration
614
615 @param weight Weight. Mixed precision configuration for weights
616 @param activation Activation. Per-layer activation mixed precision configuration
617 """
618
619 class Weight(BaseModel):
620 model_config = ConfigDict(populate_by_name=True)
621
622 """
623 @brief Mixed precision configuration for weights
624
625 @param apply bool. If true, apply mixed-precision according to the specified bit-widths
626 @param type_wise bool. Apply type-wise mixed precision
627 @param prune float. Pruning ratio
628 @param bit_2 float. Ratio of 2-bit quantization
629 @param bit_4 float. Ratio of 4-bit quantization
630 @param bit_8 float. Ratio of 8-bit quantization
631 @param importance_threshold_low float. Low importance threshold
632 @param importance_threshold_high float. High importance threshold
633 """
634 apply: bool = Field(
635 default=False,
636 description="If true, apply mixed-precision according to the specified bit-widths",
637 )
638 type_wise: bool = Field(
639 default=True,
640 alias="typeWise",
641 description="Apply type-wise mixed precision",
642 )
643 prune: float = Field(default=0, description="Pruning ratio")
644 bit_2: float = Field(
645 default=0,
646 validation_alias=AliasChoices("bit_2", "bit2"),
647 description="Ratio of 2-bit quantization",
648 )
649 bit_4: float = Field(
650 default=0,
651 validation_alias=AliasChoices("bit_4", "bit4"),
652 description="Ratio of 4-bit quantization",
653 )
654 bit_8: float = Field(
655 default=1,
656 validation_alias=AliasChoices("bit_8", "bit8"),
657 description="Ratio of 8-bit quantization",
658 )
659 importance_threshold_low: float = Field(
660 default=-1,
661 alias="importanceThreshold_low",
662 description="Low importance threshold",
663 )
664 importance_threshold_high: float = Field(
665 default=-1,
666 alias="importanceThreshold_high",
667 description="High importance threshold",
668 )
669
670 weight: Weight = Field(default_factory=Weight, alias="weight")
671
672 class Activation(BaseModel):
673 model_config = ConfigDict(populate_by_name=True)
674
675 """
676 @brief Per-layer activation mixed precision configuration
677
678 @param apply bool. If true, apply per-layer activation mixed precision
679 @param ratio_16bit float. Ratio of layers assigned 16-bit (used when importanceThreshold < 0)
680 @param importance_threshold float. Normalized importance threshold for 16-bit assignment (negative = use ratio_16bit)
681 @param search_range int. Target layer range: -1=all layers, N=first N layers
682 """
683 apply: bool = Field(
684 default=False,
685 description="If true, apply per-layer activation mixed precision",
686 )
687 ratio_16bit: float = Field(
688 default=0.45,
689 alias="ratio16Bit",
690 description="Ratio of layers assigned 16-bit (used when importanceThreshold < 0)",
691 )
692 importance_threshold: float = Field(
693 default=-1,
694 alias="importanceThreshold",
695 description="Normalized importance threshold for 16-bit assignment (negative = use ratio_16bit)",
696 )
697 search_range: int = Field(
698 default=-1,
699 alias="searchRange",
700 description="Target layer range: -1=all layers, N=first N layers",
701 )
702
703 activation: Activation = Field(
704 default_factory=Activation, alias="activation"
705 )
706
707 mixed_precision: MixedPrecision = Field(
708 default_factory=MixedPrecision, alias="mixedPrecision"
709 )
710
711 transformer: Transformer = Field(default_factory=Transformer, alias="transformer")
712
713 class SaveInfo(BaseModel):
714 model_config = ConfigDict(populate_by_name=True)
715
716 """
717 @brief Bit allocation save/load configuration
718
719 @param save_path str. Path to save the bit allocation. If empty, not saved
720 @param load_path str. Path to load the bit allocation. If empty, not loaded
721 """
722 save_path: str = Field(
723 default="",
724 alias="savePath",
725 description="Path to save the bit allocation. If empty, not saved",
726 )
727 load_path: str = Field(
728 default="",
729 alias="loadPath",
730 description="Path to load the bit allocation. If empty, not loaded",
731 )
732
733 save_info: SaveInfo = Field(default_factory=SaveInfo, alias="saveInfo")
734
735 class LayerOverrides(BaseModel):
736 model_config = ConfigDict(populate_by_name=True)
737
738 """
739 @brief Layer-specific bit-width override settings
740
741 @param activation_16bits list[string]. Layer names to force 16-bit activations
742 @param weight_16bits list[string]. Layer names to force 16-bit weights
743 @param weight_8bits list[string]. Layer names to force 8-bit weights
744 """
745 activation_16bits: List[str] = Field(
746 default=[],
747 alias="activation16Bits",
748 description="Layer names to force 16-bit activations",
749 )
750 weight_16bits: List[str] = Field(
751 default=[],
752 alias="weight16Bits",
753 description="Layer names to force 16-bit weights",
754 )
755 weight_8bits: List[str] = Field(
756 default=[],
757 alias="weight8Bits",
758 description="Layer names to force 8-bit weights",
759 )
760
761 layer_overrides: LayerOverrides = Field(
762 default_factory=LayerOverrides, alias="layerOverrides"
763 )
764
765 def with_updates(self, **kwargs) -> "BitConfig":
766 """Return a copy with updated fields."""
767 return self.model_copy(update=kwargs)
768
769
770class HessianQuantConfig(BaseModel):
771 """
772 @brief Configuration for HessianQuant algorithm
773
774 @details Defines parameters controlling whether and how HessianQuant is applied during quantization,
775 including layer-level inclusion/exclusion lists.
776
777 @param apply bool. If true, apply HessianQuant
778 @param hessian_dtype str. Storage dtype for the accumulated HessianQuant Hessian. bf16 halves its memory footprint (host RAM when the Hessian lives on CPU, VRAM when on GPU) — critical for large models such as MoE with thousands of expert FFN Hessians. Compute stays float32 regardless: the per-batch matmul and accumulation run in float32 and the solve upcasts back to float32; only the persistent accumulator is bfloat16. Accepted values:<br>
779 "fp32" - Store the Hessian in float32. The default.<br>
780 "bf16" - Store the Hessian in bfloat16 (half the memory).<br>
781 @param solver str. HessianQuant solve algorithm used to compute per-layer integer weights. symmetric targets the output on the quantized input; the asymmetric solvers target the original-weight float output and differ in how they absorb the input mismatch. Accepted values:<br>
782 "symmetric" - The default. min ||(W - Q) X_q||^2: sequential greedy rounding via a Cholesky-based Hessian solve.<br>
783 "asymmetric_causal" - min ||W X_fp - Q X_q||^2: each column's input-mismatch residual is fed only to the columns after it, scaled by alpha.<br>
784 "asymmetric_refit" - min ||W X_fp - Q X_q||^2: least-squares refit W*^T = H^-1 G W^T of every column, then a symmetric solve around W*.<br>
785 @param rescomp bool. Enable residual-compensation correction. When true, each solver additionally applies an R correction term that compensates for accumulated weight drift from inter-block error propagation. Requires crossGram collection even for solver=symmetric.
786 @param attributes Attributes. HessianQuant algorithm attributes
787 """
788
789 model_config = ConfigDict(
790 populate_by_name=True,
791 extra="forbid",
792 )
793
794 apply: bool = Field(default=False, description="If true, apply HessianQuant")
795 hessian_dtype: str = Field(
796 default="fp32",
797 alias="hessianDtype",
798 description="Storage dtype for the accumulated HessianQuant Hessian. bf16 halves its memory footprint (host RAM when the Hessian lives on CPU, VRAM when on GPU) — critical for large models such as MoE with thousands of expert FFN Hessians. Compute stays float32 regardless: the per-batch matmul and accumulation run in float32 and the solve upcasts back to float32; only the persistent accumulator is bfloat16.",
799 )
800 solver: str = Field(
801 default="symmetric",
802 description="HessianQuant solve algorithm used to compute per-layer integer weights. symmetric targets the output on the quantized input; the asymmetric solvers target the original-weight float output and differ in how they absorb the input mismatch.",
803 )
804 rescomp: bool = Field(
805 default=False,
806 description="Enable residual-compensation correction. When true, each solver additionally applies an R correction term that compensates for accumulated weight drift from inter-block error propagation. Requires crossGram collection even for solver=symmetric.",
807 )
808
809 class Attributes(BaseModel):
810 model_config = ConfigDict(populate_by_name=True)
811
812 """
813 @brief HessianQuant algorithm attributes
814
815 @param act_order bool. If true, use activation order
816 @param block_size int. Block size used for HessianQuant
817 @param perc_damp float. Percentage dampening factor
818 @param apply_layers List[str]. Layer names to apply HessianQuant. If empty, applies to all eligible layers
819 @param exclude_layers List[str]. Layer names to exclude from HessianQuant
820 @param alpha float. Scale of the secondary correction terms: the asymmetric_causal P term and the rescomp R term. symmetric and asymmetric_refit use it only through rescomp.
821 """
822 act_order: bool = Field(
823 default=True, alias="actOrder", description="If true, use activation order"
824 )
825 block_size: int = Field(
826 default=256,
827 alias="blockSize",
828 description="Block size used for HessianQuant",
829 )
830 perc_damp: float = Field(
831 default=0.01, alias="percDamp", description="Percentage dampening factor"
832 )
833 apply_layers: List[str] = Field(
834 default=[],
835 alias="applyLayers",
836 description="Layer names to apply HessianQuant. If empty, applies to all eligible layers",
837 )
838 exclude_layers: List[str] = Field(
839 default=[],
840 alias="excludeLayers",
841 description="Layer names to exclude from HessianQuant",
842 )
843 alpha: float = Field(
844 default=0.25,
845 description="Scale of the secondary correction terms: the asymmetric_causal P term and the rescomp R term. symmetric and asymmetric_refit use it only through rescomp.",
846 )
847
848 attributes: Attributes = Field(default_factory=Attributes, alias="attributes")
849
850 def with_updates(self, **kwargs) -> "HessianQuantConfig":
851 """Return a copy with updated fields."""
852 return self.model_copy(update=kwargs)
853
854
855class BiasCorrectionConfig(BaseModel):
856 """
857 @brief Configuration for in-schedule integer bias correction
858
859 @details During weight quantization, measures the per-channel activation error of each
860 quantized convolution (plain, depthwise or transposed; not grouped) against the
861 original-weight float path over every calibration sample, folds the correction
862 into the integer bias within its range, skipping a channel whose slope is too
863 flat to invert, and patches the already-produced integer
864 activations so downstream layers quantize against corrected outputs.
865 Corrections propagate layer by layer in one pass; there is no damping rate
866 and no iteration count. Under group-wise quantization both streams start each
867 group from the float boundary batch, so the correction covers the error the
868 group itself adds. A run the engine cannot drive leaf by leaf (expert
869 aggregation, an external input narrower than its per-channel qparam) keeps the
870 calibrated bias. A layer whose LUT window is re-chosen is measured on its
871 accumulator, ahead of the window; any other layer on its requantized codes.
872
873 @param apply bool. If true, correct systematic per-layer quantization bias
874 @param attributes Attributes. Bias correction attributes
875 """
876
877 model_config = ConfigDict(
878 populate_by_name=True,
879 extra="forbid",
880 )
881
882 apply: bool = Field(
883 default=True,
884 description="If true, correct systematic per-layer quantization bias",
885 )
886
887 class Attributes(BaseModel):
888 model_config = ConfigDict(populate_by_name=True)
889
890 """
891 @brief Bias correction attributes
892
893 @param apply_layers List[str]. Layer names to apply bias correction. If empty, applies to all eligible layers
894 @param exclude_layers List[str]. Layer names to exclude from bias correction
895 """
896 apply_layers: List[str] = Field(
897 default=[],
898 alias="applyLayers",
899 description="Layer names to apply bias correction. If empty, applies to all eligible layers",
900 )
901 exclude_layers: List[str] = Field(
902 default=[],
903 alias="excludeLayers",
904 description="Layer names to exclude from bias correction",
905 )
906
907 attributes: Attributes = Field(default_factory=Attributes, alias="attributes")
908
909 def with_updates(self, **kwargs) -> "BiasCorrectionConfig":
910 """Return a copy with updated fields."""
911 return self.model_copy(update=kwargs)
912
913
914class ModConfig(BaseModel):
915 """
916 @brief Configuration for Minimum Output Difference algorithm
917
918 @details Defines parameters controlling whether and how MOD is applied during quantization,
919 including layer-level inclusion/exclusion lists.
920
921 @param apply bool. If true, apply MOD
922 @param attributes Attributes. MOD algorithm attributes
923 """
924
925 model_config = ConfigDict(
926 populate_by_name=True,
927 extra="forbid",
928 )
929
930 apply: bool = Field(default=False, description="If true, apply MOD")
931
932 class Attributes(BaseModel):
933 model_config = ConfigDict(populate_by_name=True)
934
935 """
936 @brief MOD algorithm attributes
937
938 @param epochs int. Number of training epochs
939 @param warmup_epochs int. Number of warmup epochs
940 @param lr_min_ratio float. Minimum learning rate ratio
941 @param save_dir str. Directory to save MOD results
942 @param seed int. Random seed for MOD
943 @param apply_layers List[str]. Layer names to apply MOD. If empty, applies to all eligible layers
944 @param exclude_layers List[str]. Layer names to exclude from MOD
945 @param mod_after_layer_name str. Apply MOD after this layer
946 @param anchors List. Anchor configurations for detection models. Nested list of anchor box sizes for detection models. Structure: List[List[List[int]]] where outer list is per detection head (e.g. small/medium/large), middle list is anchors per head, inner list is [width, height]. Example: [[[12,16],[19,36],[40,28]], [[36,75],[76,55],[72,146]], [[142,110],[192,243],[459,401]]]
947 @param use_xyxy bool. Use XYXY format for bounding boxes
948 @param learning_rates LearningRates. Learning rate configuration for MOD
949 @param training Training. MOD training configuration
950 @param loss Loss. MOD loss configuration
951 @param post_processing PostProcessing. Post-processing configuration for detection models
952 """
953 epochs: int = Field(default=4, description="Number of training epochs")
954 warmup_epochs: int = Field(
955 default=1, alias="warmupEpochs", description="Number of warmup epochs"
956 )
957 lr_min_ratio: float = Field(
958 default=0.0001,
959 alias="lrMinRatio",
960 description="Minimum learning rate ratio",
961 )
962 save_dir: str = Field(
963 default="", alias="saveDir", description="Directory to save MOD results"
964 )
965 seed: int = Field(default=0, description="Random seed for MOD")
966 apply_layers: List[str] = Field(
967 default=[],
968 alias="applyLayers",
969 description="Layer names to apply MOD. If empty, applies to all eligible layers",
970 )
971 exclude_layers: List[str] = Field(
972 default=[],
973 alias="excludeLayers",
974 description="Layer names to exclude from MOD",
975 )
976 mod_after_layer_name: str = Field(
977 default="",
978 alias="modAfterLayerName",
979 description="Apply MOD after this layer",
980 )
981 anchors: Any = Field(
982 default=[],
983 description="Anchor configurations for detection models. Nested list of anchor box sizes for detection models. Structure: List[List[List[int]]] where outer list is per detection head (e.g. small/medium/large), middle list is anchors per head, inner list is [width, height]. Example: [[[12,16],[19,36],[40,28]], [[36,75],[76,55],[72,146]], [[142,110],[192,243],[459,401]]]",
984 )
985 use_xyxy: bool = Field(
986 default=False,
987 alias="useXYXY",
988 description="Use XYXY format for bounding boxes",
989 )
990
991 class LearningRates(BaseModel):
992 model_config = ConfigDict(populate_by_name=True)
993
994 """
995 @brief Learning rate configuration for MOD
996
997 @param act_scale float. Learning rate for activation scale
998 @param zeropoint float. Learning rate for zeropoint
999 @param weight_scale float. Learning rate for weight scale
1000 @param weight float. Learning rate for weight
1001 @param bias float. Learning rate for bias
1002 """
1003 act_scale: float = Field(
1004 default=0.0,
1005 alias="actScale",
1006 description="Learning rate for activation scale",
1007 )
1008 zeropoint: float = Field(
1009 default=0.0, description="Learning rate for zeropoint"
1010 )
1011 weight_scale: float = Field(
1012 default=0.0,
1013 alias="weightScale",
1014 description="Learning rate for weight scale",
1015 )
1016 weight: float = Field(default=4e-06, description="Learning rate for weight")
1017 bias: float = Field(default=4e-06, description="Learning rate for bias")
1018
1019 learning_rates: LearningRates = Field(
1020 default_factory=LearningRates, alias="learningRates"
1021 )
1022
1023 class Training(BaseModel):
1024 model_config = ConfigDict(populate_by_name=True)
1025
1026 """
1027 @brief MOD training configuration
1028
1029 @param batch_size int. Batch size for MOD training
1030 @param q_drop float. Quantization drop probability
1031 @param quantize_weight bool. Whether to quantize weights
1032 @param weight_scale_init str. Weight scale initialization method
1033 @param downresol_mode str. Downresolution mode
1034 @param scheduler_type str. LR scheduler type
1035 """
1036 batch_size: int = Field(
1037 default=1, alias="batchSize", description="Batch size for MOD training"
1038 )
1039 q_drop: float = Field(
1040 default=0.0, alias="qDrop", description="Quantization drop probability"
1041 )
1042 quantize_weight: bool = Field(
1043 default=True,
1044 alias="quantizeWeight",
1045 description="Whether to quantize weights",
1046 )
1047 weight_scale_init: str = Field(
1048 default="MinMax",
1049 alias="weightScaleInit",
1050 description="Weight scale initialization method",
1051 )
1052 downresol_mode: str = Field(
1053 default="STE", alias="downresolMode", description="Downresolution mode"
1054 )
1055 scheduler_type: str = Field(
1056 default="Cosine", alias="schedulerType", description="LR scheduler type"
1057 )
1058
1059 training: Training = Field(default_factory=Training, alias="training")
1060
1061 class Loss(BaseModel):
1062 model_config = ConfigDict(populate_by_name=True)
1063
1064 """
1065 @brief MOD loss configuration
1066
1067 @param type str. Loss type (MSE, KL, etc.)
1068 @param use_outputs bool. Use model outputs for loss computation
1069 @param kl_temperature float. KL divergence temperature
1070 @param recon_prob float. Reconstruction probability
1071 @param recon_coeff float. Reconstruction coefficient
1072 @param lambda_0 float. Loss weight lambda_0
1073 @param lambda_1 float. Loss weight lambda_1
1074 @param lambda_2 float. Loss weight lambda_2
1075 @param lambda_3 float. Loss weight lambda_3
1076 @param custom_loss_jit_path str. Path to custom JIT-compiled loss function. Refer to /workspace/quantizer/pyutils/mel.pt
1077 """
1078 type: str = Field(default="MSE", description="Loss type (MSE, KL, etc.)")
1079 use_outputs: bool = Field(
1080 default=False,
1081 alias="useOutputs",
1082 description="Use model outputs for loss computation",
1083 )
1084 kl_temperature: float = Field(
1085 default=1.0,
1086 alias="KLTemperature",
1087 description="KL divergence temperature",
1088 )
1089 recon_prob: float = Field(
1090 default=1.0, alias="reconProb", description="Reconstruction probability"
1091 )
1092 recon_coeff: float = Field(
1093 default=1.0,
1094 alias="reconCoeff",
1095 description="Reconstruction coefficient",
1096 )
1097 lambda_0: float = Field(
1098 default=1.0,
1099 validation_alias=AliasChoices("lambda_0", "lambda0"),
1100 description="Loss weight lambda_0",
1101 )
1102 lambda_1: float = Field(
1103 default=1.0,
1104 validation_alias=AliasChoices("lambda_1", "lambda1"),
1105 description="Loss weight lambda_1",
1106 )
1107 lambda_2: float = Field(
1108 default=1.0,
1109 validation_alias=AliasChoices("lambda_2", "lambda2"),
1110 description="Loss weight lambda_2",
1111 )
1112 lambda_3: float = Field(
1113 default=1.0,
1114 validation_alias=AliasChoices("lambda_3", "lambda3"),
1115 description="Loss weight lambda_3",
1116 )
1117 custom_loss_jit_path: str = Field(
1118 default="",
1119 alias="customLossJITPath",
1120 description="Path to custom JIT-compiled loss function. Refer to /workspace/quantizer/pyutils/mel.pt",
1121 )
1122
1123 loss: Loss = Field(default_factory=Loss, alias="loss")
1124
1125 class PostProcessing(BaseModel):
1126 model_config = ConfigDict(populate_by_name=True)
1127
1128 """
1129 @brief Post-processing configuration for detection models
1130
1131 @param post str. Post-processing type
1132 @param box_conf_thres float. Box confidence threshold
1133 @param box_iou_thres float. Box IoU threshold
1134 """
1135 post: str = Field(default="", description="Post-processing type")
1136 box_conf_thres: float = Field(
1137 default=0, alias="boxConfThres", description="Box confidence threshold"
1138 )
1139 box_iou_thres: float = Field(
1140 default=0, alias="boxIoUThres", description="Box IoU threshold"
1141 )
1142
1143 post_processing: PostProcessing = Field(
1144 default_factory=PostProcessing, alias="postProcessing"
1145 )
1146
1147 attributes: Attributes = Field(default_factory=Attributes, alias="attributes")
1148
1149 def with_updates(self, **kwargs) -> "ModConfig":
1150 """Return a copy with updated fields."""
1151 return self.model_copy(update=kwargs)
1152
1153
1154class LlmConfig(BaseModel):
1155 """
1156 @brief Configuration for Large Language Model (LLM) compilation
1157
1158 @details Defines LLM-specific settings including sequence lengths, cache configurations,
1159 and runtime parameters for efficient LLM inference.
1160
1161 @param apply bool. If True, apply LLM-specific configurations
1162 @param npu_parallel_degree int. Number of NPU partitions for FFN tensor parallelism (1 = disabled). Applied before OptimizeFFN if both active.
1163 @param attributes Attributes. LLM attributes configuration
1164 """
1165
1166 model_config = ConfigDict(
1167 populate_by_name=True,
1168 extra="forbid",
1169 )
1170
1171 apply: bool = Field(
1172 default=False, description="If True, apply LLM-specific configurations"
1173 )
1174 npu_parallel_degree: int = Field(
1175 default=1,
1176 alias="npuParallelDegree",
1177 description="Number of NPU partitions for FFN tensor parallelism (1 = disabled). Applied before OptimizeFFN if both active.",
1178 )
1179
1180 class Attributes(BaseModel):
1181 model_config = ConfigDict(populate_by_name=True)
1182
1183 """
1184 @brief LLM attributes configuration
1185
1186 @param max_data_length int. Maximum data length
1187 @param max_sequence_length int. Maximum sequence length
1188 @param max_cache_length int. Maximum cache length
1189 @param max_core_data_length int. Maximum core data length
1190 @param calibration Calibration. LLM calibration settings
1191 @param runtime Runtime. LLM runtime settings
1192 @param debug Debug. LLM debug settings
1193 """
1194 max_data_length: int = Field(
1195 default=4096, alias="maxDataLength", description="Maximum data length"
1196 )
1197 max_sequence_length: int = Field(
1198 default=4096,
1199 alias="maxSequenceLength",
1200 description="Maximum sequence length",
1201 )
1202 max_cache_length: int = Field(
1203 default=4096, alias="maxCacheLength", description="Maximum cache length"
1204 )
1205 max_core_data_length: int = Field(
1206 default=128,
1207 alias="maxCoreDataLength",
1208 description="Maximum core data length",
1209 )
1210
1211 class Calibration(BaseModel):
1212 model_config = ConfigDict(populate_by_name=True)
1213
1214 """
1215 @brief LLM calibration settings
1216
1217 @param random_seq_length int. Random sequence length used for calibration
1218 @param use_full_seq_length bool. If True, use the full sequence length for calibration
1219 """
1220 random_seq_length: int = Field(
1221 default=80,
1222 alias="randomSeqLength",
1223 description="Random sequence length used for calibration",
1224 )
1225 use_full_seq_length: bool = Field(
1226 default=False,
1227 alias="useFullSeqLength",
1228 description="If True, use the full sequence length for calibration",
1229 )
1230
1231 calibration: Calibration = Field(
1232 default_factory=Calibration, alias="calibration"
1233 )
1234
1235 class Runtime(BaseModel):
1236 model_config = ConfigDict(populate_by_name=True)
1237
1238 """
1239 @brief LLM runtime settings
1240
1241 @param use_global_core bool. If True, use a global core
1242 @param batch_size int. Batch size
1243 @param npu_core_ids List[int]. List of NPU core IDs
1244 @param dynamic_rope bool. If True, enable dynamic RoPE (rotary position embedding)
1245 @param dynamic_mask bool. If True, enable dynamic mask (attention mask as runtime input)
1246 """
1247 use_global_core: bool = Field(
1248 default=False,
1249 alias="useGlobalCore",
1250 description="If True, use a global core",
1251 )
1252 batch_size: int = Field(
1253 default=1, alias="batchSize", description="Batch size"
1254 )
1255 npu_core_ids: List[int] = Field(
1256 default=[0], alias="npuCoreIds", description="List of NPU core IDs"
1257 )
1258 dynamic_rope: bool = Field(
1259 default=False,
1260 alias="dynamicRope",
1261 description="If True, enable dynamic RoPE (rotary position embedding)",
1262 )
1263 dynamic_mask: bool = Field(
1264 default=False,
1265 alias="dynamicMask",
1266 description="If True, enable dynamic mask (attention mask as runtime input)",
1267 )
1268
1269 runtime: Runtime = Field(default_factory=Runtime, alias="runtime")
1270
1271 class Debug(BaseModel):
1272 model_config = ConfigDict(populate_by_name=True)
1273
1274 """
1275 @brief LLM debug settings
1276
1277 @param apply bool. Enable LLM debug mode
1278 @param batch_debug_bundle_size int. Batch debug bundle size
1279 """
1280 apply: bool = Field(default=False, description="Enable LLM debug mode")
1281 batch_debug_bundle_size: int = Field(
1282 default=0,
1283 alias="batchDebugBundleSize",
1284 description="Batch debug bundle size",
1285 )
1286
1287 debug: Debug = Field(default_factory=Debug, alias="debug")
1288
1289 attributes: Attributes = Field(default_factory=Attributes, alias="attributes")
1290
1291 def with_updates(self, **kwargs) -> "LlmConfig":
1292 """Return a copy with updated fields."""
1293 return self.model_copy(update=kwargs)
1294
1295
1296class MoeConfig(BaseModel):
1297 """
1298 @brief Sparse MoE expert-selection configuration (calibration only)
1299
1300 @details Controls which experts are calibrated inside SparseMoe modules. selectionMode
1301 is a calibration-only knob: it picks which experts collect statistics (and, for
1302 TopK, on which tokens). It does NOT change inference routing — the forward path
1303 always routes the router's top-K experts regardless of this setting.
1304 scoreThreshold is only used when selectionMode is Threshold.
1305
1306 @param selection_mode int. Expert selection mode index (calibration only; inference is always top-K):<br>
1307 0: TopK - Calibrate only the router's top-K experts per token (matches inference routing).<br>
1308 1: All - Calibrate every expert on the full sequence.<br>
1309 2: Threshold - Calibrate all experts whose routing score exceeds scoreThreshold.<br>
1310 @param score_threshold float. Routing score threshold used when selectionMode is Threshold (calibration only)
1311 """
1312
1313 model_config = ConfigDict(
1314 populate_by_name=True,
1315 extra="forbid",
1316 )
1317
1318 selection_mode_list: List[str] = Field(
1319 default=["TopK", "All", "Threshold"], alias="selectionModeList"
1320 )
1321 selection_mode: int = Field(default=0, alias="selectionMode")
1322
1323 score_threshold: float = Field(
1324 default=0.0,
1325 alias="scoreThreshold",
1326 description="Routing score threshold used when selectionMode is Threshold (calibration only)",
1327 ge=0,
1328 )
1329
1330 def with_updates(self, **kwargs) -> "MoeConfig":
1331 """Return a copy with updated fields."""
1332 return self.model_copy(update=kwargs)
1333
1334
1336 """
1337 @brief Configuration for equivalent transformation techniques
1338
1339 @details Defines parameters for various equivalent transformation methods including
1340 NormConv, QK smoothing, and rotation matrices for improved quantization.
1341
1342 @param seed int. Random seed for transformation
1343 @param apply_hadamard_rotation_matrix bool. Apply Hadamard rotation matrix
1344 @param norm_conv NormConv. NormConv equivalent transformation
1345 @param qk Qk. QK smoothing transformation
1346 @param ud Ud. UD transformation
1347 @param vo Vo. VO transformation
1348 @param feed_forward_multi_lut FeedForwardMultiLut. Feed-forward multi-LUT transformation
1349 @param spin_r1 SpinR1. SpinR1 rotation transformation
1350 @param head_out_ch_rotation HeadOutChRotation. Head output channel rotation transformation
1351 @param in_rotation InRotation. Input rotation transformation
1352 @param spin_r2 SpinR2. SpinR2 rotation transformation
1353 @param qk_rotation QkRotation. QK rotation transformation
1354 @param flatten_quant FlattenQuant. Flatten quantization transformation
1355 @param optimize_ffn OptimizeFfn. FFN optimization
1356 """
1357
1358 model_config = ConfigDict(
1359 populate_by_name=True,
1360 extra="forbid",
1361 )
1362
1363 seed: int = Field(default=0, description="Random seed for transformation")
1364 apply_hadamard_rotation_matrix: bool = Field(
1365 default=True,
1366 alias="applyHadamardRotationMatrix",
1367 description="Apply Hadamard rotation matrix",
1368 )
1369
1370 class NormConv(BaseModel):
1371 model_config = ConfigDict(populate_by_name=True)
1372
1373 """
1374 @brief NormConv equivalent transformation
1375
1376 @param apply bool. Apply NormConv transformation
1377 @param learn bool. Learn transformation parameters
1378 @param smoothing_factor float. Smoothing factor
1379 @param min_gamma float. Minimum gamma value
1380 @param max_gamma float. Maximum gamma value
1381 """
1382 apply: bool = Field(default=False, description="Apply NormConv transformation")
1383 learn: bool = Field(
1384 default=False, description="Learn transformation parameters"
1385 )
1386 smoothing_factor: float = Field(
1387 default=0.5, alias="smoothingFactor", description="Smoothing factor"
1388 )
1389 min_gamma: float = Field(
1390 default=0.0001, alias="minGamma", description="Minimum gamma value"
1391 )
1392 max_gamma: float = Field(
1393 default=10000.0, alias="maxGamma", description="Maximum gamma value"
1394 )
1395
1396 norm_conv: NormConv = Field(default_factory=NormConv, alias="NormConv")
1397
1398 class Qk(BaseModel):
1399 model_config = ConfigDict(populate_by_name=True)
1400
1401 """
1402 @brief QK smoothing transformation
1403
1404 @param apply bool. Apply QK transformation
1405 @param smoothing_factor float. Smoothing factor
1406 @param min_gamma float. Minimum gamma value
1407 @param max_gamma float. Maximum gamma value
1408 """
1409 apply: bool = Field(default=False, description="Apply QK transformation")
1410 smoothing_factor: float = Field(
1411 default=0.5, alias="smoothingFactor", description="Smoothing factor"
1412 )
1413 min_gamma: float = Field(
1414 default=0.0001, alias="minGamma", description="Minimum gamma value"
1415 )
1416 max_gamma: float = Field(
1417 default=10000.0, alias="maxGamma", description="Maximum gamma value"
1418 )
1419
1420 qk: Qk = Field(default_factory=Qk, alias="QK")
1421
1422 class Ud(BaseModel):
1423 model_config = ConfigDict(populate_by_name=True)
1424
1425 """
1426 @brief UD transformation
1427
1428 @param apply bool. Apply UD transformation
1429 @param learn bool. Learn transformation parameters
1430 @param smoothing_factor float. Smoothing factor
1431 @param min_gamma float. Minimum gamma value
1432 @param max_gamma float. Maximum gamma value
1433 """
1434 apply: bool = Field(default=False, description="Apply UD transformation")
1435 learn: bool = Field(
1436 default=False, description="Learn transformation parameters"
1437 )
1438 smoothing_factor: float = Field(
1439 default=0.5, alias="smoothingFactor", description="Smoothing factor"
1440 )
1441 min_gamma: float = Field(
1442 default=0.0001, alias="minGamma", description="Minimum gamma value"
1443 )
1444 max_gamma: float = Field(
1445 default=10000.0, alias="maxGamma", description="Maximum gamma value"
1446 )
1447
1448 ud: Ud = Field(default_factory=Ud, alias="UD")
1449
1450 class Vo(BaseModel):
1451 model_config = ConfigDict(populate_by_name=True)
1452
1453 """
1454 @brief VO transformation
1455
1456 @param apply bool. Apply VO transformation
1457 @param smoothing_factor float. Smoothing factor
1458 @param min_gamma float. Minimum gamma value
1459 @param max_gamma float. Maximum gamma value
1460 """
1461 apply: bool = Field(default=False, description="Apply VO transformation")
1462 smoothing_factor: float = Field(
1463 default=0.5, alias="smoothingFactor", description="Smoothing factor"
1464 )
1465 min_gamma: float = Field(
1466 default=0.0001, alias="minGamma", description="Minimum gamma value"
1467 )
1468 max_gamma: float = Field(
1469 default=10000.0, alias="maxGamma", description="Maximum gamma value"
1470 )
1471
1472 vo: Vo = Field(default_factory=Vo, alias="VO")
1473
1474 class FeedForwardMultiLut(BaseModel):
1475 """
1476 @brief Feed-forward multi-LUT transformation
1477
1478 @param apply bool. Apply feed-forward multi-LUT transformation
1479 @param breakpoints List[float]. Breakpoints for multi-LUT
1480 """
1481
1482 apply: bool = Field(
1483 default=False, description="Apply feed-forward multi-LUT transformation"
1484 )
1485 breakpoints: List[float] = Field(
1486 default=[-8.0, -4.0, 0], description="Breakpoints for multi-LUT"
1487 )
1488
1489 feed_forward_multi_lut: FeedForwardMultiLut = Field(
1490 default_factory=FeedForwardMultiLut, alias="FeedForwardMultiLUT"
1491 )
1492
1493 class SpinR1(BaseModel):
1494 model_config = ConfigDict(populate_by_name=True)
1495
1496 """
1497 @brief SpinR1 rotation transformation
1498
1499 @param apply bool. Apply SpinR1 transformation
1500 @param matrix_path str. Path to rotation matrix file
1501 """
1502 apply: bool = Field(default=False, description="Apply SpinR1 transformation")
1503 matrix_path: str = Field(
1504 default="", alias="matrixPath", description="Path to rotation matrix file"
1505 )
1506
1507 spin_r1: SpinR1 = Field(default_factory=SpinR1, alias="SpinR1")
1508
1509 class HeadOutChRotation(BaseModel):
1510 model_config = ConfigDict(populate_by_name=True)
1511
1512 """
1513 @brief Head output channel rotation transformation
1514
1515 @param apply bool. Apply head output channel rotation
1516 @param matrix_path str. Path to rotation matrix file
1517 """
1518 apply: bool = Field(
1519 default=False, description="Apply head output channel rotation"
1520 )
1521 matrix_path: str = Field(
1522 default="", alias="matrixPath", description="Path to rotation matrix file"
1523 )
1524
1525 head_out_ch_rotation: HeadOutChRotation = Field(
1526 default_factory=HeadOutChRotation, alias="HeadOutChRotation"
1527 )
1528
1529 class InRotation(BaseModel):
1530 model_config = ConfigDict(populate_by_name=True)
1531
1532 """
1533 @brief Input rotation transformation
1534
1535 @param apply bool. Apply input rotation
1536 @param matrix_path str. Path to rotation matrix file
1537 @param input_names List[str]. Names of the input layers to rotate
1538 """
1539 apply: bool = Field(default=False, description="Apply input rotation")
1540 matrix_path: str = Field(
1541 default="", alias="matrixPath", description="Path to rotation matrix file"
1542 )
1543 input_names: List[str] = Field(
1544 default=[],
1545 alias="inputNames",
1546 description="Names of the input layers to rotate",
1547 )
1548
1549 in_rotation: InRotation = Field(default_factory=InRotation, alias="InRotation")
1550
1551 class SpinR2(BaseModel):
1552 model_config = ConfigDict(populate_by_name=True)
1553
1554 """
1555 @brief SpinR2 rotation transformation
1556
1557 @param apply bool. Apply SpinR2 transformation
1558 @param learn bool. Learn rotation matrix
1559 @param matrix_path str. Path to rotation matrix file
1560 """
1561 apply: bool = Field(default=False, description="Apply SpinR2 transformation")
1562 learn: bool = Field(default=False, description="Learn rotation matrix")
1563 matrix_path: str = Field(
1564 default="", alias="matrixPath", description="Path to rotation matrix file"
1565 )
1566
1567 spin_r2: SpinR2 = Field(default_factory=SpinR2, alias="SpinR2")
1568
1569 class QkRotation(BaseModel):
1570 model_config = ConfigDict(populate_by_name=True)
1571
1572 """
1573 @brief QK rotation transformation
1574
1575 @param apply bool. Apply QK rotation transformation
1576 @param matrix_path str. Path to rotation matrix file
1577 """
1578 apply: bool = Field(
1579 default=False, description="Apply QK rotation transformation"
1580 )
1581 matrix_path: str = Field(
1582 default="", alias="matrixPath", description="Path to rotation matrix file"
1583 )
1584
1585 qk_rotation: QkRotation = Field(default_factory=QkRotation, alias="QKRotation")
1586
1587 class FlattenQuant(BaseModel):
1588 model_config = ConfigDict(populate_by_name=True)
1589
1590 """
1591 @brief Flatten quantization transformation
1592
1593 @param apply bool. Apply flatten quantization
1594 @param learn bool. Learn flattening parameters
1595 @param apply_threshold float. Threshold for applying flatten quantization
1596 @param max_overhead float. Maximum overhead allowed for flattening
1597 """
1598 apply: bool = Field(default=False, description="Apply flatten quantization")
1599 learn: bool = Field(default=False, description="Learn flattening parameters")
1600 apply_threshold: float = Field(
1601 default=0.33,
1602 alias="applyThreshold",
1603 description="Threshold for applying flatten quantization",
1604 )
1605 max_overhead: float = Field(
1606 default=0.02,
1607 alias="maxOverhead",
1608 description="Maximum overhead allowed for flattening",
1609 )
1610
1611 flatten_quant: FlattenQuant = Field(
1612 default_factory=FlattenQuant, alias="FlattenQuant"
1613 )
1614
1615 class OptimizeFfn(BaseModel):
1616 model_config = ConfigDict(populate_by_name=True)
1617
1618 """
1619 @brief FFN optimization
1620
1621 @param apply bool. Apply FFN optimization
1622 @param ch_per_ffn int. Optimize FFN split (-1 for auto)
1623 """
1624 apply: bool = Field(default=False, description="Apply FFN optimization")
1625 ch_per_ffn: int = Field(
1626 default=-1, alias="chPerFFN", description="Optimize FFN split (-1 for auto)"
1627 )
1628
1629 optimize_ffn: OptimizeFfn = Field(default_factory=OptimizeFfn, alias="OptimizeFFN")
1630
1631 def with_updates(self, **kwargs) -> "EquivalentTransformationConfig":
1632 """Return a copy with updated fields."""
1633 return self.model_copy(update=kwargs)
1634
1635
1637 """
1638 @brief Configuration for weight scale search
1639
1640 @details Defines which transformer components should have their weight scales
1641 searched for optimal quantization.
1642
1643 @param apply bool. If true, apply weight scale search
1644 @param transformer Transformer. Transformer components for weight scale search
1645 """
1646
1647 model_config = ConfigDict(
1648 populate_by_name=True,
1649 extra="forbid",
1650 )
1651
1652 apply: bool = Field(default=False, description="If true, apply weight scale search")
1653
1654 class Transformer(BaseModel):
1655 """
1656 @brief Transformer components for weight scale search
1657
1658 @param query bool. Search weight scale for query
1659 @param key bool. Search weight scale for key
1660 @param value bool. Search weight scale for value
1661 @param out bool. Search weight scale for output
1662 @param ffn bool. Search weight scale for FFN
1663 """
1664
1665 query: bool = Field(default=False, description="Search weight scale for query")
1666 key: bool = Field(default=False, description="Search weight scale for key")
1667 value: bool = Field(default=False, description="Search weight scale for value")
1668 out: bool = Field(default=False, description="Search weight scale for output")
1669 ffn: bool = Field(default=False, description="Search weight scale for FFN")
1670
1671 transformer: Transformer = Field(default_factory=Transformer, alias="transformer")
1672
1673 def with_updates(self, **kwargs) -> "SearchWeightScaleConfig":
1674 """Return a copy with updated fields."""
1675 return self.model_copy(update=kwargs)
1676
1677
1678class LoadScaleConfig(BaseModel):
1679 """
1680 @brief QAT activation scale loading configuration
1681
1682 @details Loads pre-trained activation scales from safetensors files and applies them
1683 to specified layers before scale/zeropoint computation.
1684 NOTE: entries is stored as raw JSON (type: list) because the generator does
1685 not support list[CustomStruct]. The schema records the field shape; parsing
1686 is done manually in applyQATLoadScaleQuantType / applyQATLoadScales.
1687
1688 @param apply bool. If true, load and apply QAT scales from safetensors files
1689 @param entries List. List of file entries. Each entry is a dict:
1690 { path: str, scales: [ { key: str, layers: [str] } ] }
1691 path: safetensors file path; key: tensor name in the file;
1692 layers: layer names whose activation scale will be overridden.
1693
1694 """
1695
1696 model_config = ConfigDict(
1697 populate_by_name=True,
1698 extra="forbid",
1699 )
1700
1701 apply: bool = Field(
1702 default=False,
1703 description="If true, load and apply QAT scales from safetensors files",
1704 )
1705 entries: Any = Field(
1706 default=[],
1707 description="List of file entries. Each entry is a dict: { path: str, scales: [ { key: str, layers: [str] } ] } path: safetensors file path; key: tensor name in the file; layers: layer names whose activation scale will be overridden.",
1708 )
1709
1710 def with_updates(self, **kwargs) -> "LoadScaleConfig":
1711 """Return a copy with updated fields."""
1712 return self.model_copy(update=kwargs)
1713
1714
1715class RuntimeOptions(BaseModel):
1716 """
1717 @brief Runtime options for compilation
1718
1719 @details Contains runtime-specific settings like version info and cache options.
1720
1721 @param version str. Compiler version string (e.g., 0.0.0)
1722 @param deterministic_algorithms bool. Run LUT grouping, the only compile stage found to vary between runs on one GPU, with PyTorch deterministic algorithms; false gives a faster grouping whose result can differ between runs
1723 """
1724
1725 model_config = ConfigDict(
1726 populate_by_name=True,
1727 extra="forbid",
1728 )
1729
1730 version: str = Field(
1731 default="0.0.0", description="Compiler version string (e.g., 0.0.0)"
1732 )
1733 deterministic_algorithms: bool = Field(
1734 default=True,
1735 alias="deterministicAlgorithms",
1736 description="Run LUT grouping, the only compile stage found to vary between runs on one GPU, with PyTorch deterministic algorithms; false gives a faster grouping whose result can differ between runs",
1737 )
1738
1739 def with_updates(self, **kwargs) -> "RuntimeOptions":
1740 """Return a copy with updated fields."""
1741 return self.model_copy(update=kwargs)
1742
1743
1744class SaveSampleConfig(BaseModel):
1745 """
1746 @brief Sample data generation and saving configuration
1747
1748 @param apply bool. Enable sample data saving
1749 @param mode str. Inference mode: infer (standard) or inferWithCache (LLM cache models)
1750 @param batch_size int. Number of inference batches to generate
1751 @param batch_seq_lens List. Per-batch step-wise sequence lengths for inferWithCache mode. e.g. [[80, 1], [240, 10]]
1752 @param save_folder str. Output folder for sample data
1753 @param dtype str. Data type for saved samples: float or int8
1754 """
1755
1756 model_config = ConfigDict(
1757 populate_by_name=True,
1758 extra="forbid",
1759 )
1760
1761 apply: bool = Field(default=False, description="Enable sample data saving")
1762 mode: str = Field(
1763 default="infer",
1764 description="Inference mode: infer (standard) or inferWithCache (LLM cache models)",
1765 )
1766 batch_size: int = Field(
1767 default=1,
1768 alias="batchSize",
1769 description="Number of inference batches to generate",
1770 )
1771 batch_seq_lens: Any = Field(
1772 default=[],
1773 alias="batchSeqLens",
1774 description="Per-batch step-wise sequence lengths for inferWithCache mode. e.g. [[80, 1], [240, 10]]",
1775 )
1776 save_folder: str = Field(
1777 default="sampleInout",
1778 alias="saveFolder",
1779 description="Output folder for sample data",
1780 )
1781 dtype: str = Field(
1782 default="float", description="Data type for saved samples: float or int8"
1783 )
1784
1785 def with_updates(self, **kwargs) -> "SaveSampleConfig":
1786 """Return a copy with updated fields."""
1787 return self.model_copy(update=kwargs)
1788
1789
1790class ExtraOutputConfig(BaseModel):
1791 """
1792 @brief Promote named intermediate layers to additional model outputs
1793
1794 @details Applied after quantization, so the rest of the bundle is unchanged. Names
1795 are matched against the post-fusion graph, and an unknown one is an error.
1796
1797 @param apply bool. Promote the named layers. Off by default: promotion is an explicit opt-in.
1798 @param layers List[str]. Layer names to promote; one that is already an output is left alone
1799 """
1800
1801 model_config = ConfigDict(
1802 populate_by_name=True,
1803 extra="forbid",
1804 )
1805
1806 apply: bool = Field(
1807 default=False,
1808 description="Promote the named layers. Off by default: promotion is an explicit opt-in.",
1809 )
1810 layers: List[str] = Field(
1811 default=[],
1812 description="Layer names to promote; one that is already an output is left alone",
1813 )
1814
1815 def with_updates(self, **kwargs) -> "ExtraOutputConfig":
1816 """Return a copy with updated fields."""
1817 return self.model_copy(update=kwargs)
1818
1819
1820class GroupWiseConfig(BaseModel):
1821 """
1822 @brief Group-wise streaming quantization configuration for large LLMs
1823
1824 @details Configuration for the group-wise (streaming) quantization pipeline used for
1825 LLMs that do not fit fully on GPU. Partitions the model into groups of
1826 transformer blocks and quantizes each group independently.
1827
1828 @param apply bool. Enable group-wise streaming quantization pipeline
1829 @param group_size int. Group size in number of transformer blocks (0 = auto)
1830 @param gpu_budget_gb float. GPU memory budget in GiB for group-wise execution (0 = auto-detect)
1831 @param gpu_safety_margin_gb float. Safety margin in GiB subtracted from detected GPU budget
1832 @param cache_dir str. Directory for group-wise activation/state cache (empty = system temp)
1833 @param keep_cache bool. Retain group-wise cache after run completes (for debugging)
1834 @param partition_policy str. Partitioning policy (e.g. transformer_block, moe_expert_subgroup)
1835 @param expert_groups List[List[int]]. MoE fallback partitioner: list of expert-index lists (List[List[int]]).
1836 @param retain_topology_weights bool. Keep inflated FP weights across groups (false = release after each group for tight memory budgets).
1837 @param checkpoint bool. Save per-group checkpoint so quantization can resume from the last completed group after a crash.
1838 """
1839
1840 model_config = ConfigDict(
1841 populate_by_name=True,
1842 extra="forbid",
1843 )
1844
1845 apply: bool = Field(
1846 default=False, description="Enable group-wise streaming quantization pipeline"
1847 )
1848 group_size: int = Field(
1849 default=0,
1850 alias="groupSize",
1851 description="Group size in number of transformer blocks (0 = auto)",
1852 )
1853 gpu_budget_gb: float = Field(
1854 default=0,
1855 alias="gpuBudgetGb",
1856 description="GPU memory budget in GiB for group-wise execution (0 = auto-detect)",
1857 )
1858 gpu_safety_margin_gb: float = Field(
1859 default=4,
1860 alias="gpuSafetyMarginGb",
1861 description="Safety margin in GiB subtracted from detected GPU budget",
1862 )
1863 cache_dir: str = Field(
1864 default="",
1865 alias="cacheDir",
1866 description="Directory for group-wise activation/state cache (empty = system temp)",
1867 )
1868 keep_cache: bool = Field(
1869 default=False,
1870 alias="keepCache",
1871 description="Retain group-wise cache after run completes (for debugging)",
1872 )
1873 partition_policy: str = Field(
1874 default="transformer_block",
1875 alias="partitionPolicy",
1876 description="Partitioning policy (e.g. transformer_block, moe_expert_subgroup)",
1877 )
1878 expert_groups: Any = Field(
1879 default=[],
1880 alias="expertGroups",
1881 description="MoE fallback partitioner: list of expert-index lists (List[List[int]]).",
1882 )
1883 retain_topology_weights: bool = Field(
1884 default=True,
1885 alias="retainTopologyWeights",
1886 description="Keep inflated FP weights across groups (false = release after each group for tight memory budgets).",
1887 )
1888 checkpoint: bool = Field(
1889 default=False,
1890 description="Save per-group checkpoint so quantization can resume from the last completed group after a crash.",
1891 )
1892
1893 def with_updates(self, **kwargs) -> "GroupWiseConfig":
1894 """Return a copy with updated fields."""
1895 return self.model_copy(update=kwargs)
1896
1897
1898class CompileConfig(BaseModel):
1899 """Unified compilation configuration for Mobilint MXQ compilation."""
1900
1901 model_config = ConfigDict(
1902 populate_by_name=True,
1903 extra="forbid",
1904 )
1905
1906 model_paths: List[str] = Field(
1907 default=[], alias="modelPaths", description="Paths to model files"
1908 )
1909 calib_data_path: List[str] = Field(
1910 default=[], alias="calibDataPaths", description="Paths to calibration datasets"
1911 )
1912 save_paths: List[str] = Field(
1913 default=["./tmp.mxq"],
1914 alias="savePaths",
1915 description="Output MXQ filename/paths",
1916 )
1917 use_random_calib: bool = Field(
1918 default=False, alias="useRandomCalib", description="Use random calibration"
1919 )
1920 inference_scheme: str = Field(
1921 default="single", alias="inferenceScheme", description="NPU inference scheme"
1922 )
1923 cpu_offload: bool = Field(
1924 default=False,
1925 alias="cpuOffload",
1926 description="Enable CPU offload for unsupported operators",
1927 )
1928 force_npu_input_reposition: bool = Field(
1929 default=False,
1930 alias="forceNpuInputReposition",
1931 description="Force input reposition operations to run on NPU instead of CPU",
1932 )
1933 force_npu_output_reposition: bool = Field(
1934 default=False,
1935 alias="forceNpuOutputReposition",
1936 description="Force output reposition operations to run on NPU instead of CPU",
1937 )
1938 optimize_option: int = Field(
1939 default=1,
1940 alias="optimizeOption",
1941 description="Compiler optimization selector",
1942 ge=0,
1943 )
1944 buffer_mode: int = Field(
1945 default=1, alias="bufferMode", description="Buffer serialization mode"
1946 )
1947 device: str = Field(default="gpu", description="Device for computation")
1948 dtype: str = Field(default="float", description="Data type for computation")
1949 debug: bool = Field(default=False, description="Enable debug mode")
1950 trace: bool = Field(default=False, description="Enable trace mode")
1951 image_channels: int = Field(
1952 default=0,
1953 alias="imageChannels",
1954 description="Number of image channels (0 for auto-detect)",
1955 )
1956 config_version: str = Field(
1957 default="1.0.0", alias="configVersion", description="Config schema version"
1958 )
1959 split_blocks: List[int] = Field(
1960 default=[],
1961 alias="splitBlocks",
1962 description="Multi-MXQ split points by transformer block index",
1963 )
1964 split_parts: int = Field(
1965 default=0,
1966 alias="splitParts",
1967 description="Evenly split transformer blocks into N MXQ parts",
1968 )
1969
1970 uint8_input: Uint8InputConfig = Field(
1971 default_factory=Uint8InputConfig, alias="uint8Input"
1972 )
1973 preprocessing: PreprocessingConfig = Field(default_factory=PreprocessingConfig)
1974 resource_management: ResourceManagementConfig = Field(
1975 default_factory=ResourceManagementConfig, alias="resourceManagement"
1976 )
1977 calibration: CalibrationConfig = Field(default_factory=CalibrationConfig)
1978 bit: BitConfig = Field(default_factory=BitConfig)
1979 hessian_quant: HessianQuantConfig = Field(
1980 default_factory=HessianQuantConfig, alias="hessianQuant"
1981 )
1982 bias_correction: BiasCorrectionConfig = Field(
1983 default_factory=BiasCorrectionConfig, alias="biasCorrection"
1984 )
1985 mod: ModConfig = Field(default_factory=ModConfig)
1986 llm: LlmConfig = Field(default_factory=LlmConfig)
1987 moe: MoeConfig = Field(default_factory=MoeConfig)
1988 equivalent_transformation: EquivalentTransformationConfig = Field(
1989 default_factory=EquivalentTransformationConfig, alias="equivalentTransformation"
1990 )
1991 search_weight_scale: SearchWeightScaleConfig = Field(
1992 default_factory=SearchWeightScaleConfig, alias="searchWeightScale"
1993 )
1994 load_scale: LoadScaleConfig = Field(
1995 default_factory=LoadScaleConfig, alias="loadScale"
1996 )
1997 runtime_options: RuntimeOptions = Field(
1998 default_factory=RuntimeOptions, alias="runtimeOptions"
1999 )
2000 save_sample: SaveSampleConfig = Field(
2001 default_factory=SaveSampleConfig, alias="saveSample"
2002 )
2003 extra_output: ExtraOutputConfig = Field(
2004 default_factory=ExtraOutputConfig, alias="extraOutput"
2005 )
2006 group_wise: GroupWiseConfig = Field(
2007 default_factory=GroupWiseConfig, alias="groupWise"
2008 )
2009
2010 def with_uint8_input(self, **kwargs) -> "CompileConfig":
2011 """Return a copy with uint8_input settings enabled."""
2012 data = {"apply": True, **kwargs}
2013 new_cfg = self.uint8_input.model_copy(update=data)
2014 return self.model_copy(update={"uint8_input": new_cfg})
2015
2016 def with_preprocessing(self, **kwargs) -> "CompileConfig":
2017 """Return a copy with preprocessing settings enabled."""
2018 data = {"apply": True, **kwargs}
2019 new_cfg = self.preprocessing.model_copy(update=data)
2020 return self.model_copy(update={"preprocessing": new_cfg})
2021
2022 def with_hessian_quant(self, **kwargs) -> "CompileConfig":
2023 """Return a copy with hessian_quant settings enabled."""
2024 data = {"apply": True, **kwargs}
2025 new_cfg = self.hessian_quant.model_copy(update=data)
2026 return self.model_copy(update={"hessian_quant": new_cfg})
2027
2028 def with_bias_correction(self, **kwargs) -> "CompileConfig":
2029 """Return a copy with bias_correction settings enabled."""
2030 data = {"apply": True, **kwargs}
2031 new_cfg = self.bias_correction.model_copy(update=data)
2032 return self.model_copy(update={"bias_correction": new_cfg})
2033
2034 def with_mod(self, **kwargs) -> "CompileConfig":
2035 """Return a copy with mod settings enabled."""
2036 data = {"apply": True, **kwargs}
2037 new_cfg = self.mod.model_copy(update=data)
2038 return self.model_copy(update={"mod": new_cfg})
2039
2040 def with_llm(self, **kwargs) -> "CompileConfig":
2041 """Return a copy with llm settings enabled."""
2042 data = {"apply": True, **kwargs}
2043 new_cfg = self.llm.model_copy(update=data)
2044 return self.model_copy(update={"llm": new_cfg})
2045
2046 def with_search_weight_scale(self, **kwargs) -> "CompileConfig":
2047 """Return a copy with search_weight_scale settings enabled."""
2048 data = {"apply": True, **kwargs}
2049 new_cfg = self.search_weight_scale.model_copy(update=data)
2050 return self.model_copy(update={"search_weight_scale": new_cfg})
2051
2052 def with_load_scale(self, **kwargs) -> "CompileConfig":
2053 """Return a copy with load_scale settings enabled."""
2054 data = {"apply": True, **kwargs}
2055 new_cfg = self.load_scale.model_copy(update=data)
2056 return self.model_copy(update={"load_scale": new_cfg})
2057
2058 def with_save_sample(self, **kwargs) -> "CompileConfig":
2059 """Return a copy with save_sample settings enabled."""
2060 data = {"apply": True, **kwargs}
2061 new_cfg = self.save_sample.model_copy(update=data)
2062 return self.model_copy(update={"save_sample": new_cfg})
2063
2064 def with_extra_output(self, **kwargs) -> "CompileConfig":
2065 """Return a copy with extra_output settings enabled."""
2066 data = {"apply": True, **kwargs}
2067 new_cfg = self.extra_output.model_copy(update=data)
2068 return self.model_copy(update={"extra_output": new_cfg})
2069
2070 def with_group_wise(self, **kwargs) -> "CompileConfig":
2071 """Return a copy with group_wise settings enabled."""
2072 data = {"apply": True, **kwargs}
2073 new_cfg = self.group_wise.model_copy(update=data)
2074 return self.model_copy(update={"group_wise": new_cfg})
2075
2076 @classmethod
2077 def from_file(cls, path: Union[str, Path]) -> "CompileConfig":
2078 """Load config from YAML or JSON file."""
2079 path = Path(path)
2080 with open(path) as f:
2081 if path.suffix in (".yaml", ".yml"):
2082 data = yaml.safe_load(f)
2083 else:
2084 data = json.load(f)
2085 data = cls._flatten_grouped_json(data)
2086 return cls.model_validate(data)
2087
2088 @staticmethod
2089 def _flatten_grouped_json(data: dict) -> dict:
2090 """Flatten grouped JSON keys (e.g. quantization.calibration) to flat structure."""
2091 data = data.copy()
2092 if "quantization" in data:
2093 group = data.pop("quantization")
2094 if "calibration" in group:
2095 data["calibration"] = group["calibration"]
2096 if "bit" in group:
2097 data["bit"] = group["bit"]
2098 if "advancedQuantization" in data:
2099 group = data.pop("advancedQuantization")
2100 if "hessianQuant" in group:
2101 data["hessianQuant"] = group["hessianQuant"]
2102 if "biasCorrection" in group:
2103 data["biasCorrection"] = group["biasCorrection"]
2104 if "mod" in group:
2105 data["mod"] = group["mod"]
2106 if "EquivalentTransformation" in group:
2107 data["equivalentTransformation"] = group["EquivalentTransformation"]
2108 if "searchWeightScale" in group:
2109 data["searchWeightScale"] = group["searchWeightScale"]
2110 if "loadScale" in group:
2111 data["loadScale"] = group["loadScale"]
2112 return data
2113
2114 @classmethod
2115 def from_preset(cls, name: str) -> "CompileConfig":
2116 """Load config from a preset."""
2117 from .presets import get_preset
2118
2119 return get_preset(name)
2120
2121 @staticmethod
2122 def _group_to_json(data: dict) -> dict:
2123 """Group flat keys back into nested JSON structure (inverse of _flatten_grouped_json)."""
2124 data = data.copy()
2125 quantization_group = {}
2126 if "calibration" in data:
2127 quantization_group["calibration"] = data.pop("calibration")
2128 if "bit" in data:
2129 quantization_group["bit"] = data.pop("bit")
2130 if quantization_group:
2131 data["quantization"] = quantization_group
2132 advancedQuantization_group = {}
2133 if "hessianQuant" in data:
2134 advancedQuantization_group["hessianQuant"] = data.pop("hessianQuant")
2135 if "biasCorrection" in data:
2136 advancedQuantization_group["biasCorrection"] = data.pop("biasCorrection")
2137 if "mod" in data:
2138 advancedQuantization_group["mod"] = data.pop("mod")
2139 if "equivalentTransformation" in data:
2140 advancedQuantization_group["EquivalentTransformation"] = data.pop(
2141 "equivalentTransformation"
2142 )
2143 if "searchWeightScale" in data:
2144 advancedQuantization_group["searchWeightScale"] = data.pop(
2145 "searchWeightScale"
2146 )
2147 if "loadScale" in data:
2148 advancedQuantization_group["loadScale"] = data.pop("loadScale")
2149 if advancedQuantization_group:
2150 data["advancedQuantization"] = advancedQuantization_group
2151 return data
2152
2153 def to_file(self, path: Union[str, Path]) -> None:
2154 """Save config to YAML or JSON file."""
2155 path = Path(path)
2156 data = self.model_dump(by_alias=True, exclude_none=True)
2157 data = self._group_to_json(data)
2158 with open(path, "w") as f:
2159 if path.suffix in (".yaml", ".yml"):
2160 yaml.dump(data, f, default_flow_style=False)
2161 else:
2162 json.dump(data, f, indent=2)
Configuration for in-schedule integer bias correction.
Definition models.py:855
"BiasCorrectionConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:909
FFN activation bit-widths (int shorthand sets all sublayers)
Definition models.py:522
FFN weight bit-widths (int shorthand sets all sublayers)
Definition models.py:577
Configuration for bit precision.
Definition models.py:471
"BitConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:765
Configuration for calibration during quantization.
Definition models.py:150
"CalibrationConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:466
Unified compilation configuration for Mobilint MXQ compilation.
Definition models.py:1898
"CompileConfig" with_extra_output(self, **kwargs)
Return a copy with extra_output settings enabled.
Definition models.py:2064
"CompileConfig" with_bias_correction(self, **kwargs)
Return a copy with bias_correction settings enabled.
Definition models.py:2028
"CompileConfig" with_llm(self, **kwargs)
Return a copy with llm settings enabled.
Definition models.py:2040
"CompileConfig" from_preset(cls, str name)
Load config from a preset.
Definition models.py:2115
"CompileConfig" with_search_weight_scale(self, **kwargs)
Return a copy with search_weight_scale settings enabled.
Definition models.py:2046
SearchWeightScaleConfig search_weight_scale
Definition models.py:1991
"CompileConfig" with_load_scale(self, **kwargs)
Return a copy with load_scale settings enabled.
Definition models.py:2052
BiasCorrectionConfig bias_correction
Definition models.py:1982
"CompileConfig" with_preprocessing(self, **kwargs)
Return a copy with preprocessing settings enabled.
Definition models.py:2016
dict _flatten_grouped_json(dict data)
Flatten grouped JSON keys (e.g.
Definition models.py:2089
"CompileConfig" with_mod(self, **kwargs)
Return a copy with mod settings enabled.
Definition models.py:2034
"CompileConfig" with_hessian_quant(self, **kwargs)
Return a copy with hessian_quant settings enabled.
Definition models.py:2022
dict _group_to_json(dict data)
Group flat keys back into nested JSON structure (inverse of _flatten_grouped_json).
Definition models.py:2122
"CompileConfig" with_save_sample(self, **kwargs)
Return a copy with save_sample settings enabled.
Definition models.py:2058
"CompileConfig" from_file(cls, Union[str, Path] path)
Load config from YAML or JSON file.
Definition models.py:2077
None to_file(self, Union[str, Path] path)
Save config to YAML or JSON file.
Definition models.py:2153
"CompileConfig" with_uint8_input(self, **kwargs)
Return a copy with uint8_input settings enabled.
Definition models.py:2010
Configuration for equivalent transformation techniques.
Definition models.py:1335
"EquivalentTransformationConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:1631
Promote named intermediate layers to additional model outputs.
Definition models.py:1790
"ExtraOutputConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:1815
Configuration for HessianQuant algorithm.
Definition models.py:770
"HessianQuantConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:850
Configuration for Large Language Model (LLM) compilation.
Definition models.py:1154
"LlmConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:1291
"LoadScaleConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:1710
Configuration for Minimum Output Difference algorithm.
Definition models.py:914
"ModConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:1149
Sparse MoE expert-selection configuration (calibration only)
Definition models.py:1296
"MoeConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:1330
Configuration for input preprocessing pipeline.
Definition models.py:47
"PreprocessingConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:91
Configuration for resource management during model compilation.
Definition models.py:96
"ResourceManagementConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:145
Runtime options for compilation.
Definition models.py:1715
"RuntimeOptions" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:1739
Sample data generation and saving configuration.
Definition models.py:1744
"SaveSampleConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:1785
Transformer components for weight scale search.
Definition models.py:1654
Configuration for weight scale search.
Definition models.py:1636
"SearchWeightScaleConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:1673
Configuration for uint8 input handling.
Definition models.py:13
"Uint8InputConfig" with_updates(self, **kwargs)
Return a copy with updated fields.
Definition models.py:42