presets.py Source File

presets.py Source File#

Mobilint SDK qb Compiler: presets.py Source File
Mobilint SDK qb Compiler v1.2
MCS002-EN
presets.py
1"""Auto-generated preset definitions from config_schema.yaml."""
2
3from typing import Dict, List
4from .models import CompileConfig
5
6# Preset definitions
7PRESETS: Dict[str, dict] = {
8 "classification": {
9 "description": "Image classification models (ResNet, EfficientNet, ViT, etc.)",
10 "config": {"calibration": {"mode": 1, "output": 0}},
11 },
12 "detection": {
13 "description": "Object detection models (YOLO, SSD, DETR, etc.)",
14 "config": {"calibration": {"mode": 1, "output": 1}},
15 },
16 "classification_torchvision": {
17 "description": "Torchvision classification models with standard preprocessing",
18 "extends": "classification",
19 "config": {
20 "uint8Input": {"apply": True, "inputs": []},
21 "imageChannels": 3,
22 "preprocessing": {
23 "apply": True,
24 "autoConvertFormat": True,
25 "pipeline": [
26 {"op": "resize", "height": 256, "width": 256, "mode": "bilinear"},
27 {"op": "centerCrop", "height": 224, "width": 224},
28 {
29 "op": "normalize",
30 "mean": [0.485, 0.456, 0.406],
31 "std": [0.229, 0.224, 0.225],
32 "scaleToUint8": True,
33 "fuseIntoFirstLayer": True,
34 },
35 ],
36 "inputConfigs": {},
37 },
38 },
39 },
40 "yolo_640": {
41 "description": "YOLO detection models with 640x640 letterbox preprocessing",
42 "extends": "detection",
43 "config": {
44 "uint8Input": {"apply": True, "inputs": []},
45 "imageChannels": 3,
46 "preprocessing": {
47 "apply": True,
48 "autoConvertFormat": True,
49 "pipeline": [
50 {"op": "letterbox", "height": 640, "width": 640, "padValue": 114}
51 ],
52 "inputConfigs": {},
53 },
54 },
55 },
56 "yolo_1280": {
57 "description": "YOLO detection models with 1280x1280 letterbox preprocessing",
58 "extends": "detection",
59 "config": {
60 "uint8Input": {"apply": True, "inputs": []},
61 "imageChannels": 3,
62 "preprocessing": {
63 "apply": True,
64 "autoConvertFormat": True,
65 "pipeline": [
66 {"op": "letterbox", "height": 1280, "width": 1280, "padValue": 114}
67 ],
68 "inputConfigs": {},
69 },
70 },
71 },
72 "llm": {
73 "description": "Large Language Models (LLaMA, Qwen, Gemma, etc.)",
74 "config": {
75 "llm": {
76 "apply": True,
77 "attributes": {"maxSequenceLength": 4096, "maxCacheLength": 4096},
78 },
79 "calibration": {"mode": 0, "output": 0},
80 },
81 },
82 "llm_fast": {
83 "description": "LLM with faster compilation (less accuracy optimization)",
84 "extends": "llm",
85 "config": {
86 "optq": {"apply": False},
87 "equivalentTransformation": {
88 "QK": {"apply": True},
89 "UD": {"apply": True},
90 "VO": {"apply": True},
91 "SpinR1": {"apply": True},
92 "SpinR2": {"apply": True},
93 "OptimizeFFN": {"apply": True},
94 },
95 "llm": {
96 "apply": True,
97 "attributes": {"calibration": {"useFullSeqLength": True}},
98 },
99 },
100 },
101 "vision_transformer": {
102 "description": "Vision Transformer models (ViT, DeiT, Swin, etc.)",
103 "config": {
104 "calibration": {"method": 1, "mode": 0},
105 "bit": {"transformer": {"activation": {"output": 16, "ffn": 16}}},
106 },
107 },
108 "multimodal": {
109 "description": "Multimodal models (CLIP, BLIP, LLaVA, etc.)",
110 "config": {"calibration": {"method": 3}, "llm": {"apply": True}},
111 },
112}
113
114
115def list_presets() -> List[str]:
116 """List available preset names."""
117 return list(PRESETS.keys())
118
119
120def get_preset(name: str) -> CompileConfig:
121 """Get a CompileConfig from a preset name."""
122 if name not in PRESETS:
123 available = ", ".join(list_presets())
124 raise ValueError(f"Unknown preset '{name}'. Available: {available}")
125
126 preset = PRESETS[name]
127 config_data = preset["config"].copy()
128
129 # Handle inheritance
130 if "extends" in preset:
131 base = get_preset(preset["extends"])
132 base_data = base.model_dump(by_alias=True, exclude_none=True)
133 # Deep merge
134 _deep_merge(base_data, config_data)
135 config_data = base_data
136
137 return CompileConfig.model_validate(config_data)
138
139
140def _deep_merge(base: dict, override: dict) -> None:
141 """Deep merge override into base (mutates base)."""
142 for key, value in override.items():
143 if key in base and isinstance(base[key], dict) and isinstance(value, dict):
144 _deep_merge(base[key], value)
145 else:
146 base[key] = value
147
148
149class Preset:
150 """Preset utility class."""
151
152 @staticmethod
153 def list() -> List[str]:
154 """List available preset names."""
155 return list_presets()
156
157 @staticmethod
158 def get(name: str) -> CompileConfig:
159 """Get a preset by name."""
160 return get_preset(name)
161
162 @staticmethod
163 def describe(name: str) -> str:
164 """Get preset description."""
165 if name not in PRESETS:
166 raise ValueError(f"Unknown preset: {name}")
167 return PRESETS[name].get("description", "")
Preset utility class.
Definition presets.py:149
str describe(str name)
Get preset description.
Definition presets.py:163
List[str] list()
List available preset names.
Definition presets.py:153
CompileConfig get(str name)
Get a preset by name.
Definition presets.py:158
List[str] list_presets()
List available preset names.
Definition presets.py:115
None _deep_merge(dict base, dict override)
Deep merge override into base (mutates base).
Definition presets.py:140
CompileConfig get_preset(str name)
Get a CompileConfig from a preset name.
Definition presets.py:120