Skip to content

Package

model_navigator.api.package

Package operations related API.

get_best_model_status(package, strategy=None, include_source=True)

Returns ModelStatus of best model for given strategy.

If model with given strategy cannot be found, search is repeated with MaxThroughputStrategy. If there is no model matching given strategy or MaxThroughputStrategy, function returns None.

Parameters:

Name Type Description Default
package Package

A package object to be searched for best model.

required
strategy Optional[RuntimeSearchStrategy]

Strategy for finding the best model. Defaults to MaxThroughputAndMinLatencyStrategy

None
include_source bool

Flag if Python based model has to be included in analysis

True

Returns:

Type Description
Optional[ModelStatus]

ModelStatus of best model for given strategy or None.

Source code in model_navigator/api/package.py
def get_best_model_status(
    package: Package,
    strategy: Optional[RuntimeSearchStrategy] = None,
    include_source: bool = True,
) -> Optional[ModelStatus]:
    """Returns ModelStatus of best model for given strategy.

    If model with given strategy cannot be found, search is repeated with MaxThroughputStrategy.
    If there is no model matching given strategy or MaxThroughputStrategy, function returns None.

    Args:
        package: A package object to be searched for best model.
        strategy: Strategy for finding the best model. Defaults to `MaxThroughputAndMinLatencyStrategy`
        include_source: Flag if Python based model has to be included in analysis

    Returns:
        ModelStatus of best model for given strategy or None.
    """
    return package.get_best_model_status(strategy=strategy, include_source=include_source)

load(path, workspace=None)

Load package from provided path.

Parameters:

Name Type Description Default
path Union[str, Path]

The location of package to load

required
workspace Optional[Union[str, Path]]

Workspace where packages will be extracted

None

Returns:

Type Description
Package

Package.

Source code in model_navigator/api/package.py
def load(
    path: Union[str, Path],
    workspace: Optional[Union[str, Path]] = None,
) -> Package:
    """Load package from provided path.

    Args:
        path: The location of package to load
        workspace: Workspace where packages will be extracted

    Returns:
        Package.
    """
    return Package.load(path=path, workspace=workspace)

optimize(package, target_formats=None, runners=None, profiler_config=None, verbose=False, debug=False, verify_func=None, custom_configs=None, defaults=True)

Generate target formats and run correctness and profiling tests for available runners.

Parameters:

Name Type Description Default
package Package

Package to optimize.

required
target_formats Optional[Union[Union[str, Format], Tuple[Union[str, Format], ...]]]

Formats to generate and profile. Defaults to target formats from the package.

None
runners Optional[Union[Union[str, Type[NavigatorRunner]], Tuple[Union[str, Type[NavigatorRunner]], ...]]]

Runners to run correctness tests and profiling on. Defaults to runners from the package.

None
profiler_config Optional[ProfilerConfig]

Configuration of the profiler. Defaults to config from the package.

None
verbose bool

If True enable verbose logging. Defaults to False.

False
debug bool

If True print debugging logs. Defaults to False.

False
verify_func Optional[VerifyFunction]

Function used for verifying generated models. Defaults to None.

None
custom_configs Optional[List[CustomConfig]]

Custom formats configuration. Defaults to None.

None
defaults bool

reset configuration of custom configs to defaults

True

Returns:

Type Description
Package

Optimized package

