mod_config.py Source File

mod_config.py Source File#

Mobilint SDK qb Compiler: mod_config.py Source File
Mobilint SDK qb Compiler v0.12.0.0
MCS002-KR
mod_config.py
1import numbers
2from typing import List, Optional
3from typeguard import typechecked
4from . import ConfigABC
5
6
12
13
14class ModConfig(ConfigABC):
15 """
16 @brief Configuration for Minimum Output Difference algorithm.
17
18 @details Defines parameters controlling whether and how Mod is applied during quantization, including layer-level inclusion/exclusion lists.
19 """
20
21 optional = True
22 DEFAULTS = {
23 "apply": False,
24 "epochs": 4,
25 "warmup_epochs": 1,
26 "lr_act_scale": 0.0,
27 "lr_zeropoint": 0.0,
28 "lr_weight_scale": 0.0,
29 "lr_weight": 4e-6,
30 "lr_bias": 4e-6,
31 "lr_min_ratio": 1e-4,
32 "batch_size": 1,
33 "q_drop": 0.0,
34 "quantize_weight": True,
35 "weight_scale_init": "MinMax",
36 "downresol_mode": "STE",
37 "post_processing": "",
38 "box_conf_thres": 0,
39 "box_iou_thres": 0,
40 "loss_type": "MSE",
41 "use_outputs": False,
42 "kl_temperature": 1.0,
43 "recon_prob": 1.0,
44 "recon_coeff": 1.0,
45 "lambda_0": 1.0,
46 "lambda_1": 1.0,
47 "lambda_2": 1.0,
48 "anchors": [
49 [[12, 16], [19, 36], [40, 28]],
50 [[36, 75], [76, 55], [72, 146]],
51 [[142, 110], [192, 243], [459, 401]],
52 ],
53 "use_xyxy": False,
54 "save_dir": "",
55 "seed": 0,
56 "apply_layers": [],
57 "exclude_layers": [],
58 }
59
60 py_to_cpp_name = {
61 "apply": "apply",
62 "epochs": "epochs",
63 "warmup_epochs": "warmupEpochs",
64 "lr_min_ratio": "lrMinRatio",
65 "lr_act_scale": "actScaleLR",
66 "lr_zeropoint": "zeropointLR",
67 "lr_weight_scale": "weightScaleLR",
68 "lr_weight": "weightLR",
69 "lr_bias": "biasLR",
70 "batch_size": "batchSize",
71 "q_drop": "qDrop",
72 "quantize_weight": "quantizeWeight",
73 "weight_scale_init": "weightScaleInit",
74 "downresol_mode": "downresolMode",
75 "post_processing": "post",
76 "box_conf_thres": "boxConfThres",
77 "box_iou_thres": "boxIoUThres",
78 "loss_type": "lossType",
79 "use_outputs": "useOutputs",
80 "kl_temperature": "KLTemperature",
81 "recon_prob": "reconProb",
82 "recon_coeff": "reconCoeff",
83 "lambda_0": "lambda_0",
84 "lambda_1": "lambda_1",
85 "lambda_2": "lambda_2",
86 "use_xyxy": "useXYXY",
87 "save_dir": "saveDir",
88 "seed": "seed",
89 }
90
91 min_max_check = {
92 "epochs": [0, None],
93 "warmup_epochs": [0, None],
94 "lr_min_ratio": [0, None],
95 "lr_act_scale": [0, None],
96 "lr_zeropoint": [0, None],
97 "lr_weight_scale": [0, None],
98 "lr_weight": [0, None],
99 "lr_bias": [0, None],
100 "batch_size": [0, None],
101 "q_drop": [0, 1],
102 "box_conf_thres": [0, None],
103 "box_iou_thres": [0, None],
104 "kl_temperature": [0, None],
105 "recon_prob": [0, 1],
106 "recon_coeff": [0, None],
107 "lambda_0": [0, None],
108 "lambda_1": [0, None],
109 "lambda_2": [0, None],
110 "seed": [0, None],
111 }
112
113 @typechecked
115 self,
116 apply: bool,
117 epochs: int,
118 warmup_epochs: int,
119 lr_act_scale: numbers.Number,
120 lr_zeropoint: numbers.Number,
121 lr_weight_scale: numbers.Number,
122 lr_weight: numbers.Number,
123 lr_bias: numbers.Number,
124 lr_min_ratio: numbers.Number,
125 batch_size: int,
126 q_drop: numbers.Number,
127 quantize_weight: bool,
128 weight_scale_init: str,
129 downresol_mode: str,
130 post_processing: str,
131 box_conf_thres: numbers.Number,
132 box_iou_thres: numbers.Number,
133 loss_type: str,
134 use_outputs: bool,
135 kl_temperature: numbers.Number,
136 recon_prob: numbers.Number,
137 recon_coeff: numbers.Number,
138 lambda_0: numbers.Number,
139 lambda_1: numbers.Number,
140 lambda_2: numbers.Number,
141 anchors: List,
142 use_xyxy: bool,
143 save_dir: str,
144 seed: int,
145 apply_layers: List[str],
146 exclude_layers: List[str],
147 optional: bool = optional,
148 ):
149 """
150 @brief Initialize the ModConfig.
151 @param apply bool. Whether to apply MOD.
152 @param epochs int. Number of training epochs.
153 @param warmup_epochs int. Number of warmup epochs.
154 @param lr_act_scale float. Learning rate for activation scale.
155 @param lr_zeropoint float. Learning rate for zeropoint.
156 @param lr_weight_scale float. Learning rate for weight scale.
157 @param lr_weight float. Learning rate for weight.
158 @param lr_bias float. Learning rate for bias.
159 @param lr_min_ratio float. Minimum learning rate ratio for scheduler.
160 @param batch_size int. Batch size for training.
161 @param q_drop float. QDrop ratio.
162 @param quantize_weight bool. Quantize weight during training.
163 @param weight_scale_init str. Weight scale initialization method.
164 @param downresol_mode str. Down-resolution mode for fake quantization.
165 @param post_processing str. Post-processing function.
166 @param box_conf_thres float. Box confidence threshold for NMS.
167 @param box_iou_thres float. Box IoU threshold for NMS.
168 @param loss_type str. Loss function type.
169 @param use_outputs bool. Compare only intermediate layers.
170 @param kl_temperature float. Temperature for KL divergence loss.
171 @param recon_prob float. Sampling probability for intermediate layers.
172 @param recon_coeff float. Coefficient for intermediate layer loss.
173 @param lambda_0 float. First coefficient for AnchorlessDetection loss.
174 @param lambda_1 float. Second coefficient for AnchorlessDetection loss.
175 @param lambda_2 float. Third coefficient for AnchorlessDetection loss.
176 @param anchors List. Anchor boxes configuration.
177 @param use_xyxy bool. Use XYXY format.
178 @param save_dir str. Directory to save results.
179 @param seed int. Random seed.
180 @param apply_layers List[str]. Layers to apply MOD.
181 @param exclude_layers List[str]. Layers to exclude from MOD.
182 @param optional bool. Indicates whether this config is optional. Use the default value defined as a class variable.
183 """
184 super().__init__(optional)
185 self.apply = apply
186 self.epochs = epochs
187 self.warmup_epochs = warmup_epochs
188 self.lr_act_scale = lr_act_scale
189 self.lr_zeropoint = lr_zeropoint
190 self.lr_weight_scale = lr_weight_scale
191 self.lr_weight = lr_weight
192 self.lr_bias = lr_bias
193 self.lr_min_ratio = lr_min_ratio
194 self.batch_size = batch_size
195 self.q_drop = q_drop
196 self.quantize_weight = quantize_weight
197 self.weight_scale_init = weight_scale_init
198 self.downresol_mode = downresol_mode
199 self.post_processing = post_processing
200 self.box_conf_thres = box_conf_thres
201 self.box_iou_thres = box_iou_thres
202 self.loss_type = loss_type
203 self.use_outputs = use_outputs
204 self.kl_temperature = kl_temperature
205 self.recon_prob = recon_prob
206 self.recon_coeff = recon_coeff
207 self.lambda_0 = lambda_0
208 self.lambda_1 = lambda_1
209 self.lambda_2 = lambda_2
210 self.anchors = anchors
211 self.use_xyxy = use_xyxy
212 self.save_dir = save_dir
213 self.seed = seed
214 self.apply_layers = apply_layers
215 self.exclude_layers = exclude_layers
216 self.check_valid(
217 except_list=["optional", "anchors", "apply_layers", "exclude_layers"]
218 )
219
220 @classmethod
221 def default_config(cls):
222 return cls(**cls.DEFAULTS, optional=cls.optional)
223
224 @classmethod
225 def from_dict(cls, config_dict: dict):
226 """Create ModConfig from dictionary (JSON structure)"""
227 params = cls.DEFAULTS.copy()
228
229 if "apply" in config_dict:
230 params["apply"] = config_dict["apply"]
231
232 if "attributes" in config_dict:
233 attributes = config_dict["attributes"]
234
235 if "epochs" in attributes:
236 params["epochs"] = attributes["epochs"]
237 if "warmupEpochs" in attributes:
238 params["warmup_epochs"] = attributes["warmupEpochs"]
239 if "lrMinRatio" in attributes:
240 params["lr_min_ratio"] = attributes["lrMinRatio"]
241 if "anchors" in attributes:
242 params["anchors"] = attributes["anchors"]
243 if "useXYXY" in attributes:
244 params["use_xyxy"] = attributes["useXYXY"]
245 if "saveDir" in attributes:
246 params["save_dir"] = attributes["saveDir"]
247 if "seed" in attributes:
248 params["seed"] = attributes["seed"]
249 if "applyLayers" in attributes:
250 params["apply_layers"] = attributes["applyLayers"]
251 if "excludeLayers" in attributes:
252 params["exclude_layers"] = attributes["excludeLayers"]
253
254 if "learningRates" in attributes:
255 learning_rates = attributes["learningRates"]
256 if "actScale" in learning_rates:
257 params["lr_act_scale"] = learning_rates["actScale"]
258 if "zeropoint" in learning_rates:
259 params["lr_zeropoint"] = learning_rates["zeropoint"]
260 if "weightScale" in learning_rates:
261 params["lr_weight_scale"] = learning_rates["weightScale"]
262 if "weight" in learning_rates:
263 params["lr_weight"] = learning_rates["weight"]
264 if "bias" in learning_rates:
265 params["lr_bias"] = learning_rates["bias"]
266
267 if "training" in attributes:
268 training = attributes["training"]
269 if "batchSize" in training:
270 params["batch_size"] = training["batchSize"]
271 if "qDrop" in training:
272 params["q_drop"] = training["qDrop"]
273 if "quantizeWeight" in training:
274 params["quantize_weight"] = training["quantizeWeight"]
275 if "weightScaleInit" in training:
276 params["weight_scale_init"] = training["weightScaleInit"]
277 if "downresolMode" in training:
278 params["downresol_mode"] = training["downresolMode"]
279
280 if "postProcessing" in attributes:
281 post_processing = attributes["postProcessing"]
282 if "post" in post_processing:
283 params["post_processing"] = post_processing["post"]
284 if "boxConfThres" in post_processing:
285 params["box_conf_thres"] = post_processing["boxConfThres"]
286 if "boxIoUThres" in post_processing:
287 params["box_iou_thres"] = post_processing["boxIoUThres"]
288
289 if "loss" in attributes:
290 loss = attributes["loss"]
291 if "type" in loss:
292 params["loss_type"] = loss["type"]
293 if "useOutputs" in loss:
294 params["use_outputs"] = loss["useOutputs"]
295 if "KLTemperature" in loss:
296 params["kl_temperature"] = loss["KLTemperature"]
297 if "reconProb" in loss:
298 params["recon_prob"] = loss["reconProb"]
299 if "reconCoeff" in loss:
300 params["recon_coeff"] = loss["reconCoeff"]
301 if "lambda_0" in loss:
302 params["lambda_0"] = loss["lambda_0"]
303 if "lambda_1" in loss:
304 params["lambda_1"] = loss["lambda_1"]
305 if "lambda_2" in loss:
306 params["lambda_2"] = loss["lambda_2"]
307
308 return cls(**params)
309
310 def to_dict(self):
311 return {
312 "apply": self.apply,
313 "attributes": {
314 "epochs": self.epochs,
315 "warmupEpochs": self.warmup_epochs,
316 "learningRates": {
317 "actScale": self.lr_act_scale,
318 "zeropoint": self.lr_zeropoint,
319 "weightScale": self.lr_weight_scale,
320 "weight": self.lr_weight,
321 "bias": self.lr_bias,
322 },
323 "lrMinRatio": self.lr_min_ratio,
324 "training": {
325 "batchSize": self.batch_size,
326 "qDrop": self.q_drop,
327 "quantizeWeight": self.quantize_weight,
328 "weightScaleInit": self.weight_scale_init,
329 "downresolMode": self.downresol_mode,
330 },
331 "postProcessing": {
332 "post": self.post_processing,
333 "boxConfThres": self.box_conf_thres,
334 "boxIoUThres": self.box_iou_thres,
335 },
336 "loss": {
337 "type": self.loss_type,
338 "useOutputs": self.use_outputs,
339 "KLTemperature": self.kl_temperature,
340 "reconProb": self.recon_prob,
341 "reconCoeff": self.recon_coeff,
342 "lambda_0": self.lambda_0,
343 "lambda_1": self.lambda_1,
344 "lambda_2": self.lambda_2,
345 },
346 "anchors": self.anchors,
347 "useXYXY": self.use_xyxy,
348 "saveDir": self.save_dir,
349 "seed": self.seed,
350 "applyLayers": self.apply_layers,
351 "excludeLayers": self.exclude_layers,
352 },
353 }
354
355
356def get_mod_config(**kwargs) -> ModConfig:
357 """
358 @brief Create ModConfig with partial parameters merged with defaults.
359 """
360 params = ModConfig.DEFAULTS.copy()
361 params.update(kwargs)
362 return ModConfig(**params, optional=ModConfig.optional)
363
364
365# @}
Configuration for Minimum Output Difference algorithm.
Definition mod_config.py:14
__init__(self, bool apply, int epochs, int warmup_epochs, numbers.Number lr_act_scale, numbers.Number lr_zeropoint, numbers.Number lr_weight_scale, numbers.Number lr_weight, numbers.Number lr_bias, numbers.Number lr_min_ratio, int batch_size, numbers.Number q_drop, bool quantize_weight, str weight_scale_init, str downresol_mode, str post_processing, numbers.Number box_conf_thres, numbers.Number box_iou_thres, str loss_type, bool use_outputs, numbers.Number kl_temperature, numbers.Number recon_prob, numbers.Number recon_coeff, numbers.Number lambda_0, numbers.Number lambda_1, numbers.Number lambda_2, List anchors, bool use_xyxy, str save_dir, int seed, List[str] apply_layers, List[str] exclude_layers, bool optional=optional)
Initialize the ModConfig.
ModConfig get_mod_config(**kwargs)
Create ModConfig with partial parameters merged with defaults.
from_dict(cls, dict config_dict)
Create ModConfig from dictionary (JSON structure)