15
15
from pathlib import Path
16
16
from typing import Any , Dict , List , Optional , Set , Type
17
17
18
- import jstyleson as json
18
+ import jstyleson as json # type: ignore[import-untyped]
19
19
20
20
import nncf
21
21
from nncf .common .graph .operator_metatypes import OperatorMetatype
@@ -60,7 +60,7 @@ def get_hw_config_type(target_device: str) -> Optional[HWConfigType]:
60
60
return HWConfigType (HW_CONFIG_TYPE_TARGET_DEVICE_MAP [target_device ])
61
61
62
62
63
- class HWConfig (list , ABC ):
63
+ class HWConfig (list [ Dict [ str , Any ]] , ABC ):
64
64
QUANTIZATION_ALGORITHM_NAME = "quantization"
65
65
ATTRIBUTES_NAME = "attributes"
66
66
SCALE_ATTRIBUTE_NAME = "scales"
@@ -69,23 +69,23 @@ class HWConfig(list, ABC):
69
69
70
70
TYPE_TO_CONF_NAME_DICT = {HWConfigType .CPU : "cpu.json" , HWConfigType .NPU : "npu.json" , HWConfigType .GPU : "gpu.json" }
71
71
72
- def __init__ (self ):
72
+ def __init__ (self ) -> None :
73
73
super ().__init__ ()
74
- self .registered_algorithm_configs = {}
74
+ self .registered_algorithm_configs : Dict [ str , Any ] = {}
75
75
self .target_device = None
76
76
77
77
@abstractmethod
78
78
def _get_available_operator_metatypes_for_matching (self ) -> List [Type [OperatorMetatype ]]:
79
79
pass
80
80
81
81
@staticmethod
82
- def get_path_to_hw_config (hw_config_type : HWConfigType ):
82
+ def get_path_to_hw_config (hw_config_type : HWConfigType ) -> str :
83
83
return "/" .join (
84
84
[NNCF_PACKAGE_ROOT_DIR , HW_CONFIG_RELATIVE_DIR , HWConfig .TYPE_TO_CONF_NAME_DICT [hw_config_type ]]
85
85
)
86
86
87
87
@classmethod
88
- def from_dict (cls , dct : dict ) :
88
+ def from_dict (cls , dct : Dict [ str , Any ]) -> "HWConfig" :
89
89
hw_config = cls ()
90
90
hw_config .target_device = dct ["target_device" ]
91
91
@@ -104,7 +104,7 @@ def from_dict(cls, dct: dict):
104
104
for algorithm_name in op_dict :
105
105
if algorithm_name not in hw_config .registered_algorithm_configs :
106
106
continue
107
- tmp_config = {}
107
+ tmp_config : Dict [ str , List [ Dict [ str , Any ]]] = {}
108
108
for algo_and_op_specific_field_name , algorithm_configs in op_dict [algorithm_name ].items ():
109
109
if not isinstance (algorithm_configs , list ):
110
110
algorithm_configs = [algorithm_configs ]
@@ -129,30 +129,30 @@ def from_dict(cls, dct: dict):
129
129
return hw_config
130
130
131
131
@classmethod
132
- def from_json (cls , path ) :
132
+ def from_json (cls : type [ "HWConfig" ] , path : str ) -> List [ Dict [ str , Any ]] :
133
133
file_path = Path (path ).resolve ()
134
134
with safe_open (file_path ) as f :
135
135
json_config = json .load (f , object_pairs_hook = OrderedDict )
136
136
return cls .from_dict (json_config )
137
137
138
138
@staticmethod
139
- def get_quantization_mode_from_config_value (str_val : str ):
139
+ def get_quantization_mode_from_config_value (str_val : str ) -> str :
140
140
if str_val == "symmetric" :
141
141
return QuantizationMode .SYMMETRIC
142
142
if str_val == "asymmetric" :
143
143
return QuantizationMode .ASYMMETRIC
144
144
raise nncf .ValidationError ("Invalid quantization type specified in HW config" )
145
145
146
146
@staticmethod
147
- def get_is_per_channel_from_config_value (str_val : str ):
147
+ def get_is_per_channel_from_config_value (str_val : str ) -> bool :
148
148
if str_val == "perchannel" :
149
149
return True
150
150
if str_val == "pertensor" :
151
151
return False
152
152
raise nncf .ValidationError ("Invalid quantization granularity specified in HW config" )
153
153
154
154
@staticmethod
155
- def get_qconf_from_hw_config_subdict (quantization_subdict : Dict ) :
155
+ def get_qconf_from_hw_config_subdict (quantization_subdict : Dict [ str , Any ]) -> QuantizerConfig :
156
156
bits = quantization_subdict ["bits" ]
157
157
mode = HWConfig .get_quantization_mode_from_config_value (quantization_subdict ["mode" ])
158
158
is_per_channel = HWConfig .get_is_per_channel_from_config_value (quantization_subdict ["granularity" ])
@@ -181,20 +181,22 @@ def get_qconf_from_hw_config_subdict(quantization_subdict: Dict):
181
181
)
182
182
183
183
@staticmethod
184
- def is_qconf_list_corresponding_to_unspecified_op (qconf_list : Optional [List [QuantizerConfig ]]):
184
+ def is_qconf_list_corresponding_to_unspecified_op (qconf_list : Optional [List [QuantizerConfig ]]) -> bool :
185
185
return qconf_list is None
186
186
187
187
@staticmethod
188
- def is_wildcard_quantization (qconf_list : Optional [List [QuantizerConfig ]]):
188
+ def is_wildcard_quantization (qconf_list : Optional [List [QuantizerConfig ]]) -> bool :
189
189
# Corresponds to an op itself being specified in the HW config, but having no associated quantization
190
190
# configs specified
191
191
return qconf_list is not None and len (qconf_list ) == 0
192
192
193
193
def get_metatype_vs_quantizer_configs_map (
194
- self , for_weights = False
194
+ self , for_weights : bool = False
195
195
) -> Dict [Type [OperatorMetatype ], Optional [List [QuantizerConfig ]]]:
196
196
# 'None' for ops unspecified in HW config, empty list for wildcard quantization ops
197
- retval = {k : None for k in self ._get_available_operator_metatypes_for_matching ()}
197
+ retval : Dict [Type [OperatorMetatype ], Optional [List [QuantizerConfig ]]] = {
198
+ k : None for k in self ._get_available_operator_metatypes_for_matching ()
199
+ }
198
200
config_key = "weights" if for_weights else "activations"
199
201
for op_dict in self :
200
202
hw_config_op_name = op_dict ["type" ]
0 commit comments