conversion — Model Optimizer 0.27.1 (original) (raw)

Main APIs+entrypoints for model pruning.

Functions

export Export a pruned subnet to a regular model.
convert Convert a regular PyTorch model into a model that supports design space optimization.

convert(model, mode)

Convert a regular PyTorch model into a model that supports design space optimization.

Parameters:

Returns:

A converted model with the original weights preserved that can be used for model optimization.

Return type:

Module

Note

Note that model wrappers (such as DataParallel/DistributedDataParallel) are not supported during the convert process. Please wrap the model after the convert process.

Note

convert() relies on monkey patching to augment the forward(), eval(), and train() methods of model as well as augment individual modules to make them dynamic. This renders the conversion incompatible with other monkey patches to those methods and modules! Note that convert() is still fully compatible with inheritance.

Note

  1. Configs can be customized for individual layers usingglob expressions on qualified submodule names, e.g., as shown for nn.Conv2d in the above example.
  2. Keys in the config that appear earlier in a dict have lower priority, e.g.,backbone.stages.1.0.spatial_conv will have out_channels_ratio [0.334, 0.5, 0.667, 1.0], not [1.0].
  3. Config entries without layer qualifiers are also supported, e.g., as shown fornn.Sequential in the above example.
  4. Mixed usage of configurations with and without layer qualifiers is supported for_different_ layers, e.g., as shown for nn.Conv2d and nn.Sequential in the above example. For a specific layer type, only configurations with or without layer qualifiers are supported.
  5. Use * as a wildcard matching any layer.

export(model, strict=True, calib=False)

Export a pruned subnet to a regular model.

Parameters:

Returns:

The current active subnet in regular PyTorch model format.

Return type:

Module

Note

if model is a wrapper such as DistributedDataParallel, it will be unwrapped, e.g.,model.module will be returned.