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
90cda45e
"src/nni_manager/vscode:/vscode.git/clone" did not exist on "b1a65978ca13ef8faa0aab0365c188f0db43c127"
Commit
90cda45e
authored
Dec 25, 2019
by
patrickvonplaten
Browse files
add past re-ordering for beam search
parent
6bca56fd
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
5 deletions
+10
-5
src/transformers/modeling_utils.py
src/transformers/modeling_utils.py
+10
-5
No files found.
src/transformers/modeling_utils.py
View file @
90cda45e
...
@@ -913,13 +913,18 @@ class PreTrainedModel(nn.Module):
...
@@ -913,13 +913,18 @@ class PreTrainedModel(nn.Module):
beam_words
=
input_ids
.
new
([
x
[
1
]
for
x
in
next_batch_beam
])
beam_words
=
input_ids
.
new
([
x
[
1
]
for
x
in
next_batch_beam
])
beam_idx
=
input_ids
.
new
([
x
[
2
]
for
x
in
next_batch_beam
])
beam_idx
=
input_ids
.
new
([
x
[
2
]
for
x
in
next_batch_beam
])
# re-order batch
and internal states
# re-order batch
input_ids
=
input_ids
[
beam_idx
,
:]
input_ids
=
input_ids
[
beam_idx
,
:]
input_ids
=
torch
.
cat
([
input_ids
,
beam_words
.
unsqueeze
(
1
)],
dim
=-
1
)
input_ids
=
torch
.
cat
([
input_ids
,
beam_words
.
unsqueeze
(
1
)],
dim
=-
1
)
# TODO: Activate cache
# for k in cache.keys():
# re-order internal states
# if k != 'slen':
if
past
:
# cache[k] = (cache[k][0][beam_idx], cache[k][1][beam_idx])
reordered_past
=
[]
for
layer_past
in
past
:
# copy the relevant beam idx past to past
reordered_layer_past
=
[
layer_past
[:,
i
].
unsqueeze
(
1
).
clone
().
detach
()
for
i
in
beam_idx
]
reordered_past
.
append
(
torch
.
cat
(
reordered_layer_past
,
dim
=
1
))
past
=
tuple
(
reordered_past
)
# update current length
# update current length
cur_len
=
cur_len
+
1
cur_len
=
cur_len
+
1
...
...
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