neural_compressor.adaptor.mxnet_utils.util
Mxnet util module.
Classes
Enum op types. |
|
DataLoader Wrap. |
|
DataIterLoader. |
|
Collector Base class. |
|
Collect the calibration thresholds depending on the algorithm set. |
|
Tensors collector. |
|
Name collector. |
|
Calibration data class. |
Functions
|
Checks whether object is iterable. |
|
Ensures that object is a list. |
|
Checks MXNet version. |
|
Combine capabilities. |
|
Converts a symbolic model to an Neural Compressor model. |
|
Fuse the supplied model. |
|
Get the framework name by version. |
|
Prepares sym_model and dataloader needed for quantization, calibration or running. |
|
Prepare model. |
|
Create data example by mxnet input description and ctx. |
|
Prepare dataloader. |
|
Ndarray to device. |
|
Checks whether the model is quantized. |
|
Query quantizable nodes of the given model. |
|
Quantizes the symbolic model according to the configuration. |
|
Run forward propagation on the model. |
|
Convert a symbol model to gluon SymbolBlock. |
|
Convert a symbol model to Module. |
|
Convert the strategy config to MXNet quantization config. |
|
Distributes the tensors for calibration, depending on the algorithm set in the configuration of their nodes. |
|
Calibrate the quantized symbol model using data gathered by the collector. |
|
Convert model to support amp. |
Module Contents
- neural_compressor.adaptor.mxnet_utils.util.isiterable(obj) bool [source]
Checks whether object is iterable.
- Parameters:
obj – object to check.
- Returns:
True if object is iterable, else False.
- Return type:
boolean
- neural_compressor.adaptor.mxnet_utils.util.ensure_list(x)[source]
Ensures that object is a list.
- Parameters:
x – input.
- Returns:
x if x is list, else [x].
- Return type:
list
- neural_compressor.adaptor.mxnet_utils.util.check_mx_version(version)[source]
Checks MXNet version.
- Parameters:
version (str) – version to check.
- Returns:
True if mx.__version__ >= version, else False.
- Return type:
boolean
- neural_compressor.adaptor.mxnet_utils.util.combine_capabilities(current, new)[source]
Combine capabilities.
- Parameters:
current (dict) – current capabilities.
new (dict) – new capabilities.
- Returns:
contains all capabilities.
- Return type:
dict
- neural_compressor.adaptor.mxnet_utils.util.make_nc_model(target, sym_model, ctx, input_desc)[source]
Converts a symbolic model to an Neural Compressor model.
- Parameters:
target (object) – target model type to return.
sym_model (tuple) – symbol model (symnet, args, auxs).
input_desc (list) – model input data description.
- Returns:
converted neural_compressor model
- Return type:
NCModel
- neural_compressor.adaptor.mxnet_utils.util.fuse(sym_model, ctx)[source]
Fuse the supplied model.
- Parameters:
sym_model (tuple) – symbol model (symnet, args, auxs).
- Returns:
fused symbol model (symnet, args, auxs).
- Return type:
tuple
- neural_compressor.adaptor.mxnet_utils.util.get_framework_name(ctx)[source]
Get the framework name by version.
- Parameters:
ctx (object) – mxnet context object.
- Returns:
framework name.
- Return type:
str
- neural_compressor.adaptor.mxnet_utils.util.prepare_model_data(nc_model, ctx, data_x)[source]
Prepares sym_model and dataloader needed for quantization, calibration or running.
- Parameters:
nc_model (object) – model to prepare.
data_x (object) – data iterator/loader to prepare.
- Returns:
symbol model (symnet, args, auxs) and DataLoaderWrap.
- Return type:
tuple
- neural_compressor.adaptor.mxnet_utils.util.prepare_model(nc_model, ctx, input_desc)[source]
Prepare model.
- Parameters:
nc_model (object) – model to prepare.
ctx (object) – mxnet context object.
input_desc (list) – input list of mxnet data types.
- Returns:
mxnet model (symnet, args, auxs).
- Return type:
object
- neural_compressor.adaptor.mxnet_utils.util.create_data_example(ctx, input_desc)[source]
Create data example by mxnet input description and ctx.
- Parameters:
ctx (object) – mxnet context object.
input_desc (list) – input list of mxnet data types.
- Returns:
data example.
- Return type:
list
- neural_compressor.adaptor.mxnet_utils.util.prepare_dataloader(nc_model, ctx, data_x)[source]
Prepare dataloader.
- Parameters:
nc_model (object) – model to prepare.
ctx (object) – mxnet context object.
data_x (object) – mxnet io iterable object or dataloader object.
- Returns:
dataloader.
- Return type:
object
- neural_compressor.adaptor.mxnet_utils.util.ndarray_to_device(ndarray, device)[source]
Ndarray to device.
- Parameters:
ndarray (ndarray) – model to prepare.
device (object) – mxnet device object.
- Returns:
ndarray on the device.
- Return type:
ndarray
- neural_compressor.adaptor.mxnet_utils.util.is_model_quantized(sym_model)[source]
Checks whether the model is quantized.
- Parameters:
sym_model (tuple) – symbol model (symnet, args, auxs).
- Returns:
True if model is quantized, else False.
- Return type:
boolean
- neural_compressor.adaptor.mxnet_utils.util.query_quantizable_nodes(sym_model, ctx, dataloader)[source]
Query quantizable nodes of the given model.
- Parameters:
sym_model (tuple) – symbol model (symnet, args, auxs) to query.
- Returns:
quantizable nodes of the given model. dict: tensor to node mapping.
- Return type:
list
- neural_compressor.adaptor.mxnet_utils.util.quantize_sym_model(sym_model, ctx, qconfig)[source]
Quantizes the symbolic model according to the configuration.
- Parameters:
sym_model (tuple) – symbol model (symnet, args, auxs).
qconfig (dict) – quantization configuration.
- Returns:
Symbol model (symnet, args, auxs) and list of tensors for calibration.
- Return type:
tuple
- neural_compressor.adaptor.mxnet_utils.util.run_forward(sym_model, ctx, dataloader, b_filter, collector=None, pre_batch=None, post_batch=None)[source]
Run forward propagation on the model.
- Parameters:
sym_model (tuple) – symbol model (symnet, args, auxs).
dataloader (DataLoaderWrap) – data loader.
b_filter (generator) – filter on which batches to run inference on.
collector (object) – collects information during inference.
pre_batch – function to call prior to batch inference.
post_batch – function to call after batch inference.
- Returns:
batch count.
- Return type:
int
- neural_compressor.adaptor.mxnet_utils.util.make_symbol_block(sym_model, ctx, input_desc)[source]
Convert a symbol model to gluon SymbolBlock.
- Parameters:
sym_model (tuple) – symbol model (symnet, args, auxs).
input_desc (list) – model input data description.
- Returns:
SymbolBlock model.
- Return type:
mx.gluon.SymbolBlock
- neural_compressor.adaptor.mxnet_utils.util.make_module(sym_model, ctx, input_desc)[source]
Convert a symbol model to Module.
- Parameters:
sym_model (tuple) – symbol model (symnet, args, auxs).
input_desc (list) – model input data description.
- Returns:
Module model.
- Return type:
mx.module.Module
- neural_compressor.adaptor.mxnet_utils.util.parse_tune_config(tune_cfg, quantizable_nodes)[source]
Convert the strategy config to MXNet quantization config.
- Parameters:
tune_cfg (dict) – tune config from neural_compressor strategy.
quantizable_nodes (list) – quantizable nodes in the model.
- Returns:
quantization configuration. dict: calibration configuration.
- Return type:
dict
- neural_compressor.adaptor.mxnet_utils.util.distribute_calib_tensors(calib_tensors, calib_cfg, tensor_to_node)[source]
Distributes the tensors for calibration, depending on the algorithm set in the configuration of their nodes.
- Parameters:
calib_tensors – tensors to distribute.
calib_cfg (dict) – calibration configuration.
tensor_to_node (dict) – tensor to node mapping.
- Returns:
kl tensors and minmax tensors.
- Return type:
tuple
- neural_compressor.adaptor.mxnet_utils.util.calib_model(qsym_model, calib_data, calib_cfg)[source]
Calibrate the quantized symbol model using data gathered by the collector.
- Parameters:
qsym_model (tuple) – quantized symbol model (symnet, args, auxs).
calib_data (CalibData) – data needed for calibration (thresholds).
calib_cfg (dict) – calibration configuration.
- Returns:
quantized calibrated symbol model (symnet, args, auxs).
- Return type:
tuple
- neural_compressor.adaptor.mxnet_utils.util.amp_convert(sym_model, input_desc, amp_cfg)[source]
Convert model to support amp.
- class neural_compressor.adaptor.mxnet_utils.util.DataLoaderWrap(dataloader, input_desc)[source]
DataLoader Wrap.
- class neural_compressor.adaptor.mxnet_utils.util.CalibCollector(include_tensors_kl, include_tensors_minmax, num_bins=8001)[source]
Collect the calibration thresholds depending on the algorithm set.