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
ab177588
Unverified
Commit
ab177588
authored
Dec 22, 2020
by
Sylvain Gugger
Committed by
GitHub
Dec 22, 2020
Browse files
Add speed metrics to all example scripts + template (#9260)
parent
5b5f7dd0
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
118 additions
and
19 deletions
+118
-19
examples/language-modeling/run_clm.py
examples/language-modeling/run_clm.py
+13
-2
examples/language-modeling/run_mlm.py
examples/language-modeling/run_mlm.py
+13
-2
examples/language-modeling/run_mlm_wwm.py
examples/language-modeling/run_mlm_wwm.py
+13
-2
examples/language-modeling/run_plm.py
examples/language-modeling/run_plm.py
+13
-2
examples/multiple-choice/run_swag.py
examples/multiple-choice/run_swag.py
+13
-2
examples/question-answering/run_qa.py
examples/question-answering/run_qa.py
+13
-2
examples/question-answering/run_qa_beam_search.py
examples/question-answering/run_qa_beam_search.py
+13
-2
examples/token-classification/run_ner.py
examples/token-classification/run_ner.py
+13
-2
templates/adding_a_new_example_script/{{cookiecutter.directory_name}}/run_{{cookiecutter.example_shortcut}}.py
...directory_name}}/run_{{cookiecutter.example_shortcut}}.py
+14
-3
No files found.
examples/language-modeling/run_clm.py
View file @
ab177588
...
...
@@ -341,9 +341,20 @@ def main():
if
(
model_args
.
model_name_or_path
is
not
None
and
os
.
path
.
isdir
(
model_args
.
model_name_or_path
))
else
None
)
trainer
.
train
(
model_path
=
model_path
)
train_result
=
trainer
.
train
(
model_path
=
model_path
)
trainer
.
save_model
()
# Saves the tokenizer too for easy upload
output_train_file
=
os
.
path
.
join
(
training_args
.
output_dir
,
"train_results.txt"
)
if
trainer
.
is_world_process_zero
():
with
open
(
output_train_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Train results *****"
)
for
key
,
value
in
sorted
(
train_result
.
metrics
.
items
()):
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
# Need to save the state, since Trainer.save_model saves only the tokenizer with the model
trainer
.
state
.
save_to_json
(
os
.
path
.
join
(
training_args
.
output_dir
,
"trainer_state.json"
))
# Evaluation
results
=
{}
if
training_args
.
do_eval
:
...
...
@@ -358,7 +369,7 @@ def main():
if
trainer
.
is_world_process_zero
():
with
open
(
output_eval_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Eval results *****"
)
for
key
,
value
in
results
.
items
():
for
key
,
value
in
sorted
(
results
.
items
()
)
:
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
...
...
examples/language-modeling/run_mlm.py
View file @
ab177588
...
...
@@ -376,9 +376,20 @@ def main():
if
(
model_args
.
model_name_or_path
is
not
None
and
os
.
path
.
isdir
(
model_args
.
model_name_or_path
))
else
None
)
trainer
.
train
(
model_path
=
model_path
)
train_result
=
trainer
.
train
(
model_path
=
model_path
)
trainer
.
save_model
()
# Saves the tokenizer too for easy upload
output_train_file
=
os
.
path
.
join
(
training_args
.
output_dir
,
"train_results.txt"
)
if
trainer
.
is_world_process_zero
():
with
open
(
output_train_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Train results *****"
)
for
key
,
value
in
sorted
(
train_result
.
metrics
.
items
()):
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
# Need to save the state, since Trainer.save_model saves only the tokenizer with the model
trainer
.
state
.
save_to_json
(
os
.
path
.
join
(
training_args
.
output_dir
,
"trainer_state.json"
))
# Evaluation
results
=
{}
if
training_args
.
do_eval
:
...
...
@@ -393,7 +404,7 @@ def main():
if
trainer
.
is_world_process_zero
():
with
open
(
output_eval_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Eval results *****"
)
for
key
,
value
in
results
.
items
():
for
key
,
value
in
sorted
(
results
.
items
()
)
:
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
...
...
examples/language-modeling/run_mlm_wwm.py
View file @
ab177588
...
...
@@ -334,9 +334,20 @@ def main():
if
(
model_args
.
model_name_or_path
is
not
None
and
os
.
path
.
isdir
(
model_args
.
model_name_or_path
))
else
None
)
trainer
.
train
(
model_path
=
model_path
)
train_result
=
trainer
.
train
(
model_path
=
model_path
)
trainer
.
save_model
()
# Saves the tokenizer too for easy upload
output_train_file
=
os
.
path
.
join
(
training_args
.
output_dir
,
"train_results.txt"
)
if
trainer
.
is_world_process_zero
():
with
open
(
output_train_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Train results *****"
)
for
key
,
value
in
sorted
(
train_result
.
metrics
.
items
()):
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
# Need to save the state, since Trainer.save_model saves only the tokenizer with the model
trainer
.
state
.
save_to_json
(
os
.
path
.
join
(
training_args
.
output_dir
,
"trainer_state.json"
))
# Evaluation
results
=
{}
if
training_args
.
do_eval
:
...
...
@@ -351,7 +362,7 @@ def main():
if
trainer
.
is_world_process_zero
():
with
open
(
output_eval_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Eval results *****"
)
for
key
,
value
in
results
.
items
():
for
key
,
value
in
sorted
(
results
.
items
()
)
:
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
...
...
examples/language-modeling/run_plm.py
View file @
ab177588
...
...
@@ -363,9 +363,20 @@ def main():
if
(
model_args
.
model_name_or_path
is
not
None
and
os
.
path
.
isdir
(
model_args
.
model_name_or_path
))
else
None
)
trainer
.
train
(
model_path
=
model_path
)
train_result
=
trainer
.
train
(
model_path
=
model_path
)
trainer
.
save_model
()
# Saves the tokenizer too for easy upload
output_train_file
=
os
.
path
.
join
(
training_args
.
output_dir
,
"train_results.txt"
)
if
trainer
.
is_world_process_zero
():
with
open
(
output_train_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Train results *****"
)
for
key
,
value
in
sorted
(
train_result
.
metrics
.
items
()):
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
# Need to save the state, since Trainer.save_model saves only the tokenizer with the model
trainer
.
state
.
save_to_json
(
os
.
path
.
join
(
training_args
.
output_dir
,
"trainer_state.json"
))
# Evaluation
results
=
{}
if
training_args
.
do_eval
:
...
...
@@ -380,7 +391,7 @@ def main():
if
trainer
.
is_world_process_zero
():
with
open
(
output_eval_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Eval results *****"
)
for
key
,
value
in
results
.
items
():
for
key
,
value
in
sorted
(
results
.
items
()
)
:
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
...
...
examples/multiple-choice/run_swag.py
View file @
ab177588
...
...
@@ -317,11 +317,22 @@ def main():
# Training
if
training_args
.
do_train
:
trainer
.
train
(
train_result
=
trainer
.
train
(
model_path
=
model_args
.
model_name_or_path
if
os
.
path
.
isdir
(
model_args
.
model_name_or_path
)
else
None
)
trainer
.
save_model
()
# Saves the tokenizer too for easy upload
output_train_file
=
os
.
path
.
join
(
training_args
.
output_dir
,
"train_results.txt"
)
if
trainer
.
is_world_process_zero
():
with
open
(
output_train_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Train results *****"
)
for
key
,
value
in
sorted
(
train_result
.
metrics
.
items
()):
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
# Need to save the state, since Trainer.save_model saves only the tokenizer with the model
trainer
.
state
.
save_to_json
(
os
.
path
.
join
(
training_args
.
output_dir
,
"trainer_state.json"
))
# Evaluation
results
=
{}
if
training_args
.
do_eval
:
...
...
@@ -333,7 +344,7 @@ def main():
if
trainer
.
is_world_process_zero
():
with
open
(
output_eval_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Eval results *****"
)
for
key
,
value
in
results
.
items
():
for
key
,
value
in
sorted
(
results
.
items
()
)
:
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
...
...
examples/question-answering/run_qa.py
View file @
ab177588
...
...
@@ -438,11 +438,22 @@ def main():
# Training
if
training_args
.
do_train
:
trainer
.
train
(
train_result
=
trainer
.
train
(
model_path
=
model_args
.
model_name_or_path
if
os
.
path
.
isdir
(
model_args
.
model_name_or_path
)
else
None
)
trainer
.
save_model
()
# Saves the tokenizer too for easy upload
output_train_file
=
os
.
path
.
join
(
training_args
.
output_dir
,
"train_results.txt"
)
if
trainer
.
is_world_process_zero
():
with
open
(
output_train_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Train results *****"
)
for
key
,
value
in
sorted
(
train_result
.
metrics
.
items
()):
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
# Need to save the state, since Trainer.save_model saves only the tokenizer with the model
trainer
.
state
.
save_to_json
(
os
.
path
.
join
(
training_args
.
output_dir
,
"trainer_state.json"
))
# Evaluation
results
=
{}
if
training_args
.
do_eval
:
...
...
@@ -453,7 +464,7 @@ def main():
if
trainer
.
is_world_process_zero
():
with
open
(
output_eval_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Eval results *****"
)
for
key
,
value
in
results
.
items
():
for
key
,
value
in
sorted
(
results
.
items
()
)
:
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
...
...
examples/question-answering/run_qa_beam_search.py
View file @
ab177588
...
...
@@ -481,11 +481,22 @@ def main():
# Training
if
training_args
.
do_train
:
trainer
.
train
(
train_result
=
trainer
.
train
(
model_path
=
model_args
.
model_name_or_path
if
os
.
path
.
isdir
(
model_args
.
model_name_or_path
)
else
None
)
trainer
.
save_model
()
# Saves the tokenizer too for easy upload
output_train_file
=
os
.
path
.
join
(
training_args
.
output_dir
,
"train_results.txt"
)
if
trainer
.
is_world_process_zero
():
with
open
(
output_train_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Train results *****"
)
for
key
,
value
in
sorted
(
train_result
.
metrics
.
items
()):
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
# Need to save the state, since Trainer.save_model saves only the tokenizer with the model
trainer
.
state
.
save_to_json
(
os
.
path
.
join
(
training_args
.
output_dir
,
"trainer_state.json"
))
# Evaluation
results
=
{}
if
training_args
.
do_eval
:
...
...
@@ -496,7 +507,7 @@ def main():
if
trainer
.
is_world_process_zero
():
with
open
(
output_eval_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Eval results *****"
)
for
key
,
value
in
results
.
items
():
for
key
,
value
in
sorted
(
results
.
items
()
)
:
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
...
...
examples/token-classification/run_ner.py
View file @
ab177588
...
...
@@ -340,11 +340,22 @@ def main():
# Training
if
training_args
.
do_train
:
trainer
.
train
(
train_result
=
trainer
.
train
(
model_path
=
model_args
.
model_name_or_path
if
os
.
path
.
isdir
(
model_args
.
model_name_or_path
)
else
None
)
trainer
.
save_model
()
# Saves the tokenizer too for easy upload
output_train_file
=
os
.
path
.
join
(
training_args
.
output_dir
,
"train_results.txt"
)
if
trainer
.
is_world_process_zero
():
with
open
(
output_train_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Train results *****"
)
for
key
,
value
in
sorted
(
train_result
.
metrics
.
items
()):
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
# Need to save the state, since Trainer.save_model saves only the tokenizer with the model
trainer
.
state
.
save_to_json
(
os
.
path
.
join
(
training_args
.
output_dir
,
"trainer_state.json"
))
# Evaluation
results
=
{}
if
training_args
.
do_eval
:
...
...
@@ -377,7 +388,7 @@ def main():
output_test_results_file
=
os
.
path
.
join
(
training_args
.
output_dir
,
"test_results.txt"
)
if
trainer
.
is_world_process_zero
():
with
open
(
output_test_results_file
,
"w"
)
as
writer
:
for
key
,
value
in
metrics
.
items
():
for
key
,
value
in
sorted
(
metrics
.
items
()
)
:
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
...
...
templates/adding_a_new_example_script/{{cookiecutter.directory_name}}/run_{{cookiecutter.example_shortcut}}.py
View file @
ab177588
...
...
@@ -308,7 +308,7 @@ def main():
# Training
if
training_args
.
do_train
:
{
%-
if
cookiecutter
.
can_train_from_scratch
==
"False"
%
}
trainer
.
train
(
train_result
=
trainer
.
train
(
model_path
=
model_args
.
model_name_or_path
if
os
.
path
.
isdir
(
model_args
.
model_name_or_path
)
else
None
)
{
%-
elif
cookiecutter
.
can_train_from_scratch
==
"True"
%
}
...
...
@@ -317,10 +317,21 @@ def main():
if
(
model_args
.
model_name_or_path
is
not
None
and
os
.
path
.
isdir
(
model_args
.
model_name_or_path
))
else
None
)
trainer
.
train
(
model_path
=
model_path
)
train_result
=
trainer
.
train
(
model_path
=
model_path
)
{
%
endif
%
}
trainer
.
save_model
()
# Saves the tokenizer too for easy upload
output_train_file
=
os
.
path
.
join
(
training_args
.
output_dir
,
"train_results.txt"
)
if
trainer
.
is_world_process_zero
():
with
open
(
output_train_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Train results *****"
)
for
key
,
value
in
sorted
(
train_result
.
metrics
.
items
()):
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
# Need to save the state, since Trainer.save_model saves only the tokenizer with the model
trainer
.
state
.
save_to_json
(
os
.
path
.
join
(
training_args
.
output_dir
,
"trainer_state.json"
))
# Evaluation
results
=
{}
if
training_args
.
do_eval
:
...
...
@@ -332,7 +343,7 @@ def main():
if
trainer
.
is_world_process_zero
():
with
open
(
output_eval_file
,
"w"
)
as
writer
:
logger
.
info
(
"***** Eval results *****"
)
for
key
,
value
in
results
.
items
():
for
key
,
value
in
sorted
(
results
.
items
()
)
:
logger
.
info
(
f
"
{
key
}
=
{
value
}
"
)
writer
.
write
(
f
"
{
key
}
=
{
value
}
\n
"
)
...
...
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