Skip to content

Compressor(Recommendation Compression)

Description

netspresso.compressor.v2.compressor.CompressorV2.recommendation_compression(compression_method, recommendation_method, recommendation_ratio, input_model_path, output_dir, input_shapes, framework=Framework.PYTORCH, options=RecommendationOptions(), dataset_path=None)

Compress a recommendation-based model using the given compression and recommendation methods.

Parameters:

Name Type Description Default
compression_method CompressionMethod

The selected compression method.

required
recommendation_method RecommendationMethod

The selected recommendation method.

required
recommendation_ratio float

The compression ratio recommended by the recommendation method.

required
input_model_path str

The file path where the model is located.

required
output_dir str

The local path to save the compressed model.

required
input_shapes List[Dict[str, int]]

Input shapes of the model.

required
framework Framework

The framework of the model.

PYTORCH
options(Options, optional

The options for pruning method.

required
dataset_path str

The path of the dataset used for nuclear norm compression method. Default is None.

None

Raises:

Type Description
e

If an error occurs while performing recommendation compression.

Returns:

Name Type Description
CompressorMetadata CompressorMetadata

Compress metadata.

Details of Parameters

Compression Method

Available Compression Method
Name Description
PR_L2 L2 Norm Pruning
PR_GM GM Pruning
PR_NN Nuclear Norm Pruning
PR_SNP Structured Neuron-level Pruning
FD_TK Tucker Decomposition
FD_SVD Singular Value Decomposition
Example
from netspresso.enums import CompressionMethod

COMPRESSION_METHOD = CompressionMethod.PR_L2

Warning

  • Nuclear Norm is only supported in the Tensorflow-Keras framework.
  • Structured Neuron-level is only supported in the PyTorch and ONNX frameworks.

Note

Recommendation Method

Available Recommendation Method
Name Description
SLAMP Structured Layer-adaptive Sparsity for the Magnitude-based Pruning
VBMF Variational Bayesian Matrix Factorization
Example
from netspresso.enums import RecommendationMethod

RECOMMENDATION_METHOD = RecommendationMethod.SLAMP

Note

  • If you selected PR_L2, PR_GM, PR_NN, PR_SNP for compression_method
    • The recommended_method available is SLAMP.
  • If you selected FD_TK, FD_SVD for compression_method
    • The recommended_method available is VBMF.

Recommendation Ratio

  • SLAMP (Pruning ratio)

    • Remove corresponding amounts of the filters. (e.g. 0.2 removes 20% of the filters in each layer)
    • Available ranges: 0 < ratio < 1

    • Click the link for more information. (SLAMP)

  • VBMF (Calibration ratio)

    • This function control compression level of model if the result of recommendation doesn't meet the compression level user wants. Remained rank add or subtract (removed rank x calibration ratio) according to calibration ratio range.
    • Available ranges: -1 ≤ ratio ≤ 1

    • Click the link for more information. (VBMF)

Options

Example
from netspresso.enums import Policy, LayerNorm, GroupPolicy
from netspresso.clients.compressor.v2.schemas import Options

OPTIONS = Options(
    policy=Policy.AVERAGE,
    layer_norm=LayerNorm.TSS_NORM,
    group_policy=GroupPolicy.COUNT,
    reshape_channel_axis=-1
)

Note

Warning

  • Nuclear Norm is only supported in the Tensorflow-Keras framework.
  • Structured Neuron-level is only supported in the PyTorch and ONNX frameworks.

Example

from netspresso import NetsPresso
from netspresso.enums import CompressionMethod, RecommendationMethod


netspresso = NetsPresso(email="YOUR_EMAIL", password="YOUR_PASSWORD")

compressor = netspresso.compressor_v2()
compressed_model = compressor.recommendation_compression(
    compression_method=CompressionMethod.PR_L2,
    recommendation_method=RecommendationMethod.SLAMP,
    recommendation_ratio=0.5,
    input_model_path="./examples/sample_models/graphmodule.pt",
    output_dir="./outputs/compressed/graphmodule_recommend",
    input_shapes=[{"batch": 1, "channel": 3, "dimension": [224, 224]}],
)