mindspore_gs.ptq.RoundToNearest

View Source On Gitee
class mindspore_gs.ptq.RoundToNearest(config=None)[source]

Native implementation for post training quantization based on min/max statistic values.

Parameters

config (mindspore_gs.ptq.PTQConfig, optional) – config for RoundToNearst, default is None.

Raises
  • TypeError – If config type is not PTQConfig when it's not None.

  • ValueError – If backend in config is not BackendTarget.ASCEND.

Examples

>>> import mindspore_gs
>>> from mindspore_gs import ptq
>>> from mindspore_gs.ptq import RoundToNearest as rtn
>>> from mindspore_gs.ptq import PTQConfig
>>> # Define the network structure of LeNet5. Refer to
>>> # https://gitee.com/mindspore/docs/blob/r2.2/docs/mindspore/code/lenet.py
>>> ptq = rtn()
>>> network = LeNet5()
>>> fake_quant_net = ptq.apply(net_work)
>>> quant_net = ptq.convert(fake_quant_net)
apply(network: Cell, network_helper: NetworkHelper = None, datasets: Dataset = None)[source]

Define how to add fake quantizer to network.

Parameters
  • network (Cell) – Network to be fake quantized.

  • network_helper (NetworkHelper) – Utils for decoupling algorithm with network framework.

  • datasets (Dataset) – Datasets for calibrating.

Returns

fake quantized network.

Raises
  • RuntimeError – If RoundToNearest is not well inited.

  • TypeError – If input network is not a Cell.

  • TypeError – If input network_helper is not None and is not a NetworkHelper.

  • ValueError – if network_helper is None when kvcache_quant_dtype is mindspore.int8.

convert(net_opt: Cell, ckpt_path='')[source]

Define how to convert a compressed network to a standard network before exporting.

Parameters
  • net_opt (Cell) – Network to be converted which is transformed by RoundToNearest.apply.

  • ckpt_path (str) – Path to checkpoint file for net_opt. Default is "", which means not loading checkpoint file to net_opt.

Returns

An instance of Cell represents quantized network.

Raises