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
06a6cb6f
Commit
06a6cb6f
authored
Mar 05, 2020
by
Tom Hosking
Browse files
Refactor BartModel so that input checks are handled within BartEncoder and BartDecoder
parent
30624f70
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
5 deletions
+13
-5
src/.DS_Store
src/.DS_Store
+0
-0
src/transformers/modeling_bart.py
src/transformers/modeling_bart.py
+13
-5
No files found.
src/.DS_Store
0 → 100644
View file @
06a6cb6f
File added
src/transformers/modeling_bart.py
View file @
06a6cb6f
...
...
@@ -271,6 +271,12 @@ class BartEncoder(nn.Module):
- **all_attentions** (List[Tensor]): Attention weights for each layer.
During training might not be of length n_layers because of layer dropout.
"""
# check attention mask and invert
if
attention_mask
is
not
None
:
assert
attention_mask
.
dim
()
==
2
attention_mask
=
(
1.0
-
attention_mask
.
long
())
*
-
10000.0
assert
attention_mask
.
max
()
<=
0
inputs_embeds
=
self
.
embed_tokens
(
input_ids
)
embed_pos
=
self
.
embed_positions
(
input_ids
)
x
=
inputs_embeds
+
embed_pos
...
...
@@ -448,6 +454,13 @@ class BartDecoder(nn.Module):
- hidden states
- attentions
"""
# check attention mask and invert
if
encoder_padding_mask
is
not
None
:
assert
encoder_padding_mask
.
dim
()
==
2
encoder_padding_mask
=
(
1.0
-
encoder_padding_mask
.
long
())
*
-
10000.0
assert
encoder_padding_mask
.
max
()
<=
0
# embed positions
positions
=
self
.
embed_positions
(
input_ids
,
generation_mode
=
self
.
generation_mode
)
...
...
@@ -823,11 +836,6 @@ class BartModel(PretrainedBartModel):
decoder_attention_mask
=
None
,
decoder_cached_states
=
None
,
):
if
attention_mask
is
not
None
:
assert
attention_mask
.
dim
()
==
2
attention_mask
=
(
1.0
-
attention_mask
.
long
())
*
-
10000.0
assert
attention_mask
.
max
()
<=
0
# make masks if user doesn't supply
if
not
self
.
decoder
.
generation_mode
:
...
...
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