From 83825afa84c479fca312859156aff0912eb12feb Mon Sep 17 00:00:00 2001 From: "Cheng, Penghui" Date: Thu, 1 Dec 2022 21:45:33 +0800 Subject: [PATCH] Set default value for use_bf16 and fixed random seed setting error (#186) Signed-off-by: Cheng, Penghui --- neural_compressor/conf/config.py | 6 +- neural_compressor/conf/pythonic_config.py | 5 +- neural_compressor/config.py | 83 +++++++++++++++-------- 3 files changed, 63 insertions(+), 31 deletions(-) diff --git a/neural_compressor/conf/config.py b/neural_compressor/conf/config.py index aef8f695291..640b2fd36df 100644 --- a/neural_compressor/conf/config.py +++ b/neural_compressor/conf/config.py @@ -840,7 +840,7 @@ def percent_to_float(data): str: ops_schema }, }, - Optional('use_bf16', default=False): bool, + Optional('use_bf16', default=True): bool, Optional('graph_optimization'): graph_optimization_schema, Optional('mixed_precision'): mixed_precision_schema, @@ -1371,6 +1371,10 @@ def map_pyconfig_to_cfg(self, pythonic_config): 'tuning.tensorboard': pythonic_config.options.tensorboard, }) if pythonic_config.benchmark is not None: + if pythonic_config.benchmark.inputs != []: + mapping.update({'model.inputs': pythonic_config.benchmark.inputs}) + if pythonic_config.benchmark.outputs != []: + mapping.update({'model.outputs': pythonic_config.benchmark.outputs}) mapping.update({ 'evaluation.performance.warmup': pythonic_config.benchmark.warmup, 'evaluation.performance.iteration': pythonic_config.benchmark.iteration, diff --git a/neural_compressor/conf/pythonic_config.py b/neural_compressor/conf/pythonic_config.py index 49eaa1738e7..dbf1d3dc6aa 100644 --- a/neural_compressor/conf/pythonic_config.py +++ b/neural_compressor/conf/pythonic_config.py @@ -39,11 +39,12 @@ def __init__(self, max_trials=100, performance_only=False, reduce_range=None, - use_bf16=False, + use_bf16=True, accuracy_criterion=accuracy_criterion): + extra_precisions = ["bf16"] if use_bf16 else [] super().__init__(inputs, outputs, backend, device, calibration_sampling_size, op_type_list, op_name_list, strategy, objective, timeout, max_trials, performance_only, - reduce_range, use_bf16, accuracy_criterion) + reduce_range, extra_precisions, accuracy_criterion) self._approach = approach @property diff --git a/neural_compressor/config.py b/neural_compressor/config.py index 371ba422963..f7337b546f0 100644 --- a/neural_compressor/config.py +++ b/neural_compressor/config.py @@ -136,8 +136,17 @@ def tensorboard(self, tensorboard): class BenchmarkConfig: - def __init__(self, warmup=5, iteration=-1, cores_per_instance=None, num_of_instance=None, - inter_num_of_threads=None, intra_num_of_threads=None): + def __init__(self, + inputs=[], + outputs=[], + warmup=5, + iteration=-1, + cores_per_instance=None, + num_of_instance=None, + inter_num_of_threads=None, + intra_num_of_threads=None): + self._inputs = inputs + self._outputs = outputs self._warmup = warmup self._iteration = iteration self._cores_per_instance = cores_per_instance @@ -145,6 +154,24 @@ def __init__(self, warmup=5, iteration=-1, cores_per_instance=None, num_of_insta self._inter_num_of_threads = inter_num_of_threads self._intra_num_of_threads = intra_num_of_threads + @property + def outputs(self): + return self._outputs + + @outputs.setter + def outputs(self, outputs): + if check_value('outputs', outputs, str): + self._outputs = outputs + + @property + def inputs(self): + return self._inputs + + @inputs.setter + def inputs(self, inputs): + if check_value('inputs', inputs, str): + self._inputs = inputs + @property def warmup(self): return self._warmup @@ -285,7 +312,7 @@ def __init__(self, max_trials=100, performance_only=False, reduce_range=None, - extra_precisions=[], + extra_precisions=["bf16"], accuracy_criterion=accuracy_criterion): self._inputs = inputs self._outputs = outputs @@ -503,16 +530,16 @@ def strategy(self, strategy): class PostTrainingQuantConfig(_BaseQuantizationConfig): def __init__(self, - device='cpu', + device="cpu", backend="NA", inputs=[], outputs=[], - approach='auto', + approach="auto", calibration_sampling_size=[100], op_type_list=None, op_name_list=None, reduce_range=None, - extra_precisions = [], + extra_precisions = ["bf16"], tuning_criterion=tuning_criterion, accuracy_criterion=accuracy_criterion, ): @@ -551,7 +578,7 @@ def __init__(self, op_type_list=None, op_name_list=None, reduce_range=None, - extra_precisions=[]): + extra_precisions=["bf16"]): super().__init__(inputs=inputs, outputs=outputs, device=device, backend=backend, op_type_list=op_type_list, op_name_list=op_name_list, reduce_range=reduce_range, extra_precisions=extra_precisions) @@ -789,16 +816,16 @@ def dynamic_axes(self, dynamic_axes): class Torch2ONNXConfig(ExportConfig): - def __init__( - self, - dtype="int8", - opset_version=14, - quant_format="QDQ", - example_inputs=None, - input_names=None, - output_names=None, - dynamic_axes=None, - **kwargs, + def __init__( + self, + dtype="int8", + opset_version=14, + quant_format="QDQ", + example_inputs=None, + input_names=None, + output_names=None, + dynamic_axes=None, + **kwargs, ): super().__init__( dtype=dtype, @@ -813,16 +840,16 @@ def __init__( class TF2ONNXConfig(ExportConfig): - def __init__( - self, - dtype="int8", - opset_version=14, - quant_format="QDQ", - example_inputs=None, - input_names=None, - output_names=None, - dynamic_axes=None, - **kwargs, + def __init__( + self, + dtype="int8", + opset_version=14, + quant_format="QDQ", + example_inputs=None, + input_names=None, + output_names=None, + dynamic_axes=None, + **kwargs, ): super().__init__( dtype=dtype, @@ -837,7 +864,7 @@ def __init__( def set_random_seed(seed: int): - options.random_seed + options.random_seed = seed def set_workspace(workspace: str):