(json_obj: Union[str, dict])
| 472 | |
| 473 | @ staticmethod |
| 474 | def from_json(json_obj: Union[str, dict]) -> QuantizationSetting: |
| 475 | setting = QuantizationSetting() |
| 476 | if isinstance(json_obj, str): |
| 477 | setting_dict = json.loads(json_obj) |
| 478 | else: setting_dict = json_obj |
| 479 | |
| 480 | if 'version' not in setting_dict: |
| 481 | ppq_warning('Can not find a valid version from your json input, input might not be correctly parsed.') |
| 482 | else: |
| 483 | version = setting_dict['version'] |
| 484 | if version < PPQ_CONFIG.VERSION: |
| 485 | ppq_warning(f'You are loading a json quantization setting created by PPQ of another version: ' |
| 486 | f'({version}), input setting might not be correctly parsed. ' |
| 487 | 'And all missing attributes will set as default without warning.') |
| 488 | |
| 489 | if not isinstance(setting_dict, dict): |
| 490 | raise TypeError('Can not load setting from json string, ' |
| 491 | f'expect a dictionary deserialzed from json here, ' |
| 492 | f'however {type(setting_dict)} was given.') |
| 493 | |
| 494 | def assign(obj_setting: dict, obj: object): |
| 495 | for key, value in obj_setting.items(): |
| 496 | if key in obj.__dict__: |
| 497 | if 'builtin' in obj.__dict__[key].__class__.__module__: |
| 498 | obj.__dict__[key] = copy.deepcopy(value) |
| 499 | else: |
| 500 | assert isinstance(value, dict) |
| 501 | assign(value, obj.__dict__[key]) |
| 502 | else: |
| 503 | ppq_warning( |
| 504 | f'Unexpected attribute ({key}) was found in {obj.__class__.__name__}. ' |
| 505 | 'This might because you are using a setting generated by PPQ with another version. ' |
| 506 | 'We will continue setting loading progress, while this attribute will be skipped.') |
| 507 | |
| 508 | assign(setting_dict, setting) |
| 509 | return setting |
nothing calls this directly
no test coverage detected