Skip to content

Commit a475a3f

Browse files
stashuk-olekfacebook-github-bot
authored andcommitted
Remove head_mask and attention weights from VideoGPT (facebookresearch#536)
Summary: Remove dead `head_mask`, `return_attn_weights`, and `attention_weights` from the VideoGPT stack. These features were never used by any consumer — `head_mask` was always `None` or all-ones, and `return_attn_weights` was always `False` except in tests that verified the feature itself. Reviewed By: OmarPavel Differential Revision: D92927089
1 parent 00ca165 commit a475a3f

7 files changed

Lines changed: 25 additions & 146 deletions

File tree

examples/mugen/tests/generation/test_text_video_gpt.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,17 @@ def test_lookup(model_fn, modality, expected_shape, expected_sum):
134134
def test_forward_no_pretrained(model_fn, video_seq_len, expected):
135135
test_params = {"video_seq_len": video_seq_len}
136136
kwargs = {**_model_params, **test_params}
137-
n_head = kwargs["n_head"]
138137

139138
x = torch.tensor([[1, 2, 3, 4]])
140139
y = torch.tensor([[5, 6, 7]])
141140
attn_mask = torch.tril(torch.ones(7, 7)).unsqueeze(0) # (b, seq_len, seq_len)
142-
head_mask = torch.ones(1, n_head, 7, 7) # (b, h, seq_len, seq_len)
143141

144142
model = model_fn(**kwargs)
145143
model.eval()
146144
num_tokens = model.num_in_tokens + model.num_out_tokens
147145
logits_mask = torch.ones(1, 7, num_tokens) # (b, seq_len, num_tokens)
148146

149-
out = model(x, y, attn_mask=attn_mask, head_mask=head_mask, logits_mask=logits_mask)
147+
out = model(x, y, attn_mask=attn_mask, logits_mask=logits_mask)
150148
actual = out.decoder_output.last_hidden_states
151149
assert_expected(actual.shape, (1, 7, 768))
152150
assert_expected(actual.sum().item(), expected, rtol=1e-5, atol=1e-4)
@@ -163,19 +161,17 @@ def test_forward_vqvae_pretrained(model_fn, video_seq_len, expected):
163161
"pretrained_video_vqvae_model_key": vqvae_model_key,
164162
}
165163
kwargs = {**_model_params, **test_params}
166-
n_head = kwargs["n_head"]
167164

168165
x = torch.tensor([[1, 2, 3, 4]])
169166
y = torch.tensor([[5, 6, 7]])
170167
attn_mask = torch.tril(torch.ones(7, 7)).unsqueeze(0) # (b, seq_len, seq_len)
171-
head_mask = torch.ones(1, n_head, 7, 7) # (b, h, seq_len, seq_len)
172168

173169
model = model_fn(**kwargs)
174170
model.eval()
175171
num_tokens = model.num_in_tokens + model.num_out_tokens
176172
logits_mask = torch.ones(1, 7, num_tokens) # (b, seq_len, num_tokens)
177173

178-
out = model(x, y, attn_mask=attn_mask, head_mask=head_mask, logits_mask=logits_mask)
174+
out = model(x, y, attn_mask=attn_mask, logits_mask=logits_mask)
179175
actual = out.decoder_output.last_hidden_states
180176
assert_expected(actual.shape, (1, 7, 768))
181177
assert_expected(actual.sum().item(), expected, rtol=1, atol=1e-4)
@@ -192,19 +188,17 @@ def test_forward_gpt_pretrained(model_fn, video_seq_len, expected):
192188
"pretrained_text_video_gpt_model_key": gpt_model_key,
193189
}
194190
kwargs = {**_model_params, **test_params}
195-
n_head = kwargs["n_head"]
196191

197192
x = torch.tensor([[1, 2, 3, 4]])
198193
y = torch.tensor([[5, 6, 7]])
199194
attn_mask = torch.tril(torch.ones(7, 7)).unsqueeze(0) # (b, seq_len, seq_len)
200-
head_mask = torch.ones(1, n_head, 7, 7) # (b, h, seq_len, seq_len)
201195

202196
model = model_fn(**kwargs)
203197
model.eval()
204198
num_tokens = model.num_in_tokens + model.num_out_tokens
205199
logits_mask = torch.ones(1, 7, num_tokens) # (b, seq_len, num_tokens)
206200

207-
out = model(x, y, attn_mask=attn_mask, head_mask=head_mask, logits_mask=logits_mask)
201+
out = model(x, y, attn_mask=attn_mask, logits_mask=logits_mask)
208202
actual = out.decoder_output.last_hidden_states
209203
assert_expected(actual.shape, (1, 7, 768))
210204
assert_expected(actual.sum().item(), expected, rtol=1, atol=1e-4)

tests/models/test_gpt.py

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,6 @@ def _attn_mask(q_seq_len, k_seq_len=None):
108108
return _attn_mask
109109

110110

111-
@pytest.fixture
112-
def head_mask(n_head):
113-
def _head_mask(q_seq_len, k_seq_len=None):
114-
if k_seq_len is None:
115-
k_seq_len = q_seq_len
116-
masked = torch.zeros(1, q_seq_len, k_seq_len)
117-
unmasked = torch.ones(n_head - 1, q_seq_len, k_seq_len)
118-
return torch.cat((masked, unmasked), dim=0) # (h, q_seq_len, k_seq_len)
119-
120-
return _head_mask
121-
122-
123111
@pytest.fixture
124112
def logits_mask(in_seq_len, out_seq_len, num_in_tokens, num_out_tokens):
125113
total_seq_len = in_seq_len + out_seq_len
@@ -379,19 +367,16 @@ def test_forward(
379367
in_tokens,
380368
out_tokens,
381369
attn_mask,
382-
head_mask,
383370
):
384371
gpt = gpt()
385372

386373
b, in_seq_len = in_tokens.shape
387374
b, out_seq_len = out_tokens.shape
388375
attn_mask = attn_mask(in_seq_len + out_seq_len)
389-
head_mask = head_mask(in_seq_len + out_seq_len)
390376
actual = gpt(
391377
in_tokens=in_tokens,
392378
out_tokens=out_tokens,
393379
attn_mask=attn_mask,
394-
head_mask=head_mask,
395380
use_cache=True,
396381
causal=True,
397382
right_shift=True,
@@ -401,10 +386,9 @@ def test_forward(
401386
"decoder_output": {
402387
"last_hidden_states": (
403388
torch.Size([1, 7, 4]), # (b, seq_len, d_model)
404-
64.5348,
389+
64.3909,
405390
),
406391
"hidden_states": None,
407-
"attention_weights": None,
408392
"past_key_values": (
409393
(
410394
{
@@ -424,20 +408,17 @@ def test_forward_logits_mask(
424408
in_tokens,
425409
out_tokens,
426410
attn_mask,
427-
head_mask,
428411
logits_mask,
429412
):
430413
gpt = gpt()
431414

432415
b, in_seq_len = in_tokens.shape
433416
b, out_seq_len = out_tokens.shape
434417
attn_mask = attn_mask(in_seq_len + out_seq_len)
435-
head_mask = head_mask(in_seq_len + out_seq_len)
436418
out = gpt(
437419
in_tokens=in_tokens,
438420
out_tokens=out_tokens,
439421
attn_mask=attn_mask,
440-
head_mask=head_mask,
441422
use_cache=True,
442423
causal=True,
443424
right_shift=True,
@@ -476,7 +457,6 @@ def test_forward_in_modality(self, mm_decoder, in_modality):
476457
0.2222,
477458
), # (b, in_seq_len, d_model)
478459
"hidden_states": None,
479-
"attention_weights": None,
480460
"past_key_values": None,
481461
}
482462
assert_expected_namedtuple(actual, expected, rtol=1e-5, atol=1e-4)
@@ -491,7 +471,6 @@ def test_forward_out_modality(self, mm_decoder, out_modality):
491471
5.2093,
492472
), # (b, out_seq_len, d_model)
493473
"hidden_states": None,
494-
"attention_weights": None,
495474
"past_key_values": None,
496475
}
497476
assert_expected_namedtuple(actual, expected, rtol=1e-5, atol=1e-4)
@@ -509,7 +488,6 @@ def test_forward_two_modality(self, mm_decoder, in_modality, out_modality):
509488
7.9519,
510489
), # (b, in_seq_len + out_seq_len, d_model)
511490
"hidden_states": None,
512-
"attention_weights": None,
513491
"past_key_values": None,
514492
}
515493
assert_expected_namedtuple(actual, expected, rtol=1e-5, atol=1e-4)
@@ -537,7 +515,6 @@ def test_forward_eval_right_shift_on(
537515
7.9519,
538516
), # (b, in_seq_len + out_seq_len, d_model)
539517
"hidden_states": None,
540-
"attention_weights": None,
541518
"past_key_values": None,
542519
}
543520
assert_expected_namedtuple(actual, expected, rtol=1e-5, atol=1e-4)
@@ -564,7 +541,6 @@ def test_forward_eval_right_shift_off(
564541
10.1681,
565542
), # (b, in_seq_len + out_seq_len, d_model)
566543
"hidden_states": None,
567-
"attention_weights": None,
568544
"past_key_values": None,
569545
}
570546
assert_expected_namedtuple(actual, expected, rtol=1e-5, atol=1e-4)
@@ -583,37 +559,31 @@ def test_optional_pos_ids(self, mm_decoder, in_modality):
583559

