sparsify.blueprints.utils package

Submodules

sparsify.blueprints.utils.helpers module

Helper functions and classes for flask blueprints

exception sparsify.blueprints.utils.helpers.HTTPNotFoundError(*args: object)[source]

Bases: Exception

Expected error raised when a 404 should be encountered by the user

sparsify.blueprints.utils.projects module

Helper functions and classes for flask blueprints specific to projects

sparsify.blueprints.utils.projects.get_project_by_id(project_id: str)sparsify.models.projects.Project[source]

Get a project by its project_id, with project model and project data joined.

Parameters

project_id – project id of the project

Returns

Project with the project id

sparsify.blueprints.utils.projects.get_project_model_by_project_id(project_id: str, raise_not_found: bool = True)sparsify.models.projects_model.ProjectModel[source]

Get a project model by its project_id

Parameters
  • project_id – project id of the project model

  • raise_not_found – if no model is found raise an HTTPNotFoundError, otherwise return the result no matter what

Returns

Project model with the project id

sparsify.blueprints.utils.projects_benchmark module

Helper functions and classes for flask blueprints specific to project benchmark

sparsify.blueprints.utils.projects_benchmark.get_project_benchmark_by_ids(project_id: str, benchmark_id: str)sparsify.models.projects_benchmark.ProjectBenchmark[source]

Get a project benchmark by its project_id and benchmark_id

Parameters
  • project_id – project id of the optimizer

  • benchmark_id – benchmark id of the optimizer

Returns

Project benchmark with provided ids

sparsify.blueprints.utils.projects_data module

Helper functions and classes for flask blueprints specific to projects data

sparsify.blueprints.utils.projects_data.get_project_data_by_ids(project_id: str, data_id: str)sparsify.models.projects_data.ProjectData[source]
sparsify.blueprints.utils.projects_data.validate_model_data(data_path: str, model_path: str)[source]

sparsify.blueprints.utils.projects_optimizations module

Helper functions and classes for flask blueprints specific to project optimizations

class sparsify.blueprints.utils.projects_optimizations.OptimEpochs(training_epochs, start_epoch, stabilization_epochs, pruning_epochs, fine_tuning_epochs, end_epoch, pruning_start_epoch, pruning_end_epoch, pruning_update_frequency, fine_tuning_start_epoch)

Bases: tuple

property end_epoch

Alias for field number 5

property fine_tuning_epochs

Alias for field number 4

property fine_tuning_start_epoch

Alias for field number 9

property pruning_end_epoch

Alias for field number 7

property pruning_epochs

Alias for field number 3

property pruning_start_epoch

Alias for field number 6

property pruning_update_frequency

Alias for field number 8

property stabilization_epochs

Alias for field number 2

property start_epoch

Alias for field number 1

property training_epochs

Alias for field number 0

sparsify.blueprints.utils.projects_optimizations.create_config(project: sparsify.models.projects.Project, optim: sparsify.models.projects_optimizations.ProjectOptimization, framework: str)str[source]

Creates a optimization config yaml for a given project and optimization

Parameters
  • project – project to create with

  • optim – project optimizer to create with

  • framework – the framework to create the config for

sparsify.blueprints.utils.projects_optimizations.default_epochs_distribution(training_epochs: Union[None, int])sparsify.blueprints.utils.projects_optimizations.OptimEpochs[source]

Create default epochs distribution for optimizing a model given a number of training epochs.

Parameters

training_epochs – the original training epochs, if not set will default to 100

Returns

the default epochs distribution for optimizing a model

sparsify.blueprints.utils.projects_optimizations.default_pruning_settings()[source]
Returns

the default pruning settings for optimizing a model

sparsify.blueprints.utils.projects_optimizations.get_profiles_by_id(profile_perf_id: Union[None, str], profile_loss_id: Union[None, str])Tuple[sparsify.models.projects_profiles.ProjectPerfProfile, sparsify.models.projects_profiles.ProjectLossProfile][source]

Get a performance and loss profile by their ids. If not found will return None instead of raising not found.

Parameters
  • profile_perf_id – id of the performance profile to get

  • profile_loss_id – id of the loss profile to get

Returns

tuple containing (performance profile, loss profile)

sparsify.blueprints.utils.projects_optimizations.get_project_optimizer_by_ids(project_id: str, optim_id: str)sparsify.models.projects_optimizations.ProjectOptimization[source]

Get a project optimizer by its project_id and optim_id

Parameters
  • project_id – project id of the optimizer

  • optim_id – optim id of the optimizer

Returns

Project optimizer with provided ids

sparsify.blueprints.utils.projects_optimizations.optim_lr_sched_default_mods(training_init_lr: Optional[float], training_final_lr: Optional[float], start_epoch: Optional[float], start_fine_tuning_epoch: Optional[float], end_epoch: Optional[float])List[Dict[str, Any]][source]

