nncf.api.compression#

Classes#

CompressionLoss

Used to calculate the additional loss to be added to the base loss during the

CompressionScheduler

Implements the logic of compression method control during the training process.

CompressionStage

Specifies the compression stage for the model.

CompressionAlgorithmController

A handle to the compression-specific modifications made to the model.

CompressionAlgorithmBuilder

Determines which modifications should be made to the original model in order to enable algorithm-specific

class nncf.api.compression.CompressionLoss[source]#

Bases: abc.ABC

Used to calculate the additional loss to be added to the base loss during the training process. It uses the model graph to measure variables and activations values of the layers during the loss construction. For example, the $L_0$-based sparsity algorithm calculates the number of non-zero weights in convolutional and fully-connected layers to construct the loss function.

abstract calculate(*args, **kwargs)[source]#

Calculates and returns the compression loss value.

Return type:

Any

abstract load_state(state)[source]#

Loads the compression loss state.

Parameters:

state (Dict[str, Any]) – The state of the compression loss, most likely obtained as the result of a .get_state() method call.

Return type:

None

abstract get_state()[source]#

Returns the compression loss state.

Return type:

Dict[str, Any]

__call__(*args, **kwargs)[source]#

Calculates and returns the compression loss value. Same as .calculate().

Return type:

Any

class nncf.api.compression.CompressionScheduler[source]#

Bases: abc.ABC

Implements the logic of compression method control during the training process. May change the method hyperparameters in regard to the current training step or epoch. For example, the sparsity method can smoothly increase the sparsity rate over several epochs.

The step() and epoch_step() methods of the compression scheduler must be called at the beginning of each training step and epoch, respectively:

for epoch in range(0, num_epochs):
    scheduler.epoch_step()
    for i, (x, y) in enumerate(dataset):
         scheduler.step()
         ...
abstract step(next_step=None)[source]#

Should be called at the beginning of each training step to prepare the compression method to continue training the model in the next_step.

Parameters:

next_step (Optional[int]) – The global step index for which the compression scheduler will update the state of the compression method.

Return type:

None

abstract epoch_step(next_epoch=None)[source]#

Should be called at the beginning of each training epoch to prepare the compression method to continue training the model in the next_epoch.

Parameters:

next_epoch (Optional[int]) – The epoch index for which the compression scheduler will update the state of the compression method.

Return type:

None

abstract load_state(state)[source]#

Loads the compression scheduler state, but does not update the state of the compression method.

Parameters:

state (Dict[str, Any]) – Output of get_state() method.

Return type:

None

abstract get_state()[source]#

Returns the compression scheduler state.

Return type:

Dict[str, Any]

class nncf.api.compression.CompressionStage[source]#

Bases: enum.IntEnum

Specifies the compression stage for the model.

__add__(other)[source]#

Defines compression stage of a composite compression controller, consist of two algorithms, where self is the compression stage of the first algorithm and other - compression stage of the second one.

  • UNCOMPRESSED         & UNCOMPRESSED         == UNCOMPRESSED

  • PARTIALLY_COMPRESSED & PARTIALLY_COMPRESSED == PARTIALLY_COMPRESSED

  • FULLY_COMPRESSED     & FULLY_COMPRESSED     == FULLY_COMPRESSED

  • UNCOMPRESSED         & PARTIALLY_COMPRESSED == PARTIALLY_COMPRESSED

  • UNCOMPRESSED         & FULLY_COMPRESSED     == PARTIALLY_COMPRESSED

  • PARTIALLY_COMPRESSED & FULLY_COMPRESSED     == PARTIALLY_COMPRESSED

Parameters:

other (CompressionStage) – An instance of another compression stage.

Returns:

The common compression stage of the two algorithms.

Return type:

CompressionStage

class nncf.api.compression.CompressionAlgorithmController(target_model)[source]#

Bases: abc.ABC

A handle to the compression-specific modifications made to the model. Hosts entities that are to be used during the training process, such as compression scheduler and compression loss.

Parameters:

target_model (TModel) – The model with additional modifications necessary to enable algorithm-specific compression during fine-tuning built by the CompressionAlgorithmBuilder.

property model: TModel[source]#

The compressed model object with which this controller is associated.

Return type:

TModel

abstract property loss: CompressionLoss[source]#

The compression loss for this particular algorithm combination.

Return type:

CompressionLoss

abstract property scheduler: CompressionScheduler[source]#

The compression scheduler for this particular algorithm combination.

Return type:

CompressionScheduler

abstract property name: str[source]#

Name of the compression algorithm that is being controlled. Should be unique to identify the controller and its state among other controllers and their states.

Return type:

str

abstract property compression_rate: float[source]#

Returns a float compression rate value ranging from 0 to 1 (e.g. the sparsity level, or the ratio of filters pruned).

Return type:

float

abstract property maximal_compression_rate: float[source]#

Returns the maximal model compression rate supported by the compression controller.

Return type:

float

abstract load_state(state)[source]#

Loads the compression controller state from the map of algorithm name to the dictionary with state attributes.

Parameters:

