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
ce50305e
Unverified
Commit
ce50305e
authored
Dec 22, 2019
by
Aymeric Augustin
Committed by
GitHub
Dec 22, 2019
Browse files
Merge pull request #2270 from aaugustin/remove-python-2
Remove support for Python 2
parents
b6ea0f43
1a948d70
Changes
155
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
28 additions
and
59 deletions
+28
-59
templates/adding_a_new_example_script/utils_xxx.py
templates/adding_a_new_example_script/utils_xxx.py
+0
-2
templates/adding_a_new_model/configuration_xxx.py
templates/adding_a_new_model/configuration_xxx.py
+0
-1
templates/adding_a_new_model/convert_xxx_original_tf_checkpoint_to_pytorch.py
...ew_model/convert_xxx_original_tf_checkpoint_to_pytorch.py
+0
-1
templates/adding_a_new_model/modeling_tf_xxx.py
templates/adding_a_new_model/modeling_tf_xxx.py
+0
-1
templates/adding_a_new_model/modeling_xxx.py
templates/adding_a_new_model/modeling_xxx.py
+0
-1
templates/adding_a_new_model/tests/test_modeling_tf_xxx.py
templates/adding_a_new_model/tests/test_modeling_tf_xxx.py
+1
-1
templates/adding_a_new_model/tests/test_modeling_xxx.py
templates/adding_a_new_model/tests/test_modeling_xxx.py
+1
-1
templates/adding_a_new_model/tests/test_tokenization_xxx.py
templates/adding_a_new_model/tests/test_tokenization_xxx.py
+1
-2
templates/adding_a_new_model/tokenization_xxx.py
templates/adding_a_new_model/tokenization_xxx.py
+2
-4
tests/test_configuration_common.py
tests/test_configuration_common.py
+4
-5
tests/test_hf_api.py
tests/test_hf_api.py
+3
-4
tests/test_model_card.py
tests/test_model_card.py
+4
-5
tests/test_modeling_albert.py
tests/test_modeling_albert.py
+1
-1
tests/test_modeling_auto.py
tests/test_modeling_auto.py
+1
-1
tests/test_modeling_bert.py
tests/test_modeling_bert.py
+1
-1
tests/test_modeling_common.py
tests/test_modeling_common.py
+5
-24
tests/test_modeling_ctrl.py
tests/test_modeling_ctrl.py
+1
-1
tests/test_modeling_distilbert.py
tests/test_modeling_distilbert.py
+1
-1
tests/test_modeling_gpt2.py
tests/test_modeling_gpt2.py
+1
-1
tests/test_modeling_openai.py
tests/test_modeling_openai.py
+1
-1
No files found.
templates/adding_a_new_example_script/utils_xxx.py
View file @
ce50305e
...
...
@@ -14,13 +14,11 @@
# limitations under the License.
""" Load XXX dataset. """
from
__future__
import
absolute_import
,
division
,
print_function
import
collections
import
json
import
logging
import
math
from
io
import
open
from
transformers.tokenization_bert
import
BasicTokenizer
,
whitespace_tokenize
...
...
templates/adding_a_new_model/configuration_xxx.py
View file @
ce50305e
...
...
@@ -14,7 +14,6 @@
# limitations under the License.
""" XXX model configuration """
from
__future__
import
absolute_import
,
division
,
print_function
,
unicode_literals
import
logging
...
...
templates/adding_a_new_model/convert_xxx_original_tf_checkpoint_to_pytorch.py
View file @
ce50305e
...
...
@@ -14,7 +14,6 @@
# limitations under the License.
"""Convert XXX checkpoint."""
from
__future__
import
absolute_import
,
division
,
print_function
import
argparse
import
logging
...
...
templates/adding_a_new_model/modeling_tf_xxx.py
View file @
ce50305e
...
...
@@ -19,7 +19,6 @@
# In this template, replace all the XXX (various casings) with your model name
####################################################
from
__future__
import
absolute_import
,
division
,
print_function
,
unicode_literals
import
logging
...
...
templates/adding_a_new_model/modeling_xxx.py
View file @
ce50305e
...
...
@@ -18,7 +18,6 @@
# In this template, replace all the XXX (various casings) with your model name
####################################################
from
__future__
import
absolute_import
,
division
,
print_function
,
unicode_literals
import
logging
import
os
...
...
templates/adding_a_new_model/tests/test_modeling_tf_xxx.py
View file @
ce50305e
...
...
@@ -12,7 +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.
from
__future__
import
absolute_import
,
division
,
print_function
import
unittest
...
...
templates/adding_a_new_model/tests/test_modeling_xxx.py
View file @
ce50305e
...
...
@@ -12,7 +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.
from
__future__
import
absolute_import
,
division
,
print_function
import
unittest
...
...
templates/adding_a_new_model/tests/test_tokenization_xxx.py
View file @
ce50305e
...
...
@@ -12,11 +12,10 @@
# 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.
from
__future__
import
absolute_import
,
division
,
print_function
,
unicode_literals
import
os
import
unittest
from
io
import
open
from
transformers.tokenization_bert
import
VOCAB_FILES_NAMES
,
XxxTokenizer
...
...
templates/adding_a_new_model/tokenization_xxx.py
View file @
ce50305e
...
...
@@ -14,12 +14,10 @@
# limitations under the License.
""" Tokenization class for model XXX."""
from
__future__
import
absolute_import
,
division
,
print_function
,
unicode_literals
import
collections
import
logging
import
os
from
io
import
open
from
.tokenization_utils
import
PreTrainedTokenizer
...
...
@@ -146,11 +144,11 @@ class XxxTokenizer(PreTrainedTokenizer):
return
split_tokens
def
_convert_token_to_id
(
self
,
token
):
""" Converts a token (str
/unicode
) in an id using the vocab. """
""" Converts a token (str) in an id using the vocab. """
return
self
.
vocab
.
get
(
token
,
self
.
vocab
.
get
(
self
.
unk_token
))
def
_convert_id_to_token
(
self
,
index
):
"""Converts an index (integer) in a token (str
ing/unicode
) using the vocab."""
"""Converts an index (integer) in a token (str) using the vocab."""
return
self
.
ids_to_tokens
.
get
(
index
,
self
.
unk_token
)
def
convert_tokens_to_string
(
self
,
tokens
):
...
...
tests/test_configuration_common.py
View file @
ce50305e
...
...
@@ -12,12 +12,11 @@
# 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.
from
__future__
import
absolute_import
,
division
,
print_function
import
json
import
os
from
.test_tokenization_common
import
TemporaryDirectory
import
tempfile
class
ConfigTester
(
object
):
...
...
@@ -42,7 +41,7 @@ class ConfigTester(object):
def
create_and_test_config_to_json_file
(
self
):
config_first
=
self
.
config_class
(
**
self
.
inputs_dict
)
with
TemporaryDirectory
()
as
tmpdirname
:
with
tempfile
.
TemporaryDirectory
()
as
tmpdirname
:
json_file_path
=
os
.
path
.
join
(
tmpdirname
,
"config.json"
)
config_first
.
to_json_file
(
json_file_path
)
config_second
=
self
.
config_class
.
from_json_file
(
json_file_path
)
...
...
@@ -52,7 +51,7 @@ class ConfigTester(object):
def
create_and_test_config_from_and_save_pretrained
(
self
):
config_first
=
self
.
config_class
(
**
self
.
inputs_dict
)
with
TemporaryDirectory
()
as
tmpdirname
:
with
tempfile
.
TemporaryDirectory
()
as
tmpdirname
:
config_first
.
save_pretrained
(
tmpdirname
)
config_second
=
self
.
config_class
.
from_pretrained
(
tmpdirname
)
...
...
tests/test_hf_api.py
View file @
ce50305e
...
...
@@ -12,14 +12,13 @@
# 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.
from
__future__
import
absolute_import
,
division
,
print_function
import
os
import
time
import
unittest
import
requests
import
six
from
requests.exceptions
import
HTTPError
from
transformers.hf_api
import
HfApi
,
HfFolder
,
PresignedUrl
,
S3Obj
...
...
@@ -50,7 +49,7 @@ class HfApiLoginTest(HfApiCommonTest):
def
test_login_valid
(
self
):
token
=
self
.
_api
.
login
(
username
=
USER
,
password
=
PASS
)
self
.
assertIsInstance
(
token
,
s
ix
.
string_types
)
self
.
assertIsInstance
(
token
,
s
tr
)
class
HfApiEndpointsTest
(
HfApiCommonTest
):
...
...
@@ -74,7 +73,7 @@ class HfApiEndpointsTest(HfApiCommonTest):
def
test_presign_and_upload
(
self
):
for
FILE_KEY
,
FILE_PATH
in
FILES
:
access_url
=
self
.
_api
.
presign_and_upload
(
token
=
self
.
_token
,
filename
=
FILE_KEY
,
filepath
=
FILE_PATH
)
self
.
assertIsInstance
(
access_url
,
s
ix
.
string_types
)
self
.
assertIsInstance
(
access_url
,
s
tr
)
with
open
(
FILE_PATH
,
"r"
)
as
f
:
body
=
f
.
read
()
r
=
requests
.
get
(
access_url
)
...
...
tests/test_model_card.py
View file @
ce50305e
...
...
@@ -12,16 +12,15 @@
# 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.
from
__future__
import
absolute_import
,
division
,
print_function
,
unicode_literals
import
json
import
os
import
tempfile
import
unittest
from
transformers.modelcard
import
ModelCard
from
.test_tokenization_common
import
TemporaryDirectory
class
ModelCardTester
(
unittest
.
TestCase
):
def
setUp
(
self
):
...
...
@@ -65,7 +64,7 @@ class ModelCardTester(unittest.TestCase):
def
test_model_card_to_json_file
(
self
):
model_card_first
=
ModelCard
.
from_dict
(
self
.
inputs_dict
)
with
TemporaryDirectory
()
as
tmpdirname
:
with
tempfile
.
TemporaryDirectory
()
as
tmpdirname
:
filename
=
os
.
path
.
join
(
tmpdirname
,
"modelcard.json"
)
model_card_first
.
to_json_file
(
filename
)
model_card_second
=
ModelCard
.
from_json_file
(
filename
)
...
...
@@ -75,7 +74,7 @@ class ModelCardTester(unittest.TestCase):
def
test_model_card_from_and_save_pretrained
(
self
):
model_card_first
=
ModelCard
.
from_dict
(
self
.
inputs_dict
)
with
TemporaryDirectory
()
as
tmpdirname
:
with
tempfile
.
TemporaryDirectory
()
as
tmpdirname
:
model_card_first
.
save_pretrained
(
tmpdirname
)
model_card_second
=
ModelCard
.
from_pretrained
(
tmpdirname
)
...
...
tests/test_modeling_albert.py
View file @
ce50305e
...
...
@@ -12,7 +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.
from
__future__
import
absolute_import
,
division
,
print_function
import
unittest
...
...
tests/test_modeling_auto.py
View file @
ce50305e
...
...
@@ -12,7 +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.
from
__future__
import
absolute_import
,
division
,
print_function
import
logging
import
unittest
...
...
tests/test_modeling_bert.py
View file @
ce50305e
...
...
@@ -12,7 +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.
from
__future__
import
absolute_import
,
division
,
print_function
import
unittest
...
...
tests/test_modeling_common.py
View file @
ce50305e
...
...
@@ -12,15 +12,13 @@
# 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.
from
__future__
import
absolute_import
,
division
,
print_function
import
copy
import
json
import
logging
import
os.path
import
random
import
shutil
import
sys
import
tempfile
import
unittest
import
uuid
...
...
@@ -43,23 +41,6 @@ if is_torch_available():
BERT_PRETRAINED_MODEL_ARCHIVE_MAP
,
)
if
sys
.
version_info
[
0
]
==
2
:
class
TemporaryDirectory
(
object
):
"""Context manager for tempfile.mkdtemp() so it's usable with "with" statement."""
def
__enter__
(
self
):
self
.
name
=
tempfile
.
mkdtemp
()
return
self
.
name
def
__exit__
(
self
,
exc_type
,
exc_value
,
traceback
):
shutil
.
rmtree
(
self
.
name
)
else
:
TemporaryDirectory
=
tempfile
.
TemporaryDirectory
unicode
=
str
def
_config_zero_init
(
config
):
configs_no_init
=
copy
.
deepcopy
(
config
)
...
...
@@ -92,7 +73,7 @@ class ModelTesterMixin:
out_2
=
outputs
[
0
].
numpy
()
out_2
[
np
.
isnan
(
out_2
)]
=
0
with
TemporaryDirectory
()
as
tmpdirname
:
with
tempfile
.
TemporaryDirectory
()
as
tmpdirname
:
model
.
save_pretrained
(
tmpdirname
)
model
=
model_class
.
from_pretrained
(
tmpdirname
)
model
.
to
(
torch_device
)
...
...
@@ -238,7 +219,7 @@ class ModelTesterMixin:
except
RuntimeError
:
self
.
fail
(
"Couldn't trace module."
)
with
TemporaryDirectory
()
as
tmp_dir_name
:
with
tempfile
.
TemporaryDirectory
()
as
tmp_dir_name
:
pt_file_name
=
os
.
path
.
join
(
tmp_dir_name
,
"traced_model.pt"
)
try
:
...
...
@@ -366,7 +347,7 @@ class ModelTesterMixin:
heads_to_prune
=
{
0
:
list
(
range
(
1
,
self
.
model_tester
.
num_attention_heads
)),
-
1
:
[
0
]}
model
.
prune_heads
(
heads_to_prune
)
with
TemporaryDirectory
()
as
temp_dir_name
:
with
tempfile
.
TemporaryDirectory
()
as
temp_dir_name
:
model
.
save_pretrained
(
temp_dir_name
)
model
=
model_class
.
from_pretrained
(
temp_dir_name
)
model
.
to
(
torch_device
)
...
...
@@ -435,7 +416,7 @@ class ModelTesterMixin:
self
.
assertEqual
(
attentions
[
2
].
shape
[
-
3
],
self
.
model_tester
.
num_attention_heads
)
self
.
assertEqual
(
attentions
[
3
].
shape
[
-
3
],
self
.
model_tester
.
num_attention_heads
)
with
TemporaryDirectory
()
as
temp_dir_name
:
with
tempfile
.
TemporaryDirectory
()
as
temp_dir_name
:
model
.
save_pretrained
(
temp_dir_name
)
model
=
model_class
.
from_pretrained
(
temp_dir_name
)
model
.
to
(
torch_device
)
...
...
tests/test_modeling_ctrl.py
View file @
ce50305e
...
...
@@ -11,7 +11,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.
from
__future__
import
absolute_import
,
division
,
print_function
import
unittest
...
...
tests/test_modeling_distilbert.py
View file @
ce50305e
...
...
@@ -12,7 +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.
from
__future__
import
absolute_import
,
division
,
print_function
import
unittest
...
...
tests/test_modeling_gpt2.py
View file @
ce50305e
...
...
@@ -12,7 +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.
from
__future__
import
absolute_import
,
division
,
print_function
import
unittest
...
...
tests/test_modeling_openai.py
View file @
ce50305e
...
...
@@ -12,7 +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.
from
__future__
import
absolute_import
,
division
,
print_function
import
unittest
...
...
Prev
1
2
3
4
5
6
7
8
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