584560

585561
class TestTransformerDecoder:
586-
def test_forward_mask_extended(
587-
self, decoder, decoder_input, attn_mask, head_mask, num_layers
588-
):
562+
def test_forward_mask_extended(self, decoder, decoder_input, attn_mask, num_layers):
589563
b, seq_len, _ = decoder_input.shape
590564
attn_mask = attn_mask(seq_len).unsqueeze(0) # add batch dim
591-
head_mask = head_mask(seq_len).unsqueeze(0)
592-
actual = decoder(decoder_input, attn_mask, head_mask)
565+
actual = decoder(decoder_input, attn_mask)
593566
assert isinstance(actual, TransformerDecoderOutput)
594567
assert_expected(actual.last_hidden_states.shape, torch.Size([1, 3, 4]))
595568

596-
def test_forward(self, decoder, decoder_input, attn_mask, head_mask, num_layers):
569+
def test_forward(self, decoder, decoder_input, attn_mask, num_layers):
597570
b, seq_len, _ = decoder_input.shape
598571
attn_mask = attn_mask(seq_len)
599-
head_mask = head_mask(seq_len)
600-
actual = decoder(decoder_input, attn_mask, head_mask)
572+
actual = decoder(decoder_input, attn_mask)
601573
assert isinstance(actual, TransformerDecoderOutput)
602574
assert_expected(actual.last_hidden_states.shape, torch.Size([1, 3, 4]))
603575

604576
def test_forward_additional_output(self, decoder, decoder_input, num_layers):
605577
actual = decoder(
606578
decoder_input,
607579
use_cache=True,
608-
return_attn_weights=True,
609580
return_hidden_states=True,
610581
)
611582
assert isinstance(actual, TransformerDecoderOutput)
612583
assert_expected(actual.last_hidden_states.shape, torch.Size([1, 3, 4]))
613584
assert_expected(
614585
len(actual.hidden_states), num_layers + 1
615586
) # +1 to include the input hidden_states
616-
assert_expected(len(actual.attention_weights), num_layers)
617587
assert_expected(len(actual.past_key_values), num_layers)
618588

