Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
7eee950a
Unverified
Commit
7eee950a
authored
May 06, 2021
by
Sylvain Gugger
Committed by
GitHub
May 06, 2021
Browse files
Re-styling in seq2seq attention (#11613)
parent
cf409e55
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
190 additions
and
240 deletions
+190
-240
src/transformers/models/bart/modeling_bart.py
src/transformers/models/bart/modeling_bart.py
+19
-24
src/transformers/models/blenderbot/modeling_blenderbot.py
src/transformers/models/blenderbot/modeling_blenderbot.py
+19
-24
src/transformers/models/blenderbot_small/modeling_blenderbot_small.py
...mers/models/blenderbot_small/modeling_blenderbot_small.py
+19
-24
src/transformers/models/m2m_100/modeling_m2m_100.py
src/transformers/models/m2m_100/modeling_m2m_100.py
+19
-24
src/transformers/models/marian/modeling_marian.py
src/transformers/models/marian/modeling_marian.py
+19
-24
src/transformers/models/mbart/modeling_mbart.py
src/transformers/models/mbart/modeling_mbart.py
+19
-24
src/transformers/models/pegasus/modeling_pegasus.py
src/transformers/models/pegasus/modeling_pegasus.py
+19
-24
src/transformers/models/speech_to_text/modeling_speech_to_text.py
...sformers/models/speech_to_text/modeling_speech_to_text.py
+19
-24
src/transformers/models/wav2vec2/modeling_wav2vec2.py
src/transformers/models/wav2vec2/modeling_wav2vec2.py
+19
-24
templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/modeling_{{cookiecutter.lowercase_modelname}}.py
...elname}}/modeling_{{cookiecutter.lowercase_modelname}}.py
+19
-24
No files found.
src/transformers/models/bart/modeling_bart.py
View file @
7eee950a
...
@@ -210,28 +210,26 @@ class BartAttention(nn.Module):
...
@@ -210,28 +210,26 @@ class BartAttention(nn.Module):
src_len
=
key_states
.
size
(
1
)
src_len
=
key_states
.
size
(
1
)
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
assert
attn_weights
.
size
()
==
(
if
attn_weights
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
src_len
,
)
),
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
assert
attention_mask
.
size
()
==
(
if
attention_mask
.
size
()
!=
(
bsz
,
1
,
tgt_len
,
src_len
):
bsz
,
raise
ValueError
(
1
,
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
tgt_len
,
)
src_len
,
),
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
if
layer_head_mask
is
not
None
:
if
layer_head_mask
is
not
None
:
assert
layer_head_mask
.
size
()
==
(
if
layer_head_mask
.
size
()
!=
(
self
.
num_heads
,):
self
.
num_heads
,
raise
ValueError
(
),
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
...
@@ -249,18 +247,15 @@ class BartAttention(nn.Module):
...
@@ -249,18 +247,15 @@ class BartAttention(nn.Module):
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
assert
attn_output
.
size
()
==
(
if
attn_output
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
self
.
head_dim
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
self
.
head_dim
,
),
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
attn_output
=
(
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
.
transpose
(
1
,
2
)
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
)
)
attn_output
=
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
attn_output
=
attn_output
.
transpose
(
1
,
2
)
attn_output
=
attn_output
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
attn_output
=
self
.
out_proj
(
attn_output
)
attn_output
=
self
.
out_proj
(
attn_output
)
return
attn_output
,
attn_weights_reshaped
,
past_key_value
return
attn_output
,
attn_weights_reshaped
,
past_key_value
...
...
src/transformers/models/blenderbot/modeling_blenderbot.py
View file @
7eee950a
...
@@ -211,28 +211,26 @@ class BlenderbotAttention(nn.Module):
...
@@ -211,28 +211,26 @@ class BlenderbotAttention(nn.Module):
src_len
=
key_states
.
size
(
1
)
src_len
=
key_states
.
size
(
1
)
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
assert
attn_weights
.
size
()
==
(
if
attn_weights
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
src_len
,
)
),
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
assert
attention_mask
.
size
()
==
(
if
attention_mask
.
size
()
!=
(
bsz
,
1
,
tgt_len
,
src_len
):
bsz
,
raise
ValueError
(
1
,
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
tgt_len
,
)
src_len
,
),
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
if
layer_head_mask
is
not
None
:
if
layer_head_mask
is
not
None
:
assert
layer_head_mask
.
size
()
==
(
if
layer_head_mask
.
size
()
!=
(
self
.
num_heads
,):
self
.
num_heads
,
raise
ValueError
(
),
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
...
@@ -250,18 +248,15 @@ class BlenderbotAttention(nn.Module):
...
@@ -250,18 +248,15 @@ class BlenderbotAttention(nn.Module):
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
assert
attn_output
.
size
()
==
(
if
attn_output
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
self
.
head_dim
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
self
.
head_dim
,
),
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
attn_output
=
(
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
.
transpose
(
1
,
2
)
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
)
)
attn_output
=
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
attn_output
=
attn_output
.
transpose
(
1
,
2
)
attn_output
=
attn_output
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
attn_output
=
self
.
out_proj
(
attn_output
)
attn_output
=
self
.
out_proj
(
attn_output
)
return
attn_output
,
attn_weights_reshaped
,
past_key_value
return
attn_output
,
attn_weights_reshaped
,
past_key_value
...
...
src/transformers/models/blenderbot_small/modeling_blenderbot_small.py
View file @
7eee950a
...
@@ -209,28 +209,26 @@ class BlenderbotSmallAttention(nn.Module):
...
@@ -209,28 +209,26 @@ class BlenderbotSmallAttention(nn.Module):
src_len
=
key_states
.
size
(
1
)
src_len
=
key_states
.
size
(
1
)
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
assert
attn_weights
.
size
()
==
(
if
attn_weights
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
src_len
,
)
),
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
assert
attention_mask
.
size
()
==
(
if
attention_mask
.
size
()
!=
(
bsz
,
1
,
tgt_len
,
src_len
):
bsz
,
raise
ValueError
(
1
,
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
tgt_len
,
)
src_len
,
),
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
if
layer_head_mask
is
not
None
:
if
layer_head_mask
is
not
None
:
assert
layer_head_mask
.
size
()
==
(
if
layer_head_mask
.
size
()
!=
(
self
.
num_heads
,):
self
.
num_heads
,
raise
ValueError
(
),
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
...
@@ -248,18 +246,15 @@ class BlenderbotSmallAttention(nn.Module):
...
@@ -248,18 +246,15 @@ class BlenderbotSmallAttention(nn.Module):
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
assert
attn_output
.
size
()
==
(
if
attn_output
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
self
.
head_dim
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
self
.
head_dim
,
),
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
attn_output
=
(
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
.
transpose
(
1
,
2
)
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
)
)
attn_output
=
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
attn_output
=
attn_output
.
transpose
(
1
,
2
)
attn_output
=
attn_output
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
attn_output
=
self
.
out_proj
(
attn_output
)
attn_output
=
self
.
out_proj
(
attn_output
)
return
attn_output
,
attn_weights_reshaped
,
past_key_value
return
attn_output
,
attn_weights_reshaped
,
past_key_value
...
...
src/transformers/models/m2m_100/modeling_m2m_100.py
View file @
7eee950a
...
@@ -280,28 +280,26 @@ class M2M100Attention(nn.Module):
...
@@ -280,28 +280,26 @@ class M2M100Attention(nn.Module):
src_len
=
key_states
.
size
(
1
)
src_len
=
key_states
.
size
(
1
)
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
assert
attn_weights
.
size
()
==
(
if
attn_weights
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
src_len
,
)
),
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
assert
attention_mask
.
size
()
==
(
if
attention_mask
.
size
()
!=
(
bsz
,
1
,
tgt_len
,
src_len
):
bsz
,
raise
ValueError
(
1
,
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
tgt_len
,
)
src_len
,
),
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
if
layer_head_mask
is
not
None
:
if
layer_head_mask
is
not
None
:
assert
layer_head_mask
.
size
()
==
(
if
layer_head_mask
.
size
()
!=
(
self
.
num_heads
,):
self
.
num_heads
,
raise
ValueError
(
),
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
...
@@ -319,18 +317,15 @@ class M2M100Attention(nn.Module):
...
@@ -319,18 +317,15 @@ class M2M100Attention(nn.Module):
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
assert
attn_output
.
size
()
==
(
if
attn_output
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
self
.
head_dim
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
self
.
head_dim
,
),
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
attn_output
=
(
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
.
transpose
(
1
,
2
)
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
)
)
attn_output
=
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
attn_output
=
attn_output
.
transpose
(
1
,
2
)
attn_output
=
attn_output
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
attn_output
=
self
.
out_proj
(
attn_output
)
attn_output
=
self
.
out_proj
(
attn_output
)
return
attn_output
,
attn_weights_reshaped
,
past_key_value
return
attn_output
,
attn_weights_reshaped
,
past_key_value
...
...
src/transformers/models/marian/modeling_marian.py
View file @
7eee950a
...
@@ -226,28 +226,26 @@ class MarianAttention(nn.Module):
...
@@ -226,28 +226,26 @@ class MarianAttention(nn.Module):
src_len
=
key_states
.
size
(
1
)
src_len
=
key_states
.
size
(
1
)
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
assert
attn_weights
.
size
()
==
(
if
attn_weights
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
src_len
,
)
),
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
assert
attention_mask
.
size
()
==
(
if
attention_mask
.
size
()
!=
(
bsz
,
1
,
tgt_len
,
src_len
):
bsz
,
raise
ValueError
(
1
,
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
tgt_len
,
)
src_len
,
),
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
if
layer_head_mask
is
not
None
:
if
layer_head_mask
is
not
None
:
assert
layer_head_mask
.
size
()
==
(
if
layer_head_mask
.
size
()
!=
(
self
.
num_heads
,):
self
.
num_heads
,
raise
ValueError
(
),
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
...
@@ -265,18 +263,15 @@ class MarianAttention(nn.Module):
...
@@ -265,18 +263,15 @@ class MarianAttention(nn.Module):
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
assert
attn_output
.
size
()
==
(
if
attn_output
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
self
.
head_dim
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
self
.
head_dim
,
),
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
attn_output
=
(
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
.
transpose
(
1
,
2
)
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
)
)
attn_output
=
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
attn_output
=
attn_output
.
transpose
(
1
,
2
)
attn_output
=
attn_output
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
attn_output
=
self
.
out_proj
(
attn_output
)
attn_output
=
self
.
out_proj
(
attn_output
)
return
attn_output
,
attn_weights_reshaped
,
past_key_value
return
attn_output
,
attn_weights_reshaped
,
past_key_value
...
...
src/transformers/models/mbart/modeling_mbart.py
View file @
7eee950a
...
@@ -217,28 +217,26 @@ class MBartAttention(nn.Module):
...
@@ -217,28 +217,26 @@ class MBartAttention(nn.Module):
src_len
=
key_states
.
size
(
1
)
src_len
=
key_states
.
size
(
1
)
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
assert
attn_weights
.
size
()
==
(
if
attn_weights
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
src_len
,
)
),
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
assert
attention_mask
.
size
()
==
(
if
attention_mask
.
size
()
!=
(
bsz
,
1
,
tgt_len
,
src_len
):
bsz
,
raise
ValueError
(
1
,
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
tgt_len
,
)
src_len
,
),
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
if
layer_head_mask
is
not
None
:
if
layer_head_mask
is
not
None
:
assert
layer_head_mask
.
size
()
==
(
if
layer_head_mask
.
size
()
!=
(
self
.
num_heads
,):
self
.
num_heads
,
raise
ValueError
(
),
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
...
@@ -256,18 +254,15 @@ class MBartAttention(nn.Module):
...
@@ -256,18 +254,15 @@ class MBartAttention(nn.Module):
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
assert
attn_output
.
size
()
==
(
if
attn_output
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
self
.
head_dim
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
self
.
head_dim
,
),
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
attn_output
=
(
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
.
transpose
(
1
,
2
)
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
)
)
attn_output
=
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
attn_output
=
attn_output
.
transpose
(
1
,
2
)
attn_output
=
attn_output
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
attn_output
=
self
.
out_proj
(
attn_output
)
attn_output
=
self
.
out_proj
(
attn_output
)
return
attn_output
,
attn_weights_reshaped
,
past_key_value
return
attn_output
,
attn_weights_reshaped
,
past_key_value
...
...
src/transformers/models/pegasus/modeling_pegasus.py
View file @
7eee950a
...
@@ -226,28 +226,26 @@ class PegasusAttention(nn.Module):
...
@@ -226,28 +226,26 @@ class PegasusAttention(nn.Module):
src_len
=
key_states
.
size
(
1
)
src_len
=
key_states
.
size
(
1
)
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
assert
attn_weights
.
size
()
==
(
if
attn_weights
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
src_len
,
)
),
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
assert
attention_mask
.
size
()
==
(
if
attention_mask
.
size
()
!=
(
bsz
,
1
,
tgt_len
,
src_len
):
bsz
,
raise
ValueError
(
1
,
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
tgt_len
,
)
src_len
,
),
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
if
layer_head_mask
is
not
None
:
if
layer_head_mask
is
not
None
:
assert
layer_head_mask
.
size
()
==
(
if
layer_head_mask
.
size
()
!=
(
self
.
num_heads
,):
self
.
num_heads
,
raise
ValueError
(
),
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
...
@@ -265,18 +263,15 @@ class PegasusAttention(nn.Module):
...
@@ -265,18 +263,15 @@ class PegasusAttention(nn.Module):
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
assert
attn_output
.
size
()
==
(
if
attn_output
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
self
.
head_dim
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
self
.
head_dim
,
),
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
attn_output
=
(
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
.
transpose
(
1
,
2
)
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
)
)
attn_output
=
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
attn_output
=
attn_output
.
transpose
(
1
,
2
)
attn_output
=
attn_output
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
attn_output
=
self
.
out_proj
(
attn_output
)
attn_output
=
self
.
out_proj
(
attn_output
)
return
attn_output
,
attn_weights_reshaped
,
past_key_value
return
attn_output
,
attn_weights_reshaped
,
past_key_value
...
...
src/transformers/models/speech_to_text/modeling_speech_to_text.py
View file @
7eee950a
...
@@ -293,28 +293,26 @@ class Speech2TextAttention(nn.Module):
...
@@ -293,28 +293,26 @@ class Speech2TextAttention(nn.Module):
src_len
=
key_states
.
size
(
1
)
src_len
=
key_states
.
size
(
1
)
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
assert
attn_weights
.
size
()
==
(
if
attn_weights
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
src_len
,
)
),
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
assert
attention_mask
.
size
()
==
(
if
attention_mask
.
size
()
!=
(
bsz
,
1
,
tgt_len
,
src_len
):
bsz
,
raise
ValueError
(
1
,
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
tgt_len
,
)
src_len
,
),
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
if
layer_head_mask
is
not
None
:
if
layer_head_mask
is
not
None
:
assert
layer_head_mask
.
size
()
==
(
if
layer_head_mask
.
size
()
!=
(
self
.
num_heads
,):
self
.
num_heads
,
raise
ValueError
(
),
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
...
@@ -332,18 +330,15 @@ class Speech2TextAttention(nn.Module):
...
@@ -332,18 +330,15 @@ class Speech2TextAttention(nn.Module):
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
assert
attn_output
.
size
()
==
(
if
attn_output
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
self
.
head_dim
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
self
.
head_dim
,
),
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
attn_output
=
(
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
.
transpose
(
1
,
2
)
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
)
)
attn_output
=
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
attn_output
=
attn_output
.
transpose
(
1
,
2
)
attn_output
=
attn_output
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
attn_output
=
self
.
out_proj
(
attn_output
)
attn_output
=
self
.
out_proj
(
attn_output
)
return
attn_output
,
attn_weights_reshaped
,
past_key_value
return
attn_output
,
attn_weights_reshaped
,
past_key_value
...
...
src/transformers/models/wav2vec2/modeling_wav2vec2.py
View file @
7eee950a
...
@@ -356,28 +356,26 @@ class Wav2Vec2Attention(nn.Module):
...
@@ -356,28 +356,26 @@ class Wav2Vec2Attention(nn.Module):
src_len
=
key_states
.
size
(
1
)
src_len
=
key_states
.
size
(
1
)
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
assert
attn_weights
.
size
()
==
(
if
attn_weights
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
src_len
,
)
),
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
assert
attention_mask
.
size
()
==
(
if
attention_mask
.
size
()
!=
(
bsz
,
1
,
tgt_len
,
src_len
):
bsz
,
raise
ValueError
(
1
,
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
tgt_len
,
)
src_len
,
),
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
if
layer_head_mask
is
not
None
:
if
layer_head_mask
is
not
None
:
assert
layer_head_mask
.
size
()
==
(
if
layer_head_mask
.
size
()
!=
(
self
.
num_heads
,):
self
.
num_heads
,
raise
ValueError
(
),
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
...
@@ -395,18 +393,15 @@ class Wav2Vec2Attention(nn.Module):
...
@@ -395,18 +393,15 @@ class Wav2Vec2Attention(nn.Module):
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
assert
attn_output
.
size
()
==
(
if
attn_output
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
self
.
head_dim
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
self
.
head_dim
,
),
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
attn_output
=
(
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
.
transpose
(
1
,
2
)
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
)
)
attn_output
=
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
attn_output
=
attn_output
.
transpose
(
1
,
2
)
attn_output
=
attn_output
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
attn_output
=
self
.
out_proj
(
attn_output
)
attn_output
=
self
.
out_proj
(
attn_output
)
return
attn_output
,
attn_weights_reshaped
,
past_key_value
return
attn_output
,
attn_weights_reshaped
,
past_key_value
...
...
templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/modeling_{{cookiecutter.lowercase_modelname}}.py
View file @
7eee950a
...
@@ -1721,28 +1721,26 @@ class {{cookiecutter.camelcase_modelname}}Attention(nn.Module):
...
@@ -1721,28 +1721,26 @@ class {{cookiecutter.camelcase_modelname}}Attention(nn.Module):
src_len
=
key_states
.
size
(
1
)
src_len
=
key_states
.
size
(
1
)
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
attn_weights
=
torch
.
bmm
(
query_states
,
key_states
.
transpose
(
1
,
2
))
assert
attn_weights
.
size
()
==
(
if
attn_weights
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
src_len
,
)
),
f
"Attention weights should be of size
{
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
}
, but is
{
attn_weights
.
size
()
}
"
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
assert
attention_mask
.
size
()
==
(
if
attention_mask
.
size
()
!=
(
bsz
,
1
,
tgt_len
,
src_len
):
bsz
,
raise
ValueError
(
1
,
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
tgt_len
,
)
src_len
,
),
f
"Attention mask should be of size
{
(
bsz
,
1
,
tgt_len
,
src_len
)
}
, but is
{
attention_mask
.
size
()
}
"
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
+
attention_mask
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
attn_weights
=
F
.
softmax
(
attn_weights
,
dim
=-
1
)
if
layer_head_mask
is
not
None
:
if
layer_head_mask
is
not
None
:
assert
layer_head_mask
.
size
()
==
(
if
layer_head_mask
.
size
()
!=
(
self
.
num_heads
,):
self
.
num_heads
,
raise
ValueError
(
),
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
f
"Head mask for a single layer should be of size
{
(
self
.
num_heads
,)
}
, but is
{
layer_head_mask
.
size
()
}
"
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
layer_head_mask
.
view
(
1
,
-
1
,
1
,
1
)
*
attn_weights
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
attn_weights
=
attn_weights
.
view
(
bsz
*
self
.
num_heads
,
tgt_len
,
src_len
)
...
@@ -1760,18 +1758,15 @@ class {{cookiecutter.camelcase_modelname}}Attention(nn.Module):
...
@@ -1760,18 +1758,15 @@ class {{cookiecutter.camelcase_modelname}}Attention(nn.Module):
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
attn_output
=
torch
.
bmm
(
attn_probs
,
value_states
)
assert
attn_output
.
size
()
==
(
if
attn_output
.
size
()
!=
(
bsz
*
self
.
num_heads
,
tgt_len
,
self
.
head_dim
):
bsz
*
self
.
num_heads
,
raise
ValueError
(
tgt_len
,
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
self
.
head_dim
,
),
f
"`attn_output` should be of size
{
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
}
, but is
{
attn_output
.
size
()
}
"
attn_output
=
(
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
.
transpose
(
1
,
2
)
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
)
)
attn_output
=
attn_output
.
view
(
bsz
,
self
.
num_heads
,
tgt_len
,
self
.
head_dim
)
attn_output
=
attn_output
.
transpose
(
1
,
2
)
attn_output
=
attn_output
.
reshape
(
bsz
,
tgt_len
,
embed_dim
)
attn_output
=
self
.
out_proj
(
attn_output
)
attn_output
=
self
.
out_proj
(
attn_output
)
return
attn_output
,
attn_weights_reshaped
,
past_key_value
return
attn_output
,
attn_weights_reshaped
,
past_key_value
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment