resource_management_config.py Source File

resource_management_config.py Source File#

Mobilint SDK qb Compiler: resource_management_config.py Source File
Mobilint SDK qb Compiler v0.12.0.0
MCS002-EN
resource_management_config.py
1from typeguard import typechecked
2from . import ConfigABC
3
4
5
11class ResourceManagementConfig(ConfigABC):
12 """
13 @brief Configuration for resource management during model compilation.
14 """
15
16 optional = False
17 DEFAULTS = {
18 "weight_dtype": "float32",
19 "use_gpu_only_for_calibration": True,
20 "weight_memory_method": 1,
21 }
22
23 py_to_cpp_name = {
24 "weight_dtype": "weightDtype",
25 "use_gpu_only_for_calibration": "useGPUOnlyForCalibration",
26 "weight_memory_method": "weightMemoryMethod",
27 }
28
29 min_max_check = {
30 "weight_memory_method": [0, 4],
31 }
32
33 weight_memory_methods = [
34 "DeleteFloat",
35 "SaveFloat",
36 "MoveFloat",
37 "KeepFloat",
38 "KeepAll",
39 ]
40
42 self,
43 weight_dtype: str,
44 use_gpu_only_for_calibration: bool,
45 weight_memory_method: int,
46 optional: bool = optional,
47 ):
48 """
49 @brief Initialize the ResourceManagementConfig.
50 @param weight_dtype str. Weight data type for calibration (e.g., "float32", "float16").
51 @param use_gpu_only_for_calibration bool. If True, use GPU only during the calibration phase.
52 @param weight_memory_method int. Weight memory management method index.<br>
53 0: DeleteFloat - Delete float weights after quantization.<br>
54 1: SaveFloat - Save float weights to disk.<br>
55 2: MoveFloat - Move float weights to CPU.<br>
56 3: KeepFloat - Keep float weights in memory.<br>
57 4: KeepAll - Keep all weights in memory.<br>
58 @param optional bool. Indicates whether this config is optional. Use the default value defined as a class variable.
59 """
60 super().__init__(optional)
61 self.weight_dtype = weight_dtype
62 self.use_gpu_only_for_calibration = use_gpu_only_for_calibration
63 self.weight_memory_method = weight_memory_method
64 self.check_valid()
65
66 @classmethod
67 def default_config(cls):
68 return cls(**cls.DEFAULTS, optional=cls.optional)
69
70 @classmethod
71 def from_kwargs(cls, **kwargs):
72 params = cls.DEFAULTS.copy()
73 updated_kwargs = dict()
74 for key in cls.DEFAULTS.keys():
75 if key in kwargs:
76 updated_kwargs[key] = kwargs[key]
77 params[key] = kwargs[key]
78
79 try:
80 return cls(**params, optional=cls.optional)
81 except:
82 raise ValueError(
83 f"Please review the configuration kwargs passed to {cls.__name__}. These kwargs were updated and will be used: {updated_kwargs}"
84 )
85
86 @classmethod
87 def from_dict(cls, config_dict: dict):
88 """Create ResourceManagementConfig from dictionary (JSON structure)"""
89 params = cls.DEFAULTS.copy()
90
91 if "weightDtype" in config_dict:
92 params["weight_dtype"] = config_dict["weightDtype"]
93 if "useGPUOnlyForCalibration" in config_dict:
94 params["use_gpu_only_for_calibration"] = config_dict[
95 "useGPUOnlyForCalibration"
96 ]
97
98 if "weightMemory" in config_dict:
99 weight_memory = config_dict["weightMemory"]
100 if "method" in weight_memory:
101 params["weight_memory_method"] = weight_memory["method"]
102
103 return cls(**params)
104
105 def to_dict(self):
106 return {
107 "resourceManagement": {
108 "weightDtype": self.weight_dtype,
109 "useGPUOnlyForCalibration": self.use_gpu_only_for_calibration,
110 "weightMemory": {
111 "methodList": self.weight_memory_methods,
112 "method": self.weight_memory_method,
113 },
114 }
115 }
116
117
118def get_resource_management_config(**kwargs) -> ResourceManagementConfig:
119 """
120 @brief Create ResourceManagementConfig with partial parameters merged with defaults.
121 """
122 params = ResourceManagementConfig.DEFAULTS.copy()
123 params.update(kwargs)
125 **params, optional=ResourceManagementConfig.optional
126 )
127
128
129# @}
Configuration for resource management during model compilation.
ResourceManagementConfig get_resource_management_config(**kwargs)
Create ResourceManagementConfig with partial parameters merged with defaults.
__init__(self, str weight_dtype, bool use_gpu_only_for_calibration, int weight_memory_method, bool optional=optional)
Initialize the ResourceManagementConfig.
from_dict(cls, dict config_dict)
Create ResourceManagementConfig from dictionary (JSON structure)