TrainingExtensions/torch/src/python/aimet_torch/cross_layer_equalization.py
def equalize_bn_folded_model(model: torch.nn.Module,
                             input_shapes: Union[Tuple, List[Tuple]],
                             folded_pairs: List[Tuple[torch.nn.Module, torch.nn.BatchNorm2d]]):
    """
    Perform Cross-Layer Scaling (CLS) and High Bias Folding (HBF) on a batchnorm-folded model.
    The model is equalized in place.

    :param model: Batchnorm-folded model to equalize
    :param input_shapes: Shape of the input (can be a tuple or a list of tuples if multiple inputs)
    :param folded_pairs: List of pairs of folded layers
    :return: None
    """

    device = get_device(model)
    model.cpu()

    bn_dict = {}
    for conv_bn in folded_pairs:
        bn_dict[conv_bn[0]] = conv_bn[1]

    # replace any ReLU6 layers with ReLU
    utils.replace_modules_of_type1_with_type2(model, torch.nn.ReLU6, torch.nn.ReLU)

    # perform cross-layer scaling on applicable layer sets
    cls_set_info_list = CrossLayerScaling.scale_model(model, input_shapes)

    # high-bias fold
    HighBiasFold.bias_fold(cls_set_info_list, bn_dict)

    model.to(device=device)

    Perform Cross-Layer Scaling (CLS) and High Bias Folding (HBF) on a batchnorm-folded model.
    The model is equalized in place.

    :param model: Batchnorm-folded model to equalize
    :param input_shapes: Shape of the input (can be a tuple or a list of tuples if multiple inputs)
    :param folded_pairs: List of pairs of folded layers
    :return: None
    
Perform Cross-Layer Scaling (CLS) and High Bias Folding (HBF) on a batchnorm-folded model.
The model is equalized in place.
Perform Cross - Layer Scaling ( CLS ) and High Bias Folding ( HBF ) on a batchnorm - folded model . The model is equalized in place .
def equalize_bn_folded_model(model: torch.nn.Module,
                             input_shapes: Union[Tuple, List[Tuple]],
                             folded_pairs: List[Tuple[torch.nn.Module, torch.nn.BatchNorm2d]]):
    device = get_device(model)
    model.cpu()
    bn_dict = {}
    for conv_bn in folded_pairs:
        bn_dict[conv_bn[0]] = conv_bn[1]
    utils.replace_modules_of_type1_with_type2(model, torch.nn.ReLU6, torch.nn.ReLU)
    cls_set_info_list = CrossLayerScaling.scale_model(model, input_shapes)
    HighBiasFold.bias_fold(cls_set_info_list, bn_dict)
    model.to(device=device)
def equalize_bn_folded_model ( model : torch . nn . Module , input_shapes : Union [ Tuple , List [ Tuple ] ] , folded_pairs : List [ Tuple [ torch . nn . Module , torch . nn . BatchNorm2d ] ] ) : device = get_device ( model ) model . cpu ( ) bn_dict = { } for conv_bn in folded_pairs : bn_dict [ conv_bn [ 0 ] ] = conv_bn [ 1 ] utils . replace_modules_of_type1_with_type2 ( model , torch . nn . ReLU6 , torch . nn . ReLU ) cls_set_info_list = CrossLayerScaling . scale_model ( model , input_shapes ) HighBiasFold . bias_fold ( cls_set_info_list , bn_dict ) model . to ( device = device )
Perform Cross-Layer Scaling (CLS) and High Bias Folding (HBF) on a batchnorm-folded model.
Perform Cross - Layer Scaling ( CLS ) and High Bias Folding ( HBF ) on a batchnorm - folded model .
"""
    Perform Cross-Layer Scaling (CLS) and High Bias Folding (HBF) on a batchnorm-folded model.
    The model is equalized in place.

    :param model: Batchnorm-folded model to equalize
    :param input_shapes: Shape of the input (can be a tuple or a list of tuples if multiple inputs)
    :param folded_pairs: List of pairs of folded layers
    :return: None
    """ # replace any ReLU6 layers with ReLU # perform cross-layer scaling on applicable layer sets # high-bias fold
None
model
torch.nn.Module
Batchnorm-folded model to equalize
Batchnorm - folded model to equalize
input_shapes
Union[Tuple, List[Tuple]]
Shape of the input (can be a tuple or a list of tuples if multiple inputs)
Shape of the input ( can be a tuple or a list of tuples if multiple inputs )
folded_pairs
List[Tuple[torch.nn.Module, torch.nn.BatchNorm2d]]
List of pairs of folded layers
List of pairs of folded layers