Examples/torch/compression/weight_svd.py
def aimet_weight_svd(model: torch.nn.Module,
                     evaluator: aimet_common.defs.EvalFunction) -> Tuple[torch.nn.Module,
                                                                         aimet_common.defs.CompressionStats]:
    """
    Compresses the model using AIMET's Weight SVD auto mode compression scheme.

    :param model: The model to compress.
    :param evaluator: Evaluator used during compression.
    :return: A tuple of compressed model and its statistics
    """

    # Please refer to the API documentation for more details.

    # Desired target compression ratio using Weight SVD
    # This value denotes the desired compression % of the original model.
    # To compress the model to 20% of original model, use 0.2. This would
    # compress the model by 80%.
    # We are compressing the model by 50% here.
    target_comp_ratio = Decimal(0.5)

    # Number of compression ratio used by the API at each layer
    # API will evaluate 0.1, 0.2, ..., 0.9, 1.0 ratio (total 10 candidates)
    # at each layer
    num_comp_ratio_candidates = 10

    # Creating Greedy selection parameters:
    greedy_params = aimet_torch.defs.GreedySelectionParameters(target_comp_ratio=target_comp_ratio,
                                                               num_comp_ratio_candidates=num_comp_ratio_candidates)

    # Selecting 'greedy' for rank select scheme:
    rank_select_scheme = aimet_common.defs.RankSelectScheme.greedy

    # Ignoring first convolutional layer of the model for compression
    modules_to_ignore = [model.conv1]

    # Creating Auto mode Parameters:
    auto_params = aimet_torch.defs.WeightSvdParameters.AutoModeParams(rank_select_scheme=rank_select_scheme,
                                                                      select_params=greedy_params,
                                                                      modules_to_ignore=modules_to_ignore)

    # Creating Weight SVD parameters with Auto Mode:
    params = aimet_torch.defs.WeightSvdParameters(aimet_torch.defs.WeightSvdParameters.Mode.auto,
                                                  auto_params)

    # Scheme is Weight SVD:
    scheme = aimet_common.defs.CompressionScheme.weight_svd

    # Cost metric is MAC, it can be MAC or Memory
    cost_metric = aimet_common.defs.CostMetric.mac

    # Input image shape
    image_shape = (1, image_net_config.dataset['image_channels'],
                   image_net_config.dataset['image_width'], image_net_config.dataset['image_height'])

    # Calling model compression using Weight SVD:
    # Here evaluator is passed which is used by the API to evaluate the
    # accuracy for various compression ratio of each layer. To speed up
    # the process, only 10 batches of data is being used inside evaluator
    # (by passing eval_iterations=10) instead of running evaluation on
    # complete dataset.
    results = ModelCompressor.compress_model(model=model,
                                             eval_callback=evaluator,
                                             eval_iterations=10,
                                             input_shape=image_shape,
                                             compress_scheme=scheme,
                                             cost_metric=cost_metric,
                                             parameters=params)

    return results

    Compresses the model using AIMET's Weight SVD auto mode compression scheme.

    :param model: The model to compress.
    :param evaluator: Evaluator used during compression.
    :return: A tuple of compressed model and its statistics
    
