11
11
12
12
from copy import deepcopy
13
13
from pathlib import Path
14
- from typing import Dict , List , Optional , Type
14
+ from typing import Any , Dict , List , Optional , Type
15
15
16
16
import jsonschema
17
- import jstyleson as json
17
+ import jstyleson as json # type: ignore
18
18
19
19
import nncf
20
20
from nncf .common .logging import nncf_logger
28
28
29
29
30
30
@api (canonical_alias = "nncf.NNCFConfig" )
31
- class NNCFConfig (dict ):
31
+ class NNCFConfig (dict [ str , Any ] ):
32
32
"""Contains the configuration parameters required for NNCF to apply the selected algorithms.
33
33
34
34
This is a regular dictionary object extended with some utility functions, such as the ability to attach well-defined
35
35
structures to pass non-serializable objects as parameters. It is primarily built from a .json file, or from a
36
36
Python JSON-like dictionary - both data types will be checked against a JSONSchema. See the definition of the
37
37
schema at https://openvinotoolkit.github.io/nncf/schema/, or by calling NNCFConfig.schema()."""
38
38
39
- def __init__ (self , * args , ** kwargs ) :
39
+ def __init__ (self , * args : Any , ** kwargs : Any ) -> None :
40
40
super ().__init__ (* args , ** kwargs )
41
41
self .__nncf_extra_structs : Dict [str , NNCFExtraConfigStruct ] = {}
42
42
43
43
@classmethod
44
- def from_dict (cls , nncf_dict : Dict ) -> "NNCFConfig" :
44
+ def from_dict (cls , nncf_dict : Dict [ str , Any ] ) -> "NNCFConfig" :
45
45
"""
46
46
Load NNCF config from a Python dictionary. The dict must contain only JSON-supported primitives.
47
47
48
48
:param nncf_dict: A Python dict with the JSON-style configuration for NNCF.
49
49
"""
50
50
51
- NNCFConfig .validate (nncf_dict )
51
+ cls .validate (nncf_dict )
52
52
return cls (deepcopy (nncf_dict ))
53
53
54
54
@classmethod
@@ -63,7 +63,7 @@ def from_json(cls, path: str) -> "NNCFConfig":
63
63
loaded_json = json .load (f )
64
64
return cls .from_dict (loaded_json )
65
65
66
- def register_extra_structs (self , struct_list : List [NNCFExtraConfigStruct ]):
66
+ def register_extra_structs (self , struct_list : List [NNCFExtraConfigStruct ]) -> None :
67
67
"""
68
68
Attach the supplied list of extra configuration structures to this configuration object.
69
69
@@ -78,7 +78,7 @@ def register_extra_structs(self, struct_list: List[NNCFExtraConfigStruct]):
78
78
def get_extra_struct (self , struct_cls : Type [NNCFExtraConfigStruct ]) -> NNCFExtraConfigStruct :
79
79
return self .__nncf_extra_structs [struct_cls .get_id ()]
80
80
81
- def has_extra_struct (self , struct_cls : Type [NNCFExtraConfigStruct ]) -> NNCFExtraConfigStruct :
81
+ def has_extra_struct (self , struct_cls : Type [NNCFExtraConfigStruct ]) -> bool :
82
82
return struct_cls .get_id () in self .__nncf_extra_structs
83
83
84
84
def get_all_extra_structs (self ) -> List [NNCFExtraConfigStruct ]:
@@ -108,7 +108,7 @@ def get_redefinable_global_param_value_for_algo(self, param_name: str, algo_name
108
108
return param
109
109
110
110
@staticmethod
111
- def schema () -> Dict :
111
+ def schema () -> Dict [ str , Any ] :
112
112
"""
113
113
Returns the JSONSchema against which the input data formats (.json or Python dict) are validated.
114
114
"""
@@ -124,15 +124,15 @@ def _is_path_to_algorithm_name(path_parts: List[str]) -> bool:
124
124
)
125
125
126
126
@staticmethod
127
- def validate (loaded_json ) :
127
+ def validate (loaded_json : Dict [ str , Any ]) -> None :
128
128
try :
129
129
jsonschema .validate (loaded_json , NNCFConfig .schema ())
130
130
except jsonschema .ValidationError as e :
131
131
nncf_logger .error ("Invalid NNCF config supplied!" )
132
132
absolute_path_parts = [str (x ) for x in e .absolute_path ]
133
133
if not NNCFConfig ._is_path_to_algorithm_name (absolute_path_parts ):
134
134
e .message += f"\n Refer to the NNCF config schema documentation at { SCHEMA_VISUALIZATION_URL } "
135
- e .schema = "*schema too long for stdout display*"
135
+ e .schema = "*schema too long for stdout display*" # type: ignore[assignment]
136
136
raise e
137
137
138
138
# Need to make the error more algo-specific in case the config was so bad that no
0 commit comments