neural_compressor.compression.pruner.utils

Prune utils.

Functions

get_sparsity_ratio(pruners, model)

Calculate sparsity ratio of a module/layer.

get_sparsity_ratio_tf(pruners, model)

Calculate sparsity ratio of a module/layer.

check_config(prune_config)

Check if the configuration dict is valid for running Pruning object.

reset_none_to_default(obj, key, default)

Set undefined configurations to default values.

update_params(info)

Update parameters.

process_weight_config(global_config, local_configs, ...)

Process pruning configurations.

process_yaml_config(global_config, local_configs, ...)

Process the yaml configuration file.

check_key_validity(template_config, user_config)

Check the validity of keys.

process_and_check_config(val)

Process and check configurations.

process_config(config)

Obtain a config dict object from the config file.

parse_last_linear(model)

Locate the last linear layers of the model.

parse_last_linear_tf(model)

Locate the last linear layers of the model.

parse_to_prune(config, model)

Keep target pruned layers.

parse_to_prune_tf(config, model)

Keep target pruned layers.

get_layers(model)

Get each layer's name and its module.

collect_layer_inputs(model, layers, layer_idx, ...[, ...])

Getting the forward input of a layer.

Module Contents

neural_compressor.compression.pruner.utils.get_sparsity_ratio(pruners, model)[source]

Calculate sparsity ratio of a module/layer.

Returns:

Three floats. elementwise_over_matmul_gemm_conv refers to zero elements’ ratio in pruning layers. elementwise_over_all refers to zero elements’ ratio in all layers in the model. blockwise_over_matmul_gemm_conv refers to all-zero blocks’ ratio in pruning layers.

neural_compressor.compression.pruner.utils.get_sparsity_ratio_tf(pruners, model)[source]

Calculate sparsity ratio of a module/layer.

Returns:

Three floats. elementwise_over_matmul_gemm_conv refers to zero elements’ ratio in pruning layers. elementwise_over_all refers to zero elements’ ratio in all layers in the model. blockwise_over_matmul_gemm_conv refers to all-zero blocks’ ratio in pruning layers.

neural_compressor.compression.pruner.utils.check_config(prune_config)[source]

Check if the configuration dict is valid for running Pruning object.

Parameters:

prune_config – A config dict object that contains Pruning parameters and configurations.

Returns:

None if everything is correct.

Raises:

AssertionError.

neural_compressor.compression.pruner.utils.reset_none_to_default(obj, key, default)[source]

Set undefined configurations to default values.

Parameters:
  • obj – A dict{key: value}

  • key – A string representing the key in obj.

  • default – When the key is not in obj, add key by the default item in original obj.

neural_compressor.compression.pruner.utils.update_params(info)[source]

Update parameters.

neural_compressor.compression.pruner.utils.process_weight_config(global_config, local_configs, default_config)[source]

Process pruning configurations.

Parameters:
  • global_config – A config dict object that contains pruning parameters and configurations.

  • local_config – A config dict object that contains pruning parameters and configurations.

  • default_config – A config dict object that contains pruning parameters and configurations.

Returns:

A config dict object that contains pruning parameters and configurations.

Return type:

pruners_info

neural_compressor.compression.pruner.utils.process_yaml_config(global_config, local_configs, default_config)[source]

Process the yaml configuration file.

Parameters:
  • global_config – A config dict object that contains pruning parameters and configurations.

  • local_config – A config dict object that contains pruning parameters and configurations.

  • default_config – A config dict object that contains pruning parameters and configurations.

Returns:

A config dict object that contains pruning parameters and configurations.

Return type:

pruners_info

neural_compressor.compression.pruner.utils.check_key_validity(template_config, user_config)[source]

Check the validity of keys.

Parameters:
  • template_config – A default config dict object that contains pruning parameters and configurations.

  • user_config – A user config dict object that contains pruning parameters and configurations.

neural_compressor.compression.pruner.utils.process_and_check_config(val)[source]

Process and check configurations.

Parameters:

val – A dict that contains the layer-specific pruning configurations.

neural_compressor.compression.pruner.utils.process_config(config)[source]

Obtain a config dict object from the config file.

Parameters:

config – A string representing the path to the configuration file.

Returns:

A config dict object.

neural_compressor.compression.pruner.utils.parse_last_linear(model)[source]

Locate the last linear layers of the model. While pruning, the final linear often acts like classifier head, which might cause accuracy drop.

Parameters:

model – The model to be pruned.

neural_compressor.compression.pruner.utils.parse_last_linear_tf(model)[source]

Locate the last linear layers of the model. While pruning, the final linear often acts like classifier head, which might cause accuracy drop.

Parameters:

model (tf.keras.Model) – The model to be pruned.

neural_compressor.compression.pruner.utils.parse_to_prune(config, model)[source]

Keep target pruned layers.

Parameters:
  • config – A string representing the path to the configuration file.

  • model – The model to be pruned.

neural_compressor.compression.pruner.utils.parse_to_prune_tf(config, model)[source]

Keep target pruned layers.

Parameters:
  • config (string) – A string representing the path to the configuration file.

  • model (tf.keras.Model) – The model to be pruned.

neural_compressor.compression.pruner.utils.get_layers(model)[source]

Get each layer’s name and its module.

Parameters:

model – The model to be pruned.

Returns: each layer’s name and its modules

neural_compressor.compression.pruner.utils.collect_layer_inputs(model, layers, layer_idx, layer_inputs, device='cuda:0')[source]

Getting the forward input of a layer.

Parameters:
  • model – The model to be pruned.

  • layers – Selectable layers of the model.

  • layer_idx – The layer index.

  • layer_inputs – The dataloader or the output of the previous layer.

  • device – Specify the type of device to return.

Returns: input list.