harp-dev commited on
Commit
ec83122
·
verified ·
1 Parent(s): 3ac34bb

Collect MERT's hidden states with hooks, which transformers 5 no longer returns

Browse files
Files changed (1) hide show
  1. networks.py +43 -2
networks.py CHANGED
@@ -6,6 +6,48 @@ from transformers import AutoModel, AutoConfig
6
  from transformers import Wav2Vec2Model, Wav2Vec2Processor, Data2VecAudioModel
7
  import torchmetrics
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  class cnnblock(nn.Module):
10
  def __init__(self, embed_dim=512):
11
  super(cnnblock, self).__init__()
@@ -87,8 +129,7 @@ class MERT(nn.Module):
87
 
88
  def forward(self, input_values):
89
  with torch.no_grad():
90
- outputs = self.mert(input_values, output_hidden_states=True)
91
- hidden_states = torch.stack(outputs.hidden_states)
92
  hidden_states = hidden_states.detach().clone().requires_grad_(True)
93
  time_reduced = hidden_states.mean(dim=2)
94
  time_reduced = time_reduced.permute(1, 0, 2)
 
6
  from transformers import Wav2Vec2Model, Wav2Vec2Processor, Data2VecAudioModel
7
  import torchmetrics
8
 
9
+
10
+ def stack_mert_hidden_states(model, *args, **kwargs):
11
+ """
12
+ Run a MERT model and stack the hidden states of every encoder layer.
13
+
14
+ MERT ships its own modeling code, written when transformers plumbed
15
+ output_hidden_states and return_dict through the encoder it inherits.
16
+ Transformers 5 stopped honoring those arguments there, so the model
17
+ returns hidden_states=None however it is asked, whether through the
18
+ forward keyword or the config flag. Collecting the layer outputs with
19
+ forward hooks does not depend on that plumbing and so survives the
20
+ change.
21
+
22
+ Args:
23
+ model: A MERT (or other Wav2Vec2/Hubert-style) model.
24
+ *args: Positional arguments for the model's forward.
25
+ **kwargs: Keyword arguments for the model's forward.
26
+
27
+ Returns:
28
+ hidden_states (torch.Tensor): Shaped (1 + layers, batch, frames,
29
+ features): the input to the first layer followed by each layer's
30
+ output, which is the order transformers used to return.
31
+ """
32
+
33
+ states, handles = [], []
34
+ layers = model.encoder.layers
35
+
36
+ handles.append(layers[0].register_forward_pre_hook(
37
+ lambda module, inputs: states.append(inputs[0])))
38
+ for layer in layers:
39
+ handles.append(layer.register_forward_hook(
40
+ lambda module, inputs, output:
41
+ states.append(output[0] if isinstance(output, tuple) else output)))
42
+
43
+ try:
44
+ model(*args, **kwargs)
45
+ finally:
46
+ for handle in handles:
47
+ handle.remove()
48
+
49
+ return torch.stack(states)
50
+
51
  class cnnblock(nn.Module):
52
  def __init__(self, embed_dim=512):
53
  super(cnnblock, self).__init__()
 
129
 
130
  def forward(self, input_values):
131
  with torch.no_grad():
132
+ hidden_states = stack_mert_hidden_states(self.mert, input_values)
 
133
  hidden_states = hidden_states.detach().clone().requires_grad_(True)
134
  time_reduced = hidden_states.mean(dim=2)
135
  time_reduced = time_reduced.permute(1, 0, 2)