MCPcopy Create free account
hub / github.com/pytorch/pytorch / quantize

Function quantize

torch/ao/quantization/quantize.py:358–384  ·  view source on GitHub ↗

r"""Quantize the input float model with post training static quantization. First it will prepare the model for calibration, then it calls `run_fn` which will run the calibration step, after that we will convert the model to a quantized model. Args: model: input float model

(model, run_fn, run_args, mapping=None, inplace=False)

Source from the content-addressed store, hash-verified

356 _remove_activation_post_process(module)
357
358def quantize(model, run_fn, run_args, mapping=None, inplace=False):
359 r"""Quantize the input float model with post training static quantization.
360
361 First it will prepare the model for calibration, then it calls
362 `run_fn` which will run the calibration step, after that we will
363 convert the model to a quantized model.
364
365 Args:
366 model: input float model
367 run_fn: a calibration function for calibrating the prepared model
368 run_args: positional arguments for `run_fn`
369 inplace: carry out model transformations in-place, the original module is mutated
370 mapping: correspondence between original module types and quantized counterparts
371
372 Return:
373 Quantized model.
374 """
375 torch._C._log_api_usage_once("quantization_api.quantize.quantize")
376 if mapping is None:
377 mapping = get_default_static_quant_module_mappings()
378 if not inplace:
379 model = copy.deepcopy(model)
380 model.eval()
381 prepare(model, inplace=True)
382 run_fn(model, *run_args)
383 convert(model, mapping, inplace=True)
384 return model
385
386def quantize_dynamic(model, qconfig_spec=None, dtype=torch.qint8,
387 mapping=None, inplace=False):

Callers 15

test_fuse_module_evalMethod · 0.90
test_single_layerMethod · 0.90
test_two_layersMethod · 0.90
test_nested1Method · 0.90
test_nested2Method · 0.90
test_nested3Method · 0.90
test_skip_quantMethod · 0.90
test_manualMethod · 0.90
test_normalizationMethod · 0.90
test_activationsMethod · 0.90

Calls 4

prepareFunction · 0.70
convertFunction · 0.70
evalMethod · 0.45

Tested by 15

test_fuse_module_evalMethod · 0.72
test_single_layerMethod · 0.72
test_two_layersMethod · 0.72
test_nested1Method · 0.72
test_nested2Method · 0.72
test_nested3Method · 0.72
test_skip_quantMethod · 0.72
test_manualMethod · 0.72
test_normalizationMethod · 0.72
test_activationsMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…