network — Model Optimizer 0.27.1 (original) (raw)

Utility functions for PyTorch models.

Functions

compare_dict Compare two dictionaries and return keys with unmatched values.
get_model_attributes Get the key attributes of a PyTorch model.
get_module_device Get the device of a PyTorch module.
get_same_padding Get the same padding for a given kernel size.
init_model_from_model_like Initialize a model from a model-like object.
is_channels_last Check if the model is using channels last memory format.
is_parallel Check if a PyTorch model is parallelized.
make_divisible Function taken from the original tf repo.
model_to Convert model to the same device, dtype and memory layout as the target_model.
param_num Get the number of parameters of a PyTorch model.
param_num_from_forward Get the number of parameters of a PyTorch model from a forward pass.
remove_bn Remove all batch normalization layers in the network.
run_forward_loop Run multiple forward passes with a model according to the provided data loader.
set_submodule The set function that complements nn.Module.get_submodule().
standardize_model_args Standardize model arguments according to torch.onnx.export.
standardize_model_like_tuple Standardize a model-like tuple.
standardize_named_model_args Standardize model arguments according to torch.onnx.export and give them a name.
standardize_constructor_args Standardize a constructor-like tuple.
unwrap_model Unwrap a model that is wrapped by supported wrapper module or return original model.
zero_grad Set any gradients in the model's parameters to None.
create_param_grad_clear_hook Create a hook to clear gradients for a parameter.
get_unwrapped_name Get the cleaned module name (i.e, the name before wrapping with sharded modules).

compare_dict(dict1, dict2)

Compare two dictionaries and return keys with unmatched values.

Parameters:

Return type:

_tuple_[str, …]

create_param_grad_clear_hook(param)

Create a hook to clear gradients for a parameter.

The hook will be fired after the gradient is accumulated for the parameter. Important: For this to work, accum_grad should be kept alive as longs as this utility is needed.

get_model_attributes(model)

Get the key attributes of a PyTorch model.

Parameters:

model (Module) –

Return type:

_dict_[str, _Any_]

get_module_device(module)

Get the device of a PyTorch module.

Parameters:

module (Module) –

Return type:

device

get_same_padding(kernel_size)

Get the same padding for a given kernel size.

Parameters:

kernel_size (int | tuple [ int , int ]) –

Return type:

int | tuple

get_unwrapped_name(name)

Get the cleaned module name (i.e, the name before wrapping with sharded modules).

Parameters:

name (str) –

Return type:

str

init_model_from_model_like(model)

Initialize a model from a model-like object.

Parameters:

model (Module | type [ Module ] | tuple | Callable) – A model-like object. Can be a nn.Module (returned as it is), a model class or callable, or a tuple. If a tuple, it must be of the form (model_cls_or_callable,) or (model_cls_or_callable, args) or (model_cls_or_callable, args, kwargs). Model will be initialized as model_cls_or_callable(*args, **kwargs).

Return type:

Module

is_channels_last(model)

Check if the model is using channels last memory format.

Parameters:

model (Module) –

is_parallel(model)

Check if a PyTorch model is parallelized.

Parameters:

model (Module) –

Return type:

bool

make_divisible(v, divisor, min_val=None)

Function taken from the original tf repo.

It ensures that all layers have a channel number that is divisible by 8 It can be seen here:https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py

Parameters:

Return type:

int | float

model_to(model, target_model)

Convert model to the same device, dtype and memory layout as the target_model.

Parameters:

param_num(network, trainable_only=False, unit=1000000.0)

Get the number of parameters of a PyTorch model.

Parameters:

Returns:

The number of parameters in the model in the given unit.

Return type:

float

param_num_from_forward(model, trainable_only=False, args=None, unit=1000000.0)

Get the number of parameters of a PyTorch model from a forward pass.

Parameters:

Returns:

The number of parameters from the model’s forward pass in the given unit.

This can helpful for dynamic modules, where the state dict might contain extra parameters that is not actively used in the model, e.g., because of a DynamicModule that is deactivated for the forward pass. We circumvent this issue by just counting parameters of modules that appear in a forward pass.

remove_bn(model)

Remove all batch normalization layers in the network.

Parameters:

model (Module) –

run_forward_loop(model, data_loader, max_iters=None, collect_func=None, progress_bar=None, post_process=None)

Run multiple forward passes with a model according to the provided data loader.

Parameters:

set_submodule(model, target, target_submodule)

The set function that complements nn.Module.get_submodule().

Parameters:

standardize_constructor_args(constructor_args)

Standardize a constructor-like tuple.

Parameters:

constructor_args (Callable | tuple) –

Return type:

_tuple_[Callable, tuple, _dict_]

standardize_model_args(model_or_fw_or_sig, args, use_kwargs=False)

Standardize model arguments according to torch.onnx.export.

Parameters:

Returns:

Standardized model args that can be used in model.forward() in the same standardized way no matter how they were provided, see below for more info.

Return type:

tuple

Warning

If use_kwargs == False the model’s forward() method cannot contain keyword-only arguments (e.g. forward(..., *, kw_only_args)) without default values and you must not provide them in args.

Warning

If use_kwargs == False you must not provide variable keyword arguments in args that are processed via variable keyword arguments in the model’s forward() method (e.g. forward(..., **kwargs)).

standardize_model_like_tuple(model)

Standardize a model-like tuple.

Parameters:

model (Module | type [ Module ] | tuple | Callable) –

Return type:

_tuple_[_type_[_Module_], tuple, _dict_]

standardize_named_model_args(model_or_fw_or_sig, args)

Standardize model arguments according to torch.onnx.export and give them a name.

Parameters:

Return type:

_tuple_[_dict_[str, _Any_], _set_[_str_]]

Returns: A tuple (args_normalized, args_with_default) where

args_normalized is a dictionary of ordered model args where the key represents a unique

serialized string based on the the argument’s name in the function signature and the value contains the actual argument,

args_with_default is a set indicating whether the argument was retrieved from the default

value in the function signature of the model’s forward() method or whether the argument exactly corresponds to the default value.

Note

See standardize_model_args() for more info as well.

unwrap_model(model, warn=False, raise_error=False, msg='', force_unwrap=False)

Unwrap a model that is wrapped by supported wrapper module or return original model.

Parameters:

Return type:

Module

zero_grad(model)

Set any gradients in the model’s parameters to None.

Parameters:

model (Module) –

Return type:

None