MCPcopy Create free account
hub / github.com/huggingface/transformers / prune_layer

Function prune_layer

src/transformers/modeling_utils.py:2146–2156  ·  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

2144
2145
2146def prune_layer(layer, index, dim=None):
2147 """ Prune a Conv1D or nn.Linear layer (a model parameters) to keep only entries in index.
2148 Return the pruned layer as a new layer with requires_grad=True.
2149 Used to remove heads.
2150 """
2151 if isinstance(layer, nn.Linear):
2152 return prune_linear_layer(layer, index, dim=0 if dim is None else dim)
2153 elif isinstance(layer, Conv1D):
2154 return prune_conv1d_layer(layer, index, dim=1 if dim is None else dim)
2155 else:
2156 raise ValueError("Can't prune layer of class {}".format(layer.__class__))
2157
2158
2159def 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