Default modifiers for an LR schedule for pruning a model. If training_init_lr is set, adds a set LR modifier. If training_init_lr and training_final_lr are set, adds a step LR modifier.

Parameters
  • training_init_lr – the initial LR for training

  • training_final_lr – the final LR for training

  • start_epoch – the epoch training should start at

  • start_fine_tuning_epoch – the epoch fine tuning should start at

  • end_epoch – the final epoch for training

Returns

the default modifiers for an LR schedule

sparsify.blueprints.utils.projects_optimizations.optim_lr_sched_updater(lr_sched: sparsify.models.projects_optimizations.ProjectOptimizationModifierLRSchedule, lr_mods: Union[None, List[Dict[str, Any]]] = None, global_start_epoch: Union[None, float] = None, global_end_epoch: Union[None, float] = None)[source]

Update an LR schedule DB model. Will always update schedule level details from the contained lr_mods

Parameters
  • lr_sched – the DB model

  • lr_mods – the mods to set, if any

  • global_start_epoch – the optim’s start epoch, if set and greater than current start_epoch will set start_epoch to this

  • global_end_epoch – the optim’s end epoch, if set and less than current end_epoch will set end_epoch to this

sparsify.blueprints.utils.projects_optimizations.optim_pruning_updater(pruning: sparsify.models.projects_optimizations.ProjectOptimizationModifierPruning, start_epoch: Union[None, float] = None, end_epoch: Union[None, float] = None, update_frequency: Union[None, float] = None, pruning_settings: Optional[sparsify.blueprints.utils.projects_optimizations_pruning.PruningSettings] = None, mask_type: Optional[str] = None, sparsity: Union[None, float] = None, balance_perf_loss: Union[None, float] = None, filter_min_sparsity: Union[None, float] = None, filter_min_perf_gain: Union[None, float] = None, filter_min_recovery: Union[None, float] = None, nodes: Union[None, List[Dict[str, Any]]] = None, model: Union[None, sparsify.models.projects_model.ProjectModel] = None, profile_perf: Union[None, sparsify.models.projects_profiles.ProjectPerfProfile] = None, profile_loss: Union[None, sparsify.models.projects_profiles.ProjectLossProfile] = None, global_start_epoch: Union[None, float] = None, global_end_epoch: Union[None, float] = None)[source]

Update a pruning DB model

Parameters
  • pruning – the DB model

  • start_epoch – the start_epoch to set, if any

  • end_epoch – the end_epoch to set, if any

  • update_frequency – the update_frequency to set, if any

  • pruning_settings – the pruning_settings to use for updating / automatically generating new sparsity levels for nodes. If provided, overrides mask_type, sparsity, balance_perf_loss, filter_min_sparsity, filter_min_perf_gain, and filter_min_recovery

  • mask_type – the mask_type to set, if any

  • sparsity – the sparsity level to set, if set will update / automatically generate new sparsity levels for nodes

  • balance_perf_loss – the balance_perf_loss to set, if any

  • filter_min_sparsity – the filter_min_sparsity to set, if any

  • filter_min_perf_gain – the filter_min_perf_gain to set, if any

  • filter_min_recovery – the filter_min_recovery to set, if any

  • nodes – the nodes to set, if set will update node and model metrics for perf and loss

  • model – the model to use to update values with

  • profile_perf – the performance profile to use to update values with, if set will update nodes

  • profile_loss – the loss profile to use to update values with, if set will update nodes

  • global_start_epoch – the optim’s start epoch, if set and greater than current start_epoch will set start_epoch to this

  • global_end_epoch – the optim’s end epoch, if set and less than current end_epoch will set end_epoch to this

sparsify.blueprints.utils.projects_optimizations.optim_trainable_default_nodes(default_trainable: bool, model_analysis: Dict, node_overrides: Union[None, List[Dict[str, Any]]] = None)List[Dict[str, Any]][source]

Create the default trainable nodes for optimizing a model. Creates a node for all prunable nodes in the model with trainable set to default_trainable.

Parameters
  • default_trainable – True to default all prunable nodes to trainable, False otherwise

  • model_analysis – the analysis for the model

  • node_overrides – specific node overrides to use instead of default_trainable

Returns

the default trainable nodes

sparsify.blueprints.utils.projects_optimizations.optim_trainable_updater(trainable: sparsify.models.projects_optimizations.ProjectOptimizationModifierTrainable, analysis: Dict[str, List], start_epoch: Union[None, float] = None, end_epoch: Union[None, float] = None, nodes: Union[None, List[Dict[str, Any]]] = None, default_trainable: Union[None, bool] = None, global_start_epoch: Union[None, float] = None, global_end_epoch: Union[None, float] = None)[source]

