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
gaoqiong
lm-evaluation-harness
Commits
ba2b849c
Commit
ba2b849c
authored
Sep 06, 2020
by
Leo Gao
Browse files
GPT2: add device parameter
parent
5bb5e96d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
4 deletions
+5
-4
gpt2.py
gpt2.py
+5
-4
No files found.
gpt2.py
View file @
ba2b849c
...
...
@@ -5,12 +5,13 @@ import torch.nn.functional as F
class
GPT2LM
(
LM
):
def
__init__
(
self
):
self
.
gpt2
=
transformers
.
GPT2LMHeadModel
.
from_pretrained
(
'gpt2'
)
def
__init__
(
self
,
dev
=
'cpu'
):
self
.
gpt2
=
transformers
.
GPT2LMHeadModel
.
from_pretrained
(
'gpt2'
)
.
to
(
dev
)
self
.
tok
=
transformers
.
GPT2Tokenizer
.
from_pretrained
(
'gpt2'
)
self
.
dev
=
dev
def
generate
(
self
,
context
,
until
):
context
=
torch
.
tensor
([
self
.
tok
.
encode
(
context
.
strip
())],
dtype
=
torch
.
long
)
context
=
torch
.
tensor
([
self
.
tok
.
encode
(
context
.
strip
())],
dtype
=
torch
.
long
)
.
to
(
self
.
dev
)
res
=
self
.
gpt2
.
generate
(
context
,
eos_token_id
=
self
.
tok
.
encoder
[
until
],
do_sample
=
False
,
max_length
=
1024
)
# chop off the prompt and the final eos token
...
...
@@ -18,7 +19,7 @@ class GPT2LM(LM):
def
loglikelihood
(
self
,
context
,
continuation
):
print
(
'likelihood:'
,
context
,
continuation
)
inp
=
torch
.
tensor
([
self
.
tok
.
encode
(
context
+
continuation
)],
dtype
=
torch
.
long
)
inp
=
torch
.
tensor
([
self
.
tok
.
encode
(
context
+
continuation
)],
dtype
=
torch
.
long
)
.
to
(
self
.
dev
)
ctxlen
=
len
(
self
.
tok
.
encode
(
context
.
strip
()))
cont_toks
=
inp
[:,
ctxlen
:]
# [batch, seq]
...
...
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