search_weight_scale_config.py Source File

search_weight_scale_config.py Source File#

Mobilint SDK qb Compiler: search_weight_scale_config.py Source File
Mobilint SDK qb Compiler v0.12.0.0
MCS002-KR
search_weight_scale_config.py
1from typeguard import typechecked
2from . import ConfigABC
3
4
5
11class SearchWeightScaleConfig(ConfigABC):
12 """
13 @brief Configuration for searching weight scale.
14 """
15
16 optional = False
17 DEFAULTS = {
18 "apply": False,
19 "query": False,
20 "key": False,
21 "value": False,
22 "out": False,
23 "ffn": False,
24 }
25
27 self,
28 apply: bool,
29 query: bool,
30 key: bool,
31 value: bool,
32 out: bool,
33 ffn: bool,
34 optional: bool = optional,
35 ):
36 """
37 @brief Initialize the SearchWeightScaleConfig.
38 @param apply bool. Whether to apply weight scale search.
39 @param query bool. Apply to query layer.
40 @param key bool. Apply to key layer.
41 @param value bool. Apply to value layer.
42 @param out bool. Apply to output layer.
43 @param ffn bool. Apply to FFN layer.
44 @param optional bool. Indicates whether this config is optional. Use the default value defined as a class variable.
45 """
46 super().__init__(optional)
47 self.apply = apply
48 self.query = query
49 self.key = key
50 self.value = value
51 self.out = out
52 self.ffn = ffn
53
54 @classmethod
55 def default_config(cls):
56 return cls(**cls.DEFAULTS, optional=cls.optional)
57
58 @classmethod
59 def from_dict(cls, config_dict: dict):
60 """Create SearchWeightScaleConfig from dictionary (JSON structure)"""
61 params = cls.DEFAULTS.copy()
62
63 if "apply" in config_dict:
64 params["apply"] = config_dict["apply"]
65
66 if "transformer" in config_dict:
67 transformer = config_dict["transformer"]
68 if "query" in transformer:
69 params["query"] = transformer["query"]
70 if "key" in transformer:
71 params["key"] = transformer["key"]
72 if "value" in transformer:
73 params["value"] = transformer["value"]
74 if "out" in transformer:
75 params["out"] = transformer["out"]
76 if "ffn" in transformer:
77 params["ffn"] = transformer["ffn"]
78
79 return cls(**params)
80
81 def to_dict(self):
82 return {
83 "apply": self.apply,
84 "transformer": {
85 "query": self.query,
86 "key": self.key,
87 "value": self.value,
88 "out": self.out,
89 "ffn": self.ffn,
90 },
91 }
92
93
94def get_search_weight_scale_config(**kwargs) -> SearchWeightScaleConfig:
95 """
96 @brief Create SearchWeightScaleConfig with partial parameters merged with defaults.
97 """
98 params = SearchWeightScaleConfig.DEFAULTS.copy()
99 params.update(kwargs)
100 return SearchWeightScaleConfig(**params, optional=SearchWeightScaleConfig.optional)
101
102
103# }@
SearchWeightScaleConfig get_search_weight_scale_config(**kwargs)
Create SearchWeightScaleConfig with partial parameters merged with defaults.
__init__(self, bool apply, bool query, bool key, bool value, bool out, bool ffn, bool optional=optional)
Initialize the SearchWeightScaleConfig.
from_dict(cls, dict config_dict)
Create SearchWeightScaleConfig from dictionary (JSON structure)