Update a trainable DB model

Parameters
  • trainable – the DB model

  • analysis – model analysis

  • start_epoch – the start_epoch to set, if any

  • end_epoch – the end_epoch to set, if any

  • nodes – the nodes to set, if any

  • default_trainable – the default trainable to set, if any

  • global_start_epoch – the optim’s start epoch, if set and greater than current start_epoch will set start_epoch to this

  • global_end_epoch – the optim’s end epoch, if set and less than current end_epoch will set end_epoch to this

sparsify.blueprints.utils.projects_optimizations.optim_updater(optim: sparsify.models.projects_optimizations.ProjectOptimization, name: Optional[str] = None, profile_perf: Union[None, sparsify.models.projects_profiles.ProjectPerfProfile] = - 1, profile_loss: Union[None, sparsify.models.projects_profiles.ProjectLossProfile] = - 1, start_epoch: Union[None, float] = None, end_epoch: Union[None, float] = None, mod_start_epoch: Union[None, float] = None, mod_end_epoch: Union[None, float] = None)[source]

Update an optim DB model

Parameters
  • optim – the DB model

  • name – the name to set, if any

  • profile_perf – the performance profile to set, if any

  • profile_loss – the loss profile to set, if any

  • start_epoch – the start_epoch to set, if any

  • end_epoch – the end_epoch to set, if any

  • mod_start_epoch – a contained modifier’s updated start epoch, if set and less than current start_epoch will set start_epoch to this

  • mod_end_epoch – a contained modifier’s updated end epoch, if set and greater than current end_epoch will set end_epoch to this

sparsify.blueprints.utils.projects_optimizations.optim_validate_and_get_project_by_id(project_id: str)sparsify.models.projects.Project[source]

Get a project by project id and validate that it is setup correctly for optims. Raises not found errors for no project and validation errors for no project model, and no project analysis.

Parameters

project_id – id of the project to get

Returns

the retrieved project

sparsify.blueprints.utils.projects_optimizations.sparse_training_available(project: sparsify.models.projects.Project)[source]
sparsify.blueprints.utils.projects_optimizations.validate_pruning_nodes(project: sparsify.models.projects.Project, nodes: List[Dict[str, Any]])[source]

Validate a list of given nodes are prunable in the model. Raises a validation error if nodes are set that are not prunable.

Parameters
  • project – the project to validate with

  • nodes – the nodes to validate

sparsify.blueprints.utils.projects_optimizations_pruning module

Helper functions and classes for flask blueprints specific to project optimizations for pruning

class sparsify.blueprints.utils.projects_optimizations_pruning.PruningModelEvaluator(model_analysis: Dict, perf_analysis: Union[None, Dict], loss_analysis: Union[None, Dict])[source]

Bases: object

Evaluator for a model for pruning. Able to estimate the effect of pruning on a model and each prunable node in a model for performance, loss, etc

Parameters
  • model_analysis – analysis of the model

  • perf_analysis – performance analysis of the model, if any

  • loss_analysis – loss analysis of the model, if any

EVAL_SENSITIVITY_SPARSITY = 0.95
MAX_NODE_SPARSITY = 0.95
apply_node_overrides(node_overrides: List[Dict[str, Any]])[source]

Apply any node override sparsity levels to the current evaluated nodes. Must be called after eval_pruning if eval_pruning is invoked at all to have any effect.

Parameters

node_overrides – the override sparsity levels for nodes to set with

eval_baseline(baseline_sparsity: float)[source]

Evaluate the baseline (no performance data, only loss) recommended sparsities to assign for each node to best maximize recovery.

Parameters

baseline_sparsity – the baseline_sparsity to use and evaluate with

eval_pruning(settings: sparsify.blueprints.utils.projects_optimizations_pruning.PruningSettings)[source]

Evaluate the model to assign the evaluate sparsity levels for each node in the model given the input pruning settings.

Parameters

settings – the pruning settings to use and evaluate with

to_dict_values()Tuple[List[Dict[str, Any]], Dict[str, Any]][source]

Create the dictionary values containing the recommended sparsity levels for pruning and their estimated times. eval_baseline and (eval_pruning and/or apply_node_overrides) must be called before

Returns

a tuple containing (model info, list of node info)

class sparsify.blueprints.utils.projects_optimizations_pruning.PruningSettings(mask_type, sparsity, balance_perf_loss, filter_min_sparsity, filter_min_perf_gain, filter_min_recovery)

Bases: tuple

property balance_perf_loss

Alias for field number 2

property filter_min_perf_gain

Alias for field number 4

property filter_min_recovery

Alias for field number 5

property filter_min_sparsity

Alias for field number 3

property mask_type

Alias for field number 0

property sparsity

Alias for field number 1

Module contents

Utility functions and classes for flask blueprints