neural_compressor.experimental.nas.dynast.dynas_utils

Common methods for DyNAS.

Module Contents

Classes

Runner

The Runner base class.

OFARunner

The OFARunner class.

TransformerLTRunner

The Runner base class.

EvaluationInterface

Evaluation Interface class.

EvaluationInterfaceResNet50

Evaluation Interface class for ResNet50.

EvaluationInterfaceMobileNetV3

Evaluation Interface class for MobileNetV3.

EvaluationInterfaceTransformerLT

Evaluation Interface class.

TorchVisionReference

Baseline of the torchvision model.

Functions

get_macs(, device)

Get the MACs of the model.

measure_latency(, warmup_steps, measure_steps, device, ...)

Measure Torch model's latency.

get_torchvision_model(→ torch)

Get the torchvision model.

neural_compressor.experimental.nas.dynast.dynas_utils.get_macs(model: torch, input_size: Tuple[int, int, int, int] = (1, 3, 224, 224), device: str = 'cpu') int

Get the MACs of the model.

Parameters:
  • model (torch.nn.Module) – The torch model.

  • input_size (Tuple) – The input dimension.

  • device (str) – The device on which the model runs.

Returns:

The MACs of the model.

neural_compressor.experimental.nas.dynast.dynas_utils.measure_latency(model: torch, input_size: Tuple[int, int, int, int] = (1, 3, 224, 224), warmup_steps: int = None, measure_steps: int = None, device: str = 'cpu') Tuple[float, float]

Measure Torch model’s latency.

Parameters:
  • model (torch.nn.Module) – Torch model.

  • input_size (Tuple) – a tuple (batch size, channels, resolution, resolution).

  • warmup_steps (int) – how many data batches to use to warm up the device. If ‘None’ it will be adjusted automatically w.r.t batch size.

  • measure_steps (int) – how many data batches to use for latency measurement. If ‘None’ it will be adjusted automatically w.r.t batch size.

  • device (str) – which device is being used for latency measurement.

Returns:

mean latency; std latency.

class neural_compressor.experimental.nas.dynast.dynas_utils.Runner

The Runner base class.

class neural_compressor.experimental.nas.dynast.dynas_utils.OFARunner(supernet: str, acc_predictor: neural_compressor.experimental.nas.dynast.dynas_predictor.Predictor, macs_predictor: neural_compressor.experimental.nas.dynast.dynas_predictor.Predictor, latency_predictor: neural_compressor.experimental.nas.dynast.dynas_predictor.Predictor, datasetpath: str, batch_size: int, num_workers: int = 20, **kwargs)

Bases: Runner

The OFARunner class.

The OFARunner class manages the sub-network selection from the OFA super-network and the validation measurements of the sub-networks. ResNet50, MobileNetV3 w1.0, and MobileNetV3 w1.2 are currently supported. Imagenet is required for these super-networks imagenet-ilsvrc2012.

estimate_accuracy_top1(subnet_cfg: dict) float

Estimate the top1 accuracy of the subnet with the predictor.

Parameters:

subnet_cfg (dict) – The dictionary describing the subnet.

Returns:

Top1 accuracy of the subnet.

estimate_macs(subnet_cfg: dict) int

Estimate the MACs of the subnet with the predictor.

Parameters:

subnet_cfg (dict) – The dictionary describing the subnet.

Returns:

MACs of the subnet.

estimate_latency(subnet_cfg: dict) float

Estimate the latency of the subnet with the predictor.

Parameters:

subnet_cfg (dict) – The dictionary describing the subnet.

Returns:

Latency of the subnet.

validate_top1(subnet_cfg: dict) float

Validate the top1 accuracy of the subnet on the dataset.

Parameters:

subnet_cfg (dict) – The dictionary describing the subnet.

Returns:

Top1 accuracy of the subnet.

validate_macs(subnet_cfg: dict) float

Measure subnet’s FLOPs/MACs as per FVCore calculation.

Parameters:

subnet_cfg (dict) – The dictionary describing the subnet.

Returns:

MACs of the subnet.

measure_latency(subnet_cfg: dict, warmup_steps: int = None, measure_steps: int = None) Tuple[float, float]

Measure subnet’s latency.

Parameters:

subnet_cfg (dict) – The dictionary describing the subnet.

Returns:

mean latency; std latency.

get_subnet(subnet_cfg: dict) torch

Get subnet.

Parameters:

subnet_cfg (dict) – The dictionary describing the subnet.

Returns:

The subnet.

class neural_compressor.experimental.nas.dynast.dynas_utils.TransformerLTRunner(supernet: str, acc_predictor: neural_compressor.experimental.nas.dynast.dynas_predictor.Predictor, macs_predictor: neural_compressor.experimental.nas.dynast.dynas_predictor.Predictor, latency_predictor: neural_compressor.experimental.nas.dynast.dynas_predictor.Predictor, datasetpath: str, batch_size: int, checkpoint_path: str, **kwargs)

