MCPcopy Create free account
hub / github.com/SooLab/CGFormer / prune_layer

Function prune_layer

bert/modeling_utils.py:1195–1205  ·  view source on GitHub ↗

Prune a Conv1D or nn.Linear layer (a model parameters) to keep only entries in index. Return the pruned layer as a new layer with requires_grad=True. Used to remove heads.

(layer, index, dim=None)

Source from the content-addressed store, hash-verified

1193
1194
1195def prune_layer(layer, index, dim=None):
1196 """ Prune a Conv1D or nn.Linear layer (a model parameters) to keep only entries in index.
1197 Return the pruned layer as a new layer with requires_grad=True.
1198 Used to remove heads.
1199 """
1200 if isinstance(layer, nn.Linear):
1201 return prune_linear_layer(layer, index, dim=0 if dim is None else dim)
1202 elif isinstance(layer, Conv1D):
1203 return prune_conv1d_layer(layer, index, dim=1 if dim is None else dim)
1204 else:
1205 raise ValueError("Can't prune layer of class {}".format(layer.__class__))
1206
1207
1208def apply_chunking_to_forward(

Callers

nothing calls this directly

Calls 2

prune_linear_layerFunction · 0.85
prune_conv1d_layerFunction · 0.85

Tested by

no test coverage detected