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
c90e14fb
Unverified
Commit
c90e14fb
authored
Jul 28, 2023
by
Yoni Gottesman
Committed by
GitHub
Jul 28, 2023
Browse files
Fix beam search to sample at least 1 non eos token (#25103) (#25115)
parent
31f137c0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
6 deletions
+9
-6
src/transformers/generation/utils.py
src/transformers/generation/utils.py
+9
-6
No files found.
src/transformers/generation/utils.py
View file @
c90e14fb
...
@@ -3068,9 +3068,10 @@ class GenerationMixin:
...
@@ -3068,9 +3068,10 @@ class GenerationMixin:
vocab_size
=
next_token_scores
.
shape
[
-
1
]
vocab_size
=
next_token_scores
.
shape
[
-
1
]
next_token_scores
=
next_token_scores
.
view
(
batch_size
,
num_beams
*
vocab_size
)
next_token_scores
=
next_token_scores
.
view
(
batch_size
,
num_beams
*
vocab_size
)
# Sample 2 next tokens for each beam (so we have some spare tokens and match output of beam search)
# Sample 1 + len(eos_token_id) next tokens for each beam so we have at least 1 non eos token per beam.
n_eos_tokens
=
len
(
eos_token_id
)
if
eos_token_id
else
0
next_token_scores
,
next_tokens
=
torch
.
topk
(
next_token_scores
,
next_tokens
=
torch
.
topk
(
next_token_scores
,
2
*
num_beams
,
dim
=
1
,
largest
=
True
,
sorted
=
True
next_token_scores
,
max
(
2
,
1
+
n_eos_tokens
)
*
num_beams
,
dim
=
1
,
largest
=
True
,
sorted
=
True
)
)
next_indices
=
torch
.
div
(
next_tokens
,
vocab_size
,
rounding_mode
=
"floor"
)
next_indices
=
torch
.
div
(
next_tokens
,
vocab_size
,
rounding_mode
=
"floor"
)
...
@@ -3746,9 +3747,10 @@ class GenerationMixin:
...
@@ -3746,9 +3747,10 @@ class GenerationMixin:
# reshape for beam search
# reshape for beam search
next_token_scores
=
next_token_scores
.
view
(
batch_size
,
group_size
*
vocab_size
)
next_token_scores
=
next_token_scores
.
view
(
batch_size
,
group_size
*
vocab_size
)
# Sample 2 next tokens for each beam (so we have some spare tokens and match output of beam search)
# Sample 1 + len(eos_token_id) next tokens for each beam so we have at least 1 non eos token per beam.
n_eos_tokens
=
len
(
eos_token_id
)
if
eos_token_id
else
0
next_token_scores
,
next_tokens
=
torch
.
topk
(
next_token_scores
,
next_tokens
=
torch
.
topk
(
next_token_scores
,
2
*
group_size
,
dim
=
1
,
largest
=
True
,
sorted
=
True
next_token_scores
,
max
(
2
,
1
+
n_eos_tokens
)
*
group_size
,
dim
=
1
,
largest
=
True
,
sorted
=
True
)
)
next_indices
=
torch
.
div
(
next_tokens
,
vocab_size
,
rounding_mode
=
"floor"
)
next_indices
=
torch
.
div
(
next_tokens
,
vocab_size
,
rounding_mode
=
"floor"
)
...
@@ -4119,9 +4121,10 @@ class GenerationMixin:
...
@@ -4119,9 +4121,10 @@ class GenerationMixin:
vocab_size
=
next_token_scores
.
shape
[
-
1
]
vocab_size
=
next_token_scores
.
shape
[
-
1
]
next_token_scores
=
next_token_scores
.
view
(
batch_size
,
num_beams
*
vocab_size
)
next_token_scores
=
next_token_scores
.
view
(
batch_size
,
num_beams
*
vocab_size
)
# Sample 2 next tokens for each beam (so we have some spare tokens and match output of beam search)
# Sample 1 + len(eos_token_id) next tokens for each beam so we have at least 1 non eos token per beam.
n_eos_tokens
=
len
(
eos_token_id
)
if
eos_token_id
else
0
next_token_scores
,
next_tokens
=
torch
.
topk
(
next_token_scores
,
next_tokens
=
torch
.
topk
(
next_token_scores
,
2
*
num_beams
,
dim
=
1
,
largest
=
True
,
sorted
=
True
next_token_scores
,
max
(
2
,
1
+
n_eos_tokens
)
*
num_beams
,
dim
=
1
,
largest
=
True
,
sorted
=
True
)
)
next_indices
=
(
next_tokens
/
vocab_size
).
long
()
next_indices
=
(
next_tokens
/
vocab_size
).
long
()
...
...
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