Bases: Runner

The Runner base class.

validate_macs(subnet_cfg: dict) float

Measure Torch model’s FLOPs/MACs as per FVCore calculation.

Parameters:

subnet_cfg – sub-network Torch model

Returns:

macs

measure_latency(subnet_cfg: dict) Tuple[float, float]

Measure model’s latency.

Parameters:

subnet_cfg – sub-network Torch model

Returns:

mean latency; std latency

class neural_compressor.experimental.nas.dynast.dynas_utils.EvaluationInterface(evaluator: Runner, manager: neural_compressor.experimental.nas.dynast.dynas_manager.ParameterManager, metrics: list = ['acc', 'macs'], predictor_mode: bool = False, csv_path: str = None)

Evaluation Interface class.

The interface class update is required to be updated for each unique SuperNetwork framework as it controls how evaluation calls are made from DyNAS-T.

Parameters:
  • evaluator (class) – The ‘runner’ that performs the validation or prediction.

  • manager (class) – The DyNAS-T manager that translates between PyMoo and the parameter dict.

  • csv_path (str, Optional) – The csv file that get written to during the subnetwork search.

eval_subnet(x: list) Tuple[dict, float, float]

Evaluate the subnet.

clear_csv() None

Clear the csv file.

class neural_compressor.experimental.nas.dynast.dynas_utils.EvaluationInterfaceResNet50(evaluator: Runner, manager: neural_compressor.experimental.nas.dynast.dynas_manager.ParameterManager, metrics: list = ['acc', 'macs'], predictor_mode: bool = False, csv_path: str = None)

Bases: EvaluationInterface

Evaluation Interface class for ResNet50.

Parameters:
  • evaluator (class) – The ‘runner’ that performs the validation or prediction.

  • manager (class) – The DyNAS-T manager that translates between PyMoo and the parameter dict.

  • csv_path (str, Optional) – The csv file that get written to during the subnetwork search.

eval_subnet(x: list) Tuple[dict, float, float]

Evaluate the subnet.

class neural_compressor.experimental.nas.dynast.dynas_utils.EvaluationInterfaceMobileNetV3(evaluator: Runner, manager: neural_compressor.experimental.nas.dynast.dynas_manager.ParameterManager, metrics=['acc', 'macs'], predictor_mode=False, csv_path=None)

Bases: EvaluationInterface

Evaluation Interface class for MobileNetV3.

Parameters:
  • evaluator (class) – The ‘runner’ that performs the validation or prediction.

  • manager (class) – The DyNAS-T manager that translates between PyMoo and the parameter dict.

  • csv_path (str, Optional) – The csv file that get written to during the subnetwork search.

eval_subnet(x: list) Tuple[dict, float, float]

Evaluate the subnet.

class neural_compressor.experimental.nas.dynast.dynas_utils.EvaluationInterfaceTransformerLT(evaluator: Runner, manager: neural_compressor.experimental.nas.dynast.dynas_manager.ParameterManager, metrics=['acc', 'macs'], predictor_mode=False, csv_path=None)

Bases: EvaluationInterface

Evaluation Interface class.

The interface class update is required to be updated for each unique SuperNetwork framework as it controls how evaluation calls are made from DyNAS-T.

Parameters:
  • evaluator (class) – The ‘runner’ that performs the validation or prediction.

  • manager (class) – The DyNAS-T manager that translates between PyMoo and the parameter dict.

  • csv_path (str, Optional) – The csv file that get written to during the subnetwork search.

eval_subnet(x: list) Tuple[dict, float, float]

Evaluate the subnet.

clear_csv() None

Clear the csv file.

neural_compressor.experimental.nas.dynast.dynas_utils.get_torchvision_model(model_name: str) torch

Get the torchvision model.

Parameters:

model_name (str) – The name of the torchvision model.

Returns:

The specified torch model.

class neural_compressor.experimental.nas.dynast.dynas_utils.TorchVisionReference(model_name: str, dataset_path: str, batch_size: int, input_size: int = 224, num_workers: int = 20)

Baseline of the torchvision model.

Parameters:
  • model_name (str) – The name of the torchvision model.

  • dataset_path (str) – The path to the dataset.

  • batch_size (int) – Batch size of the input.

  • input_size (int) – Input image’s width and height.

  • num_workers (int) – How many subprocesses to use for data loading.

validate_top1() Tuple[float, float, float]

Get the top1 accuracy of the model on the dataset.

Returns:

Top1 accuracy of the model.

validate_macs(device: str = 'cpu') int

Get the MACs of the model.

Returns:

MACs of the model.

measure_latency(device: str = 'cpu', warmup_steps: int = None, measure_steps: int = None) Tuple[float, float]

Measure the latency of the model.

Returns:

Latency of the model.