Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
cec3cdda
Unverified
Commit
cec3cdda
authored
Mar 19, 2020
by
Patrick von Platen
Committed by
GitHub
Mar 19, 2020
Browse files
Fix input ids can be none attn mask (#3345)
* fix issue 3289 * fix attention mask if input_ids None behavior
parent
f6d813aa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
7 additions
and
2 deletions
+7
-2
src/transformers/modeling_ctrl.py
src/transformers/modeling_ctrl.py
+4
-1
src/transformers/modeling_gpt2.py
src/transformers/modeling_gpt2.py
+3
-1
No files found.
src/transformers/modeling_ctrl.py
View file @
cec3cdda
...
@@ -330,8 +330,10 @@ class CTRLModel(CTRLPreTrainedModel):
...
@@ -330,8 +330,10 @@ class CTRLModel(CTRLPreTrainedModel):
elif
input_ids
is
not
None
:
elif
input_ids
is
not
None
:
input_shape
=
input_ids
.
size
()
input_shape
=
input_ids
.
size
()
input_ids
=
input_ids
.
view
(
-
1
,
input_shape
[
-
1
])
input_ids
=
input_ids
.
view
(
-
1
,
input_shape
[
-
1
])
batch_size
=
input_ids
.
shape
[
0
]
elif
inputs_embeds
is
not
None
:
elif
inputs_embeds
is
not
None
:
input_shape
=
inputs_embeds
.
size
()[:
-
1
]
input_shape
=
inputs_embeds
.
size
()[:
-
1
]
batch_size
=
inputs_embeds
.
shape
[
0
]
else
:
else
:
raise
ValueError
(
"You have to specify either input_ids or inputs_embeds"
)
raise
ValueError
(
"You have to specify either input_ids or inputs_embeds"
)
...
@@ -347,7 +349,8 @@ class CTRLModel(CTRLPreTrainedModel):
...
@@ -347,7 +349,8 @@ class CTRLModel(CTRLPreTrainedModel):
# Attention mask.
# Attention mask.
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
attention_mask
=
attention_mask
.
view
(
-
1
,
input_shape
[
-
1
])
assert
batch_size
>
0
,
"batch_size has to be defined and > 0"
attention_mask
=
attention_mask
.
view
(
batch_size
,
-
1
)
# We create a 3D attention mask from a 2D tensor mask.
# We create a 3D attention mask from a 2D tensor mask.
# Sizes are [batch_size, 1, 1, to_seq_length]
# Sizes are [batch_size, 1, 1, to_seq_length]
# So we can broadcast to [batch_size, num_heads, from_seq_length, to_seq_length]
# So we can broadcast to [batch_size, num_heads, from_seq_length, to_seq_length]
...
...
src/transformers/modeling_gpt2.py
View file @
cec3cdda
...
@@ -402,8 +402,10 @@ class GPT2Model(GPT2PreTrainedModel):
...
@@ -402,8 +402,10 @@ class GPT2Model(GPT2PreTrainedModel):
elif
input_ids
is
not
None
:
elif
input_ids
is
not
None
:
input_shape
=
input_ids
.
size
()
input_shape
=
input_ids
.
size
()
input_ids
=
input_ids
.
view
(
-
1
,
input_shape
[
-
1
])
input_ids
=
input_ids
.
view
(
-
1
,
input_shape
[
-
1
])
batch_size
=
input_ids
.
shape
[
0
]
elif
inputs_embeds
is
not
None
:
elif
inputs_embeds
is
not
None
:
input_shape
=
inputs_embeds
.
size
()[:
-
1
]
input_shape
=
inputs_embeds
.
size
()[:
-
1
]
batch_size
=
inputs_embeds
.
shape
[
0
]
else
:
else
:
raise
ValueError
(
"You have to specify either input_ids or inputs_embeds"
)
raise
ValueError
(
"You have to specify either input_ids or inputs_embeds"
)
...
@@ -424,7 +426,7 @@ class GPT2Model(GPT2PreTrainedModel):
...
@@ -424,7 +426,7 @@ class GPT2Model(GPT2PreTrainedModel):
# Attention mask.
# Attention mask.
if
attention_mask
is
not
None
:
if
attention_mask
is
not
None
:
batch_size
=
input_ids
.
shape
[
0
]
assert
batch_size
>
0
,
"batch_size has to be defined and > 0"
attention_mask
=
attention_mask
.
view
(
batch_size
,
-
1
)
attention_mask
=
attention_mask
.
view
(
batch_size
,
-
1
)
# We create a 3D attention mask from a 2D tensor mask.
# We create a 3D attention mask from a 2D tensor mask.
# Sizes are [batch_size, 1, 1, to_seq_length]
# Sizes are [batch_size, 1, 1, to_seq_length]
...
...
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