Source code in model_navigator/api/package.py
def optimize(
    package: Package,
    target_formats: Optional[Union[Union[str, Format], Tuple[Union[str, Format], ...]]] = None,
    runners: Optional[Union[Union[str, Type[NavigatorRunner]], Tuple[Union[str, Type[NavigatorRunner]], ...]]] = None,
    profiler_config: Optional[ProfilerConfig] = None,
    verbose: bool = False,
    debug: bool = False,
    verify_func: Optional[VerifyFunction] = None,
    custom_configs: Optional[List[CustomConfig]] = None,
    defaults: bool = True,
) -> Package:
    """Generate target formats and run correctness and profiling tests for available runners.

    Args:
        package: Package to optimize.
        target_formats: Formats to generate and profile. Defaults to target formats from the package.
        runners: Runners to run correctness tests and profiling on. Defaults to runners from the package.
        profiler_config: Configuration of the profiler. Defaults to config from the package.
        verbose: If True enable verbose logging. Defaults to False.
        debug: If True print debugging logs. Defaults to False.
        verify_func: Function used for verifying generated models. Defaults to None.
        custom_configs: Custom formats configuration. Defaults to None.
        defaults: reset configuration of custom configs to defaults

    Returns:
        Optimized package
    """
    if package.is_empty() and package.model is None:
        raise ModelNavigatorEmptyPackageError(
            "Package is empty and source model is not loaded. Unable to run optimize."
        )
    config = package.config

    if target_formats is None:
        target_formats = DEFAULT_TARGET_FORMATS[package.framework]
        if package.framework == Framework.TORCH and config.batch_dim is not None:
            target_formats, custom_configs = update_allowed_batching_parameters(
                target_formats=target_formats,
                custom_configs=custom_configs,
            )

    if runners is None:
        runners = default_runners()

    if profiler_config is None:
        profiler_config = ProfilerConfig()

    is_source_available = package.model is not None
    _update_config(
        config=config,
        is_source_available=is_source_available,
        target_formats=target_formats,
        runners=runners,
        profiler_config=profiler_config,
        verbose=verbose,
        debug=debug,
        verify_func=verify_func,
        custom_configs=custom_configs,
        defaults=defaults,
    )

    builders = _get_builders(
        framework=package.framework,
        run_profiling=config.profiler_config.run_profiling,
    )

    model_configs = _get_model_configs(
        config=config,
        custom_configs=list(config.custom_configs.values()),
    )

    optimized_package = PipelineManager.run(
        pipeline_builders=builders,
        config=config,
        models_config=model_configs,
        package=package,
    )

    return optimized_package

save(package, path, keep_workspace=True, override=False, save_data=True)

Save export results into the .nav package at given path.

Parameters:

Name Type Description Default
package Package

A package object to prepare the package

required
path Union[str, Path]

A path to file where the package has to be saved

required
keep_workspace bool

flag to remove the working directory after saving the package

True
override bool

flag to override existing package in provided path

False
save_data bool

disable saving samples from the dataloader

True
Source code in model_navigator/api/package.py
def save(
    package: Package,
    path: Union[str, Path],
    keep_workspace: bool = True,
    override: bool = False,
    save_data: bool = True,
) -> None:
    """Save export results into the .nav package at given path.

    Args:
        package: A package object to prepare the package
        path: A path to file where the package has to be saved
        keep_workspace: flag to remove the working directory after saving the package
        override: flag to override existing package in provided path
        save_data: disable saving samples from the dataloader
    """
    package.save(
        path=path,
        keep_workspace=keep_workspace,
        override=override,
        save_data=save_data,
    )

set_verified(package, model_key, runner_name)

Set verified status for model and runner.

Parameters:

Name Type Description Default
package Package

Package.

required
model_key str

Unique key of the model.

required
runner_name str

Name of the runner.

required

Raises:

Type Description
ModelNavigatorNotFoundError

When model and runner not found.

Source code in model_navigator/api/package.py
def set_verified(
    package: Package,
    model_key: str,
    runner_name: str,
) -> None:
    """Set verified status for model and runner.

    Args:
        package (Package): Package.
        model_key (str): Unique key of the model.
        runner_name (str): Name of the runner.

    Raises:
        ModelNavigatorNotFoundError: When model and runner not found.
    """
    try:
        runner_results = package.status.models_status[model_key].runners_status[runner_name]
    except KeyError:
        raise ModelNavigatorNotFoundError(f"Model {model_key} and runner {runner_name} not found.")
    runner_results.status[VerifyModel.__name__] = CommandStatus.OK