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
359114fd
Commit
359114fd
authored
Feb 07, 2021
by
Leo Gao
Browse files
LM: handle empty context
parent
5d56a47d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
17 additions
and
3 deletions
+17
-3
lm_eval/base.py
lm_eval/base.py
+2
-1
lm_eval/models/gpt2.py
lm_eval/models/gpt2.py
+7
-1
lm_eval/models/gpt3.py
lm_eval/models/gpt3.py
+6
-1
tests/test_models.py
tests/test_models.py
+2
-0
No files found.
lm_eval/base.py
View file @
359114fd
...
...
@@ -15,7 +15,8 @@ class LM(abc.ABC):
:param requests: list
A list of pairs (context, continuation)
context: str
Context string
Context string. Implementations of LM must be able to handle an
empty context string.
continuation: str
The continuation over which log likelihood will be calculated. If
there is a word boundary, the space should be in the continuation.
...
...
lm_eval/models/gpt2.py
View file @
359114fd
...
...
@@ -24,7 +24,13 @@ class GPT2LM(LM):
# TODO: vectorize properly
for
context
,
continuation
in
tqdm
(
requests
):
# when too long to fit in context, truncate from the left
context_enc
=
self
.
tokenizer
.
encode
(
context
)
if
context
==
""
:
# end of text as context
context_enc
=
[
50256
]
else
:
context_enc
=
self
.
tokenizer
.
encode
(
context
)
continuation_enc
=
self
.
tokenizer
.
encode
(
continuation
)
inp
=
torch
.
tensor
([(
context_enc
+
continuation_enc
)[
-
1024
:]],
dtype
=
torch
.
long
).
to
(
self
.
device
)
ctxlen
=
len
(
context_enc
)
-
max
(
0
,
len
(
context_enc
)
+
len
(
continuation_enc
)
-
1024
)
...
...
lm_eval/models/gpt3.py
View file @
359114fd
...
...
@@ -72,7 +72,12 @@ class GPT3LM(LM):
inps
=
[]
ctxlens
=
[]
for
context
,
continuation
in
chunk
:
context_enc
=
self
.
tokenizer
.
encode
(
context
)
if
context
==
""
:
# end of text as context
context_enc
=
[
50256
]
else
:
context_enc
=
self
.
tokenizer
.
encode
(
context
)
continuation_enc
=
self
.
tokenizer
.
encode
(
continuation
)
inp
=
(
context_enc
+
continuation_enc
)[
-
self
.
MAX_LENGTH
:]
ctxlen
=
len
(
context_enc
)
-
max
(
0
,
len
(
context_enc
)
+
len
(
continuation_enc
)
-
self
.
MAX_LENGTH
)
...
...
tests/test_models.py
View file @
359114fd
...
...
@@ -11,3 +11,5 @@ def test_gpt2():
assert
ll_dog
>
ll_cat
assert
not
ig_cat
# test empty context
gpt2
.
loglikelihood
([(
''
,
'test'
)])
\ No newline at end of file
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