Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc21aaca78 | ||
|
|
38d0ee2ff2 | ||
|
|
791dbf9af4 | ||
|
|
56c2c58519 | ||
|
|
35807511ee | ||
|
|
2c8b508cca | ||
|
|
654c584f38 | ||
|
|
6d034d58c5 | ||
|
|
af20bbb318 | ||
|
|
1504b5311a | ||
|
|
defd039bae |
@@ -249,6 +249,8 @@ conda install -c huggingface transformers
|
||||
|
||||
Follow the installation pages of Flax, PyTorch or TensorFlow to see how to install them with conda.
|
||||
|
||||
> **_NOTE:_** On Windows, you may be prompted to activate Developer Mode in order to benefit from caching. If this is not an option for you, please let us know in [this issue](https://github.com/huggingface/huggingface_hub/issues/1062).
|
||||
|
||||
## Model architectures
|
||||
|
||||
**[All the model checkpoints](https://huggingface.co/models)** provided by 🤗 Transformers are seamlessly integrated from the huggingface.co [model hub](https://huggingface.co) where they are uploaded directly by [users](https://huggingface.co/users) and [organizations](https://huggingface.co/organizations).
|
||||
|
||||
2
setup.py
2
setup.py
@@ -400,7 +400,7 @@ install_requires = [
|
||||
|
||||
setup(
|
||||
name="transformers",
|
||||
version="4.22.0", # expected format is one of x.y.z.dev0, or x.y.z.rc1 or x.y.z (no to dashes, yes to dots)
|
||||
version="4.22.2", # expected format is one of x.y.z.dev0, or x.y.z.rc1 or x.y.z (no to dashes, yes to dots)
|
||||
author="The Hugging Face team (past and future) with the help of all our contributors (https://github.com/huggingface/transformers/graphs/contributors)",
|
||||
author_email="transformers@huggingface.co",
|
||||
description="State-of-the-art Machine Learning for JAX, PyTorch and TensorFlow",
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
# to defer the actual importing for when the objects are requested. This way `import transformers` provides the names
|
||||
# in the namespace without actually importing anything (and especially none of the backends).
|
||||
|
||||
__version__ = "4.22.0"
|
||||
__version__ = "4.22.2"
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
|
||||
@@ -282,8 +282,20 @@ class GPT2Converter(Converter):
|
||||
|
||||
tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=self.original_tokenizer.add_prefix_space)
|
||||
tokenizer.decoder = decoders.ByteLevel()
|
||||
tokenizer.post_processor = processors.ByteLevel(trim_offsets=False)
|
||||
|
||||
if self.original_tokenizer.add_bos_token:
|
||||
bos = self.original_tokenizer.bos_token
|
||||
bos_token_id = self.original_tokenizer.bos_token_id
|
||||
tokenizer.post_processor = processors.TemplateProcessing(
|
||||
single=f"{bos}:0 $A:0", # token_type_id is 2 for Funnel transformer
|
||||
pair=f"{bos}:0 $A:0 $B:1",
|
||||
special_tokens=[
|
||||
(bos, bos_token_id),
|
||||
],
|
||||
)
|
||||
else:
|
||||
# XXX trim_offsets=False actually means this post_processor doesn't
|
||||
# really do anything.
|
||||
tokenizer.post_processor = processors.ByteLevel(trim_offsets=False)
|
||||
return tokenizer
|
||||
|
||||
|
||||
|
||||
@@ -680,7 +680,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin):
|
||||
archive_file = pretrained_model_name_or_path
|
||||
is_local = True
|
||||
elif is_remote_url(pretrained_model_name_or_path):
|
||||
archive_file = pretrained_model_name_or_path
|
||||
filename = pretrained_model_name_or_path
|
||||
resolved_archive_file = download_url(pretrained_model_name_or_path)
|
||||
else:
|
||||
filename = WEIGHTS_NAME if from_pt else FLAX_WEIGHTS_NAME
|
||||
|
||||
@@ -2348,7 +2348,7 @@ class TFPreTrainedModel(tf.keras.Model, TFModelUtilsMixin, TFGenerationMixin, Pu
|
||||
archive_file = pretrained_model_name_or_path + ".index"
|
||||
is_local = True
|
||||
elif is_remote_url(pretrained_model_name_or_path):
|
||||
archive_file = pretrained_model_name_or_path
|
||||
filename = pretrained_model_name_or_path
|
||||
resolved_archive_file = download_url(pretrained_model_name_or_path)
|
||||
else:
|
||||
# set correct filename
|
||||
|
||||
@@ -2001,7 +2001,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix
|
||||
archive_file = os.path.join(subfolder, pretrained_model_name_or_path + ".index")
|
||||
is_local = True
|
||||
elif is_remote_url(pretrained_model_name_or_path):
|
||||
archive_file = pretrained_model_name_or_path
|
||||
filename = pretrained_model_name_or_path
|
||||
resolved_archive_file = download_url(pretrained_model_name_or_path)
|
||||
else:
|
||||
# set correct filename
|
||||
|
||||
@@ -146,16 +146,7 @@ class GPT2TokenizerFast(PreTrainedTokenizerFast):
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
if kwargs.pop("add_bos_token", False):
|
||||
model_id = kwargs.pop("name_or_path", "")
|
||||
raise ValueError(
|
||||
"Currenty GPT2's fast tokenizer does NOT support adding a BOS token."
|
||||
"Instead you should use GPT2's slow tokenizer class `GPT2Tokenizer` as follows: \n"
|
||||
f"`GPT2Tokenizer.from_pretrained('{model_id}')`\nor\n"
|
||||
f"`AutoTokenizer.from_pretrained('{model_id}', use_fast=False)`\n"
|
||||
"This issue will be fixed soon, see: https://github.com/huggingface/tokenizers/pull/1005."
|
||||
" so that the fast tokenizer works correctly."
|
||||
)
|
||||
self.add_bos_token = kwargs.pop("add_bos_token", False)
|
||||
|
||||
pre_tok_state = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__())
|
||||
if pre_tok_state.get("add_prefix_space", add_prefix_space) != add_prefix_space:
|
||||
|
||||
@@ -1670,7 +1670,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin):
|
||||
init_configuration = {}
|
||||
|
||||
is_local = os.path.isdir(pretrained_model_name_or_path)
|
||||
if os.path.isfile(pretrained_model_name_or_path):
|
||||
if os.path.isfile(pretrained_model_name_or_path) or is_remote_url(pretrained_model_name_or_path):
|
||||
if len(cls.vocab_files_names) > 1:
|
||||
raise ValueError(
|
||||
f"Calling {cls.__name__}.from_pretrained() with the path to a single file or url is not "
|
||||
@@ -1726,6 +1726,8 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin):
|
||||
for file_id, file_path in vocab_files.items():
|
||||
if file_path is None:
|
||||
resolved_vocab_files[file_id] = None
|
||||
elif os.path.isfile(file_path):
|
||||
resolved_vocab_files[file_id] = file_path
|
||||
elif is_remote_url(file_path):
|
||||
resolved_vocab_files[file_id] = download_url(file_path, proxies=proxies)
|
||||
else:
|
||||
|
||||
@@ -119,7 +119,6 @@ class OptimizerNames(ExplicitEnum):
|
||||
|
||||
@dataclass
|
||||
class TrainingArguments:
|
||||
framework = "pt"
|
||||
"""
|
||||
TrainingArguments is the subset of the arguments we use in our example scripts **which relate to the training loop
|
||||
itself**.
|
||||
@@ -500,6 +499,7 @@ class TrainingArguments:
|
||||
Whether to use Apple Silicon chip based `mps` device.
|
||||
"""
|
||||
|
||||
framework = "pt"
|
||||
output_dir: str = field(
|
||||
metadata={"help": "The output directory where the model predictions and checkpoints will be written."},
|
||||
)
|
||||
|
||||
@@ -28,7 +28,6 @@ if is_tf_available():
|
||||
|
||||
@dataclass
|
||||
class TFTrainingArguments(TrainingArguments):
|
||||
framework = "tf"
|
||||
"""
|
||||
TrainingArguments is the subset of the arguments we use in our example scripts **which relate to the training loop
|
||||
itself**.
|
||||
@@ -162,6 +161,7 @@ class TFTrainingArguments(TrainingArguments):
|
||||
Whether to activate the XLA compilation or not.
|
||||
"""
|
||||
|
||||
framework = "tf"
|
||||
tpu_name: Optional[str] = field(
|
||||
default=None,
|
||||
metadata={"help": "Name of TPU"},
|
||||
|
||||
@@ -435,7 +435,7 @@ def cached_file(
|
||||
except LocalEntryNotFoundError:
|
||||
# We try to see if we have a cached version (not up to date):
|
||||
resolved_file = try_to_load_from_cache(path_or_repo_id, full_filename, cache_dir=cache_dir, revision=revision)
|
||||
if resolved_file is not None:
|
||||
if resolved_file is not None and resolved_file != _CACHED_NO_EXIST:
|
||||
return resolved_file
|
||||
if not _raise_exceptions_for_missing_entries or not _raise_exceptions_for_connection_errors:
|
||||
return None
|
||||
@@ -457,7 +457,7 @@ def cached_file(
|
||||
except HTTPError as err:
|
||||
# First we try to see if we have a cached version (not up to date):
|
||||
resolved_file = try_to_load_from_cache(path_or_repo_id, full_filename, cache_dir=cache_dir, revision=revision)
|
||||
if resolved_file is not None:
|
||||
if resolved_file is not None and resolved_file != _CACHED_NO_EXIST:
|
||||
return resolved_file
|
||||
if not _raise_exceptions_for_connection_errors:
|
||||
return None
|
||||
@@ -1104,8 +1104,9 @@ else:
|
||||
with open(cache_version_file) as f:
|
||||
cache_version = int(f.read())
|
||||
|
||||
cache_is_not_empty = os.path.isdir(TRANSFORMERS_CACHE) and len(os.listdir(TRANSFORMERS_CACHE)) > 0
|
||||
|
||||
if cache_version < 1:
|
||||
if cache_version < 1 and cache_is_not_empty:
|
||||
if is_offline_mode():
|
||||
logger.warn(
|
||||
"You are offline and the cache for model files in Transformers v4.22.0 has been updated while your local "
|
||||
@@ -1128,9 +1129,9 @@ if cache_version < 1:
|
||||
except Exception as e:
|
||||
trace = "\n".join(traceback.format_tb(e.__traceback__))
|
||||
logger.error(
|
||||
f"There was a problem when trying to move your cache:\n\n{trace}\n\nPlease file an issue at "
|
||||
"https://github.com/huggingface/transformers/issues/new/choose and copy paste this whole message and we "
|
||||
"will do our best to help."
|
||||
f"There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease "
|
||||
"file an issue at https://github.com/huggingface/transformers/issues/new/choose and copy paste this whole "
|
||||
"message and we will do our best to help."
|
||||
)
|
||||
|
||||
try:
|
||||
|
||||
@@ -18,7 +18,7 @@ import json
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from transformers import GPT2Tokenizer, GPT2TokenizerFast
|
||||
from transformers import AutoTokenizer, GPT2Tokenizer, GPT2TokenizerFast
|
||||
from transformers.models.gpt2.tokenization_gpt2 import VOCAB_FILES_NAMES
|
||||
from transformers.testing_utils import require_tokenizers
|
||||
|
||||
@@ -275,3 +275,57 @@ class GPT2TokenizationTest(TokenizerTesterMixin, unittest.TestCase):
|
||||
]
|
||||
filtered_sequence = [x for x in filtered_sequence if x is not None]
|
||||
self.assertEqual(encoded_sequence, filtered_sequence)
|
||||
|
||||
|
||||
@require_tokenizers
|
||||
class OPTTokenizationTest(unittest.TestCase):
|
||||
def test_serialize_deserialize_fast_opt(self):
|
||||
# More context:
|
||||
# https://huggingface.co/wjmcat/opt-350m-paddle/discussions/1
|
||||
# https://huggingface.slack.com/archives/C01N44FJDHT/p1653511495183519
|
||||
# https://github.com/huggingface/transformers/pull/17088#discussion_r871246439
|
||||
|
||||
tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m", from_slow=True)
|
||||
text = "A photo of a cat"
|
||||
|
||||
tokens_ids = tokenizer.encode(
|
||||
text,
|
||||
)
|
||||
self.assertEqual(tokens_ids, [2, 250, 1345, 9, 10, 4758])
|
||||
tokenizer.save_pretrained("test_opt")
|
||||
|
||||
tokenizer = AutoTokenizer.from_pretrained("./test_opt")
|
||||
tokens_ids = tokenizer.encode(
|
||||
text,
|
||||
)
|
||||
self.assertEqual(tokens_ids, [2, 250, 1345, 9, 10, 4758])
|
||||
|
||||
def test_fast_slow_equivalence(self):
|
||||
tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m", use_slow=True)
|
||||
text = "A photo of a cat"
|
||||
|
||||
tokens_ids = tokenizer.encode(
|
||||
text,
|
||||
)
|
||||
# Same as above
|
||||
self.assertEqual(tokens_ids, [2, 250, 1345, 9, 10, 4758])
|
||||
|
||||
def test_users_can_modify_bos(self):
|
||||
tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m", from_slow=True)
|
||||
|
||||
tokenizer.bos_token = "bos"
|
||||
tokenizer.bos_token_id = tokenizer.get_vocab()["bos"]
|
||||
|
||||
text = "A photo of a cat"
|
||||
tokens_ids = tokenizer.encode(
|
||||
text,
|
||||
)
|
||||
# We changed the bos token
|
||||
self.assertEqual(tokens_ids, [31957, 250, 1345, 9, 10, 4758])
|
||||
tokenizer.save_pretrained("./tok")
|
||||
tokenizer = AutoTokenizer.from_pretrained("./tok")
|
||||
self.assertTrue(tokenizer.is_fast)
|
||||
tokens_ids = tokenizer.encode(
|
||||
text,
|
||||
)
|
||||
self.assertEqual(tokens_ids, [31957, 250, 1345, 9, 10, 4758])
|
||||
|
||||
@@ -360,6 +360,12 @@ class ConfigTestUtils(unittest.TestCase):
|
||||
# This check we did call the fake head request
|
||||
mock_head.assert_called()
|
||||
|
||||
def test_legacy_load_from_url(self):
|
||||
# This test is for deprecated behavior and can be removed in v5
|
||||
_ = BertConfig.from_pretrained(
|
||||
"https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json"
|
||||
)
|
||||
|
||||
|
||||
class ConfigurationVersioningTest(unittest.TestCase):
|
||||
def test_local_versioning(self):
|
||||
|
||||
@@ -182,6 +182,12 @@ class FeatureExtractorUtilTester(unittest.TestCase):
|
||||
# This check we did call the fake head request
|
||||
mock_head.assert_called()
|
||||
|
||||
def test_legacy_load_from_url(self):
|
||||
# This test is for deprecated behavior and can be removed in v5
|
||||
_ = Wav2Vec2FeatureExtractor.from_pretrained(
|
||||
"https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json"
|
||||
)
|
||||
|
||||
|
||||
@is_staging_test
|
||||
class FeatureExtractorPushToHubTester(unittest.TestCase):
|
||||
|
||||
@@ -33,6 +33,7 @@ import numpy as np
|
||||
|
||||
import transformers
|
||||
from huggingface_hub import HfFolder, delete_repo, set_access_token
|
||||
from huggingface_hub.file_download import http_get
|
||||
from requests.exceptions import HTTPError
|
||||
from transformers import (
|
||||
AutoConfig,
|
||||
@@ -2949,6 +2950,26 @@ class ModelUtilsTest(TestCasePlus):
|
||||
# This check we did call the fake head request
|
||||
mock_head.assert_called()
|
||||
|
||||
def test_load_from_one_file(self):
|
||||
try:
|
||||
tmp_file = tempfile.mktemp()
|
||||
with open(tmp_file, "wb") as f:
|
||||
http_get(
|
||||
"https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/pytorch_model.bin", f
|
||||
)
|
||||
|
||||
config = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert")
|
||||
_ = BertModel.from_pretrained(tmp_file, config=config)
|
||||
finally:
|
||||
os.remove(tmp_file)
|
||||
|
||||
def test_legacy_load_from_url(self):
|
||||
# This test is for deprecated behavior and can be removed in v5
|
||||
config = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert")
|
||||
_ = BertModel.from_pretrained(
|
||||
"https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/pytorch_model.bin", config=config
|
||||
)
|
||||
|
||||
|
||||
@require_torch
|
||||
@is_staging_test
|
||||
|
||||
@@ -30,6 +30,7 @@ from typing import List, Tuple, get_type_hints
|
||||
from datasets import Dataset
|
||||
|
||||
from huggingface_hub import HfFolder, Repository, delete_repo, set_access_token
|
||||
from huggingface_hub.file_download import http_get
|
||||
from requests.exceptions import HTTPError
|
||||
from transformers import is_tf_available, is_torch_available
|
||||
from transformers.configuration_utils import PretrainedConfig
|
||||
@@ -1868,6 +1869,24 @@ class UtilsFunctionsTest(unittest.TestCase):
|
||||
# This check we did call the fake head request
|
||||
mock_head.assert_called()
|
||||
|
||||
def test_load_from_one_file(self):
|
||||
try:
|
||||
tmp_file = tempfile.mktemp()
|
||||
with open(tmp_file, "wb") as f:
|
||||
http_get("https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/tf_model.h5", f)
|
||||
|
||||
config = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert")
|
||||
_ = TFBertModel.from_pretrained(tmp_file, config=config)
|
||||
finally:
|
||||
os.remove(tmp_file)
|
||||
|
||||
def test_legacy_load_from_url(self):
|
||||
# This test is for deprecated behavior and can be removed in v5
|
||||
config = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert")
|
||||
_ = TFBertModel.from_pretrained(
|
||||
"https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/tf_model.h5", config=config
|
||||
)
|
||||
|
||||
# tests whether the unpack_inputs function behaves as expected
|
||||
def test_unpack_inputs(self):
|
||||
class DummyModel:
|
||||
|
||||
@@ -31,6 +31,7 @@ from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any, Dict, List, Tuple, Union
|
||||
|
||||
from huggingface_hub import HfFolder, delete_repo, set_access_token
|
||||
from huggingface_hub.file_download import http_get
|
||||
from parameterized import parameterized
|
||||
from requests.exceptions import HTTPError
|
||||
from transformers import (
|
||||
@@ -39,6 +40,7 @@ from transformers import (
|
||||
AutoTokenizer,
|
||||
BertTokenizer,
|
||||
BertTokenizerFast,
|
||||
GPT2TokenizerFast,
|
||||
PreTrainedTokenizer,
|
||||
PreTrainedTokenizerBase,
|
||||
PreTrainedTokenizerFast,
|
||||
@@ -3880,12 +3882,45 @@ class TokenizerUtilTester(unittest.TestCase):
|
||||
# Download this model to make sure it's in the cache.
|
||||
_ = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert")
|
||||
|
||||
# Under the mock environment we get a 500 error when trying to reach the model.
|
||||
# Under the mock environment we get a 500 error when trying to reach the tokenizer.
|
||||
with mock.patch("requests.request", return_value=response_mock) as mock_head:
|
||||
_ = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert")
|
||||
# This check we did call the fake head request
|
||||
mock_head.assert_called()
|
||||
|
||||
@require_tokenizers
|
||||
def test_cached_files_are_used_when_internet_is_down_missing_files(self):
|
||||
# A mock response for an HTTP head request to emulate server down
|
||||
response_mock = mock.Mock()
|
||||
response_mock.status_code = 500
|
||||
response_mock.headers = {}
|
||||
response_mock.raise_for_status.side_effect = HTTPError
|
||||
response_mock.json.return_value = {}
|
||||
|
||||
# Download this model to make sure it's in the cache.
|
||||
_ = GPT2TokenizerFast.from_pretrained("gpt2")
|
||||
|
||||
# Under the mock environment we get a 500 error when trying to reach the tokenizer.
|
||||
with mock.patch("requests.request", return_value=response_mock) as mock_head:
|
||||
_ = GPT2TokenizerFast.from_pretrained("gpt2")
|
||||
# This check we did call the fake head request
|
||||
mock_head.assert_called()
|
||||
|
||||
def test_legacy_load_from_one_file(self):
|
||||
# This test is for deprecated behavior and can be removed in v5
|
||||
try:
|
||||
tmp_file = tempfile.mktemp()
|
||||
with open(tmp_file, "wb") as f:
|
||||
http_get("https://huggingface.co/albert-base-v1/resolve/main/spiece.model", f)
|
||||
|
||||
_ = AlbertTokenizer.from_pretrained(tmp_file)
|
||||
finally:
|
||||
os.remove(tmp_file)
|
||||
|
||||
def test_legacy_load_from_url(self):
|
||||
# This test is for deprecated behavior and can be removed in v5
|
||||
_ = AlbertTokenizer.from_pretrained("https://huggingface.co/albert-base-v1/resolve/main/spiece.model")
|
||||
|
||||
|
||||
@is_staging_test
|
||||
class TokenizerPushToHubTester(unittest.TestCase):
|
||||
|
||||
@@ -15,28 +15,14 @@
|
||||
import contextlib
|
||||
import importlib
|
||||
import io
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import transformers
|
||||
|
||||
# Try to import everything from transformers to ensure every object can be loaded.
|
||||
from transformers import * # noqa F406
|
||||
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER
|
||||
from transformers.utils import (
|
||||
FLAX_WEIGHTS_NAME,
|
||||
TF2_WEIGHTS_NAME,
|
||||
WEIGHTS_NAME,
|
||||
ContextManagers,
|
||||
find_labels,
|
||||
get_file_from_repo,
|
||||
has_file,
|
||||
is_flax_available,
|
||||
is_tf_available,
|
||||
is_torch_available,
|
||||
)
|
||||
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available
|
||||
|
||||
|
||||
MODEL_ID = DUMMY_UNKNOWN_IDENTIFIER
|
||||
@@ -77,38 +63,6 @@ class TestImportMechanisms(unittest.TestCase):
|
||||
assert importlib.util.find_spec("transformers") is not None
|
||||
|
||||
|
||||
class GetFromCacheTests(unittest.TestCase):
|
||||
def test_has_file(self):
|
||||
self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only", WEIGHTS_NAME))
|
||||
self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only", TF2_WEIGHTS_NAME))
|
||||
self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only", FLAX_WEIGHTS_NAME))
|
||||
|
||||
def test_get_file_from_repo_distant(self):
|
||||
# `get_file_from_repo` returns None if the file does not exist
|
||||
self.assertIsNone(get_file_from_repo("bert-base-cased", "ahah.txt"))
|
||||
|
||||
# The function raises if the repository does not exist.
|
||||
with self.assertRaisesRegex(EnvironmentError, "is not a valid model identifier"):
|
||||
get_file_from_repo("bert-base-case", "config.json")
|
||||
|
||||
# The function raises if the revision does not exist.
|
||||
with self.assertRaisesRegex(EnvironmentError, "is not a valid git identifier"):
|
||||
get_file_from_repo("bert-base-cased", "config.json", revision="ahaha")
|
||||
|
||||
resolved_file = get_file_from_repo("bert-base-cased", "config.json")
|
||||
# The name is the cached name which is not very easy to test, so instead we load the content.
|
||||
config = json.loads(open(resolved_file, "r").read())
|
||||
self.assertEqual(config["hidden_size"], 768)
|
||||
|
||||
def test_get_file_from_repo_local(self):
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
filename = Path(tmp_dir) / "a.txt"
|
||||
filename.touch()
|
||||
self.assertEqual(get_file_from_repo(tmp_dir, "a.txt"), str(filename))
|
||||
|
||||
self.assertIsNone(get_file_from_repo(tmp_dir, "b.txt"))
|
||||
|
||||
|
||||
class GenericUtilTests(unittest.TestCase):
|
||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_context_managers_no_context(self, mock_stdout):
|
||||
|
||||
125
tests/utils/test_hub_utils.py
Normal file
125
tests/utils/test_hub_utils.py
Normal file
@@ -0,0 +1,125 @@
|
||||
# Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# 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 json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from pathlib import Path
|
||||
|
||||
from requests.exceptions import HTTPError
|
||||
from transformers.utils import (
|
||||
CONFIG_NAME,
|
||||
FLAX_WEIGHTS_NAME,
|
||||
TF2_WEIGHTS_NAME,
|
||||
TRANSFORMERS_CACHE,
|
||||
WEIGHTS_NAME,
|
||||
cached_file,
|
||||
get_file_from_repo,
|
||||
has_file,
|
||||
)
|
||||
|
||||
|
||||
RANDOM_BERT = "hf-internal-testing/tiny-random-bert"
|
||||
CACHE_DIR = os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
|
||||
FULL_COMMIT_HASH = "9b8c223d42b2188cb49d29af482996f9d0f3e5a6"
|
||||
|
||||
|
||||
class GetFromCacheTests(unittest.TestCase):
|
||||
def test_cached_file(self):
|
||||
archive_file = cached_file(RANDOM_BERT, CONFIG_NAME)
|
||||
# Should have downloaded the file in here
|
||||
self.assertTrue(os.path.isdir(CACHE_DIR))
|
||||
# Cache should contain at least those three subfolders:
|
||||
for subfolder in ["blobs", "refs", "snapshots"]:
|
||||
self.assertTrue(os.path.isdir(os.path.join(CACHE_DIR, subfolder)))
|
||||
with open(os.path.join(CACHE_DIR, "refs", "main")) as f:
|
||||
main_commit = f.read()
|
||||
self.assertEqual(archive_file, os.path.join(CACHE_DIR, "snapshots", main_commit, CONFIG_NAME))
|
||||
self.assertTrue(os.path.isfile(archive_file))
|
||||
|
||||
# File is cached at the same place the second time.
|
||||
new_archive_file = cached_file(RANDOM_BERT, CONFIG_NAME)
|
||||
self.assertEqual(archive_file, new_archive_file)
|
||||
|
||||
# Using a specific revision to test the full commit hash.
|
||||
archive_file = cached_file(RANDOM_BERT, CONFIG_NAME, revision="9b8c223")
|
||||
self.assertEqual(archive_file, os.path.join(CACHE_DIR, "snapshots", FULL_COMMIT_HASH, CONFIG_NAME))
|
||||
|
||||
def test_cached_file_errors(self):
|
||||
with self.assertRaisesRegex(EnvironmentError, "is not a valid model identifier"):
|
||||
_ = cached_file("tiny-random-bert", CONFIG_NAME)
|
||||
|
||||
with self.assertRaisesRegex(EnvironmentError, "is not a valid git identifier"):
|
||||
_ = cached_file(RANDOM_BERT, CONFIG_NAME, revision="aaaa")
|
||||
|
||||
with self.assertRaisesRegex(EnvironmentError, "does not appear to have a file named"):
|
||||
_ = cached_file(RANDOM_BERT, "conf")
|
||||
|
||||
def test_non_existence_is_cached(self):
|
||||
with self.assertRaisesRegex(EnvironmentError, "does not appear to have a file named"):
|
||||
_ = cached_file(RANDOM_BERT, "conf")
|
||||
|
||||
with open(os.path.join(CACHE_DIR, "refs", "main")) as f:
|
||||
main_commit = f.read()
|
||||
self.assertTrue(os.path.isfile(os.path.join(CACHE_DIR, ".no_exist", main_commit, "conf")))
|
||||
|
||||
path = cached_file(RANDOM_BERT, "conf", _raise_exceptions_for_missing_entries=False)
|
||||
self.assertIsNone(path)
|
||||
|
||||
path = cached_file(RANDOM_BERT, "conf", local_files_only=True, _raise_exceptions_for_missing_entries=False)
|
||||
self.assertIsNone(path)
|
||||
|
||||
response_mock = mock.Mock()
|
||||
response_mock.status_code = 500
|
||||
response_mock.headers = {}
|
||||
response_mock.raise_for_status.side_effect = HTTPError
|
||||
response_mock.json.return_value = {}
|
||||
|
||||
# Under the mock environment we get a 500 error when trying to reach the tokenizer.
|
||||
with mock.patch("requests.request", return_value=response_mock) as mock_head:
|
||||
path = cached_file(RANDOM_BERT, "conf", _raise_exceptions_for_connection_errors=False)
|
||||
self.assertIsNone(path)
|
||||
# This check we did call the fake head request
|
||||
mock_head.assert_called()
|
||||
|
||||
def test_has_file(self):
|
||||
self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only", WEIGHTS_NAME))
|
||||
self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only", TF2_WEIGHTS_NAME))
|
||||
self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only", FLAX_WEIGHTS_NAME))
|
||||
|
||||
def test_get_file_from_repo_distant(self):
|
||||
# `get_file_from_repo` returns None if the file does not exist
|
||||
self.assertIsNone(get_file_from_repo("bert-base-cased", "ahah.txt"))
|
||||
|
||||
# The function raises if the repository does not exist.
|
||||
with self.assertRaisesRegex(EnvironmentError, "is not a valid model identifier"):
|
||||
get_file_from_repo("bert-base-case", CONFIG_NAME)
|
||||
|
||||
# The function raises if the revision does not exist.
|
||||
with self.assertRaisesRegex(EnvironmentError, "is not a valid git identifier"):
|
||||
get_file_from_repo("bert-base-cased", CONFIG_NAME, revision="ahaha")
|
||||
|
||||
resolved_file = get_file_from_repo("bert-base-cased", CONFIG_NAME)
|
||||
# The name is the cached name which is not very easy to test, so instead we load the content.
|
||||
config = json.loads(open(resolved_file, "r").read())
|
||||
self.assertEqual(config["hidden_size"], 768)
|
||||
|
||||
def test_get_file_from_repo_local(self):
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
filename = Path(tmp_dir) / "a.txt"
|
||||
filename.touch()
|
||||
self.assertEqual(get_file_from_repo(tmp_dir, "a.txt"), str(filename))
|
||||
|
||||
self.assertIsNone(get_file_from_repo(tmp_dir, "b.txt"))
|
||||
@@ -354,7 +354,7 @@ SPECIAL_MODULE_TO_TEST_MAP = {
|
||||
"feature_extraction_utils.py": "test_feature_extraction_common.py",
|
||||
"file_utils.py": ["utils/test_file_utils.py", "utils/test_model_output.py"],
|
||||
"utils/generic.py": ["utils/test_file_utils.py", "utils/test_model_output.py", "utils/test_generic.py"],
|
||||
"utils/hub.py": "utils/test_file_utils.py",
|
||||
"utils/hub.py": "utils/test_hub_utils.py",
|
||||
"modelcard.py": "utils/test_model_card.py",
|
||||
"modeling_flax_utils.py": "test_modeling_flax_common.py",
|
||||
"modeling_tf_utils.py": ["test_modeling_tf_common.py", "utils/test_modeling_tf_core.py"],
|
||||
|
||||
Reference in New Issue
Block a user