22import subprocess
33import copy
44import time
5+ from . import default_static_qconfig , prepare
56
67
78def autotune (prepared_model , calib_dataloader , eval_func , sampling_sizes = [100 ], accuracy_criterion = {'relative' : 0.01 }, tuning_time = 0 ):
@@ -33,6 +34,8 @@ def autotune(prepared_model, calib_dataloader, eval_func, sampling_sizes=[100],
3334 assert False , "Unable to import neural_compressor from the local environment."
3435 from neural_compressor import config
3536 from neural_compressor .experimental import Quantization , common
37+ from neural_compressor .adaptor .torch_utils .util import auto_copy
38+ from neural_compressor .adaptor .pytorch import get_example_inputs
3639
3740 config .quantization .backend = 'pytorch_ipex'
3841 config .quantization .approach = 'post_training_static_quant'
@@ -45,12 +48,17 @@ def autotune(prepared_model, calib_dataloader, eval_func, sampling_sizes=[100],
4548 config .quantization .accuracy_criterion .absolute = accuracy_criterion .get ('absolute' )
4649 config .quantization .timeout = tuning_time
4750 quantizer = Quantization (config )
48- quantizer .model = common .Model (copy .deepcopy (prepared_model ))
51+ fp32_model = auto_copy (prepared_model )
52+ quantizer .model = common .Model (fp32_model )
4953 quantizer .calib_dataloader = calib_dataloader
5054 quantizer .eval_func = eval_func
5155 q_model = quantizer .fit ()
5256 dirname_str = './saved_tuning_results_' + time .strftime ("%Y%m%d_%H%M%S" )
5357 q_model .save (dirname_str )
54- prepared_model .load_qconf_summary (qconf_summary = dirname_str + '/best_configure.json' )
5558
56- return prepared_model
59+ # This is a workaround for the bug that the auto_copy function will change its input prepared_model.
60+ qconfig = default_static_qconfig
61+ new_prepared_model = prepare (fp32_model , qconfig , example_inputs = get_example_inputs (calib_dataloader ), inplace = True )
62+ new_prepared_model .load_qconf_summary (qconf_summary = dirname_str + '/best_configure.json' )
63+
64+ return new_prepared_model
0 commit comments