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
8315dce7
Commit
8315dce7
authored
Dec 27, 2020
by
uyhcire
Browse files
Fix eval script to normalize loglikelihoods
parent
599045ba
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
31 deletions
+39
-31
batch_eval/__init__.py
batch_eval/__init__.py
+0
-0
batch_eval/main.py
batch_eval/main.py
+39
-31
No files found.
batch_eval/__init__.py
0 → 100644
View file @
8315dce7
batch_eval/main.py
View file @
8315dce7
...
@@ -4,6 +4,7 @@ import time
...
@@ -4,6 +4,7 @@ import time
import
click
import
click
import
torch
import
torch
import
torch.nn.functional
as
F
from
transformers
import
AutoConfig
,
AutoModelForCausalLM
,
AutoTokenizer
from
transformers
import
AutoConfig
,
AutoModelForCausalLM
,
AutoTokenizer
...
@@ -53,18 +54,18 @@ def evaluate_examples(model_runner, examples):
...
@@ -53,18 +54,18 @@ def evaluate_examples(model_runner, examples):
for
prompt
,
example
in
zip
(
prompts
,
examples
)
for
prompt
,
example
in
zip
(
prompts
,
examples
)
]
]
average_token_log
it
s_with_sentence_1
=
(
average_token_log
likelihood
s_with_sentence_1
=
(
model_runner
.
compute_average_token_log
it
s_on_batch
(
inputs_for_sentence_1
)
model_runner
.
compute_average_token_log
likelihood
s_on_batch
(
inputs_for_sentence_1
)
)
)
average_token_log
it
s_with_sentence_2
=
(
average_token_log
likelihood
s_with_sentence_2
=
(
model_runner
.
compute_average_token_log
it
s_on_batch
(
inputs_for_sentence_2
)
model_runner
.
compute_average_token_log
likelihood
s_on_batch
(
inputs_for_sentence_2
)
)
)
evaluation_results
=
[]
evaluation_results
=
[]
for
i
in
range
(
len
(
examples
)):
for
i
in
range
(
len
(
examples
)):
if
(
if
(
average_token_log
it
s_with_sentence_1
[
i
]
average_token_log
likelihood
s_with_sentence_1
[
i
]
>
average_token_log
it
s_with_sentence_2
[
i
]
>
average_token_log
likelihood
s_with_sentence_2
[
i
]
):
):
model_answer
=
examples
[
i
][
"RandomFifthSentenceQuiz1"
]
model_answer
=
examples
[
i
][
"RandomFifthSentenceQuiz1"
]
model_answer_code
=
"1"
model_answer_code
=
"1"
...
@@ -96,15 +97,15 @@ class ModelRunner:
...
@@ -96,15 +97,15 @@ class ModelRunner:
model_runner
.
model
=
AutoModelForCausalLM
.
from_pretrained
(
model_runner
.
model
=
AutoModelForCausalLM
.
from_pretrained
(
# 117M
# 117M
pretrained_model_name_or_path
=
"gpt2"
,
pretrained_model_name_or_path
=
"gpt2
-large
"
,
config
=
AutoConfig
.
from_pretrained
(
config
=
AutoConfig
.
from_pretrained
(
"gpt2"
,
"gpt2
-large
"
,
# <|endoftext|>
# <|endoftext|>
pad_token_id
=
50256
,
pad_token_id
=
50256
,
),
),
).
to
(
"cuda"
)
).
to
(
"cuda"
)
model_runner
.
model
=
model_runner
.
model
.
eval
()
model_runner
.
model
=
model_runner
.
model
.
eval
()
model_runner
.
tokenizer
=
AutoTokenizer
.
from_pretrained
(
"gpt2"
)
model_runner
.
tokenizer
=
AutoTokenizer
.
from_pretrained
(
"gpt2
-large
"
)
model_runner
.
tokenizer
.
pad_token
=
"<|endoftext|>"
model_runner
.
tokenizer
.
pad_token
=
"<|endoftext|>"
prompt
=
"The quick brown fox jumps over"
prompt
=
"The quick brown fox jumps over"
...
@@ -126,11 +127,11 @@ class ModelRunner:
...
@@ -126,11 +127,11 @@ class ModelRunner:
return
model_runner
return
model_runner
def
compute_average_token_log
it
s_on_batch
(
self
,
input_texts
):
def
compute_average_token_log
likelihood
s_on_batch
(
self
,
input_texts
):
"""
"""
For each input text in the batch, compute the average
logit (
log-likelihood
)
over all tokens.
For each input text in the batch, compute the average log-likelihood over all tokens.
For example, if an input sequence is 3 tokens long, and the token log
it
s are [-1, -2, -3], the "average token log
it
" is -2.
For example, if an input sequence is 3 tokens long, and the token log
likelihood
s are [-1, -2, -3], the "average token log
likelihood
" is -2.
"""
"""
# The ModelRunner can take a big batch on input_texts, and it can be as large as the caller wants.
# The ModelRunner can take a big batch on input_texts, and it can be as large as the caller wants.
# But to prevent the GPU from running out of memory, we need to subdivide the overall batch
# But to prevent the GPU from running out of memory, we need to subdivide the overall batch
...
@@ -138,16 +139,16 @@ class ModelRunner:
...
@@ -138,16 +139,16 @@ class ModelRunner:
# For GPT-2-117M, a GPU can process a batch of roughly 10 or so inputs before the inference latency starts to increase.
# For GPT-2-117M, a GPU can process a batch of roughly 10 or so inputs before the inference latency starts to increase.
gpu_batch_size
=
20
gpu_batch_size
=
20
average_token_log
it
s
=
[]
average_token_log
likelihood
s
=
[]
for
i
in
range
(
0
,
len
(
input_texts
),
gpu_batch_size
):
for
i
in
range
(
0
,
len
(
input_texts
),
gpu_batch_size
):
average_token_log
it
s
.
extend
(
average_token_log
likelihood
s
.
extend
(
self
.
_average_token_log
it
s_on_gpu_batch
(
self
.
_average_token_log
likelihood
s_on_gpu_batch
(
input_texts
[
i
:
i
+
gpu_batch_size
]
input_texts
[
i
:
i
+
gpu_batch_size
]
)
)
)
)
return
average_token_log
it
s
return
average_token_log
likelihood
s
def
_average_token_log
it
s_on_gpu_batch
(
self
,
input_texts
):
def
_average_token_log
likelihood
s_on_gpu_batch
(
self
,
input_texts
):
tokenized_inputs
=
self
.
tokenizer
(
tokenized_inputs
=
self
.
tokenizer
(
input_texts
,
input_texts
,
add_special_tokens
=
False
,
add_special_tokens
=
False
,
...
@@ -164,42 +165,49 @@ class ModelRunner:
...
@@ -164,42 +165,49 @@ class ModelRunner:
output_logits
=
self
.
model
(
tokenized_inputs
).
logits
output_logits
=
self
.
model
(
tokenized_inputs
).
logits
self
.
num_inferences
+=
1
self
.
num_inferences
+=
1
# Align the output logits to the input tokens.
# Normalize probabilities - at each position, the token likelihoods should add up to 1
logits_for_input_positions
=
output_logits
[
output_loglikelihoods
=
F
.
log_softmax
(
output_logits
,
# The embedding dimension
dim
=-
1
,
)
# Align the output loglikelihoods to the input tokens.
loglikelihoods_for_input_positions
=
output_loglikelihoods
[
# The batch dimension
# The batch dimension
:,
:,
# The position dimension
# The position dimension
# The last log
it
needs to be dropped, because it's predicting the "next token", and it doesn't correspond to any input token
# The last log
likelihood
needs to be dropped, because it's predicting the "next token", and it doesn't correspond to any input token
:
-
1
,
:
-
1
,
# The embedding dimension
# The embedding dimension
:,
:,
]
]
input_tokens_at_positions_with_log
it
s
=
tokenized_inputs
[
input_tokens_at_positions_with_log
likelihood
s
=
tokenized_inputs
[
# The batch dimension
# The batch dimension
:,
:,
# The position dimension
# The position dimension
# The model does not predict the first input token, so the first token needs to be dropped.
# The model does not predict the first input token, so the first token needs to be dropped.
1
:,
1
:,
]
]
# At each position, the model outputs ~50k log
it
s, one for every possible token.
# At each position, the model outputs ~50k log
likelihood
s, one for every possible token.
# To get the log
it
s of the tokens that were actually provided, we need to select the right log
it
at each position.
# To get the log
likelihood
s of the tokens that were actually provided, we need to select the right log
likelihood
at each position.
log
it
s_for_provided_tokens
=
torch
.
gather
(
log
likelihood
s_for_provided_tokens
=
torch
.
gather
(
log
it
s_for_input_positions
,
log
likelihood
s_for_input_positions
,
2
,
2
,
input_tokens_at_positions_with_log
it
s
.
unsqueeze
(
2
),
input_tokens_at_positions_with_log
likelihood
s
.
unsqueeze
(
2
),
).
squeeze
(
2
)
).
squeeze
(
2
)
mask_for_non_padded_positions
=
input_tokens_at_positions_with_log
it
s
!=
50256
mask_for_non_padded_positions
=
input_tokens_at_positions_with_log
likelihood
s
!=
50256
average_token_log
it
s
=
(
average_token_log
likelihood
s
=
(
log
it
s_for_provided_tokens
*
mask_for_non_padded_positions
log
likelihood
s_for_provided_tokens
*
mask_for_non_padded_positions
).
sum
(
1
)
/
mask_for_non_padded_positions
.
sum
(
1
)
).
sum
(
1
)
/
mask_for_non_padded_positions
.
sum
(
1
)
average_token_log
it
s
=
average_token_log
it
s
.
tolist
()
average_token_log
likelihood
s
=
average_token_log
likelihood
s
.
tolist
()
end_time
=
time
.
time
()
end_time
=
time
.
time
()
print
(
print
(
f
"Time to evaluate once (inference #
{
self
.
num_inferences
}
):
{
end_time
-
start_time
}
"
f
"Time to evaluate once (inference #
{
self
.
num_inferences
}
):
{
end_time
-
start_time
}
"
)
)
return
average_token_log
it
s
return
average_token_log
likelihood
s
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
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