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
0603564e
Commit
0603564e
authored
Nov 19, 2020
by
Sylvain Gugger
Browse files
Merge remote-tracking branch 'origin/master'
parents
1e08af38
d86b5ffc
Changes
84
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
165 additions
and
25 deletions
+165
-25
tests/test_modeling_tf_dpr.py
tests/test_modeling_tf_dpr.py
+43
-12
tests/test_modeling_tf_electra.py
tests/test_modeling_tf_electra.py
+1
-0
tests/test_modeling_tf_longformer.py
tests/test_modeling_tf_longformer.py
+66
-13
tests/test_trainer.py
tests/test_trainer.py
+55
-0
No files found.
tests/test_modeling_tf_dpr.py
View file @
0603564e
...
...
@@ -12,8 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
tempfile
import
unittest
from
transformers
import
is_tf_available
...
...
@@ -124,8 +123,6 @@ class TFDPRModelTester:
type_vocab_size
=
self
.
type_vocab_size
,
is_decoder
=
False
,
initializer_range
=
self
.
initializer_range
,
# MODIFY
return_dict
=
False
,
)
config
=
DPRConfig
(
projection_dim
=
self
.
projection_dim
,
**
config
.
to_dict
())
...
...
@@ -137,7 +134,7 @@ class TFDPRModelTester:
model
=
TFDPRContextEncoder
(
config
=
config
)
result
=
model
(
input_ids
,
attention_mask
=
input_mask
,
token_type_ids
=
token_type_ids
)
result
=
model
(
input_ids
,
token_type_ids
=
token_type_ids
)
result
=
model
(
input_ids
,
return_dict
=
True
)
# MODIFY
result
=
model
(
input_ids
)
self
.
parent
.
assertEqual
(
result
.
pooler_output
.
shape
,
(
self
.
batch_size
,
self
.
projection_dim
or
self
.
hidden_size
))
def
create_and_check_dpr_question_encoder
(
...
...
@@ -146,14 +143,14 @@ class TFDPRModelTester:
model
=
TFDPRQuestionEncoder
(
config
=
config
)
result
=
model
(
input_ids
,
attention_mask
=
input_mask
,
token_type_ids
=
token_type_ids
)
result
=
model
(
input_ids
,
token_type_ids
=
token_type_ids
)
result
=
model
(
input_ids
,
return_dict
=
True
)
# MODIFY
result
=
model
(
input_ids
)
self
.
parent
.
assertEqual
(
result
.
pooler_output
.
shape
,
(
self
.
batch_size
,
self
.
projection_dim
or
self
.
hidden_size
))
def
create_and_check_dpr_reader
(
self
,
config
,
input_ids
,
token_type_ids
,
input_mask
,
sequence_labels
,
token_labels
,
choice_labels
):
model
=
TFDPRReader
(
config
=
config
)
result
=
model
(
input_ids
,
attention_mask
=
input_mask
,
return_dict
=
True
)
# MODIFY
result
=
model
(
input_ids
,
attention_mask
=
input_mask
)
self
.
parent
.
assertEqual
(
result
.
start_logits
.
shape
,
(
self
.
batch_size
,
self
.
seq_length
))
self
.
parent
.
assertEqual
(
result
.
end_logits
.
shape
,
(
self
.
batch_size
,
self
.
seq_length
))
...
...
@@ -214,27 +211,61 @@ class TFDPRModelTest(TFModelTesterMixin, unittest.TestCase):
@
slow
def
test_model_from_pretrained
(
self
):
for
model_name
in
TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST
[:
1
]:
model
=
TFDPRContextEncoder
.
from_pretrained
(
model_name
,
from_pt
=
True
)
model
=
TFDPRContextEncoder
.
from_pretrained
(
model_name
)
self
.
assertIsNotNone
(
model
)
for
model_name
in
TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST
[:
1
]:
model
=
TFDPRContextEncoder
.
from_pretrained
(
model_name
,
from_pt
=
True
)
model
=
TFDPRContextEncoder
.
from_pretrained
(
model_name
)
self
.
assertIsNotNone
(
model
)
for
model_name
in
TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST
[:
1
]:
model
=
TFDPRQuestionEncoder
.
from_pretrained
(
model_name
,
from_pt
=
True
)
model
=
TFDPRQuestionEncoder
.
from_pretrained
(
model_name
)
self
.
assertIsNotNone
(
model
)
for
model_name
in
TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST
[:
1
]:
model
=
TFDPRReader
.
from_pretrained
(
model_name
,
from_pt
=
True
)
model
=
TFDPRReader
.
from_pretrained
(
model_name
)
self
.
assertIsNotNone
(
model
)
@
slow
def
test_saved_model_with_attentions_output
(
self
):
config
,
inputs_dict
=
self
.
model_tester
.
prepare_config_and_inputs_for_common
()
config
.
output_attentions
=
True
encoder_seq_length
=
getattr
(
self
.
model_tester
,
"encoder_seq_length"
,
self
.
model_tester
.
seq_length
)
encoder_key_length
=
getattr
(
self
.
model_tester
,
"key_length"
,
encoder_seq_length
)
for
model_class
in
self
.
all_model_classes
:
print
(
model_class
)
class_inputs_dict
=
self
.
_prepare_for_class
(
inputs_dict
,
model_class
)
model
=
model_class
(
config
)
num_out
=
len
(
model
(
class_inputs_dict
))
model
.
_saved_model_inputs_spec
=
None
model
.
_set_save_spec
(
class_inputs_dict
)
with
tempfile
.
TemporaryDirectory
()
as
tmpdirname
:
tf
.
saved_model
.
save
(
model
,
tmpdirname
)
model
=
tf
.
keras
.
models
.
load_model
(
tmpdirname
)
outputs
=
model
(
class_inputs_dict
)
if
self
.
is_encoder_decoder
:
output
=
outputs
[
"encoder_attentions"
]
if
isinstance
(
outputs
,
dict
)
else
outputs
[
-
1
]
else
:
output
=
outputs
[
"attentions"
]
if
isinstance
(
outputs
,
dict
)
else
outputs
[
-
1
]
attentions
=
[
t
.
numpy
()
for
t
in
output
]
self
.
assertEqual
(
len
(
outputs
),
num_out
)
self
.
assertEqual
(
len
(
attentions
),
self
.
model_tester
.
num_hidden_layers
)
self
.
assertListEqual
(
list
(
attentions
[
0
].
shape
[
-
3
:]),
[
self
.
model_tester
.
num_attention_heads
,
encoder_seq_length
,
encoder_key_length
],
)
@
require_tf
class
TFDPRModelIntegrationTest
(
unittest
.
TestCase
):
@
slow
def
test_inference_no_head
(
self
):
model
=
TFDPRQuestionEncoder
.
from_pretrained
(
"facebook/dpr-question_encoder-single-nq-base"
,
return_dict
=
False
)
model
=
TFDPRQuestionEncoder
.
from_pretrained
(
"facebook/dpr-question_encoder-single-nq-base"
)
input_ids
=
tf
.
constant
(
[[
101
,
7592
,
1010
,
2003
,
2026
,
3899
,
10140
,
1029
,
102
]]
...
...
tests/test_modeling_tf_electra.py
View file @
0603564e
...
...
@@ -249,6 +249,7 @@ class TFElectraModelTest(TFModelTesterMixin, unittest.TestCase):
self
.
assertIsNotNone
(
model
)
@
require_tf
class
TFElectraModelIntegrationTest
(
unittest
.
TestCase
):
@
slow
def
test_inference_masked_lm
(
self
):
...
...
tests/test_modeling_tf_longformer.py
View file @
0603564e
...
...
@@ -29,7 +29,10 @@ if is_tf_available():
from
transformers
import
(
LongformerConfig
,
TFLongformerForMaskedLM
,
TFLongformerForMultipleChoice
,
TFLongformerForQuestionAnswering
,
TFLongformerForSequenceClassification
,
TFLongformerForTokenClassification
,
TFLongformerModel
,
TFLongformerSelfAttention
,
)
...
...
@@ -130,7 +133,7 @@ class TFLongformerModelTester:
output_without_mask
=
model
(
input_ids
)[
0
]
tf
.
debugging
.
assert_near
(
output_with_mask
[
0
,
0
,
:
5
],
output_without_mask
[
0
,
0
,
:
5
],
rtol
=
1e-4
)
def
create_and_check_
longformer_
model
(
def
create_and_check_model
(
self
,
config
,
input_ids
,
token_type_ids
,
input_mask
,
sequence_labels
,
token_labels
,
choice_labels
):
config
.
return_dict
=
True
...
...
@@ -144,7 +147,7 @@ class TFLongformerModelTester:
)
self
.
parent
.
assertListEqual
(
shape_list
(
result
.
pooler_output
),
[
self
.
batch_size
,
self
.
hidden_size
])
def
create_and_check_
longformer_
model_with_global_attention_mask
(
def
create_and_check_model_with_global_attention_mask
(
self
,
config
,
input_ids
,
token_type_ids
,
input_mask
,
sequence_labels
,
token_labels
,
choice_labels
):
config
.
return_dict
=
True
...
...
@@ -172,7 +175,7 @@ class TFLongformerModelTester:
)
self
.
parent
.
assertListEqual
(
shape_list
(
result
.
pooler_output
),
[
self
.
batch_size
,
self
.
hidden_size
])
def
create_and_check_
longformer_
for_masked_lm
(
def
create_and_check_for_masked_lm
(
self
,
config
,
input_ids
,
token_type_ids
,
input_mask
,
sequence_labels
,
token_labels
,
choice_labels
):
config
.
return_dict
=
True
...
...
@@ -180,7 +183,7 @@ class TFLongformerModelTester:
result
=
model
(
input_ids
,
attention_mask
=
input_mask
,
token_type_ids
=
token_type_ids
,
labels
=
token_labels
)
self
.
parent
.
assertListEqual
(
shape_list
(
result
.
logits
),
[
self
.
batch_size
,
self
.
seq_length
,
self
.
vocab_size
])
def
create_and_check_
longformer_
for_question_answering
(
def
create_and_check_for_question_answering
(
self
,
config
,
input_ids
,
token_type_ids
,
input_mask
,
sequence_labels
,
token_labels
,
choice_labels
):
config
.
return_dict
=
True
...
...
@@ -196,6 +199,41 @@ class TFLongformerModelTester:
self
.
parent
.
assertListEqual
(
shape_list
(
result
.
start_logits
),
[
self
.
batch_size
,
self
.
seq_length
])
self
.
parent
.
assertListEqual
(
shape_list
(
result
.
end_logits
),
[
self
.
batch_size
,
self
.
seq_length
])
def
create_and_check_for_sequence_classification
(
self
,
config
,
input_ids
,
token_type_ids
,
input_mask
,
sequence_labels
,
token_labels
,
choice_labels
):
config
.
num_labels
=
self
.
num_labels
model
=
TFLongformerForSequenceClassification
(
config
=
config
)
output
=
model
(
input_ids
,
attention_mask
=
input_mask
,
token_type_ids
=
token_type_ids
,
labels
=
sequence_labels
).
logits
self
.
parent
.
assertListEqual
(
shape_list
(
output
),
[
self
.
batch_size
,
self
.
num_labels
])
def
create_and_check_for_token_classification
(
self
,
config
,
input_ids
,
token_type_ids
,
input_mask
,
sequence_labels
,
token_labels
,
choice_labels
):
config
.
num_labels
=
self
.
num_labels
model
=
TFLongformerForTokenClassification
(
config
=
config
)
output
=
model
(
input_ids
,
attention_mask
=
input_mask
,
token_type_ids
=
token_type_ids
,
labels
=
token_labels
).
logits
self
.
parent
.
assertListEqual
(
shape_list
(
output
),
[
self
.
batch_size
,
self
.
seq_length
,
self
.
num_labels
])
def
create_and_check_for_multiple_choice
(
self
,
config
,
input_ids
,
token_type_ids
,
input_mask
,
sequence_labels
,
token_labels
,
choice_labels
):
config
.
num_choices
=
self
.
num_choices
model
=
TFLongformerForMultipleChoice
(
config
=
config
)
multiple_choice_inputs_ids
=
tf
.
tile
(
tf
.
expand_dims
(
input_ids
,
1
),
(
1
,
self
.
num_choices
,
1
))
multiple_choice_token_type_ids
=
tf
.
tile
(
tf
.
expand_dims
(
token_type_ids
,
1
),
(
1
,
self
.
num_choices
,
1
))
multiple_choice_input_mask
=
tf
.
tile
(
tf
.
expand_dims
(
input_mask
,
1
),
(
1
,
self
.
num_choices
,
1
))
output
=
model
(
multiple_choice_inputs_ids
,
attention_mask
=
multiple_choice_input_mask
,
global_attention_mask
=
multiple_choice_input_mask
,
token_type_ids
=
multiple_choice_token_type_ids
,
labels
=
choice_labels
,
).
logits
self
.
parent
.
assertListEqual
(
list
(
output
.
shape
),
[
self
.
batch_size
,
self
.
num_choices
])
def
prepare_config_and_inputs_for_common
(
self
):
config_and_inputs
=
self
.
prepare_config_and_inputs
()
(
...
...
@@ -252,6 +290,9 @@ class TFLongformerModelTest(TFModelTesterMixin, unittest.TestCase):
TFLongformerModel
,
TFLongformerForMaskedLM
,
TFLongformerForQuestionAnswering
,
TFLongformerForSequenceClassification
,
TFLongformerForMultipleChoice
,
TFLongformerForTokenClassification
,
)
if
is_tf_available
()
else
()
...
...
@@ -264,25 +305,37 @@ class TFLongformerModelTest(TFModelTesterMixin, unittest.TestCase):
def
test_config
(
self
):
self
.
config_tester
.
run_common_tests
()
def
test_
longformer_
model_attention_mask_determinism
(
self
):
def
test_model_attention_mask_determinism
(
self
):
config_and_inputs
=
self
.
model_tester
.
prepare_config_and_inputs
()
self
.
model_tester
.
create_and_check_attention_mask_determinism
(
*
config_and_inputs
)
def
test_
longformer_
model
(
self
):
def
test_model
(
self
):
config_and_inputs
=
self
.
model_tester
.
prepare_config_and_inputs
()
self
.
model_tester
.
create_and_check_
longformer_
model
(
*
config_and_inputs
)
self
.
model_tester
.
create_and_check_model
(
*
config_and_inputs
)
def
test_
longformer_
model_global_attention_mask
(
self
):
def
test_model_global_attention_mask
(
self
):
config_and_inputs
=
self
.
model_tester
.
prepare_config_and_inputs
()
self
.
model_tester
.
create_and_check_
longformer_
model_with_global_attention_mask
(
*
config_and_inputs
)
self
.
model_tester
.
create_and_check_model_with_global_attention_mask
(
*
config_and_inputs
)
def
test_
longformer_
for_masked_lm
(
self
):
def
test_for_masked_lm
(
self
):
config_and_inputs
=
self
.
model_tester
.
prepare_config_and_inputs
()
self
.
model_tester
.
create_and_check_
longformer_
for_masked_lm
(
*
config_and_inputs
)
self
.
model_tester
.
create_and_check_for_masked_lm
(
*
config_and_inputs
)
def
test_
longformer_
for_question_answering
(
self
):
def
test_for_question_answering
(
self
):
config_and_inputs
=
self
.
model_tester
.
prepare_config_and_inputs_for_question_answering
()
self
.
model_tester
.
create_and_check_longformer_for_question_answering
(
*
config_and_inputs
)
self
.
model_tester
.
create_and_check_for_question_answering
(
*
config_and_inputs
)
def
test_for_sequence_classification
(
self
):
config_and_inputs
=
self
.
model_tester
.
prepare_config_and_inputs
()
self
.
model_tester
.
create_and_check_for_sequence_classification
(
*
config_and_inputs
)
def
test_for_token_classification
(
self
):
config_and_inputs
=
self
.
model_tester
.
prepare_config_and_inputs
()
self
.
model_tester
.
create_and_check_for_token_classification
(
*
config_and_inputs
)
def
test_for_multiple_choice
(
self
):
config_and_inputs
=
self
.
model_tester
.
prepare_config_and_inputs
()
self
.
model_tester
.
create_and_check_for_multiple_choice
(
*
config_and_inputs
)
@
slow
def
test_saved_model_with_attentions_output
(
self
):
...
...
tests/test_trainer.py
View file @
0603564e
...
...
@@ -44,6 +44,8 @@ if is_torch_available():
DataCollatorForLanguageModeling
,
GlueDataset
,
GlueDataTrainingArguments
,
GPT2Config
,
GPT2LMHeadModel
,
LineByLineTextDataset
,
PreTrainedModel
,
TextDataset
,
...
...
@@ -73,6 +75,18 @@ class RegressionDataset:
return
result
class
RepeatDataset
:
def
__init__
(
self
,
x
,
length
=
64
):
self
.
x
=
x
self
.
length
=
length
def
__len__
(
self
):
return
self
.
length
def
__getitem__
(
self
,
i
):
return
{
"input_ids"
:
self
.
x
,
"labels"
:
self
.
x
}
class
DynamicShapesDataset
:
def
__init__
(
self
,
length
=
64
,
seed
=
42
,
batch_size
=
8
):
self
.
length
=
length
...
...
@@ -136,6 +150,20 @@ if is_torch_available():
loss
=
torch
.
nn
.
functional
.
mse_loss
(
y
,
labels
)
return
(
loss
,
y
,
y
)
if
self
.
double_output
else
(
loss
,
y
)
class
RegressionDictModel
(
torch
.
nn
.
Module
):
def
__init__
(
self
,
a
=
0
,
b
=
0
):
super
().
__init__
()
self
.
a
=
torch
.
nn
.
Parameter
(
torch
.
tensor
(
a
).
float
())
self
.
b
=
torch
.
nn
.
Parameter
(
torch
.
tensor
(
b
).
float
())
self
.
config
=
None
def
forward
(
self
,
input_x
=
None
,
labels
=
None
,
**
kwargs
):
y
=
input_x
*
self
.
a
+
self
.
b
result
=
{
"output"
:
y
}
if
labels
is
not
None
:
result
[
"loss"
]
=
torch
.
nn
.
functional
.
mse_loss
(
y
,
labels
)
return
result
class
RegressionPreTrainedModel
(
PreTrainedModel
):
config_class
=
RegressionModelConfig
base_model_prefix
=
"regression"
...
...
@@ -236,6 +264,33 @@ class TrainerIntegrationTest(unittest.TestCase):
metrics
=
trainer
.
evaluate
()
self
.
assertEqual
(
metrics
[
metric
],
best_value
)
def
test_trainer_works_with_dict
(
self
):
# Edge case because Apex with mode O2 will change our models to return dicts. This test checks it doesn't break
# anything.
train_dataset
=
RegressionDataset
()
eval_dataset
=
RegressionDataset
()
model
=
RegressionDictModel
()
args
=
TrainingArguments
(
"./regression"
)
trainer
=
Trainer
(
model
,
args
,
train_dataset
=
train_dataset
,
eval_dataset
=
eval_dataset
)
trainer
.
train
()
_
=
trainer
.
evaluate
()
_
=
trainer
.
predict
(
eval_dataset
)
def
test_evaluation_with_keys_to_drop
(
self
):
config
=
GPT2Config
(
vocab_size
=
100
,
n_positions
=
128
,
n_ctx
=
128
,
n_embd
=
32
,
n_layer
=
3
,
n_head
=
4
)
tiny_gpt2
=
GPT2LMHeadModel
(
config
)
x
=
torch
.
randint
(
0
,
100
,
(
128
,))
eval_dataset
=
RepeatDataset
(
x
)
args
=
TrainingArguments
(
"./test"
)
trainer
=
Trainer
(
tiny_gpt2
,
args
,
eval_dataset
=
eval_dataset
)
# By default the past_key_values are removed
result
=
trainer
.
predict
(
eval_dataset
)
self
.
assertTrue
(
isinstance
(
result
.
predictions
,
np
.
ndarray
))
# We can still get them by setting ignore_keys to []
result
=
trainer
.
predict
(
eval_dataset
,
ignore_keys
=
[])
self
.
assertTrue
(
isinstance
(
result
.
predictions
,
tuple
))
self
.
assertEqual
(
len
(
result
.
predictions
),
2
)
def
test_training_arguments_are_left_untouched
(
self
):
trainer
=
get_regression_trainer
()
trainer
.
train
()
...
...
Prev
1
2
3
4
5
Next
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