Skip to content

Optimize

model_navigator.package.optimize

optimize(package, target_formats=None, target_device=DeviceKind.CUDA, runners=None, optimization_profile=None, verbose=False, debug=False, verify_func=None, custom_configs=None, defaults=True, fail_on_empty=True)

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

Parameters:

  • package (Package) –

    Package to optimize.

  • target_formats (Optional[Tuple[Union[str, Format], ...]], default: None ) –

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

  • target_device (Optional[DeviceKind], default: CUDA ) –

    Target device for optimize process, default is CUDA

  • runners (Optional[Tuple[Union[str, Type[NavigatorRunner]], ...]], default: None ) –

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

  • optimization_profile (Optional[OptimizationProfile], default: None ) –

    Optimization profile used for conversion and profiling.

  • verbose (bool, default: False ) –

    If True enable verbose logging. Defaults to False.

  • debug (bool, default: False ) –

    If True print debugging logs. Defaults to False.

  • verify_func (Optional[VerifyFunction], default: None ) –

    Function used for verifying generated models. Defaults to None.

  • custom_configs (Optional[List[CustomConfig]], default: None ) –

    Custom formats configuration. Defaults to None.

  • defaults (bool, default: True ) –

    reset configuration of custom configs to defaults

  • fail_on_empty (bool, default: True ) –

    Fail optimization when empty (no model or base exported model) package provided

Returns:

Source code in model_navigator/package/__init__.py
def optimize(
    package: Package,
    target_formats: Optional[Tuple[Union[str, Format], ...]] = None,
    target_device: Optional[DeviceKind] = DeviceKind.CUDA,
    runners: Optional[Tuple[Union[str, Type[NavigatorRunner]], ...]] = None,
    optimization_profile: Optional[OptimizationProfile] = None,
    verbose: bool = False,
    debug: bool = False,
    verify_func: Optional[VerifyFunction] = None,
    custom_configs: Optional[List[CustomConfig]] = None,
    defaults: bool = True,
    fail_on_empty: 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.
        target_device: Target device for optimize process, default is CUDA
        runners: Runners to run correctness tests and profiling on. Defaults to runners from the package.
        optimization_profile: Optimization profile used for conversion and profiling.
        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
        fail_on_empty: Fail optimization when empty (no model or base exported model) package provided

    Returns:
        Optimized package
    """
    if fail_on_empty and 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

    is_source_available = package.model is not None
    if target_formats is None:
        target_formats = get_target_formats(framework=package.framework, is_source_available=is_source_available)

    if runners is None:
        runners = default_runners(device_kind=target_device)

    if optimization_profile is None:
        optimization_profile = OptimizationProfile()

    _update_config(
        config=config,
        is_source_available=is_source_available,
        target_formats=target_formats,
        runners=runners,
        optimization_profile=optimization_profile,
        verbose=verbose,
        debug=debug,
        verify_func=verify_func,
        custom_configs=custom_configs,
        defaults=defaults,
        target_device=target_device,
    )

    builders = _get_builders(
        framework=package.framework,
    )

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

    optimized_package = optimize_pipeline(
        package=package,
        workspace=package.workspace.path,
        builders=builders,
        config=config,
        models_config=models_config,
    )

    return optimized_package