Example #1
0
def prune_linear(O0O0OOO00OO000OOO: nn.modules.linear.Linear,
                 O0O0OOOO00O0O0OOO: list,
                 inplace: list = True,
                 dry_run: list = False):  #line:85
    ""  #line:91
    O0O0000OOO000OO00 = len(
        O0O0OOOO00O0O0OOO) * O0O0OOO00OO000OOO.weight.shape[1] + (
            len(O0O0OOOO00O0O0OOO) if O0O0OOO00OO000OOO.bias is not None else 0
        )  #line:92
    if dry_run:  #line:93
        return O0O0OOO00OO000OOO, O0O0000OOO000OO00  #line:94
    if not inplace:  #line:96
        O0O0OOO00OO000OOO = deepcopy(O0O0OOO00OO000OOO)  #line:97
    OOOO00O0OOOOO00OO = [
        O0O0O000OO0000O0O
        for O0O0O000OO0000O0O in range(O0O0OOO00OO000OOO.out_features)
        if O0O0O000OO0000O0O not in O0O0OOOO00O0O0OOO
    ]  #line:98
    O0O0OOO00OO000OOO.out_features = O0O0OOO00OO000OOO.out_features - len(
        O0O0OOOO00O0O0OOO)  #line:99
    O0O0OOO00OO000OOO.weight = torch.nn.Parameter(
        O0O0OOO00OO000OOO.weight.data.clone()[OOOO00O0OOOOO00OO])  #line:100
    if O0O0OOO00OO000OOO.bias is not None:  #line:101
        O0O0OOO00OO000OOO.bias = torch.nn.Parameter(
            O0O0OOO00OO000OOO.bias.data.clone()[OOOO00O0OOOOO00OO])  #line:102
    return O0O0OOO00OO000OOO, O0O0000OOO000OO00  #line:103
Example #2
0
def prune_related_linear(layer: nn.modules.linear.Linear,
                         idxs: list,
                         inplace: list = True,
                         dry_run: list = False):  #line:105
    ""  #line:111
    O00O00O00O0000O0O = len(idxs) * layer.weight.shape[0]  #line:112
    if dry_run:  #line:113
        return layer, O00O00O00O0000O0O  #line:114
    if not inplace:  #line:116
        layer = deepcopy(layer)  #line:117
    OO0OOOO0O0O0000OO = [
        O00OO0O000O0O0O0O for O00OO0O000O0O0O0O in range(layer.in_features)
        if O00OO0O000O0O0O0O not in idxs
    ]  #line:118
    layer.in_features = layer.in_features - len(idxs)  #line:119
    layer.weight = torch.nn.Parameter(
        layer.weight.data.clone()[:, OO0OOOO0O0O0000OO])  #line:120
    return layer, O00O00O00O0000O0O  #line:121
Example #3
0
def prune_related_linear(layer: nn.modules.linear.Linear,
                         idxs: list,
                         inplace: list = True,
                         dry_run: list = False):
    """Prune weights for the related (affected) fully-connected layer, e.g. [256 x 128] => [256 x 96]
    
    Args:
        layer: a fully-connected layer.
        idxs: pruning index.
    """
    num_pruned = len(idxs) * layer.weight.shape[0]
    if dry_run:
        return layer, num_pruned

    if not inplace:
        layer = deepcopy(layer)
    keep_idxs = [i for i in range(layer.in_features) if i not in idxs]
    layer.in_features = layer.in_features - len(idxs)
    layer.weight = torch.nn.Parameter(layer.weight.data.clone()[:, keep_idxs])
    return layer, num_pruned
Example #4
0
def prune_related_linear(OOO00000O0OOO0000: nn.modules.linear.Linear,
                         O00OOOOOOOOOO000O: list,
                         inplace: list = True,
                         dry_run: list = False):  #line:105
    ""  #line:111
    OO0OOO00O000O00O0 = len(
        O00OOOOOOOOOO000O) * OOO00000O0OOO0000.weight.shape[0]  #line:112
    if dry_run:  #line:113
        return OOO00000O0OOO0000, OO0OOO00O000O00O0  #line:114
    if not inplace:  #line:116
        OOO00000O0OOO0000 = deepcopy(OOO00000O0OOO0000)  #line:117
    O0O00000OOO0O0OO0 = [
        OOO0O000O00OOO00O
        for OOO0O000O00OOO00O in range(OOO00000O0OOO0000.in_features)
        if OOO0O000O00OOO00O not in O00OOOOOOOOOO000O
    ]  #line:118
    OOO00000O0OOO0000.in_features = OOO00000O0OOO0000.in_features - len(
        O00OOOOOOOOOO000O)  #line:119
    OOO00000O0OOO0000.weight = torch.nn.Parameter(
        OOO00000O0OOO0000.weight.data.clone()[:, O0O00000OOO0O0OO0])  #line:120
    return OOO00000O0OOO0000, OO0OOO00O000O00O0  #line:121
Example #5
0
def prune_linear(layer: nn.modules.linear.Linear,
                 idxs: list,
                 inplace: list = True,
                 dry_run: list = False):  #line:85
    ""  #line:91
    OOO00OO00O000OOO0 = len(idxs) * layer.weight.shape[1] + (
        len(idxs) if layer.bias is not None else 0)  #line:92
    if dry_run:  #line:93
        return layer, OOO00OO00O000OOO0  #line:94
    if not inplace:  #line:96
        layer = deepcopy(layer)  #line:97
    O0OOOOOOO000000OO = [
        OOO0OO000OO0000O0 for OOO0OO000OO0000O0 in range(layer.out_features)
        if OOO0OO000OO0000O0 not in idxs
    ]  #line:98
    layer.out_features = layer.out_features - len(idxs)  #line:99
    layer.weight = torch.nn.Parameter(
        layer.weight.data.clone()[O0OOOOOOO000000OO])  #line:100
    if layer.bias is not None:  #line:101
        layer.bias = torch.nn.Parameter(
            layer.bias.data.clone()[O0OOOOOOO000000OO])  #line:102
    return layer, OOO00OO00O000OOO0  #line:103
Example #6
0
def prune_linear(layer: nn.modules.linear.Linear,
                 idxs: list,
                 inplace: list = True,
                 dry_run: list = False):
    """Prune neurons for the fully-connected layer, e.g. [256 x 128] => [192 x 128]
    
    Args:
        layer: a fully-connected layer.
        idxs: pruning index.
    """
    num_pruned = len(idxs) * layer.weight.shape[1] + (len(idxs) if layer.bias
                                                      is not None else 0)
    if dry_run:
        return layer, num_pruned

    if not inplace:
        layer = deepcopy(layer)
    keep_idxs = [i for i in range(layer.out_features) if i not in idxs]
    layer.out_features = layer.out_features - len(idxs)
    layer.weight = torch.nn.Parameter(layer.weight.data.clone()[keep_idxs])
    if layer.bias is not None:
        layer.bias = torch.nn.Parameter(layer.bias.data.clone()[keep_idxs])
    return layer, num_pruned