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
27e015bd
Commit
27e015bd
authored
Nov 06, 2019
by
Julien Chaumond
Browse files
[tests] Flag to test on cuda
parent
13d9135f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
7 deletions
+22
-7
transformers/tests/conftest.py
transformers/tests/conftest.py
+8
-0
transformers/tests/modeling_bert_test.py
transformers/tests/modeling_bert_test.py
+14
-7
No files found.
transformers/tests/conftest.py
View file @
27e015bd
...
...
@@ -7,6 +7,9 @@ def pytest_addoption(parser):
parser
.
addoption
(
"--runslow"
,
action
=
"store_true"
,
default
=
False
,
help
=
"run slow tests"
)
parser
.
addoption
(
"--use_cuda"
,
action
=
"store_true"
,
default
=
False
,
help
=
"run tests on gpu"
)
def
pytest_configure
(
config
):
...
...
@@ -21,3 +24,8 @@ def pytest_collection_modifyitems(config, items):
for
item
in
items
:
if
"slow"
in
item
.
keywords
:
item
.
add_marker
(
skip_slow
)
@
pytest
.
fixture
def
use_cuda
(
request
):
""" Run test on gpu """
return
request
.
config
.
getoption
(
"--use_cuda"
)
transformers/tests/modeling_bert_test.py
View file @
27e015bd
...
...
@@ -35,6 +35,7 @@ else:
pytestmark
=
pytest
.
mark
.
skip
(
"Require Torch"
)
@
pytest
.
mark
.
usefixtures
(
"use_cuda"
)
class
BertModelTest
(
CommonTestCases
.
CommonModelTester
):
all_model_classes
=
(
BertModel
,
BertForMaskedLM
,
BertForNextSentencePrediction
,
...
...
@@ -66,6 +67,7 @@ class BertModelTest(CommonTestCases.CommonModelTester):
num_labels
=
3
,
num_choices
=
4
,
scope
=
None
,
device
=
'cpu'
,
):
self
.
parent
=
parent
self
.
batch_size
=
batch_size
...
...
@@ -89,25 +91,26 @@ class BertModelTest(CommonTestCases.CommonModelTester):
self
.
num_labels
=
num_labels
self
.
num_choices
=
num_choices
self
.
scope
=
scope
self
.
device
=
device
def
prepare_config_and_inputs
(
self
):
input_ids
=
ids_tensor
([
self
.
batch_size
,
self
.
seq_length
],
self
.
vocab_size
)
input_ids
=
ids_tensor
([
self
.
batch_size
,
self
.
seq_length
],
self
.
vocab_size
)
.
to
(
self
.
device
)
input_mask
=
None
if
self
.
use_input_mask
:
input_mask
=
ids_tensor
([
self
.
batch_size
,
self
.
seq_length
],
vocab_size
=
2
)
input_mask
=
ids_tensor
([
self
.
batch_size
,
self
.
seq_length
],
vocab_size
=
2
)
.
to
(
self
.
device
)
token_type_ids
=
None
if
self
.
use_token_type_ids
:
token_type_ids
=
ids_tensor
([
self
.
batch_size
,
self
.
seq_length
],
self
.
type_vocab_size
)
token_type_ids
=
ids_tensor
([
self
.
batch_size
,
self
.
seq_length
],
self
.
type_vocab_size
)
.
to
(
self
.
device
)
sequence_labels
=
None
token_labels
=
None
choice_labels
=
None
if
self
.
use_labels
:
sequence_labels
=
ids_tensor
([
self
.
batch_size
],
self
.
type_sequence_label_size
)
token_labels
=
ids_tensor
([
self
.
batch_size
,
self
.
seq_length
],
self
.
num_labels
)
choice_labels
=
ids_tensor
([
self
.
batch_size
],
self
.
num_choices
)
sequence_labels
=
ids_tensor
([
self
.
batch_size
],
self
.
type_sequence_label_size
)
.
to
(
self
.
device
)
token_labels
=
ids_tensor
([
self
.
batch_size
,
self
.
seq_length
],
self
.
num_labels
)
.
to
(
self
.
device
)
choice_labels
=
ids_tensor
([
self
.
batch_size
],
self
.
num_choices
)
.
to
(
self
.
device
)
config
=
BertConfig
(
vocab_size_or_config_json_file
=
self
.
vocab_size
,
...
...
@@ -141,6 +144,7 @@ class BertModelTest(CommonTestCases.CommonModelTester):
def
create_and_check_bert_model
(
self
,
config
,
input_ids
,
token_type_ids
,
input_mask
,
sequence_labels
,
token_labels
,
choice_labels
):
model
=
BertModel
(
config
=
config
)
model
.
to
(
input_ids
.
device
)
model
.
eval
()
sequence_output
,
pooled_output
=
model
(
input_ids
,
attention_mask
=
input_mask
,
token_type_ids
=
token_type_ids
)
sequence_output
,
pooled_output
=
model
(
input_ids
,
token_type_ids
=
token_type_ids
)
...
...
@@ -309,7 +313,10 @@ class BertModelTest(CommonTestCases.CommonModelTester):
def
test_config
(
self
):
self
.
config_tester
.
run_common_tests
()
def
test_bert_model
(
self
):
def
test_bert_model
(
self
,
use_cuda
=
False
):
# ^^ This could be a real fixture
if
use_cuda
:
self
.
model_tester
.
device
=
"cuda"
config_and_inputs
=
self
.
model_tester
.
prepare_config_and_inputs
()
self
.
model_tester
.
create_and_check_bert_model
(
*
config_and_inputs
)
...
...
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