def get_predefined_configuration( data: Dict[str, Any], ) -> Union[Dict[str, Any], List[Dict[str, Any]]]: """Get configuration.""" from lpot.ux.utils.utils import get_framework_from_path, get_predefined_config_path from lpot.ux.utils.workload.config import Config model_path = data.get("model_path", "") if not os.path.isfile(model_path): raise ClientErrorException( f"Could not find model in specified path: {model_path}.", ) model_name = Path(model_path).stem domain = data.get("domain", None) if not domain: raise ClientErrorException("Domain is not defined!") framework = get_framework_from_path(model_path) if framework is None: raise ClientErrorException( f"Could not find framework for specified model {model_name} in path {model_path}.", ) config = Config() predefined_config_path = get_predefined_config_path(framework, domain) config.load(predefined_config_path) return { "config": config.serialize(), "framework": framework, "name": model_name, "domain": domain, }
def test_load(self) -> None: """Test load.""" config = Config() read_yaml = yaml.dump(self.predefined_config, sort_keys=False) with patch( "lpot.ux.utils.workload.config.open", mock_open(read_data=read_yaml), ) as mocked_open: config.load("path to yaml file") mocked_open.assert_called_once_with("path to yaml file") expected = Config(self.predefined_config) self.assertEqual(expected.serialize(), config.serialize())
def test_dump(self) -> None: """Test dump.""" config = Config(self.predefined_config) expected_yaml = yaml.dump( config.serialize(), indent=4, default_flow_style=None, sort_keys=False, ) with patch("lpot.ux.utils.workload.config.open", mock_open()) as mocked_open: config.dump("path to yaml file") mocked_open.assert_called_once_with("path to yaml file", "w") mocked_open().write.assert_called_once_with(expected_yaml)
def test_config_serializer(self) -> None: """Test Config serializer.""" config = Config(predefined_config) result = config.serialize() print(result) self.assertDictEqual( result, { "domain": "image_recognition", "device": "cpu", "model": { "name": "resnet50_v1_5", "framework": "tensorflow", "outputs": "softmax_tensor", }, "quantization": { "calibration": { "sampling_size": 100, "dataloader": { "batch_size": 1, "dataset": { "ImageRecord": { "root": "/path/to/calibration/dataset", }, }, "transform": { "ParseDecodeImagenet": None, "ResizeCropImagenet": { "height": 224, "width": 224, "mean_value": [123.68, 116.78, 103.94], }, }, }, }, "model_wise": { "activation": { "algorithm": "minmax" } }, "approach": "post_training_static_quant", }, "evaluation": { "accuracy": { "metric": { "topk": 1 }, "dataloader": { "batch_size": 32, "dataset": { "ImageRecord": { "root": "/path/to/evaluation/dataset", }, }, "transform": { "ParseDecodeImagenet": None, "ResizeCropImagenet": { "height": 224, "width": 224, "mean_value": [123.68, 116.78, 103.94], }, }, }, }, "performance": { "warmup": 10, "iteration": -1, "configs": { "cores_per_instance": 3, "num_of_instance": 2, }, "dataloader": { "batch_size": 1, "dataset": { "ImageRecord": { "root": "/path/to/evaluation/dataset", }, }, "transform": { "ParseDecodeImagenet": None, "ResizeCropImagenet": { "height": 224, "width": 224, "mean_value": [123.68, 116.78, 103.94], }, }, }, }, }, "tuning": { "strategy": { "name": "basic", }, "accuracy_criterion": { "relative": 0.01 }, "exit_policy": { "timeout": 0 }, "random_seed": 9527, }, }, )
def test_config_serializer(self) -> None: """Test Config serializer.""" config = Config(self.predefined_config) result = config.serialize() self.assertDictEqual( result, { "domain": "image_recognition", "device": "cpu", "model": { "name": "resnet50_v1_5", "framework": "tensorflow", "outputs": "softmax_tensor", }, "quantization": { "calibration": { "sampling_size": 100, "dataloader": { "batch_size": 1, "dataset": { "ImageRecord": { "root": "/path/to/calibration/dataset", }, }, "transform": { "ResizeCropImagenet": { "height": 224, "width": 224, "mean_value": [123.68, 116.78, 103.94], }, }, }, }, "model_wise": { "activation": { "algorithm": "minmax" } }, "approach": "post_training_static_quant", }, "evaluation": { "accuracy": { "metric": { "topk": 1 }, "dataloader": { "batch_size": 32, "dataset": { "ImageRecord": { "root": "/path/to/evaluation/dataset", }, }, "transform": { "ResizeCropImagenet": { "height": 224, "width": 224, "mean_value": [123.68, 116.78, 103.94], }, }, }, "postprocess": { "transform": { "LabelShift": { "Param1": True, }, }, }, }, "performance": { "warmup": 10, "iteration": -1, "configs": { "cores_per_instance": 3, "num_of_instance": 2, }, "dataloader": { "batch_size": 1, "dataset": { "ImageRecord": { "root": "/path/to/evaluation/dataset", }, }, "transform": { "ResizeCropImagenet": { "height": 224, "width": 224, "mean_value": [123.68, 116.78, 103.94], }, }, }, }, }, "tuning": { "strategy": { "name": "basic", }, "accuracy_criterion": { "relative": 0.01 }, "exit_policy": { "timeout": 0 }, "random_seed": 9527, }, "graph_optimization": { "precisions": "bf16,fp32", "op_wise": { "weight": { "granularity": "per_channel", "scheme": "asym", "dtype": "bf16", "algorithm": "kl", }, "activation": { "granularity": "per_tensor", "scheme": "sym", "dtype": "int8", "algorithm": "minmax", }, }, }, "pruning": { "magnitude": { "weights": [1, 2, 3], "method": "per_tensor", "init_sparsity": 0.42, "target_sparsity": 0.1337, "start_epoch": 13, "end_epoch": 888, }, "start_epoch": 1, "end_epoch": 2, "frequency": 3, "init_sparsity": 0.4, "target_sparsity": 0.5, }, }, )