Compresses the model using AIMET's Weight SVD auto mode compression scheme.
Compresses the model using AIMET ' s Weight SVD auto mode compression scheme .
def aimet_weight_svd(model: torch.nn.Module,
                     evaluator: aimet_common.defs.EvalFunction) -> Tuple[torch.nn.Module,
                                                                         aimet_common.defs.CompressionStats]:
    target_comp_ratio = Decimal(0.5)
    num_comp_ratio_candidates = 10
    greedy_params = aimet_torch.defs.GreedySelectionParameters(target_comp_ratio=target_comp_ratio,
                                                               num_comp_ratio_candidates=num_comp_ratio_candidates)
    rank_select_scheme = aimet_common.defs.RankSelectScheme.greedy
    modules_to_ignore = [model.conv1]
    auto_params = aimet_torch.defs.WeightSvdParameters.AutoModeParams(rank_select_scheme=rank_select_scheme,
                                                                      select_params=greedy_params,
                                                                      modules_to_ignore=modules_to_ignore)
    params = aimet_torch.defs.WeightSvdParameters(aimet_torch.defs.WeightSvdParameters.Mode.auto,
                                                  auto_params)
    scheme = aimet_common.defs.CompressionScheme.weight_svd
    cost_metric = aimet_common.defs.CostMetric.mac
    image_shape = (1, image_net_config.dataset['image_channels'],
                   image_net_config.dataset['image_width'], image_net_config.dataset['image_height'])
    results = ModelCompressor.compress_model(model=model,
                                             eval_callback=evaluator,
                                             eval_iterations=10,
                                             input_shape=image_shape,
                                             compress_scheme=scheme,
                                             cost_metric=cost_metric,
                                             parameters=params)
    return results
def aimet_weight_svd ( model : torch . nn . Module , evaluator : aimet_common . defs . EvalFunction ) -> Tuple [ torch . nn . Module , aimet_common . defs . CompressionStats ] : target_comp_ratio = Decimal ( 0.5 ) num_comp_ratio_candidates = 10 greedy_params = aimet_torch . defs . GreedySelectionParameters ( target_comp_ratio = target_comp_ratio , num_comp_ratio_candidates = num_comp_ratio_candidates ) rank_select_scheme = aimet_common . defs . RankSelectScheme . greedy modules_to_ignore = [ model . conv1 ] auto_params = aimet_torch . defs . WeightSvdParameters . AutoModeParams ( rank_select_scheme = rank_select_scheme , select_params = greedy_params , modules_to_ignore = modules_to_ignore ) params = aimet_torch . defs . WeightSvdParameters ( aimet_torch . defs . WeightSvdParameters . Mode . auto , auto_params ) scheme = aimet_common . defs . CompressionScheme . weight_svd cost_metric = aimet_common . defs . CostMetric . mac image_shape = ( 1 , image_net_config . dataset [ 'image_channels' ] , image_net_config . dataset [ 'image_width' ] , image_net_config . dataset [ 'image_height' ] ) results = ModelCompressor . compress_model ( model = model , eval_callback = evaluator , eval_iterations = 10 , input_shape = image_shape , compress_scheme = scheme , cost_metric = cost_metric , parameters = params ) return results
Compresses the model using AIMET's Weight SVD auto mode compression scheme.
Compresses the model using AIMET ' s Weight SVD auto mode compression scheme .
"""
    Compresses the model using AIMET's Weight SVD auto mode compression scheme.

    :param model: The model to compress.
    :param evaluator: Evaluator used during compression.
    :return: A tuple of compressed model and its statistics
    """ # Please refer to the API documentation for more details. # Desired target compression ratio using Weight SVD # This value denotes the desired compression % of the original model. # To compress the model to 20% of original model, use 0.2. This would # compress the model by 80%. # We are compressing the model by 50% here. # Number of compression ratio used by the API at each layer # API will evaluate 0.1, 0.2, ..., 0.9, 1.0 ratio (total 10 candidates) # at each layer # Creating Greedy selection parameters: # Selecting 'greedy' for rank select scheme: # Ignoring first convolutional layer of the model for compression # Creating Auto mode Parameters: # Creating Weight SVD parameters with Auto Mode: # Scheme is Weight SVD: # Cost metric is MAC, it can be MAC or Memory # Input image shape # Calling model compression using Weight SVD: # Here evaluator is passed which is used by the API to evaluate the # accuracy for various compression ratio of each layer. To speed up # the process, only 10 batches of data is being used inside evaluator # (by passing eval_iterations=10) instead of running evaluation on # complete dataset.
A tuple of compressed model and its statistics
A tuple of compressed model and its statistics
model
torch.nn.Module
The model to compress.
The model to compress .
evaluator
aimet_common.defs.EvalFunction
Evaluator used during compression.
Evaluator used during compression .