619589

@@ -623,33 +593,26 @@ def test_forward(self, decoder_layer, decoder_input):
623593
assert isinstance(actual, TransformerLayerOutput)
624594
expected = {
625595
"hidden_states": (torch.Size([1, 3, 4]), 5.1956), # (b, seq_len, d_model)
626-
"attention_weights": None,
627596
"past_key_values": None,
628597
}
629598
assert_expected_namedtuple(actual, expected, rtol=1e-5, atol=1e-4)
630599

631-
def test_forward_masked(self, decoder_layer, decoder_input, attn_mask, head_mask):
600+
def test_forward_masked(self, decoder_layer, decoder_input, attn_mask):
632601
b, seq_len, _ = decoder_input.shape
633602
attn_mask = attn_mask(seq_len)
634-
head_mask = head_mask(seq_len)
635-
actual = decoder_layer(decoder_input, attn_mask, head_mask)
603+
actual = decoder_layer(decoder_input, attn_mask)
636604
assert isinstance(actual, TransformerLayerOutput)
637605
expected = {
638-
"hidden_states": (torch.Size([1, 3, 4]), 7.0397), # (b, seq_len, seq_len)
639-
"attention_weights": None,
606+
"hidden_states": (torch.Size([1, 3, 4]), 5.6602), # (b, seq_len, seq_len)
640607
"past_key_values": None,
641608
}
642609
assert_expected_namedtuple(actual, expected, rtol=1e-5, atol=1e-4)
643610

644611
def test_forward_additional_output(self, decoder_layer, decoder_input):
645-
actual = decoder_layer(decoder_input, use_cache=True, return_attn_weights=True)
612+
actual = decoder_layer(decoder_input, use_cache=True)
646613
assert isinstance(actual, TransformerLayerOutput)
647614
expected = {
648615
"hidden_states": (torch.Size([1, 3, 4]), 5.1956), # (b, seq_len, seq_len)
649-
"attention_weights": (
650-
torch.Size([1, 2, 3, 3]),
651-
6.0,
652-
), # (b, h, seq_len, seq_len)
653616
"past_key_values": {
654617
"k": ([1, 2, 3, 2], 4.8075),
655618
"v": ([1, 2, 3, 2], -5.6613),

tests/models/test_video_gpt.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,9 @@ def test_forward(self, model_fn, model_params):
9595
model = model_fn(**model_params)
9696
model.eval()
9797

98-
n_head = model_params["n_head"]
99-
10098
x = torch.tensor([[1, 2, 3, 4]]) # (b, in_seq_len)
10199
y = torch.tensor([[5, 6, 7]]) # (b, out_seq_len)
102100
attn_mask = torch.tril(torch.ones(7, 7)).unsqueeze(0) # (b, seq_len, seq_len)
103-
head_mask = torch.ones(1, n_head, 7, 7) # (b, h, seq_len, seq_len)
104101

105102
num_tokens = model.num_in_tokens + model.num_out_tokens
106103
logits_mask = torch.ones(1, 7, num_tokens) # (b, seq_len, num_tokens)
@@ -109,11 +106,9 @@ def test_forward(self, model_fn, model_params):
109106
x,
110107
y,
111108
attn_mask=attn_mask,
112-
head_mask=head_mask,
113109
logits_mask=logits_mask,
114110
use_cache=True,
115111
causal=True,
116-
return_attn_weights=True,
117112
return_hidden_states=True,
118113
)
119114
actual = out.decoder_output.last_hidden_states

tests/models/test_video_vqvae.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def axial_attn(self):
7272

7373
def test_axial_attention(self, axial_attn, q, kv):
7474
k = v = kv
75-
actual, _ = axial_attn(q, k, v)
75+
actual = axial_attn(q, k, v)
7676
expected = torch.tensor(
7777
[
7878
[

0 commit comments

Comments
 (0)