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
a98173cc
Unverified
Commit
a98173cc
authored
Jan 20, 2021
by
LSinev
Committed by
GitHub
Jan 20, 2021
Browse files
make RepetitionPenaltyLogitsProcessor faster (#9600)
parent
a1ad16a4
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
2 additions
and
3 deletions
+2
-3
src/transformers/generation_logits_process.py
src/transformers/generation_logits_process.py
+2
-3
No files found.
src/transformers/generation_logits_process.py
View file @
a98173cc
...
@@ -155,13 +155,12 @@ class RepetitionPenaltyLogitsProcessor(LogitsProcessor):
...
@@ -155,13 +155,12 @@ class RepetitionPenaltyLogitsProcessor(LogitsProcessor):
self
.
penalty
=
penalty
self
.
penalty
=
penalty
def
__call__
(
self
,
input_ids
:
torch
.
LongTensor
,
scores
:
torch
.
FloatTensor
)
->
torch
.
FloatTensor
:
def
__call__
(
self
,
input_ids
:
torch
.
LongTensor
,
scores
:
torch
.
FloatTensor
)
->
torch
.
FloatTensor
:
ranges
=
torch
.
arange
(
scores
.
shape
[
0
])
score
=
torch
.
gather
(
scores
,
1
,
input_ids
)
score
=
scores
[
ranges
[:,
None
],
input_ids
]
# if score < 0 then repetition penalty has to be multiplied to reduce the previous token probability
# if score < 0 then repetition penalty has to be multiplied to reduce the previous token probability
score
=
torch
.
where
(
score
<
0
,
score
*
self
.
penalty
,
score
/
self
.
penalty
)
score
=
torch
.
where
(
score
<
0
,
score
*
self
.
penalty
,
score
/
self
.
penalty
)
scores
[
ranges
[:,
None
]
,
input_ids
]
=
score
scores
.
scatter_
(
1
,
input_ids
,
score
)
return
scores
return
scores
...
...
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