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
3c01dfb7
Unverified
Commit
3c01dfb7
authored
Mar 06, 2019
by
Thomas Wolf
Committed by
GitHub
Mar 06, 2019
Browse files
Merge pull request #338 from CatalinVoss/patch-3
Fix top k generation for k != 0
parents
477ec4b6
4b4b0792
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
3 deletions
+9
-3
examples/run_gpt2.py
examples/run_gpt2.py
+9
-3
No files found.
examples/run_gpt2.py
View file @
3c01dfb7
...
@@ -16,11 +16,17 @@ logging.basicConfig(format = '%(asctime)s - %(levelname)s - %(name)s - %(messa
...
@@ -16,11 +16,17 @@ logging.basicConfig(format = '%(asctime)s - %(levelname)s - %(name)s - %(messa
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
def
top_k_logits
(
logits
,
k
):
def
top_k_logits
(
logits
,
k
):
"""
Masks everything but the k top entries as -infinity (1e10).
Used to mask logits such that e^-infinity -> 0 won't contribute to the
sum of the denominator.
"""
if
k
==
0
:
if
k
==
0
:
return
logits
return
logits
values
,
_
=
torch
.
topk
(
logits
,
k
)
else
:
min_values
=
values
[:,
-
1
]
values
=
torch
.
topk
(
logits
,
k
)[
0
]
return
torch
.
where
(
logits
<
min_values
,
torch
.
ones_like
(
logits
,
dtype
=
logits
.
dtype
)
*
-
1e10
,
logits
)
batch_mins
=
values
[:,
-
1
].
view
(
-
1
,
1
).
expand_as
(
logits
)
return
torch
.
where
(
logits
<
batch_mins
,
torch
.
ones_like
(
logits
)
*
-
1e10
,
logits
)
def
sample_sequence
(
model
,
length
,
start_token
=
None
,
batch_size
=
None
,
context
=
None
,
temperature
=
1
,
top_k
=
0
,
device
=
'cuda'
,
sample
=
True
):
def
sample_sequence
(
model
,
length
,
start_token
=
None
,
batch_size
=
None
,
context
=
None
,
temperature
=
1
,
top_k
=
0
,
device
=
'cuda'
,
sample
=
True
):
if
start_token
is
None
:
if
start_token
is
None
:
...
...
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