mode — Model Optimizer 0.29.0 (original) (raw)

Interface and utilities for optimization modes/algorithms.

A mode is a specific type or algorithms for model optimization, e.g., some type of algorithm for pruning or quantization. It can also specify a single step within an optimization algorithm instead of the whole algorithm. For example, a mode can prepare a model for pruning or export (i.e. fix the optimal model configuration) after pruning.

Within modelopt, a mode constitutes the unit for model optimization. We can define arbitrary modes, each mode gets recorded in the model’s modelopt state dict, and we can define workflows as a sequence of modes.

Classes

ModeDescriptor Abstract class to describe a mode.

class ModeDescriptor

Bases: ABC

Abstract class to describe a mode.

assert_compatibility_as_next_mode_of(other_mode)

Assert that this mode is compatible as a next mode of the other mode.

Parameters:

other_mode (ModeDescriptor | str) –

Return type:

None

abstract property config_class_: type[ModeloptBaseConfig]_

Specifies the config class for the mode.

abstract property convert_: Callable[[Module, ModeloptBaseConfig], tuple[Module, dict[str, Any]]] | Callable[[Module, ModeloptBaseConfig, Any], tuple[Module, dict[str, Any]]]_

The mode’s entrypoint for converting a model.

The function signature of the convert entrypoint is described below:

Parameters:

Returns:

A tuple consisting of

  1. the in-place modified model. If the modification failed, the entrypoint can return None instead
  2. The config dict that can be used to call the restore entrypoint to instantly _restore_the modified model.
  3. The metatdata that can be used to call the restore entrypoint to instantly_restore_ the modified model from the provided initial state, see below’s description for the restore entrypoint to get more info about metadata.

Raises:

property export_mode_: str | None_

The mode that corresponds to the export mode of this mode.

Certain modes require a subsequent export step. For example, after pruning, we might want to fine-tune the model and then export the model. This property specifies that mode if it exists.

None indicates that there exists no such mode.

Returns:

The (optional) mode name that corresponds to the export mode of this mode. Defaults to None.

property is_export_mode_: bool_

Whether the mode is an export mode.

Returns:

True if the mode is an export mode, False otherwise. Defaults to False.

abstract property name_: str_

Returns the string name of the mode.

property next_modes_: set[str] | None_

Modes that must immediately follow this mode.

Certain modes only makes sense if they are followed by certain other modes.

An empty set indicates that _no_ mode can follow this mode. A None value indicates that there are no restrictions on the following mode.

Returns:

A set of mode names that must immediately follow this mode. Defaults to None.

property next_prohibited_modes_: set[str] | None_

Modes that should not be applied after this mode.

This is used to specify that certain modes should not be applied after this mode. A None value indicates that incompatibitly check is skipped for this mode. This is useful if there are many allowed modes but only a few prohibited modes.

Returns:

A set of mode names that should not be applied after this mode. Defaults to None.

property require_model_like_: bool_

Whether the mode requires a ModelLike input?

Returns:

False

abstract property restore_: Callable[[Module, ModeloptBaseConfig, dict[str, Any]], Module]_

The mode’s entrypoint for restoring a model.

The function signature of the restore entrypoint is described below:

Parameters:

Returns:

The in-place modified and restored model. If the modification failed, the entrypoint can

return None instead

Raises:

property search_algorithm_: type[BaseSearcher]_

Specifies the search algorithm to use for this mode (if any).

property update_for_new_mode_: Callable[[Module, ModeloptBaseConfig, dict[str, Any]], None]_

The mode’s (optional) entrypoint for updating a model’s config and metadata before a new mode.

This is useful if metadata or config needs to be updated before adding a new mode. For example, a after adding a new mode, the current mode’s restore might only need a subset of the metadata/config.

The function signature of this update entrypoint is described below:

Parameters:

Returns:

None.

property update_for_save_: Callable[[Module, ModeloptBaseConfig, dict[str, Any]], None]_

The mode’s (optional) entrypoint for updating a model’s config and metadata before saving.

This is useful if metadata or config needs to be updated for saving (and restoring) the mode.

The function signature of this update entrypoint is described below:

Parameters:

Returns:

None.