presets.py Source File

presets.py Source File#

Mobilint SDK qb Compiler: presets.py Source File
Mobilint SDK qb Compiler v1.4
MCS002-KR
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", "size": 256, "mode": "bilinear", "backend": "pil"},
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 {
51 "op": "letterbox",
52 "height": 640,
53 "width": 640,
54 "padValue": 114,
55 "backend": "opencv",
56 }
57 ],
58 "inputConfigs": {},
59 },
60 },
61 },
62 "yolo_1280": {
63 "description": "YOLO detection models with 1280x1280 letterbox preprocessing",
64 "extends": "detection",
65 "config": {
66 "uint8Input": {"apply": True, "inputs": []},
67 "imageChannels": 3,
68 "preprocessing": {
69 "apply": True,
70 "autoConvertFormat": True,
71 "pipeline": [
72 {
73 "op": "letterbox",
74 "height": 1280,
75 "width": 1280,
76 "padValue": 114,
77 "backend": "opencv",
78 }
79 ],
80 "inputConfigs": {},
81 },
82 },
83 },
84 "llm": {
85 "description": "Large Language Models (LLaMA, Qwen, Gemma, etc.)",
86 "config": {
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": {
98 "maxSequenceLength": 4096,
99 "maxCacheLength": 4096,
100 "calibration": {"useFullSeqLength": True},
101 },
102 },
103 "calibration": {"mode": 0, "output": 0},
104 },
105 },
106 "llm_fast": {
107 "description": "LLM with faster compilation (less accuracy optimization)",
108 "extends": "llm",
109 "config": {
110 "equivalentTransformation": {
111 "QK": {"apply": False},
112 "UD": {"apply": False},
113 "VO": {"apply": False},
114 "SpinR1": {"apply": False},
115 "SpinR2": {"apply": False},
116 "OptimizeFFN": {"apply": False},
117 },
118 "llm": {
119 "apply": True,
120 "attributes": {"calibration": {"useFullSeqLength": False}},
121 },
122 },
123 },
124 "vision_transformer": {
125 "description": "Vision Transformer models (ViT, DeiT, Swin, etc.)",
126 "config": {
127 "calibration": {"method": 1, "mode": 0},
128 "bit": {"transformer": {"activation": {"output": 16, "ffn": 16}}},
129 },
130 },
131 "multimodal": {
132 "description": "Multimodal models (CLIP, BLIP, LLaVA, etc.)",
133 "config": {"calibration": {"method": 3}, "llm": {"apply": True}},
134 },
135}
136
137
138def list_presets() -> List[str]:
139 """List available preset names."""
140 return list(PRESETS.keys())
141
142
143def get_preset(name: str) -> CompileConfig:
144 """Get a CompileConfig from a preset name."""
145 if name not in PRESETS:
146 available = ", ".join(list_presets())
147 raise ValueError(f"Unknown preset '{name}'. Available: {available}")
148
149 preset = PRESETS[name]
150 config_data = preset["config"].copy()
151
152 # Handle inheritance
153 if "extends" in preset:
154 base = get_preset(preset["extends"])
155 base_data = base.model_dump(by_alias=True, exclude_none=True)
156 # Deep merge
157 _deep_merge(base_data, config_data)
158 config_data = base_data
159
160 return CompileConfig.model_validate(config_data)
161
162
163def _deep_merge(base: dict, override: dict) -> None:
164 """Deep merge override into base (mutates base)."""
165 for key, value in override.items():
166 if key in base and isinstance(base[key], dict) and isinstance(value, dict):
167 _deep_merge(base[key], value)
168 else:
169 base[key] = value
170
171
172class Preset:
173 """Preset utility class."""
174
175 @staticmethod
176 def list() -> List[str]:
177 """List available preset names."""
178 return list_presets()
179
180 @staticmethod
181 def get(name: str) -> CompileConfig:
182 """Get a preset by name."""
183 return get_preset(name)
184
185 @staticmethod
186 def describe(name: str) -> str:
187 """Get preset description."""
188 if name not in PRESETS:
189 raise ValueError(f"Unknown preset: {name}")
190 return PRESETS[name].get("description", "")
Preset utility class.
Definition presets.py:172
str describe(str name)
Get preset description.
Definition presets.py:186
List[str] list()
List available preset names.
Definition presets.py:176
CompileConfig get(str name)
Get a preset by name.
Definition presets.py:181
List[str] list_presets()
List available preset names.
Definition presets.py:138
None _deep_merge(dict base, dict override)
Deep merge override into base (mutates base).
Definition presets.py:163
CompileConfig get_preset(str name)
Get a CompileConfig from a preset name.
Definition presets.py:143