state (Dict[str, Dict[str, Any]]) – map of the algorithm name to the dictionary with the corresponding state attributes.

Return type:

None

abstract get_state()[source]#

Returns the compression controller state, which is the map of the algorithm name to the dictionary with the corresponding state attributes.

Return type:

Dict[str, Dict[str, Any]]

abstract get_compression_state()[source]#

Returns the compression state - builder and controller state. This state should be used to unambiguously resume compression via compression_state argument of create_compressed_model method.

Returns:

Compression state of the model to resume compression from it.

Return type:

Dict[str, Any]

compression_stage()[source]#

Returns the compression stage. Should be used on saving best checkpoints to distinguish between uncompressed, partially compressed, and fully compressed models.

Returns:

The compression stage of the target model.

Return type:

CompressionStage

abstract statistics(quickly_collected_only=False)[source]#

Returns a Statistics class instance that contains compression algorithm statistics.

Parameters:

quickly_collected_only (bool) – Enables collection of the statistics that don’t take too much time to compute. Can be helpful for the case when need to keep track of statistics on each training batch/step/iteration.

Return type:

nncf.api.statistics.Statistics

strip_model(model, do_copy=False)[source]#

Strips auxiliary layers that were used for the model compression, as it’s only needed for training. The method is used before exporting the model in the target format.

Parameters:
  • model (TModel) – The compressed model.

  • do_copy (bool) – Modify copy of the model, defaults to False.

Returns:

The stripped model.

Return type:

TModel

prepare_for_export()[source]#

Prepare the compressed model for exporting to a backend-specific model serialization format.

Return type:

None

strip(do_copy=True)[source]#

Returns the model object with as much custom NNCF additions as possible removed while still preserving the functioning of the model object as a compressed model.

Parameters:

do_copy (bool) – If True (default), will return a copy of the currently associated model object. If False, will return the currently associated model object “stripped” in-place.

Returns:

The stripped model.

Return type:

TModel

abstract export_model(save_path, save_format=None, input_names=None, output_names=None, model_args=None)[source]#

Exports the compressed model to the specified format for deployment.

Makes method-specific preparations of the model, (e.g. removing auxiliary layers that were used for the model compression), then exports the model to the specified path.

Parameters:
  • save_path (str) – The path where the model will be saved.

  • save_format (Optional[str]) – Saving format. The default format will be used if save_format is not specified.

  • input_names (Optional[List[str]]) – Names to be assigned to the input tensors of the model.

  • output_names (Optional[List[str]]) – Names to be assigned to the output tensors of the model.

  • model_args (Optional[Tuple[Any, Ellipsis]]) –

    Tuple of additional positional and keyword arguments which are required for the model’s forward during export. Should be specified in the following format:

    • (a, b, {‘x’: None, ‘y’: y}) for positional and keyword arguments.

    • (a, b, {}) for positional arguments only.

    • ({‘x’: None, ‘y’: y},) for keyword arguments only.

Return type:

None

abstract disable_scheduler()[source]#

Disables current compression scheduler during training by changing it to a dummy one that does not change the compression rate.

Return type:

None

class nncf.api.compression.CompressionAlgorithmBuilder[source]#

Bases: abc.ABC

Determines which modifications should be made to the original model in order to enable algorithm-specific compression during fine-tuning.

abstract property name: str[source]#
Returns:

name of the compression algorithm that is being built. Should be unique to identify the builder

Return type:

str

and its state among other builders and their states.

abstract apply_to(model)[source]#

Applies algorithm-specific modifications to the model.

Parameters:

model (TModel) – The original uncompressed model.

Returns:

The model with additional modifications necessary to enable algorithm-specific compression during fine-tuning.

Return type:

TModel

abstract build_controller(model)[source]#

Builds an instance of algorithm-specific nncf.api.compression.CompressionAlgorithmController to handle the additional modules, parameters, and hooks inserted into the model to enable algorithm-specific compression.

Parameters:

model (TModel) – The model with additional modifications necessary to enable algorithm-specific compression during fine-tuning.

Returns:

The instance of a CompressionAlgorithmController-derived class, specific for this algorithm.

Return type:

CompressionAlgorithmController

abstract get_transformation_layout(model)[source]#

Computes necessary model transformations to enable algorithm-specific compression.

Parameters:

model (TModel) – The original uncompressed model.

Returns:

The instance of the TransformationLayout class containing a list of algorithm-specific modifications.

Return type:

nncf.common.graph.transformations.layout.TransformationLayout

abstract initialize(model)[source]#

Initialize model parameters before training.

Parameters:

model (TModel) – The model with additional modifications necessary to enable algorithm-specific compression during fine-tuning.

Return type:

None

abstract load_state(state)[source]#

Initializes object from the supplied state.

Parameters:

state (Dict[str, Any]) – The state of the builder, most likely obtained as the result of a .get_state() call.

Return type:

None

abstract get_state()[source]#

Returns a dictionary with Python data structures (dict, list, tuple, str, int, float, True, False, None) that represents state of the object.

Return type:

Dict[str, Any]