create_quant_config

Applicable Products

Product

Supported

Ascend 950PR&950DT products

√

Atlas A3 products

√

Atlas A2 products

√

Atlas 200I/500 A2 inference products

√

Atlas inference products

√

Atlas training products

√

Description

Finds all quantizable layers in a graph, creates a quantization configuration file, and writes the quantization configuration of the quantizable layers to the configuration file.

Prototype

1
create_quant_config(config_file, model, input_data, skip_layers=None, batch_num=1, activation_offset=True, config_defination=None)

Parameters

Parameter

Input/Output

Description

config_file

Input

Path (including the file name) of the quantization configuration file. The existing file (if any) in the path will be overwritten upon this API call.

A string.

model

Input

Original model for quantization, with weights loaded.

A torch.nn.Module.

input_data

Input

Input data of the model. A torch.tensor is replaced with an equivalent tuple(torch.tensor).

A tuple.

skip_layers

Input

Layers to skip quantization.

Default: None

A list of strings.

Constraints: If a simplified quantization configuration file is passed as an input argument, this parameter must be configured in the file. In this case, any configuration of this parameter passed via input arguments does not take effect.

batch_num

Input

Number of batches taken to generate the quantization factors.

An int.

Value range: an integer greater than or equal to 0. Default value: 1.

Constraints:

  • batch_num must not be too large. The product of batch_num and batch_size equals the number of images used during quantization. Too many images consume too much memory.
  • If a simplified quantization configuration file is passed as an input argument, this parameter must be configured in the file. In this case, any configuration of this parameter passed via input arguments does not take effect.

activation_offset

Input

Whether to quantize activations with offset.

Default: True

A bool.

Constraints: If a simplified quantization configuration file is passed as an input argument, this parameter must be configured in the file. In this case, any configuration of this parameter passed via input arguments does not take effect.

config_defination

Input

Simplified quantization configuration file quant.cfg generated based on the calibration_config_pytorch.proto file. The *.proto file is stored in /amct_pytorch/proto/ under the AMCT installation directory.

For details about the parameters in the *.proto file and the generated simplified quantization configuration file quant.cfg, see Simplified PTQ Configuration File.

Default: None

A string.

Constraints: If it is set to None, a configuration file is generated based on the input arguments (including skip_layers, batch_num, and activation_offset). Otherwise, other input quantization parameters (skip_layers, batch_num, and activation_offset) are ignored, and a JSON configuration file is generated according to the config_defination parameter in the simplified quantization configuration file.

Returns

None

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import amct_pytorch as amct
# Build a graph of the network to be quantized.
model = build_model()
model.load_state_dict(torch.load(state_dict_path))
input_data = tuple([torch.randn(input_shape)])
model.eval()

# Create a quantization configuration file.
amct.create_quant_config(config_file="./configs/config.json",
                         model=model,
                         input_data=input_data,
                         skip_layers=None,
                         batch_num=1,
                         activation_offset=True)

The flush file is a quantization configuration file in JSON format. The following is an example. (The quantization configuration file output by this API will be overwritten when quantization is performed again.) For details about the parameters, see Quantization Configuration File.

  • PTQ configuration file (see IFMR Algorithm for activation quantization)
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    {
        "version":1,
        "batch_num":2,
        "activation_offset":true,
        "do_fusion":true,
        "skip_fusion_layers":[],
        "conv1":{
            "quant_enable":true,
            "dmq_balancer_param":0.5,
            "activation_quant_params":{
                "num_bits":8,
                "max_percentile":0.999999,
                "min_percentile":0.999999,
                "search_range":[
                    0.7,
                    1.3
                ],
                "search_step":0.01,
                "act_algo":"ifmr",
                "asymmetric":false
            },
            "weight_quant_params":{
                "num_bits":8,
                "wts_algo":"arq_quantize",
                "channel_wise":true
            }
        },
        "fc":{
            "quant_enable":true,
            "dmq_balancer_param":0.5,
            "activation_quant_params":{
                "num_bits":8,
                "max_percentile":0.999999,
                "min_percentile":0.999999,
                "search_range":[
                    0.7,
                    1.3
                ],
                "search_step":0.01,
                "act_algo":"ifmr",
                "asymmetric":false
            },
            "weight_quant_params":{
                "num_bits":8,
                "wts_algo":"arq_quantize",
                "channel_wise":false
            }
        }
    }
    
  • PTQ configuration file (see HFMG Algorithm for activation quantization)
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    {
        "version":1,
        "batch_num":2,
        "activation_offset":true,
        "do_fusion":true,
        "skip_fusion_layers":[],
        "conv1":{
            "quant_enable":true,
            "dmq_balancer_param":0.5,
            "activation_quant_params":{
                "num_bits":8,
                "act_algo":"hfmg",
                "num_of_bins":4096,
                "asymmetric":false
            },
            "weight_quant_params":{
                "num_bits":8,
                "wts_algo":"arq_quantize",
                "channel_wise":true
            }
        }
    }
    
  • Simplified configuration file for adaptive rounding quantization (see ADA for weight quantization)
    "layer_name1":{
            "quant_enable":true,
            "weight_quant_params":{
                "wts_algo":"ada_quantize",
                "num_iteration":10000,
                "reg_param":0.1,
                "beta_range":[20,2], 
    		"warm_start":0.2,
    		"num_bits":8,
    		"channel_wise":true
            }
        }