init
This commit is contained in:
0
transformers/tests/utils/__init__.py
Normal file
0
transformers/tests/utils/__init__.py
Normal file
23
transformers/tests/utils/import_structures/failing_export.py
Normal file
23
transformers/tests/utils/import_structures/failing_export.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# Copyright 2024 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.
|
||||
|
||||
# fmt: off
|
||||
|
||||
from transformers.utils.import_utils import requires
|
||||
|
||||
|
||||
@requires(backends=("random_item_that_should_not_exist",))
|
||||
class A0:
|
||||
def __init__(self):
|
||||
pass
|
||||
@@ -0,0 +1,78 @@
|
||||
# Copyright 2024 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.
|
||||
|
||||
# fmt: off
|
||||
|
||||
from transformers.utils.import_utils import requires
|
||||
|
||||
|
||||
@requires()
|
||||
class A0:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires()
|
||||
def a0():
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch",))
|
||||
class A1:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch",))
|
||||
def a1():
|
||||
pass
|
||||
|
||||
|
||||
@requires(
|
||||
backends=("torch",)
|
||||
)
|
||||
class A2:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(
|
||||
backends=("torch",)
|
||||
)
|
||||
def a2():
|
||||
pass
|
||||
|
||||
|
||||
@requires(
|
||||
backends=(
|
||||
"torch",
|
||||
)
|
||||
)
|
||||
class A3:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(
|
||||
backends=(
|
||||
"torch",
|
||||
)
|
||||
)
|
||||
def a3():
|
||||
pass
|
||||
|
||||
@requires(backends=())
|
||||
class A4:
|
||||
def __init__(self):
|
||||
pass
|
||||
@@ -0,0 +1,92 @@
|
||||
# Copyright 2024 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.
|
||||
|
||||
# fmt: off
|
||||
|
||||
from transformers.utils.import_utils import requires
|
||||
|
||||
|
||||
@requires(backends=("torch>=2.5",))
|
||||
class D0:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch>=2.5",))
|
||||
def d0():
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch>2.5",))
|
||||
class D1:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch>2.5",))
|
||||
def d1():
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch<=2.5",))
|
||||
class D2:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch<=2.5",))
|
||||
def d2():
|
||||
pass
|
||||
|
||||
@requires(backends=("torch<2.5",))
|
||||
class D3:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch<2.5",))
|
||||
def d3():
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch==2.5",))
|
||||
class D4:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch==2.5",))
|
||||
def d4():
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch!=2.5",))
|
||||
class D5:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch!=2.5",))
|
||||
def d5():
|
||||
pass
|
||||
|
||||
@requires(backends=("torch>=2.5", "accelerate<0.20"))
|
||||
class D6:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch>=2.5", "accelerate<0.20"))
|
||||
def d6():
|
||||
pass
|
||||
@@ -0,0 +1,77 @@
|
||||
# Copyright 2024 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.
|
||||
|
||||
# fmt: off
|
||||
|
||||
from transformers.utils.import_utils import requires
|
||||
|
||||
|
||||
@requires()
|
||||
# That's a statement
|
||||
class B0:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires()
|
||||
# That's a statement
|
||||
def b0():
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch",))
|
||||
# That's a statement
|
||||
class B1:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch",))
|
||||
# That's a statement
|
||||
def b1():
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch",))
|
||||
# That's a statement
|
||||
class B2:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch",))
|
||||
# That's a statement
|
||||
def b2():
|
||||
pass
|
||||
|
||||
|
||||
@requires(
|
||||
backends=(
|
||||
"torch",
|
||||
)
|
||||
)
|
||||
# That's a statement
|
||||
class B3:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(
|
||||
backends=(
|
||||
"torch",
|
||||
)
|
||||
)
|
||||
# That's a statement
|
||||
def b3():
|
||||
pass
|
||||
@@ -0,0 +1,77 @@
|
||||
# Copyright 2024 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.
|
||||
|
||||
# fmt: off
|
||||
|
||||
from transformers.utils.import_utils import requires
|
||||
|
||||
|
||||
@requires(backends=("torch", "torch"))
|
||||
class C0:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch", "torch"))
|
||||
def c0():
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch", "torch"))
|
||||
# That's a statement
|
||||
class C1:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch", "torch"))
|
||||
# That's a statement
|
||||
def c1():
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch", "torch"))
|
||||
# That's a statement
|
||||
class C2:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(backends=("torch", "torch"))
|
||||
# That's a statement
|
||||
def c2():
|
||||
pass
|
||||
|
||||
|
||||
@requires(
|
||||
backends=(
|
||||
"torch",
|
||||
"torch"
|
||||
)
|
||||
)
|
||||
# That's a statement
|
||||
class C3:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@requires(
|
||||
backends=(
|
||||
"torch",
|
||||
"torch"
|
||||
)
|
||||
)
|
||||
# That's a statement
|
||||
def c3():
|
||||
pass
|
||||
74
transformers/tests/utils/test_activations.py
Normal file
74
transformers/tests/utils/test_activations.py
Normal file
@@ -0,0 +1,74 @@
|
||||
# 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 unittest
|
||||
|
||||
from transformers import is_torch_available
|
||||
from transformers.testing_utils import require_torch
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
from transformers.activations import gelu_new, gelu_python, get_activation
|
||||
|
||||
|
||||
@require_torch
|
||||
class TestActivations(unittest.TestCase):
|
||||
def test_gelu_versions(self):
|
||||
x = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100])
|
||||
torch_builtin = get_activation("gelu")
|
||||
torch.testing.assert_close(gelu_python(x), torch_builtin(x))
|
||||
self.assertFalse(torch.allclose(gelu_python(x), gelu_new(x)))
|
||||
|
||||
def test_gelu_10(self):
|
||||
x = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100])
|
||||
torch_builtin = get_activation("gelu")
|
||||
gelu10 = get_activation("gelu_10")
|
||||
|
||||
y_gelu = torch_builtin(x)
|
||||
y_gelu_10 = gelu10(x)
|
||||
|
||||
clipped_mask = torch.where(y_gelu_10 < 10.0, 1, 0)
|
||||
|
||||
self.assertTrue(torch.max(y_gelu_10).item() == 10.0)
|
||||
torch.testing.assert_close(y_gelu * clipped_mask, y_gelu_10 * clipped_mask)
|
||||
|
||||
def test_get_activation(self):
|
||||
get_activation("gelu")
|
||||
get_activation("gelu_10")
|
||||
get_activation("gelu_fast")
|
||||
get_activation("gelu_new")
|
||||
get_activation("gelu_python")
|
||||
get_activation("gelu_pytorch_tanh")
|
||||
get_activation("linear")
|
||||
get_activation("mish")
|
||||
get_activation("quick_gelu")
|
||||
get_activation("relu")
|
||||
get_activation("sigmoid")
|
||||
get_activation("silu")
|
||||
get_activation("swish")
|
||||
get_activation("tanh")
|
||||
with self.assertRaises(KeyError):
|
||||
get_activation("bogus")
|
||||
with self.assertRaises(KeyError):
|
||||
get_activation(None)
|
||||
|
||||
def test_activations_are_distinct_objects(self):
|
||||
act1 = get_activation("gelu")
|
||||
act1.a = 1
|
||||
act2 = get_activation("gelu")
|
||||
self.assertEqual(act1.a, 1)
|
||||
with self.assertRaises(AttributeError):
|
||||
_ = act2.a
|
||||
826
transformers/tests/utils/test_add_new_model_like.py
Normal file
826
transformers/tests/utils/test_add_new_model_like.py
Normal file
@@ -0,0 +1,826 @@
|
||||
# Copyright 2022 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 os
|
||||
import shutil
|
||||
import tempfile
|
||||
import textwrap
|
||||
import unittest
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
|
||||
import transformers.commands.add_new_model_like
|
||||
from transformers.commands.add_new_model_like import ModelInfos, create_new_model_like
|
||||
from transformers.testing_utils import require_torch
|
||||
|
||||
|
||||
REPO_PATH = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
|
||||
MODELS_TO_COPY = ("auto", "llama", "phi4_multimodal")
|
||||
CURRENT_YEAR = date.today().year
|
||||
|
||||
|
||||
@require_torch
|
||||
class TestAddNewModelLike(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""
|
||||
Create a temporary repo with the same structure as Transformers, with just 2 models.
|
||||
"""
|
||||
cls.FAKE_REPO = tempfile.TemporaryDirectory().name
|
||||
os.makedirs(os.path.join(cls.FAKE_REPO, "src", "transformers", "models"), exist_ok=True)
|
||||
os.makedirs(os.path.join(cls.FAKE_REPO, "tests", "models"), exist_ok=True)
|
||||
os.makedirs(os.path.join(cls.FAKE_REPO, "docs", "source", "en", "model_doc"), exist_ok=True)
|
||||
|
||||
# We need to copy the utils to run the cleanup commands
|
||||
utils_src = os.path.join(REPO_PATH, "utils")
|
||||
shutil.copytree(utils_src, utils_src.replace(REPO_PATH, cls.FAKE_REPO))
|
||||
# Copy the __init__ files
|
||||
model_init = os.path.join(REPO_PATH, "src", "transformers", "models", "__init__.py")
|
||||
shutil.copy(model_init, model_init.replace(REPO_PATH, cls.FAKE_REPO))
|
||||
doc_toc = os.path.join(REPO_PATH, "docs", "source", "en", "_toctree.yml")
|
||||
shutil.copy(doc_toc, doc_toc.replace(REPO_PATH, cls.FAKE_REPO))
|
||||
# We need the pyproject for ruff as well
|
||||
pyproject = os.path.join(REPO_PATH, "pyproject.toml")
|
||||
shutil.copy(pyproject, pyproject.replace(REPO_PATH, cls.FAKE_REPO))
|
||||
# Copy over all the specific model files
|
||||
for model in MODELS_TO_COPY:
|
||||
model_src = os.path.join(REPO_PATH, "src", "transformers", "models", model)
|
||||
shutil.copytree(model_src, model_src.replace(REPO_PATH, cls.FAKE_REPO))
|
||||
|
||||
test_src = os.path.join(REPO_PATH, "tests", "models", model)
|
||||
shutil.copytree(test_src, test_src.replace(REPO_PATH, cls.FAKE_REPO))
|
||||
|
||||
if model != "auto":
|
||||
doc_src = os.path.join(REPO_PATH, "docs", "source", "en", "model_doc", f"{model}.md")
|
||||
shutil.copy(doc_src, doc_src.replace(REPO_PATH, cls.FAKE_REPO))
|
||||
|
||||
# Replace the globals
|
||||
cls.ORIGINAL_REPO = transformers.commands.add_new_model_like.REPO_PATH
|
||||
cls.ORIGINAL_TRANSFORMERS_REPO = transformers.commands.add_new_model_like.TRANSFORMERS_PATH
|
||||
transformers.commands.add_new_model_like.REPO_PATH = Path(cls.FAKE_REPO)
|
||||
transformers.commands.add_new_model_like.TRANSFORMERS_PATH = Path(cls.FAKE_REPO) / "src" / "transformers"
|
||||
|
||||
# For convenience
|
||||
cls.MODEL_PATH = os.path.join(cls.FAKE_REPO, "src", "transformers", "models")
|
||||
cls.TESTS_MODEL_PATH = os.path.join(cls.FAKE_REPO, "tests", "models")
|
||||
cls.DOC_PATH = os.path.join(cls.FAKE_REPO, "docs", "source", "en")
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
transformers.commands.add_new_model_like.REPO_PATH = cls.ORIGINAL_REPO
|
||||
transformers.commands.add_new_model_like.TRANSFORMERS_PATH = cls.ORIGINAL_TRANSFORMERS_REPO
|
||||
del cls.FAKE_REPO
|
||||
|
||||
def assertFileIsEqual(self, text: str, filepath: str):
|
||||
with open(filepath, "r") as f:
|
||||
file_text = f.read()
|
||||
self.assertEqual(file_text.strip(), text.strip())
|
||||
|
||||
def assertInFile(self, text: str, filepath: str):
|
||||
with open(filepath, "r") as f:
|
||||
file_text = f.read()
|
||||
self.assertTrue(text in file_text)
|
||||
|
||||
def test_llama_without_tokenizers(self):
|
||||
# This is the structure without adding the tokenizers
|
||||
filenames_to_add = (
|
||||
("configuration_llama.py", True),
|
||||
("modeling_llama.py", True),
|
||||
("tokenization_llama.py", False),
|
||||
("tokenization_llama_fast.py", False),
|
||||
("image_processing_llama.py", False),
|
||||
("image_processing_llama_fast.py", False),
|
||||
("video_processing_llama.py", False),
|
||||
("feature_extraction_llama.py", False),
|
||||
("processing_llama.py", False),
|
||||
)
|
||||
# Run the command
|
||||
create_new_model_like(
|
||||
old_model_infos=ModelInfos("llama"),
|
||||
new_lowercase_name="my_test",
|
||||
new_model_paper_name="MyTest",
|
||||
filenames_to_add=filenames_to_add,
|
||||
create_fast_image_processor=False,
|
||||
)
|
||||
|
||||
# First assert that all files were created correctly
|
||||
model_repo = os.path.join(self.MODEL_PATH, "my_test")
|
||||
tests_repo = os.path.join(self.TESTS_MODEL_PATH, "my_test")
|
||||
self.assertTrue(os.path.isfile(os.path.join(model_repo, "modular_my_test.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(model_repo, "modeling_my_test.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(model_repo, "configuration_my_test.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(model_repo, "__init__.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(self.DOC_PATH, "model_doc", "my_test.md")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(tests_repo, "__init__.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(tests_repo, "test_modeling_my_test.py")))
|
||||
|
||||
# Now assert the correct imports/auto mappings/toctree were added
|
||||
self.assertInFile(
|
||||
"from .my_test import *\n",
|
||||
os.path.join(self.MODEL_PATH, "__init__.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test", "MyTestConfig"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "configuration_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test", "MyTest"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "configuration_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test", "MyTestModel"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "modeling_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test", "MyTestForCausalLM"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "modeling_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test", "MyTestForSequenceClassification"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "modeling_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test", "MyTestForQuestionAnswering"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "modeling_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test", "MyTestForTokenClassification"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "modeling_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
"- local: model_doc/my_test\n title: MyTest\n",
|
||||
os.path.join(self.DOC_PATH, "_toctree.yml"),
|
||||
)
|
||||
|
||||
# Check some exact file creation. For model definition, only check modular as modeling/config/etc... are created
|
||||
# directly from it
|
||||
EXPECTED_MODULAR = textwrap.dedent(
|
||||
f"""
|
||||
# coding=utf-8
|
||||
# Copyright {CURRENT_YEAR} 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.
|
||||
|
||||
from ..llama.configuration_llama import LlamaConfig
|
||||
from ..llama.modeling_llama import (
|
||||
LlamaAttention,
|
||||
LlamaDecoderLayer,
|
||||
LlamaForCausalLM,
|
||||
LlamaForQuestionAnswering,
|
||||
LlamaForSequenceClassification,
|
||||
LlamaForTokenClassification,
|
||||
LlamaMLP,
|
||||
LlamaModel,
|
||||
LlamaPreTrainedModel,
|
||||
LlamaRMSNorm,
|
||||
LlamaRotaryEmbedding,
|
||||
)
|
||||
|
||||
|
||||
class MyTestConfig(LlamaConfig):
|
||||
pass
|
||||
|
||||
|
||||
class MyTestRMSNorm(LlamaRMSNorm):
|
||||
pass
|
||||
|
||||
|
||||
class MyTestRotaryEmbedding(LlamaRotaryEmbedding):
|
||||
pass
|
||||
|
||||
|
||||
class MyTestMLP(LlamaMLP):
|
||||
pass
|
||||
|
||||
|
||||
class MyTestAttention(LlamaAttention):
|
||||
pass
|
||||
|
||||
|
||||
class MyTestDecoderLayer(LlamaDecoderLayer):
|
||||
pass
|
||||
|
||||
|
||||
class MyTestPreTrainedModel(LlamaPreTrainedModel):
|
||||
pass
|
||||
|
||||
|
||||
class MyTestModel(LlamaModel):
|
||||
pass
|
||||
|
||||
|
||||
class MyTestForCausalLM(LlamaForCausalLM):
|
||||
pass
|
||||
|
||||
|
||||
class MyTestForSequenceClassification(LlamaForSequenceClassification):
|
||||
pass
|
||||
|
||||
|
||||
class MyTestForQuestionAnswering(LlamaForQuestionAnswering):
|
||||
pass
|
||||
|
||||
|
||||
class MyTestForTokenClassification(LlamaForTokenClassification):
|
||||
pass
|
||||
|
||||
|
||||
__all__ = [
|
||||
"MyTestConfig",
|
||||
"MyTestForCausalLM",
|
||||
"MyTestModel",
|
||||
"MyTestPreTrainedModel",
|
||||
"MyTestForSequenceClassification",
|
||||
"MyTestForQuestionAnswering",
|
||||
"MyTestForTokenClassification",
|
||||
]
|
||||
"""
|
||||
)
|
||||
self.assertFileIsEqual(EXPECTED_MODULAR, os.path.join(model_repo, "modular_my_test.py"))
|
||||
|
||||
EXPECTED_INIT = textwrap.dedent(
|
||||
f"""
|
||||
# coding=utf-8
|
||||
# Copyright {CURRENT_YEAR} 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.
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ...utils import _LazyModule
|
||||
from ...utils.import_utils import define_import_structure
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .configuration_my_test import *
|
||||
from .modeling_my_test import *
|
||||
else:
|
||||
import sys
|
||||
|
||||
_file = globals()["__file__"]
|
||||
sys.modules[__name__] = _LazyModule(__name__, _file, define_import_structure(_file), module_spec=__spec__)
|
||||
|
||||
"""
|
||||
)
|
||||
self.assertFileIsEqual(EXPECTED_INIT, os.path.join(model_repo, "__init__.py"))
|
||||
|
||||
EXPECTED_DOC = textwrap.dedent(
|
||||
f"""
|
||||
<!--Copyright {CURRENT_YEAR} 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.
|
||||
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer.
|
||||
|
||||
-->
|
||||
|
||||
|
||||
# MyTest
|
||||
|
||||
## Overview
|
||||
|
||||
The MyTest model was proposed in [<INSERT PAPER NAME HERE>](<INSERT PAPER LINK HERE>) by <INSERT AUTHORS HERE>.
|
||||
<INSERT SHORT SUMMARY HERE>
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
<INSERT PAPER ABSTRACT HERE>
|
||||
|
||||
Tips:
|
||||
|
||||
<INSERT TIPS ABOUT MODEL HERE>
|
||||
|
||||
This model was contributed by [INSERT YOUR HF USERNAME HERE](https://huggingface.co/<INSERT YOUR HF USERNAME HERE>).
|
||||
The original code can be found [here](<INSERT LINK TO GITHUB REPO HERE>).
|
||||
|
||||
## Usage examples
|
||||
|
||||
<INSERT SOME NICE EXAMPLES HERE>
|
||||
|
||||
## MyTestConfig
|
||||
|
||||
[[autodoc]] MyTestConfig
|
||||
|
||||
## MyTestForCausalLM
|
||||
|
||||
[[autodoc]] MyTestForCausalLM
|
||||
|
||||
## MyTestModel
|
||||
|
||||
[[autodoc]] MyTestModel
|
||||
- forward
|
||||
|
||||
## MyTestPreTrainedModel
|
||||
|
||||
[[autodoc]] MyTestPreTrainedModel
|
||||
- forward
|
||||
|
||||
## MyTestForSequenceClassification
|
||||
|
||||
[[autodoc]] MyTestForSequenceClassification
|
||||
|
||||
## MyTestForQuestionAnswering
|
||||
|
||||
[[autodoc]] MyTestForQuestionAnswering
|
||||
|
||||
## MyTestForTokenClassification
|
||||
|
||||
[[autodoc]] MyTestForTokenClassification
|
||||
"""
|
||||
)
|
||||
self.assertFileIsEqual(EXPECTED_DOC, os.path.join(self.DOC_PATH, "model_doc", "my_test.md"))
|
||||
|
||||
def test_phi4_with_all_processors(self):
|
||||
# This is the structure without adding the tokenizers
|
||||
filenames_to_add = (
|
||||
("configuration_phi4_multimodal.py", True),
|
||||
("modeling_phi4_multimodal.py", True),
|
||||
("tokenization_phi4_multimodal.py", False),
|
||||
("tokenization_phi4_multimodal_fast.py", False),
|
||||
("image_processing_phi4_multimodal.py", False),
|
||||
("image_processing_phi4_multimodal_fast.py", True),
|
||||
("video_processing_phi4_multimodal.py", False),
|
||||
("feature_extraction_phi4_multimodal.py", True),
|
||||
("processing_phi4_multimodal.py", True),
|
||||
)
|
||||
# Run the command
|
||||
create_new_model_like(
|
||||
old_model_infos=ModelInfos("phi4_multimodal"),
|
||||
new_lowercase_name="my_test2",
|
||||
new_model_paper_name="MyTest2",
|
||||
filenames_to_add=filenames_to_add,
|
||||
create_fast_image_processor=False,
|
||||
)
|
||||
|
||||
# First assert that all files were created correctly
|
||||
model_repo = os.path.join(self.MODEL_PATH, "my_test2")
|
||||
tests_repo = os.path.join(self.TESTS_MODEL_PATH, "my_test2")
|
||||
self.assertTrue(os.path.isfile(os.path.join(model_repo, "modular_my_test2.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(model_repo, "modeling_my_test2.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(model_repo, "configuration_my_test2.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(model_repo, "image_processing_my_test2_fast.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(model_repo, "feature_extraction_my_test2.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(model_repo, "processing_my_test2.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(model_repo, "__init__.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(self.DOC_PATH, "model_doc", "my_test2.md")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(tests_repo, "__init__.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(tests_repo, "test_modeling_my_test2.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(tests_repo, "test_feature_extraction_my_test2.py")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(tests_repo, "test_image_processing_my_test2.py")))
|
||||
|
||||
# Now assert the correct imports/auto mappings/toctree were added
|
||||
self.assertInFile(
|
||||
"from .my_test2 import *\n",
|
||||
os.path.join(self.MODEL_PATH, "__init__.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test2", "MyTest2Config"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "configuration_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test2", "MyTest2"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "configuration_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test2", "MyTest2Model"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "modeling_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test2", "MyTest2ForCausalLM"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "modeling_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test2", (None, "MyTest2ImageProcessorFast")),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "image_processing_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test2", "MyTest2FeatureExtractor"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "feature_extraction_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
'("my_test2", "MyTest2Processor"),\n',
|
||||
os.path.join(self.MODEL_PATH, "auto", "processing_auto.py"),
|
||||
)
|
||||
self.assertInFile(
|
||||
"- local: model_doc/my_test2\n title: MyTest2\n",
|
||||
os.path.join(self.DOC_PATH, "_toctree.yml"),
|
||||
)
|
||||
|
||||
# Check some exact file creation. For model definition, only check modular as modeling/config/etc... are created
|
||||
# directly from it
|
||||
EXPECTED_MODULAR = textwrap.dedent(
|
||||
f"""
|
||||
# coding=utf-8
|
||||
# Copyright {CURRENT_YEAR} 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.
|
||||
|
||||
from ..phi4_multimodal.configuration_phi4_multimodal import (
|
||||
Phi4MultimodalAudioConfig,
|
||||
Phi4MultimodalConfig,
|
||||
Phi4MultimodalVisionConfig,
|
||||
)
|
||||
from ..phi4_multimodal.feature_extraction_phi4_multimodal import Phi4MultimodalFeatureExtractor
|
||||
from ..phi4_multimodal.image_processing_phi4_multimodal_fast import (
|
||||
Phi4MultimodalFastImageProcessorKwargs,
|
||||
Phi4MultimodalImageProcessorFast,
|
||||
)
|
||||
from ..phi4_multimodal.modeling_phi4_multimodal import (
|
||||
Phi4MultimodalAttention,
|
||||
Phi4MultimodalAudioAttention,
|
||||
Phi4MultimodalAudioConformerEncoderLayer,
|
||||
Phi4MultimodalAudioConvModule,
|
||||
Phi4MultimodalAudioDepthWiseSeparableConv1d,
|
||||
Phi4MultimodalAudioEmbedding,
|
||||
Phi4MultimodalAudioGluPointWiseConv,
|
||||
Phi4MultimodalAudioMeanVarianceNormLayer,
|
||||
Phi4MultimodalAudioMLP,
|
||||
Phi4MultimodalAudioModel,
|
||||
Phi4MultimodalAudioNemoConvSubsampling,
|
||||
Phi4MultimodalAudioPreTrainedModel,
|
||||
Phi4MultimodalAudioRelativeAttentionBias,
|
||||
Phi4MultimodalDecoderLayer,
|
||||
Phi4MultimodalFeatureEmbedding,
|
||||
Phi4MultimodalForCausalLM,
|
||||
Phi4MultimodalImageEmbedding,
|
||||
Phi4MultimodalMLP,
|
||||
Phi4MultimodalModel,
|
||||
Phi4MultimodalPreTrainedModel,
|
||||
Phi4MultimodalRMSNorm,
|
||||
Phi4MultimodalRotaryEmbedding,
|
||||
Phi4MultimodalVisionAttention,
|
||||
Phi4MultimodalVisionEmbeddings,
|
||||
Phi4MultimodalVisionEncoder,
|
||||
Phi4MultimodalVisionEncoderLayer,
|
||||
Phi4MultimodalVisionMLP,
|
||||
Phi4MultimodalVisionModel,
|
||||
Phi4MultimodalVisionMultiheadAttentionPoolingHead,
|
||||
Phi4MultimodalVisionPreTrainedModel,
|
||||
)
|
||||
from ..phi4_multimodal.processing_phi4_multimodal import Phi4MultimodalProcessor, Phi4MultimodalProcessorKwargs
|
||||
|
||||
|
||||
class MyTest2VisionConfig(Phi4MultimodalVisionConfig):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioConfig(Phi4MultimodalAudioConfig):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2Config(Phi4MultimodalConfig):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2VisionMLP(Phi4MultimodalVisionMLP):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2VisionAttention(Phi4MultimodalVisionAttention):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2VisionEncoderLayer(Phi4MultimodalVisionEncoderLayer):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2VisionEncoder(Phi4MultimodalVisionEncoder):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2VisionPreTrainedModel(Phi4MultimodalVisionPreTrainedModel):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2VisionEmbeddings(Phi4MultimodalVisionEmbeddings):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2VisionMultiheadAttentionPoolingHead(Phi4MultimodalVisionMultiheadAttentionPoolingHead):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2VisionModel(Phi4MultimodalVisionModel):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2ImageEmbedding(Phi4MultimodalImageEmbedding):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioMLP(Phi4MultimodalAudioMLP):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioAttention(Phi4MultimodalAudioAttention):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioDepthWiseSeparableConv1d(Phi4MultimodalAudioDepthWiseSeparableConv1d):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioGluPointWiseConv(Phi4MultimodalAudioGluPointWiseConv):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioConvModule(Phi4MultimodalAudioConvModule):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioConformerEncoderLayer(Phi4MultimodalAudioConformerEncoderLayer):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioNemoConvSubsampling(Phi4MultimodalAudioNemoConvSubsampling):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioRelativeAttentionBias(Phi4MultimodalAudioRelativeAttentionBias):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioMeanVarianceNormLayer(Phi4MultimodalAudioMeanVarianceNormLayer):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioPreTrainedModel(Phi4MultimodalAudioPreTrainedModel):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioModel(Phi4MultimodalAudioModel):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2AudioEmbedding(Phi4MultimodalAudioEmbedding):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2RMSNorm(Phi4MultimodalRMSNorm):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2MLP(Phi4MultimodalMLP):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2Attention(Phi4MultimodalAttention):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2DecoderLayer(Phi4MultimodalDecoderLayer):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2FeatureEmbedding(Phi4MultimodalFeatureEmbedding):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2RotaryEmbedding(Phi4MultimodalRotaryEmbedding):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2PreTrainedModel(Phi4MultimodalPreTrainedModel):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2Model(Phi4MultimodalModel):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2ForCausalLM(Phi4MultimodalForCausalLM):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2FastImageProcessorKwargs(Phi4MultimodalFastImageProcessorKwargs):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2ImageProcessorFast(Phi4MultimodalImageProcessorFast):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2FeatureExtractor(Phi4MultimodalFeatureExtractor):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2ProcessorKwargs(Phi4MultimodalProcessorKwargs):
|
||||
pass
|
||||
|
||||
|
||||
class MyTest2Processor(Phi4MultimodalProcessor):
|
||||
pass
|
||||
|
||||
|
||||
__all__ = [
|
||||
"MyTest2VisionConfig",
|
||||
"MyTest2AudioConfig",
|
||||
"MyTest2Config",
|
||||
"MyTest2AudioPreTrainedModel",
|
||||
"MyTest2AudioModel",
|
||||
"MyTest2VisionPreTrainedModel",
|
||||
"MyTest2VisionModel",
|
||||
"MyTest2PreTrainedModel",
|
||||
"MyTest2Model",
|
||||
"MyTest2ForCausalLM",
|
||||
"MyTest2ImageProcessorFast",
|
||||
"MyTest2FeatureExtractor",
|
||||
"MyTest2Processor",
|
||||
]
|
||||
"""
|
||||
)
|
||||
self.assertFileIsEqual(EXPECTED_MODULAR, os.path.join(model_repo, "modular_my_test2.py"))
|
||||
|
||||
EXPECTED_INIT = textwrap.dedent(
|
||||
f"""
|
||||
# coding=utf-8
|
||||
# Copyright {CURRENT_YEAR} 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.
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ...utils import _LazyModule
|
||||
from ...utils.import_utils import define_import_structure
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .configuration_my_test2 import *
|
||||
from .feature_extraction_my_test2 import *
|
||||
from .image_processing_my_test2_fast import *
|
||||
from .modeling_my_test2 import *
|
||||
from .processing_my_test2 import *
|
||||
else:
|
||||
import sys
|
||||
|
||||
_file = globals()["__file__"]
|
||||
sys.modules[__name__] = _LazyModule(__name__, _file, define_import_structure(_file), module_spec=__spec__)
|
||||
"""
|
||||
)
|
||||
self.assertFileIsEqual(EXPECTED_INIT, os.path.join(model_repo, "__init__.py"))
|
||||
|
||||
EXPECTED_DOC = textwrap.dedent(
|
||||
f"""
|
||||
<!--Copyright {CURRENT_YEAR} 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.
|
||||
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer.
|
||||
|
||||
-->
|
||||
|
||||
|
||||
# MyTest2
|
||||
|
||||
## Overview
|
||||
|
||||
The MyTest2 model was proposed in [<INSERT PAPER NAME HERE>](<INSERT PAPER LINK HERE>) by <INSERT AUTHORS HERE>.
|
||||
<INSERT SHORT SUMMARY HERE>
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
<INSERT PAPER ABSTRACT HERE>
|
||||
|
||||
Tips:
|
||||
|
||||
<INSERT TIPS ABOUT MODEL HERE>
|
||||
|
||||
This model was contributed by [INSERT YOUR HF USERNAME HERE](https://huggingface.co/<INSERT YOUR HF USERNAME HERE>).
|
||||
The original code can be found [here](<INSERT LINK TO GITHUB REPO HERE>).
|
||||
|
||||
## Usage examples
|
||||
|
||||
<INSERT SOME NICE EXAMPLES HERE>
|
||||
|
||||
## MyTest2VisionConfig
|
||||
|
||||
[[autodoc]] MyTest2VisionConfig
|
||||
|
||||
## MyTest2AudioConfig
|
||||
|
||||
[[autodoc]] MyTest2AudioConfig
|
||||
|
||||
## MyTest2Config
|
||||
|
||||
[[autodoc]] MyTest2Config
|
||||
|
||||
## MyTest2AudioPreTrainedModel
|
||||
|
||||
[[autodoc]] MyTest2AudioPreTrainedModel
|
||||
- forward
|
||||
|
||||
## MyTest2AudioModel
|
||||
|
||||
[[autodoc]] MyTest2AudioModel
|
||||
- forward
|
||||
|
||||
## MyTest2VisionPreTrainedModel
|
||||
|
||||
[[autodoc]] MyTest2VisionPreTrainedModel
|
||||
- forward
|
||||
|
||||
## MyTest2VisionModel
|
||||
|
||||
[[autodoc]] MyTest2VisionModel
|
||||
- forward
|
||||
|
||||
## MyTest2PreTrainedModel
|
||||
|
||||
[[autodoc]] MyTest2PreTrainedModel
|
||||
- forward
|
||||
|
||||
## MyTest2Model
|
||||
|
||||
[[autodoc]] MyTest2Model
|
||||
- forward
|
||||
|
||||
## MyTest2ForCausalLM
|
||||
|
||||
[[autodoc]] MyTest2ForCausalLM
|
||||
|
||||
## MyTest2ImageProcessorFast
|
||||
|
||||
[[autodoc]] MyTest2ImageProcessorFast
|
||||
|
||||
## MyTest2FeatureExtractor
|
||||
|
||||
[[autodoc]] MyTest2FeatureExtractor
|
||||
|
||||
## MyTest2Processor
|
||||
|
||||
[[autodoc]] MyTest2Processor
|
||||
"""
|
||||
)
|
||||
self.assertFileIsEqual(EXPECTED_DOC, os.path.join(self.DOC_PATH, "model_doc", "my_test2.md"))
|
||||
127
transformers/tests/utils/test_attention_visualizer.py
Normal file
127
transformers/tests/utils/test_attention_visualizer.py
Normal file
@@ -0,0 +1,127 @@
|
||||
# Copyright 2025 The HuggingFace Inc. team.
|
||||
#
|
||||
# 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 builtins
|
||||
import io
|
||||
import re
|
||||
import unittest
|
||||
|
||||
from transformers.testing_utils import require_read_token, require_torch
|
||||
from transformers.utils.attention_visualizer import AttentionMaskVisualizer
|
||||
|
||||
|
||||
ANSI_RE = re.compile(r"\x1b\[[0-9;]*m")
|
||||
|
||||
|
||||
def _normalize(s: str) -> str:
|
||||
# drop ANSI (colors may be disabled on CI), normalize line endings,
|
||||
# and strip trailing spaces without touching alignment inside lines
|
||||
s = ANSI_RE.sub("", s)
|
||||
s = s.replace("\r\n", "\n").replace("\r", "\n")
|
||||
return "\n".join(line.rstrip() for line in s.split("\n")).strip()
|
||||
|
||||
|
||||
@require_torch
|
||||
class AttentionMaskVisualizerTester(unittest.TestCase):
|
||||
"""Test suite for AttentionMaskVisualizer"""
|
||||
|
||||
@require_read_token
|
||||
def test_paligemma_multimodal_visualization(self):
|
||||
"""Test AttentionMaskVisualizer with PaliGemma multimodal model"""
|
||||
model_name = "hf-internal-testing/namespace_google_repo_name_paligemma-3b-pt-224"
|
||||
input_text = "<img> What is in this image?"
|
||||
|
||||
buf = io.StringIO()
|
||||
orig_print = builtins.print
|
||||
|
||||
def _print(*args, **kwargs):
|
||||
kwargs.setdefault("file", buf)
|
||||
orig_print(*args, **kwargs)
|
||||
|
||||
try:
|
||||
builtins.print = _print
|
||||
visualizer = AttentionMaskVisualizer(model_name)
|
||||
visualizer(input_text)
|
||||
finally:
|
||||
builtins.print = orig_print
|
||||
output = buf.getvalue()
|
||||
|
||||
expected_output = """
|
||||
##########################################################################################################################################################################################################################################
|
||||
## Attention visualization for \033[1mpaligemma:hf-internal-testing/namespace_google_repo_name_paligemma-3b-pt-224\033[0m PaliGemmaModel ##
|
||||
##########################################################################################################################################################################################################################################
|
||||
\033[92m■\033[0m: i == j (diagonal) \033[93m■\033[0m: token_type_ids
|
||||
Attention Matrix
|
||||
|
||||
|
||||
\033[93m'<image>'\033[0m: 0 \033[93m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
\033[93m'<image>'\033[0m: 1 \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
\033[93m'<image>'\033[0m: 2 \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
\033[93m'<image>'\033[0m: 3 \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
\033[93m'<image>'\033[0m: 4 \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
'<bos>' : 5 ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
'▁What' : 6 ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
'▁is' : 7 ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
'▁in' : 8 ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
'▁this' : 9 ■ ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ |
|
||||
'▁image' : 10 ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ |
|
||||
'?' : 11 ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ |
|
||||
'\\n' : 12 ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ |
|
||||
'<eos>' : 13 ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m |
|
||||
##########################################################################################################################################################################################################################################
|
||||
""" # noqa
|
||||
|
||||
self.assertEqual(_normalize(output), _normalize(expected_output))
|
||||
|
||||
@require_read_token
|
||||
def test_llama_text_only_visualization(self):
|
||||
"""Test AttentionMaskVisualizer with Llama text-only model"""
|
||||
model_name = "hf-internal-testing/namespace_meta-llama_repo_name_Llama-2-7b-hf"
|
||||
input_text = "Plants create energy through a process known as"
|
||||
|
||||
buf = io.StringIO()
|
||||
orig_print = builtins.print
|
||||
|
||||
def _print(*args, **kwargs):
|
||||
kwargs.setdefault("file", buf)
|
||||
orig_print(*args, **kwargs)
|
||||
|
||||
try:
|
||||
builtins.print = _print
|
||||
visualizer = AttentionMaskVisualizer(model_name)
|
||||
visualizer(input_text)
|
||||
finally:
|
||||
builtins.print = orig_print
|
||||
output = buf.getvalue()
|
||||
|
||||
expected_output = """
|
||||
##########################################################################################################################################################################################################
|
||||
## Attention visualization for \033[1mllama:hf-internal-testing/namespace_meta-llama_repo_name_Llama-2-7b-hf\033[0m LlamaModel ##
|
||||
##########################################################################################################################################################################################################
|
||||
\033[92m■\033[0m: i == j (diagonal) \033[93m■\033[0m: token_type_ids
|
||||
Attention Matrix
|
||||
|
||||
'▁Pl' : 0 \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
'ants' : 1 ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
'▁create' : 2 ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
'▁energy' : 3 ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ |
|
||||
'▁through': 4 ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ |
|
||||
'▁a' : 5 ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ |
|
||||
'▁process': 6 ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ |
|
||||
'▁known' : 7 ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ |
|
||||
'▁as' : 8 ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m |
|
||||
##########################################################################################################################################################################################################
|
||||
""" # noqa
|
||||
|
||||
self.assertEqual(_normalize(output), _normalize(expected_output))
|
||||
1751
transformers/tests/utils/test_audio_utils.py
Normal file
1751
transformers/tests/utils/test_audio_utils.py
Normal file
File diff suppressed because it is too large
Load Diff
88
transformers/tests/utils/test_auto_docstring.py
Normal file
88
transformers/tests/utils/test_auto_docstring.py
Normal file
File diff suppressed because one or more lines are too long
272
transformers/tests/utils/test_backbone_utils.py
Normal file
272
transformers/tests/utils/test_backbone_utils.py
Normal file
@@ -0,0 +1,272 @@
|
||||
# Copyright 2023 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 unittest
|
||||
|
||||
import pytest
|
||||
|
||||
from transformers import DetrConfig, MaskFormerConfig, ResNetBackbone, ResNetConfig, TimmBackbone
|
||||
from transformers.testing_utils import require_torch, slow
|
||||
from transformers.utils.backbone_utils import (
|
||||
BackboneMixin,
|
||||
get_aligned_output_features_output_indices,
|
||||
load_backbone,
|
||||
verify_out_features_out_indices,
|
||||
)
|
||||
from transformers.utils.import_utils import is_torch_available
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
from transformers import BertPreTrainedModel
|
||||
|
||||
|
||||
class BackboneUtilsTester(unittest.TestCase):
|
||||
def test_get_aligned_output_features_output_indices(self):
|
||||
stage_names = ["a", "b", "c"]
|
||||
|
||||
# Defaults to last layer if both are None
|
||||
out_features, out_indices = get_aligned_output_features_output_indices(None, None, stage_names)
|
||||
self.assertEqual(out_features, ["c"])
|
||||
self.assertEqual(out_indices, [2])
|
||||
|
||||
# Out indices set to match out features
|
||||
out_features, out_indices = get_aligned_output_features_output_indices(["a", "c"], None, stage_names)
|
||||
self.assertEqual(out_features, ["a", "c"])
|
||||
self.assertEqual(out_indices, [0, 2])
|
||||
|
||||
# Out features set to match out indices
|
||||
out_features, out_indices = get_aligned_output_features_output_indices(None, [0, 2], stage_names)
|
||||
self.assertEqual(out_features, ["a", "c"])
|
||||
self.assertEqual(out_indices, [0, 2])
|
||||
|
||||
# Out features selected from negative indices
|
||||
out_features, out_indices = get_aligned_output_features_output_indices(None, [-3, -1], stage_names)
|
||||
self.assertEqual(out_features, ["a", "c"])
|
||||
self.assertEqual(out_indices, [-3, -1])
|
||||
|
||||
def test_verify_out_features_out_indices(self):
|
||||
# Stage names must be set
|
||||
with pytest.raises(ValueError, match="Stage_names must be set for transformers backbones"):
|
||||
verify_out_features_out_indices(["a", "b"], (0, 1), None)
|
||||
|
||||
# Out features must be a list
|
||||
with pytest.raises(ValueError, match="out_features must be a list got <class 'tuple'>"):
|
||||
verify_out_features_out_indices(("a", "b"), (0, 1), ["a", "b"])
|
||||
|
||||
# Out features must be a subset of stage names
|
||||
with pytest.raises(
|
||||
ValueError, match=r"out_features must be a subset of stage_names: \['a'\] got \['a', 'b'\]"
|
||||
):
|
||||
verify_out_features_out_indices(["a", "b"], [0, 1], ["a"])
|
||||
|
||||
# Out features must contain no duplicates
|
||||
with pytest.raises(ValueError, match=r"out_features must not contain any duplicates, got \['a', 'a'\]"):
|
||||
verify_out_features_out_indices(["a", "a"], None, ["a"])
|
||||
|
||||
# Out indices must be a list
|
||||
with pytest.raises(ValueError, match="out_indices must be a list, got <class 'int'>"):
|
||||
verify_out_features_out_indices(None, 0, ["a", "b"])
|
||||
|
||||
with pytest.raises(ValueError, match="out_indices must be a list, got <class 'tuple'>"):
|
||||
verify_out_features_out_indices(None, (0, 1), ["a", "b"])
|
||||
|
||||
# Out indices must be a subset of stage names
|
||||
with pytest.raises(
|
||||
ValueError, match=r"out_indices must be valid indices for stage_names \['a'\], got \[0, 1\]"
|
||||
):
|
||||
verify_out_features_out_indices(None, [0, 1], ["a"])
|
||||
|
||||
# Out indices must contain no duplicates
|
||||
with pytest.raises(ValueError, match=r"out_indices must not contain any duplicates, got \[0, 0\]"):
|
||||
verify_out_features_out_indices(None, [0, 0], ["a"])
|
||||
|
||||
# Out features and out indices must be the same length
|
||||
with pytest.raises(
|
||||
ValueError, match="out_features and out_indices should have the same length if both are set"
|
||||
):
|
||||
verify_out_features_out_indices(["a", "b"], [0], ["a", "b", "c"])
|
||||
|
||||
# Out features should match out indices
|
||||
with pytest.raises(
|
||||
ValueError, match="out_features and out_indices should correspond to the same stages if both are set"
|
||||
):
|
||||
verify_out_features_out_indices(["a", "b"], [0, 2], ["a", "b", "c"])
|
||||
|
||||
# Out features and out indices should be in order
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match=r"out_features must be in the same order as stage_names, expected \['a', 'b'\] got \['b', 'a'\]",
|
||||
):
|
||||
verify_out_features_out_indices(["b", "a"], [0, 1], ["a", "b"])
|
||||
|
||||
with pytest.raises(
|
||||
ValueError, match=r"out_indices must be in the same order as stage_names, expected \[-2, 1\] got \[1, -2\]"
|
||||
):
|
||||
verify_out_features_out_indices(["a", "b"], [1, -2], ["a", "b"])
|
||||
|
||||
# Check passes with valid inputs
|
||||
verify_out_features_out_indices(["a", "b", "d"], [0, 1, -1], ["a", "b", "c", "d"])
|
||||
|
||||
def test_backbone_mixin(self):
|
||||
backbone = BackboneMixin()
|
||||
|
||||
backbone.stage_names = ["a", "b", "c"]
|
||||
backbone._out_features = ["a", "c"]
|
||||
backbone._out_indices = [0, 2]
|
||||
|
||||
# Check that the output features and indices are set correctly
|
||||
self.assertEqual(backbone.out_features, ["a", "c"])
|
||||
self.assertEqual(backbone.out_indices, [0, 2])
|
||||
|
||||
# Check out features and indices are updated correctly
|
||||
backbone.out_features = ["a", "b"]
|
||||
self.assertEqual(backbone.out_features, ["a", "b"])
|
||||
self.assertEqual(backbone.out_indices, [0, 1])
|
||||
|
||||
backbone.out_indices = [-3, -1]
|
||||
self.assertEqual(backbone.out_features, ["a", "c"])
|
||||
self.assertEqual(backbone.out_indices, [-3, -1])
|
||||
|
||||
@slow
|
||||
@require_torch
|
||||
def test_load_backbone_from_config(self):
|
||||
"""
|
||||
Test that load_backbone correctly loads a backbone from a backbone config.
|
||||
"""
|
||||
config = MaskFormerConfig(backbone_config=ResNetConfig(out_indices=(0, 2)))
|
||||
backbone = load_backbone(config)
|
||||
self.assertEqual(backbone.out_features, ["stem", "stage2"])
|
||||
self.assertEqual(backbone.out_indices, (0, 2))
|
||||
self.assertIsInstance(backbone, ResNetBackbone)
|
||||
|
||||
@slow
|
||||
@require_torch
|
||||
def test_load_backbone_from_checkpoint(self):
|
||||
"""
|
||||
Test that load_backbone correctly loads a backbone from a checkpoint.
|
||||
"""
|
||||
config = MaskFormerConfig(backbone="microsoft/resnet-18", backbone_config=None)
|
||||
backbone = load_backbone(config)
|
||||
self.assertEqual(backbone.out_indices, [4])
|
||||
self.assertEqual(backbone.out_features, ["stage4"])
|
||||
self.assertIsInstance(backbone, ResNetBackbone)
|
||||
|
||||
config = MaskFormerConfig(
|
||||
backbone="resnet18",
|
||||
use_timm_backbone=True,
|
||||
)
|
||||
backbone = load_backbone(config)
|
||||
# We can't know ahead of time the exact output features and indices, or the layer names before
|
||||
# creating the timm model, so it defaults to the last layer (-1,) and has a different layer name
|
||||
self.assertEqual(backbone.out_indices, (-1,))
|
||||
self.assertEqual(backbone.out_features, ["layer4"])
|
||||
self.assertIsInstance(backbone, TimmBackbone)
|
||||
|
||||
@slow
|
||||
@require_torch
|
||||
def test_load_backbone_backbone_kwargs(self):
|
||||
"""
|
||||
Test that load_backbone correctly configures the loaded backbone with the provided kwargs.
|
||||
"""
|
||||
config = MaskFormerConfig(backbone="resnet18", use_timm_backbone=True, backbone_kwargs={"out_indices": (0, 1)})
|
||||
backbone = load_backbone(config)
|
||||
self.assertEqual(backbone.out_indices, (0, 1))
|
||||
self.assertIsInstance(backbone, TimmBackbone)
|
||||
|
||||
config = MaskFormerConfig(backbone="microsoft/resnet-18", backbone_kwargs={"out_indices": (0, 2)})
|
||||
backbone = load_backbone(config)
|
||||
self.assertEqual(backbone.out_indices, (0, 2))
|
||||
self.assertIsInstance(backbone, ResNetBackbone)
|
||||
|
||||
# Check can't be passed with a backone config
|
||||
with pytest.raises(ValueError):
|
||||
config = MaskFormerConfig(
|
||||
backbone="microsoft/resnet-18",
|
||||
backbone_config=ResNetConfig(out_indices=(0, 2)),
|
||||
backbone_kwargs={"out_indices": (0, 1)},
|
||||
)
|
||||
|
||||
@slow
|
||||
@require_torch
|
||||
def test_load_backbone_in_new_model(self):
|
||||
"""
|
||||
Tests that new model can be created, with its weights instantiated and pretrained backbone weights loaded.
|
||||
"""
|
||||
|
||||
# Inherit from PreTrainedModel to ensure that the weights are initialized
|
||||
class NewModel(BertPreTrainedModel):
|
||||
def __init__(self, config):
|
||||
super().__init__(config)
|
||||
self.backbone = load_backbone(config)
|
||||
self.layer_0 = torch.nn.Linear(config.hidden_size, config.hidden_size)
|
||||
self.layer_1 = torch.nn.Linear(config.hidden_size, config.hidden_size)
|
||||
|
||||
def get_equal_not_equal_weights(model_0, model_1):
|
||||
equal_weights = []
|
||||
not_equal_weights = []
|
||||
for (k0, v0), (k1, v1) in zip(model_0.named_parameters(), model_1.named_parameters()):
|
||||
self.assertEqual(k0, k1)
|
||||
weights_are_equal = torch.allclose(v0, v1)
|
||||
if weights_are_equal:
|
||||
equal_weights.append(k0)
|
||||
else:
|
||||
not_equal_weights.append(k0)
|
||||
return equal_weights, not_equal_weights
|
||||
|
||||
config = MaskFormerConfig(use_pretrained_backbone=False, backbone="microsoft/resnet-18")
|
||||
model_0 = NewModel(config)
|
||||
model_1 = NewModel(config)
|
||||
equal_weights, not_equal_weights = get_equal_not_equal_weights(model_0, model_1)
|
||||
|
||||
# Norm layers are always initialized with the same weights
|
||||
equal_weights = [w for w in equal_weights if "normalization" not in w]
|
||||
self.assertEqual(len(equal_weights), 0)
|
||||
self.assertEqual(len(not_equal_weights), 24)
|
||||
|
||||
# Now we create a new model with backbone weights that are pretrained
|
||||
config.use_pretrained_backbone = True
|
||||
model_0 = NewModel(config)
|
||||
model_1 = NewModel(config)
|
||||
equal_weights, not_equal_weights = get_equal_not_equal_weights(model_0, model_1)
|
||||
|
||||
# Norm layers are always initialized with the same weights
|
||||
equal_weights = [w for w in equal_weights if "normalization" not in w]
|
||||
self.assertEqual(len(equal_weights), 20)
|
||||
# Linear layers are still initialized randomly
|
||||
self.assertEqual(len(not_equal_weights), 4)
|
||||
|
||||
# Check loading in timm backbone
|
||||
config = DetrConfig(use_pretrained_backbone=False, backbone="resnet18", use_timm_backbone=True)
|
||||
model_0 = NewModel(config)
|
||||
model_1 = NewModel(config)
|
||||
equal_weights, not_equal_weights = get_equal_not_equal_weights(model_0, model_1)
|
||||
|
||||
# Norm layers are always initialized with the same weights
|
||||
equal_weights = [w for w in equal_weights if "bn" not in w and "downsample.1" not in w]
|
||||
self.assertEqual(len(equal_weights), 0)
|
||||
self.assertEqual(len(not_equal_weights), 24)
|
||||
|
||||
# Now we create a new model with backbone weights that are pretrained
|
||||
config.use_pretrained_backbone = True
|
||||
model_0 = NewModel(config)
|
||||
model_1 = NewModel(config)
|
||||
equal_weights, not_equal_weights = get_equal_not_equal_weights(model_0, model_1)
|
||||
|
||||
# Norm layers are always initialized with the same weights
|
||||
equal_weights = [w for w in equal_weights if "bn" not in w and "downsample.1" not in w]
|
||||
self.assertEqual(len(equal_weights), 20)
|
||||
# Linear layers are still initialized randomly
|
||||
self.assertEqual(len(not_equal_weights), 4)
|
||||
1307
transformers/tests/utils/test_cache_utils.py
Normal file
1307
transformers/tests/utils/test_cache_utils.py
Normal file
File diff suppressed because it is too large
Load Diff
544
transformers/tests/utils/test_chat_template_utils.py
Normal file
544
transformers/tests/utils/test_chat_template_utils.py
Normal file
@@ -0,0 +1,544 @@
|
||||
# Copyright 2024 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 unittest
|
||||
from typing import Literal, Optional, Union
|
||||
|
||||
from transformers.utils import DocstringParsingException, TypeHintParsingException, get_json_schema
|
||||
|
||||
|
||||
class JsonSchemaGeneratorTest(unittest.TestCase):
|
||||
def test_simple_function(self):
|
||||
def fn(x: int):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
"""
|
||||
return x
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"x": {"type": "integer", "description": "The input"}},
|
||||
"required": ["x"],
|
||||
},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_no_arguments(self):
|
||||
def fn():
|
||||
"""
|
||||
Test function
|
||||
"""
|
||||
return True
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {"type": "object", "properties": {}},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_union(self):
|
||||
def fn(x: Union[int, float]):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
"""
|
||||
return x
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"x": {"type": ["integer", "number"], "description": "The input"}},
|
||||
"required": ["x"],
|
||||
},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_optional(self):
|
||||
def fn(x: Optional[int]):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
"""
|
||||
return x
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"x": {"type": "integer", "description": "The input", "nullable": True}},
|
||||
"required": ["x"],
|
||||
},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_default_arg(self):
|
||||
def fn(x: int = 42):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
"""
|
||||
return x
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {"type": "object", "properties": {"x": {"type": "integer", "description": "The input"}}},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_nested_list(self):
|
||||
def fn(x: list[list[Union[str, int]]]):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
"""
|
||||
return x
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"x": {
|
||||
"type": "array",
|
||||
"items": {"type": "array", "items": {"type": ["integer", "string"]}},
|
||||
"description": "The input",
|
||||
}
|
||||
},
|
||||
"required": ["x"],
|
||||
},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_multiple_arguments(self):
|
||||
def fn(x: int, y: str):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
y: Also the input
|
||||
"""
|
||||
return x
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"x": {"type": "integer", "description": "The input"},
|
||||
"y": {"type": "string", "description": "Also the input"},
|
||||
},
|
||||
"required": ["x", "y"],
|
||||
},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_multiple_complex_arguments(self):
|
||||
def fn(x: list[Union[int, float]], y: Optional[Union[int, str]] = None):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
y: Also the input
|
||||
"""
|
||||
return x
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"x": {"type": "array", "items": {"type": ["integer", "number"]}, "description": "The input"},
|
||||
"y": {
|
||||
"type": ["integer", "string"],
|
||||
"nullable": True,
|
||||
"description": "Also the input",
|
||||
},
|
||||
},
|
||||
"required": ["x"],
|
||||
},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_missing_docstring(self):
|
||||
def fn(x: int):
|
||||
return x
|
||||
|
||||
with self.assertRaises(DocstringParsingException):
|
||||
get_json_schema(fn)
|
||||
|
||||
def test_missing_param_docstring(self):
|
||||
def fn(x: int):
|
||||
"""
|
||||
Test function
|
||||
"""
|
||||
return x
|
||||
|
||||
with self.assertRaises(DocstringParsingException):
|
||||
get_json_schema(fn)
|
||||
|
||||
def test_missing_type_hint(self):
|
||||
def fn(x):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
"""
|
||||
return x
|
||||
|
||||
with self.assertRaises(TypeHintParsingException):
|
||||
get_json_schema(fn)
|
||||
|
||||
def test_return_value(self):
|
||||
def fn(x: int) -> int:
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
"""
|
||||
return x
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"x": {"type": "integer", "description": "The input"}},
|
||||
"required": ["x"],
|
||||
},
|
||||
"return": {"type": "integer"},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_return_value_docstring(self):
|
||||
def fn(x: int) -> int:
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
|
||||
|
||||
Returns:
|
||||
The output
|
||||
"""
|
||||
return x
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"x": {"type": "integer", "description": "The input"}},
|
||||
"required": ["x"],
|
||||
},
|
||||
"return": {"type": "integer", "description": "The output"},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_tuple(self):
|
||||
def fn(x: tuple[int, str]):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
|
||||
|
||||
Returns:
|
||||
The output
|
||||
"""
|
||||
return x
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"x": {
|
||||
"type": "array",
|
||||
"prefixItems": [{"type": "integer"}, {"type": "string"}],
|
||||
"description": "The input",
|
||||
}
|
||||
},
|
||||
"required": ["x"],
|
||||
},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_single_element_tuple_fails(self):
|
||||
def fn(x: tuple[int]):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
|
||||
|
||||
Returns:
|
||||
The output
|
||||
"""
|
||||
return x
|
||||
|
||||
# Single-element tuples should just be the type itself, or List[type] for variable-length inputs
|
||||
with self.assertRaises(TypeHintParsingException):
|
||||
get_json_schema(fn)
|
||||
|
||||
def test_ellipsis_type_fails(self):
|
||||
def fn(x: tuple[int, ...]):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The input
|
||||
|
||||
|
||||
Returns:
|
||||
The output
|
||||
"""
|
||||
return x
|
||||
|
||||
# Variable length inputs should be specified with List[type], not Tuple[type, ...]
|
||||
with self.assertRaises(TypeHintParsingException):
|
||||
get_json_schema(fn)
|
||||
|
||||
def test_enum_extraction(self):
|
||||
def fn(temperature_format: str):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
temperature_format: The temperature format to use (Choices: ["celsius", "fahrenheit"])
|
||||
|
||||
|
||||
Returns:
|
||||
The temperature
|
||||
"""
|
||||
return -40.0
|
||||
|
||||
# Let's see if that gets correctly parsed as an enum
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"temperature_format": {
|
||||
"type": "string",
|
||||
"enum": ["celsius", "fahrenheit"],
|
||||
"description": "The temperature format to use",
|
||||
}
|
||||
},
|
||||
"required": ["temperature_format"],
|
||||
},
|
||||
}
|
||||
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_literal(self):
|
||||
def fn(
|
||||
temperature_format: Literal["celsius", "fahrenheit"],
|
||||
booleanish: Literal[True, False, 0, 1, "y", "n"] = False,
|
||||
):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
temperature_format: The temperature format to use
|
||||
booleanish: A value that can be regarded as boolean
|
||||
|
||||
|
||||
Returns:
|
||||
The temperature
|
||||
"""
|
||||
return -40.0
|
||||
|
||||
# Let's see if that gets correctly parsed as an enum
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"temperature_format": {
|
||||
"type": "string",
|
||||
"enum": ["celsius", "fahrenheit"],
|
||||
"description": "The temperature format to use",
|
||||
},
|
||||
"booleanish": {
|
||||
"type": ["boolean", "integer", "string"],
|
||||
"enum": [True, False, 0, 1, "y", "n"],
|
||||
"description": "A value that can be regarded as boolean",
|
||||
},
|
||||
},
|
||||
"required": ["temperature_format"],
|
||||
},
|
||||
}
|
||||
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_multiline_docstring_with_types(self):
|
||||
def fn(x: int, y: int):
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The first input
|
||||
|
||||
y: The second input. This is a longer description
|
||||
that spans multiple lines with indentation and stuff.
|
||||
|
||||
Returns:
|
||||
God knows what
|
||||
"""
|
||||
pass
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"x": {"type": "integer", "description": "The first input"},
|
||||
"y": {
|
||||
"type": "integer",
|
||||
"description": "The second input. This is a longer description that spans multiple lines with indentation and stuff.",
|
||||
},
|
||||
},
|
||||
"required": ["x", "y"],
|
||||
},
|
||||
}
|
||||
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_return_none(self):
|
||||
def fn(x: int) -> None:
|
||||
"""
|
||||
Test function
|
||||
|
||||
Args:
|
||||
x: The first input
|
||||
"""
|
||||
pass
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"x": {"type": "integer", "description": "The first input"},
|
||||
},
|
||||
"required": ["x"],
|
||||
},
|
||||
"return": {"type": "null"},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
|
||||
def test_everything_all_at_once(self):
|
||||
def fn(
|
||||
x: str, y: Optional[list[Union[str, int]]], z: tuple[Union[str, int], str] = (42, "hello")
|
||||
) -> tuple[int, str]:
|
||||
"""
|
||||
Test function with multiple args, and docstring args that we have to strip out.
|
||||
|
||||
Args:
|
||||
x: The first input. It's got a big multiline
|
||||
description and also contains
|
||||
(choices: ["a", "b", "c"])
|
||||
|
||||
y: The second input. It's a big list with a single-line description.
|
||||
|
||||
z: The third input. It's some kind of tuple with a default arg.
|
||||
|
||||
Returns:
|
||||
The output. The return description is also a big multiline
|
||||
description that spans multiple lines.
|
||||
"""
|
||||
pass
|
||||
|
||||
schema = get_json_schema(fn)
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function with multiple args, and docstring args that we have to strip out.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"x": {
|
||||
"type": "string",
|
||||
"enum": ["a", "b", "c"],
|
||||
"description": "The first input. It's got a big multiline description and also contains",
|
||||
},
|
||||
"y": {
|
||||
"type": "array",
|
||||
"items": {"type": ["integer", "string"]},
|
||||
"nullable": True,
|
||||
"description": "The second input. It's a big list with a single-line description.",
|
||||
},
|
||||
"z": {
|
||||
"type": "array",
|
||||
"prefixItems": [{"type": ["integer", "string"]}, {"type": "string"}],
|
||||
"description": "The third input. It's some kind of tuple with a default arg.",
|
||||
},
|
||||
},
|
||||
"required": ["x", "y"],
|
||||
},
|
||||
"return": {
|
||||
"type": "array",
|
||||
"prefixItems": [{"type": "integer"}, {"type": "string"}],
|
||||
"description": "The output. The return description is also a big multiline\n description that spans multiple lines.",
|
||||
},
|
||||
}
|
||||
self.assertEqual(schema["function"], expected_schema)
|
||||
77
transformers/tests/utils/test_cli.py
Normal file
77
transformers/tests/utils/test_cli.py
Normal file
@@ -0,0 +1,77 @@
|
||||
# Copyright 2019-present, the HuggingFace Inc. team.
|
||||
#
|
||||
# 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 os
|
||||
import shutil
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from transformers.testing_utils import CaptureStd, require_torch
|
||||
|
||||
|
||||
class CLITest(unittest.TestCase):
|
||||
@patch("sys.argv", ["fakeprogrampath", "env"])
|
||||
def test_cli_env(self):
|
||||
# test transformers env
|
||||
import transformers.commands.transformers_cli
|
||||
|
||||
with CaptureStd() as cs:
|
||||
transformers.commands.transformers_cli.main()
|
||||
self.assertIn("Python version", cs.out)
|
||||
self.assertIn("Platform", cs.out)
|
||||
self.assertIn("Using distributed or parallel set-up in script?", cs.out)
|
||||
|
||||
@require_torch
|
||||
@patch("sys.argv", ["fakeprogrampath", "download", "hf-internal-testing/tiny-random-gptj", "--cache-dir", "/tmp"])
|
||||
def test_cli_download(self):
|
||||
import transformers.commands.transformers_cli
|
||||
|
||||
# # remove any previously downloaded model to start clean
|
||||
shutil.rmtree("/tmp/models--hf-internal-testing--tiny-random-gptj", ignore_errors=True)
|
||||
|
||||
# run the command
|
||||
transformers.commands.transformers_cli.main()
|
||||
|
||||
# check if the model files are downloaded correctly on /tmp/models--hf-internal-testing--tiny-random-gptj
|
||||
self.assertTrue(os.path.exists("/tmp/models--hf-internal-testing--tiny-random-gptj/blobs"))
|
||||
self.assertTrue(os.path.exists("/tmp/models--hf-internal-testing--tiny-random-gptj/refs"))
|
||||
self.assertTrue(os.path.exists("/tmp/models--hf-internal-testing--tiny-random-gptj/snapshots"))
|
||||
|
||||
@require_torch
|
||||
@patch(
|
||||
"sys.argv",
|
||||
[
|
||||
"fakeprogrampath",
|
||||
"download",
|
||||
"hf-internal-testing/test_dynamic_model_with_tokenizer",
|
||||
"--trust-remote-code",
|
||||
"--cache-dir",
|
||||
"/tmp",
|
||||
],
|
||||
)
|
||||
def test_cli_download_trust_remote(self):
|
||||
import transformers.commands.transformers_cli
|
||||
|
||||
# # remove any previously downloaded model to start clean
|
||||
shutil.rmtree("/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer", ignore_errors=True)
|
||||
|
||||
# run the command
|
||||
transformers.commands.transformers_cli.main()
|
||||
|
||||
# check if the model files are downloaded correctly on /tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer
|
||||
self.assertTrue(os.path.exists("/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/blobs"))
|
||||
self.assertTrue(os.path.exists("/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/refs"))
|
||||
self.assertTrue(
|
||||
os.path.exists("/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/snapshots")
|
||||
)
|
||||
358
transformers/tests/utils/test_configuration_utils.py
Normal file
358
transformers/tests/utils/test_configuration_utils.py
Normal file
@@ -0,0 +1,358 @@
|
||||
# Copyright 2019 HuggingFace Inc.
|
||||
#
|
||||
# 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 shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
import warnings
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
|
||||
from transformers import AutoConfig, BertConfig, Florence2Config, GPT2Config
|
||||
from transformers.configuration_utils import PretrainedConfig
|
||||
from transformers.testing_utils import TOKEN, TemporaryHubRepo, is_staging_test, require_torch
|
||||
|
||||
|
||||
sys.path.append(str(Path(__file__).parent.parent.parent / "utils"))
|
||||
|
||||
from test_module.custom_configuration import CustomConfig # noqa E402
|
||||
|
||||
|
||||
config_common_kwargs = {
|
||||
"return_dict": False,
|
||||
"output_hidden_states": True,
|
||||
"output_attentions": True,
|
||||
"torchscript": True,
|
||||
"dtype": "float16",
|
||||
"pruned_heads": {"a": 1},
|
||||
"tie_word_embeddings": False,
|
||||
"is_decoder": True,
|
||||
"cross_attention_hidden_size": 128,
|
||||
"add_cross_attention": True,
|
||||
"tie_encoder_decoder": True,
|
||||
"max_length": 50,
|
||||
"min_length": 3,
|
||||
"do_sample": True,
|
||||
"early_stopping": True,
|
||||
"num_beams": 3,
|
||||
"num_beam_groups": 3,
|
||||
"diversity_penalty": 0.5,
|
||||
"temperature": 2.0,
|
||||
"top_k": 10,
|
||||
"top_p": 0.7,
|
||||
"typical_p": 0.2,
|
||||
"repetition_penalty": 0.8,
|
||||
"length_penalty": 0.8,
|
||||
"no_repeat_ngram_size": 5,
|
||||
"encoder_no_repeat_ngram_size": 5,
|
||||
"bad_words_ids": [1, 2, 3],
|
||||
"num_return_sequences": 3,
|
||||
"chunk_size_feed_forward": 5,
|
||||
"output_scores": True,
|
||||
"return_dict_in_generate": True,
|
||||
"forced_bos_token_id": 2,
|
||||
"forced_eos_token_id": 3,
|
||||
"remove_invalid_values": True,
|
||||
"architectures": ["BertModel"],
|
||||
"finetuning_task": "translation",
|
||||
"id2label": {0: "label"},
|
||||
"label2id": {"label": "0"},
|
||||
"tokenizer_class": "BertTokenizerFast",
|
||||
"prefix": "prefix",
|
||||
"bos_token_id": 6,
|
||||
"pad_token_id": 7,
|
||||
"eos_token_id": 8,
|
||||
"sep_token_id": 9,
|
||||
"decoder_start_token_id": 10,
|
||||
"exponential_decay_length_penalty": (5, 1.01),
|
||||
"suppress_tokens": [0, 1],
|
||||
"begin_suppress_tokens": 2,
|
||||
"task_specific_params": {"translation": "some_params"},
|
||||
"problem_type": "regression",
|
||||
}
|
||||
|
||||
|
||||
@is_staging_test
|
||||
class ConfigPushToHubTester(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls._token = TOKEN
|
||||
|
||||
def test_push_to_hub(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
config = BertConfig(
|
||||
vocab_size=99, hidden_size=32, num_hidden_layers=5, num_attention_heads=4, intermediate_size=37
|
||||
)
|
||||
config.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
new_config = BertConfig.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in config.to_dict().items():
|
||||
if k != "transformers_version":
|
||||
self.assertEqual(v, getattr(new_config, k))
|
||||
|
||||
def test_push_to_hub_via_save_pretrained(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
config = BertConfig(
|
||||
vocab_size=99, hidden_size=32, num_hidden_layers=5, num_attention_heads=4, intermediate_size=37
|
||||
)
|
||||
# Push to hub via save_pretrained
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
config.save_pretrained(tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token)
|
||||
|
||||
new_config = BertConfig.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in config.to_dict().items():
|
||||
if k != "transformers_version":
|
||||
self.assertEqual(v, getattr(new_config, k))
|
||||
|
||||
def test_push_to_hub_in_organization(self):
|
||||
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
|
||||
config = BertConfig(
|
||||
vocab_size=99, hidden_size=32, num_hidden_layers=5, num_attention_heads=4, intermediate_size=37
|
||||
)
|
||||
config.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
new_config = BertConfig.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in config.to_dict().items():
|
||||
if k != "transformers_version":
|
||||
self.assertEqual(v, getattr(new_config, k))
|
||||
|
||||
def test_push_to_hub_in_organization_via_save_pretrained(self):
|
||||
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
|
||||
config = BertConfig(
|
||||
vocab_size=99, hidden_size=32, num_hidden_layers=5, num_attention_heads=4, intermediate_size=37
|
||||
)
|
||||
# Push to hub via save_pretrained
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
config.save_pretrained(tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token)
|
||||
|
||||
new_config = BertConfig.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in config.to_dict().items():
|
||||
if k != "transformers_version":
|
||||
self.assertEqual(v, getattr(new_config, k))
|
||||
|
||||
def test_push_to_hub_dynamic_config(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
CustomConfig.register_for_auto_class()
|
||||
config = CustomConfig(attribute=42)
|
||||
|
||||
config.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
# This has added the proper auto_map field to the config
|
||||
self.assertDictEqual(config.auto_map, {"AutoConfig": "custom_configuration.CustomConfig"})
|
||||
|
||||
new_config = AutoConfig.from_pretrained(tmp_repo.repo_id, trust_remote_code=True)
|
||||
# Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module
|
||||
self.assertEqual(new_config.__class__.__name__, "CustomConfig")
|
||||
self.assertEqual(new_config.attribute, 42)
|
||||
|
||||
|
||||
class ConfigTestUtils(unittest.TestCase):
|
||||
def test_config_from_string(self):
|
||||
c = GPT2Config()
|
||||
|
||||
# attempt to modify each of int/float/bool/str config records and verify they were updated
|
||||
n_embd = c.n_embd + 1 # int
|
||||
resid_pdrop = c.resid_pdrop + 1.0 # float
|
||||
scale_attn_weights = not c.scale_attn_weights # bool
|
||||
summary_type = c.summary_type + "foo" # str
|
||||
c.update_from_string(
|
||||
f"n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}"
|
||||
)
|
||||
self.assertEqual(n_embd, c.n_embd, "mismatch for key: n_embd")
|
||||
self.assertEqual(resid_pdrop, c.resid_pdrop, "mismatch for key: resid_pdrop")
|
||||
self.assertEqual(scale_attn_weights, c.scale_attn_weights, "mismatch for key: scale_attn_weights")
|
||||
self.assertEqual(summary_type, c.summary_type, "mismatch for key: summary_type")
|
||||
|
||||
def test_config_common_kwargs_is_complete(self):
|
||||
base_config = PretrainedConfig()
|
||||
missing_keys = [key for key in base_config.__dict__ if key not in config_common_kwargs]
|
||||
# If this part of the test fails, you have arguments to add in config_common_kwargs above.
|
||||
self.assertListEqual(
|
||||
missing_keys,
|
||||
[
|
||||
"_output_attentions",
|
||||
"is_encoder_decoder",
|
||||
"_name_or_path",
|
||||
"_commit_hash",
|
||||
"_attn_implementation_internal",
|
||||
"transformers_version",
|
||||
],
|
||||
)
|
||||
keys_with_defaults = [key for key, value in config_common_kwargs.items() if value == getattr(base_config, key)]
|
||||
if len(keys_with_defaults) > 0:
|
||||
raise ValueError(
|
||||
"The following keys are set with the default values in"
|
||||
" `test_configuration_common.config_common_kwargs` pick another value for them:"
|
||||
f" {', '.join(keys_with_defaults)}."
|
||||
)
|
||||
|
||||
def test_nested_config_load_from_dict(self):
|
||||
config = AutoConfig.from_pretrained(
|
||||
"hf-internal-testing/tiny-random-CLIPModel", text_config={"num_hidden_layers": 2}
|
||||
)
|
||||
self.assertNotIsInstance(config.text_config, dict)
|
||||
self.assertEqual(config.text_config.__class__.__name__, "CLIPTextConfig")
|
||||
|
||||
def test_from_pretrained_subfolder(self):
|
||||
config = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert-subfolder")
|
||||
self.assertIsNotNone(config)
|
||||
|
||||
config = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert-subfolder", subfolder="bert")
|
||||
self.assertIsNotNone(config)
|
||||
|
||||
def test_cached_files_are_used_when_internet_is_down(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 = httpx.HTTPStatusError(
|
||||
"failed", request=mock.Mock(), response=mock.Mock()
|
||||
)
|
||||
response_mock.json.return_value = {}
|
||||
|
||||
# Download this model to make sure it's in the cache.
|
||||
_ = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert")
|
||||
|
||||
# Under the mock environment we get a 500 error when trying to reach the model.
|
||||
with mock.patch("httpx.Client.request", return_value=response_mock) as mock_head:
|
||||
_ = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert")
|
||||
# This check we did call the fake head request
|
||||
mock_head.assert_called()
|
||||
|
||||
def test_local_versioning(self):
|
||||
configuration = AutoConfig.from_pretrained("google-bert/bert-base-cased")
|
||||
configuration.configuration_files = ["config.4.0.0.json"]
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
configuration.save_pretrained(tmp_dir)
|
||||
configuration.hidden_size = 2
|
||||
json.dump(configuration.to_dict(), open(os.path.join(tmp_dir, "config.4.0.0.json"), "w"))
|
||||
|
||||
# This should pick the new configuration file as the version of Transformers is > 4.0.0
|
||||
new_configuration = AutoConfig.from_pretrained(tmp_dir)
|
||||
self.assertEqual(new_configuration.hidden_size, 2)
|
||||
|
||||
# Will need to be adjusted if we reach v42 and this test is still here.
|
||||
# Should pick the old configuration file as the version of Transformers is < 4.42.0
|
||||
configuration.configuration_files = ["config.42.0.0.json"]
|
||||
configuration.hidden_size = 768
|
||||
configuration.save_pretrained(tmp_dir)
|
||||
shutil.move(os.path.join(tmp_dir, "config.4.0.0.json"), os.path.join(tmp_dir, "config.42.0.0.json"))
|
||||
new_configuration = AutoConfig.from_pretrained(tmp_dir)
|
||||
self.assertEqual(new_configuration.hidden_size, 768)
|
||||
|
||||
def test_repo_versioning_before(self):
|
||||
# This repo has two configuration files, one for v4.0.0 and above with a different hidden size.
|
||||
repo = "hf-internal-testing/test-two-configs"
|
||||
|
||||
import transformers as new_transformers
|
||||
|
||||
new_transformers.configuration_utils.__version__ = "v4.0.0"
|
||||
new_configuration, kwargs = new_transformers.models.auto.AutoConfig.from_pretrained(
|
||||
repo, return_unused_kwargs=True
|
||||
)
|
||||
self.assertEqual(new_configuration.hidden_size, 2)
|
||||
# This checks `_configuration_file` ia not kept in the kwargs by mistake.
|
||||
self.assertDictEqual(kwargs, {})
|
||||
|
||||
# Testing an older version by monkey-patching the version in the module it's used.
|
||||
import transformers as old_transformers
|
||||
|
||||
old_transformers.configuration_utils.__version__ = "v3.0.0"
|
||||
old_configuration = old_transformers.models.auto.AutoConfig.from_pretrained(repo)
|
||||
self.assertEqual(old_configuration.hidden_size, 768)
|
||||
|
||||
def test_saving_config_with_custom_generation_kwargs_raises_warning(self):
|
||||
config = BertConfig(min_length=3) # `min_length = 3` is a non-default generation kwarg
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
with self.assertWarns(UserWarning) as cm:
|
||||
config.save_pretrained(tmp_dir)
|
||||
self.assertIn("min_length", str(cm.warning))
|
||||
|
||||
def test_get_non_default_generation_parameters(self):
|
||||
config = BertConfig()
|
||||
self.assertFalse(len(config._get_non_default_generation_parameters()) > 0)
|
||||
config = BertConfig(min_length=3)
|
||||
self.assertTrue(len(config._get_non_default_generation_parameters()) > 0)
|
||||
config = BertConfig(min_length=0) # `min_length = 0` is a default generation kwarg
|
||||
self.assertFalse(len(config._get_non_default_generation_parameters()) > 0)
|
||||
|
||||
def test_loading_config_do_not_raise_future_warnings(self):
|
||||
"""Regression test for https://github.com/huggingface/transformers/issues/31002."""
|
||||
# Loading config should not raise a FutureWarning. It was the case before.
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("error")
|
||||
PretrainedConfig.from_pretrained("bert-base-uncased")
|
||||
|
||||
def test_get_text_config(self):
|
||||
"""Tests the `get_text_config` method."""
|
||||
# 1. model with only text input -> returns the original config instance
|
||||
config = AutoConfig.from_pretrained("hf-internal-testing/tiny-random-LlamaForCausalLM")
|
||||
self.assertEqual(config.get_text_config(), config)
|
||||
self.assertEqual(config.get_text_config(decoder=True), config)
|
||||
|
||||
# 2. composite model (VLM) -> returns the text component
|
||||
config = AutoConfig.from_pretrained("hf-internal-testing/tiny-random-LlavaForConditionalGeneration")
|
||||
self.assertEqual(config.get_text_config(), config.text_config)
|
||||
self.assertEqual(config.get_text_config(decoder=True), config.text_config)
|
||||
|
||||
# 3. ! corner case! : composite model whose sub-config is an old composite model (should behave as above)
|
||||
config = Florence2Config()
|
||||
self.assertEqual(config.get_text_config(), config.text_config)
|
||||
self.assertEqual(config.get_text_config(decoder=True), config.text_config)
|
||||
|
||||
# 4. old composite model -> may remove components based on the `decoder` or `encoder` argument
|
||||
config = AutoConfig.from_pretrained("hf-internal-testing/tiny-random-bart")
|
||||
self.assertEqual(config.get_text_config(), config)
|
||||
# both encoder_layers and decoder_layers exist
|
||||
self.assertTrue(getattr(config, "encoder_layers", None) is not None)
|
||||
self.assertTrue(getattr(config, "decoder_layers", None) is not None)
|
||||
decoder_config = config.get_text_config(decoder=True)
|
||||
self.assertNotEqual(decoder_config, config)
|
||||
self.assertEqual(decoder_config.num_hidden_layers, config.decoder_layers)
|
||||
self.assertTrue(getattr(decoder_config, "encoder_layers", None) is None) # encoder_layers is removed
|
||||
encoder_config = config.get_text_config(encoder=True)
|
||||
self.assertNotEqual(encoder_config, config)
|
||||
self.assertEqual(encoder_config.num_hidden_layers, config.encoder_layers)
|
||||
self.assertTrue(getattr(encoder_config, "decoder_layers", None) is None) # decoder_layers is removed
|
||||
|
||||
@require_torch
|
||||
def test_bc_torch_dtype(self):
|
||||
import torch
|
||||
|
||||
config = PretrainedConfig(dtype="bfloat16")
|
||||
self.assertEqual(config.dtype, torch.bfloat16)
|
||||
|
||||
config = PretrainedConfig(torch_dtype="bfloat16")
|
||||
self.assertEqual(config.dtype, torch.bfloat16)
|
||||
|
||||
# Check that if we pass both, `dtype` is used
|
||||
config = PretrainedConfig(dtype="bfloat16", torch_dtype="float32")
|
||||
self.assertEqual(config.dtype, torch.bfloat16)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
config.save_pretrained(tmpdirname)
|
||||
|
||||
config = PretrainedConfig.from_pretrained(tmpdirname)
|
||||
self.assertEqual(config.dtype, torch.bfloat16)
|
||||
|
||||
config = PretrainedConfig.from_pretrained(tmpdirname, dtype="float32")
|
||||
self.assertEqual(config.dtype, "float32")
|
||||
|
||||
config = PretrainedConfig.from_pretrained(tmpdirname, torch_dtype="float32")
|
||||
self.assertEqual(config.dtype, "float32")
|
||||
39
transformers/tests/utils/test_convert_slow_tokenizer.py
Normal file
39
transformers/tests/utils/test_convert_slow_tokenizer.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import unittest
|
||||
import warnings
|
||||
from dataclasses import dataclass
|
||||
|
||||
from transformers.convert_slow_tokenizer import SpmConverter
|
||||
from transformers.testing_utils import get_tests_dir
|
||||
|
||||
|
||||
@dataclass
|
||||
class FakeOriginalTokenizer:
|
||||
vocab_file: str
|
||||
|
||||
|
||||
class ConvertSlowTokenizerTest(unittest.TestCase):
|
||||
def test_spm_converter_bytefallback_warning(self):
|
||||
spm_model_file_without_bytefallback = get_tests_dir("fixtures/test_sentencepiece.model")
|
||||
spm_model_file_with_bytefallback = get_tests_dir("fixtures/test_sentencepiece_with_bytefallback.model")
|
||||
|
||||
original_tokenizer_without_bytefallback = FakeOriginalTokenizer(vocab_file=spm_model_file_without_bytefallback)
|
||||
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
_ = SpmConverter(original_tokenizer_without_bytefallback)
|
||||
# We are looking for if there is any `UserWarning` with
|
||||
# `The sentencepiece tokenizer that you are converting to a fast tokenizer uses the byte fallback option which is not implemented in the fast tokenizers.`
|
||||
w = [x for x in w if x.category.__name__ != "DeprecationWarning"]
|
||||
self.assertEqual(len(w), 0)
|
||||
|
||||
original_tokenizer_with_bytefallback = FakeOriginalTokenizer(vocab_file=spm_model_file_with_bytefallback)
|
||||
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
_ = SpmConverter(original_tokenizer_with_bytefallback)
|
||||
w = [x for x in w if x.category.__name__ != "DeprecationWarning"]
|
||||
self.assertEqual(len(w), 1)
|
||||
|
||||
self.assertIn(
|
||||
"The sentencepiece tokenizer that you are converting to a fast tokenizer uses the byte fallback option"
|
||||
" which is not implemented in the fast tokenizers.",
|
||||
str(w[0].message),
|
||||
)
|
||||
197
transformers/tests/utils/test_deprecation.py
Normal file
197
transformers/tests/utils/test_deprecation.py
Normal file
@@ -0,0 +1,197 @@
|
||||
# Copyright 2024 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 unittest
|
||||
import warnings
|
||||
|
||||
import pytest
|
||||
from parameterized import parameterized
|
||||
|
||||
from transformers import __version__, is_torch_available
|
||||
from transformers.testing_utils import require_torch_accelerator, torch_device
|
||||
from transformers.utils.deprecation import deprecate_kwarg
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
|
||||
INFINITE_VERSION = "9999.0.0"
|
||||
|
||||
|
||||
class DeprecationDecoratorTester(unittest.TestCase):
|
||||
def test_rename_kwarg(self):
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore")
|
||||
|
||||
@deprecate_kwarg("deprecated_name", new_name="new_name", version=INFINITE_VERSION)
|
||||
def dummy_function(new_name=None, other_name=None):
|
||||
return new_name, other_name
|
||||
|
||||
# Test keyword argument is renamed
|
||||
value, other_value = dummy_function(deprecated_name="old_value")
|
||||
self.assertEqual(value, "old_value")
|
||||
self.assertIsNone(other_value)
|
||||
|
||||
# Test deprecated keyword argument not passed
|
||||
value, other_value = dummy_function(new_name="new_value")
|
||||
self.assertEqual(value, "new_value")
|
||||
self.assertIsNone(other_value)
|
||||
|
||||
# Test other keyword argument
|
||||
value, other_value = dummy_function(other_name="other_value")
|
||||
self.assertIsNone(value)
|
||||
self.assertEqual(other_value, "other_value")
|
||||
|
||||
# Test deprecated and new args are passed, the new one should be returned
|
||||
value, other_value = dummy_function(deprecated_name="old_value", new_name="new_value")
|
||||
self.assertEqual(value, "new_value")
|
||||
self.assertIsNone(other_value)
|
||||
|
||||
def test_rename_multiple_kwargs(self):
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore")
|
||||
|
||||
@deprecate_kwarg("deprecated_name1", new_name="new_name1", version=INFINITE_VERSION)
|
||||
@deprecate_kwarg("deprecated_name2", new_name="new_name2", version=INFINITE_VERSION)
|
||||
def dummy_function(new_name1=None, new_name2=None, other_name=None):
|
||||
return new_name1, new_name2, other_name
|
||||
|
||||
# Test keyword argument is renamed
|
||||
value1, value2, other_value = dummy_function(deprecated_name1="old_value1", deprecated_name2="old_value2")
|
||||
self.assertEqual(value1, "old_value1")
|
||||
self.assertEqual(value2, "old_value2")
|
||||
self.assertIsNone(other_value)
|
||||
|
||||
# Test deprecated keyword argument is not passed
|
||||
value1, value2, other_value = dummy_function(new_name1="new_value1", new_name2="new_value2")
|
||||
self.assertEqual(value1, "new_value1")
|
||||
self.assertEqual(value2, "new_value2")
|
||||
self.assertIsNone(other_value)
|
||||
|
||||
# Test other keyword argument is passed and correctly returned
|
||||
value1, value2, other_value = dummy_function(other_name="other_value")
|
||||
self.assertIsNone(value1)
|
||||
self.assertIsNone(value2)
|
||||
self.assertEqual(other_value, "other_value")
|
||||
|
||||
def test_warnings(self):
|
||||
# Test warning is raised for future version
|
||||
@deprecate_kwarg("deprecated_name", new_name="new_name", version=INFINITE_VERSION)
|
||||
def dummy_function(new_name=None, other_name=None):
|
||||
return new_name, other_name
|
||||
|
||||
with self.assertWarns(FutureWarning):
|
||||
dummy_function(deprecated_name="old_value")
|
||||
|
||||
# Test warning is not raised for past version, but arg is still renamed
|
||||
@deprecate_kwarg("deprecated_name", new_name="new_name", version="0.0.0")
|
||||
def dummy_function(new_name=None, other_name=None):
|
||||
return new_name, other_name
|
||||
|
||||
with warnings.catch_warnings(record=True) as raised_warnings:
|
||||
warnings.simplefilter("always")
|
||||
|
||||
value, other_value = dummy_function(deprecated_name="old_value")
|
||||
|
||||
self.assertEqual(value, "old_value")
|
||||
self.assertIsNone(other_value)
|
||||
self.assertEqual(len(raised_warnings), 0, f"Warning raised: {[w.message for w in raised_warnings]}")
|
||||
|
||||
# Test warning is raised for future version if warn_if_greater_or_equal_version is set
|
||||
@deprecate_kwarg("deprecated_name", version="0.0.0", warn_if_greater_or_equal_version=True)
|
||||
def dummy_function(deprecated_name=None):
|
||||
return deprecated_name
|
||||
|
||||
with self.assertWarns(FutureWarning):
|
||||
value = dummy_function(deprecated_name="deprecated_value")
|
||||
self.assertEqual(value, "deprecated_value")
|
||||
|
||||
# Test arg is not renamed if new_name is not specified, but warning is raised
|
||||
@deprecate_kwarg("deprecated_name", version=INFINITE_VERSION)
|
||||
def dummy_function(deprecated_name=None):
|
||||
return deprecated_name
|
||||
|
||||
with self.assertWarns(FutureWarning):
|
||||
value = dummy_function(deprecated_name="deprecated_value")
|
||||
self.assertEqual(value, "deprecated_value")
|
||||
|
||||
def test_raises(self):
|
||||
# Test if deprecated name and new name are both passed and raise_if_both_names is set -> raise error
|
||||
@deprecate_kwarg("deprecated_name", new_name="new_name", version=INFINITE_VERSION, raise_if_both_names=True)
|
||||
def dummy_function(new_name=None, other_name=None):
|
||||
return new_name, other_name
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
dummy_function(deprecated_name="old_value", new_name="new_value")
|
||||
|
||||
# Test for current version == deprecation version
|
||||
@deprecate_kwarg("deprecated_name", version=__version__, raise_if_greater_or_equal_version=True)
|
||||
def dummy_function(deprecated_name=None):
|
||||
return deprecated_name
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
dummy_function(deprecated_name="old_value")
|
||||
|
||||
# Test for current version > deprecation version
|
||||
@deprecate_kwarg("deprecated_name", version="0.0.0", raise_if_greater_or_equal_version=True)
|
||||
def dummy_function(deprecated_name=None):
|
||||
return deprecated_name
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
dummy_function(deprecated_name="old_value")
|
||||
|
||||
def test_additional_message(self):
|
||||
# Test additional message is added to the warning
|
||||
@deprecate_kwarg("deprecated_name", version=INFINITE_VERSION, additional_message="Additional message")
|
||||
def dummy_function(deprecated_name=None):
|
||||
return deprecated_name
|
||||
|
||||
with warnings.catch_warnings(record=True) as raised_warnings:
|
||||
warnings.simplefilter("always")
|
||||
dummy_function(deprecated_name="old_value")
|
||||
|
||||
self.assertTrue("Additional message" in str(raised_warnings[0].message))
|
||||
|
||||
@parameterized.expand(["0.0.0", __version__, INFINITE_VERSION])
|
||||
def test_warning_for_both_names(self, version):
|
||||
# We should raise warning if both names are passed for any specified version
|
||||
@deprecate_kwarg("deprecated_name", new_name="new_name", version=version)
|
||||
def dummy_function(new_name=None, **kwargs):
|
||||
return new_name
|
||||
|
||||
with self.assertWarns(FutureWarning):
|
||||
result = dummy_function(deprecated_name="old_value", new_name="new_value")
|
||||
self.assertEqual(result, "new_value")
|
||||
|
||||
@pytest.mark.torch_compile_test
|
||||
@require_torch_accelerator
|
||||
def test_compile_safe(self):
|
||||
@deprecate_kwarg("deprecated_factor", new_name="new_factor", version=INFINITE_VERSION)
|
||||
def dummy_function(new_factor=None, **kwargs):
|
||||
return new_factor * torch.ones(1, device=torch_device)
|
||||
|
||||
compiled_function = torch.compile(dummy_function, fullgraph=True)
|
||||
|
||||
# Check that we can correctly call the compiled function with the old name, without raising errors
|
||||
out = compiled_function(deprecated_factor=2)
|
||||
self.assertEqual(out.item(), 2)
|
||||
|
||||
# Check that we can correctly call the compiled function with the new name, without raising errors
|
||||
out = compiled_function(new_factor=2)
|
||||
self.assertEqual(out.item(), 2)
|
||||
|
||||
# Check that we can correctly call the compiled function with both names, without raising errors
|
||||
out = compiled_function(new_factor=2, deprecated_factor=10)
|
||||
self.assertEqual(out.item(), 2)
|
||||
111
transformers/tests/utils/test_doc_samples.py
Normal file
111
transformers/tests/utils/test_doc_samples.py
Normal file
@@ -0,0 +1,111 @@
|
||||
# Copyright 2019-present, the HuggingFace Inc. team.
|
||||
#
|
||||
# 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 doctest
|
||||
import logging
|
||||
import os
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from typing import Union
|
||||
|
||||
import transformers
|
||||
from transformers.testing_utils import require_torch, slow
|
||||
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
@unittest.skip(reason="Temporarily disable the doc tests.")
|
||||
@require_torch
|
||||
@slow
|
||||
class TestCodeExamples(unittest.TestCase):
|
||||
def analyze_directory(
|
||||
self,
|
||||
directory: Path,
|
||||
identifier: Union[str, None] = None,
|
||||
ignore_files: Union[list[str], None] = None,
|
||||
n_identifier: Union[str, list[str], None] = None,
|
||||
only_modules: bool = True,
|
||||
):
|
||||
"""
|
||||
Runs through the specific directory, looking for the files identified with `identifier`. Executes
|
||||
the doctests in those files
|
||||
|
||||
Args:
|
||||
directory (`Path`): Directory containing the files
|
||||
identifier (`str`): Will parse files containing this
|
||||
ignore_files (`List[str]`): List of files to skip
|
||||
n_identifier (`str` or `List[str]`): Will not parse files containing this/these identifiers.
|
||||
only_modules (`bool`): Whether to only analyze modules
|
||||
"""
|
||||
files = [file for file in os.listdir(directory) if os.path.isfile(os.path.join(directory, file))]
|
||||
|
||||
if identifier is not None:
|
||||
files = [file for file in files if identifier in file]
|
||||
|
||||
if n_identifier is not None:
|
||||
if isinstance(n_identifier, list):
|
||||
for n_ in n_identifier:
|
||||
files = [file for file in files if n_ not in file]
|
||||
else:
|
||||
files = [file for file in files if n_identifier not in file]
|
||||
|
||||
ignore_files = ignore_files or []
|
||||
ignore_files.append("__init__.py")
|
||||
files = [file for file in files if file not in ignore_files]
|
||||
|
||||
for file in files:
|
||||
# Open all files
|
||||
print("Testing", file)
|
||||
|
||||
if only_modules:
|
||||
module_identifier = file.split(".")[0]
|
||||
try:
|
||||
module_identifier = getattr(transformers, module_identifier)
|
||||
suite = doctest.DocTestSuite(module_identifier)
|
||||
result = unittest.TextTestRunner().run(suite)
|
||||
self.assertIs(len(result.failures), 0)
|
||||
except AttributeError:
|
||||
logger.info(f"{module_identifier} is not a module.")
|
||||
else:
|
||||
result = doctest.testfile(str(".." / directory / file), optionflags=doctest.ELLIPSIS)
|
||||
self.assertIs(result.failed, 0)
|
||||
|
||||
def test_modeling_examples(self):
|
||||
transformers_directory = Path("src/transformers")
|
||||
files = "modeling"
|
||||
ignore_files = [
|
||||
"modeling_ctrl.py",
|
||||
"modeling_tf_ctrl.py",
|
||||
]
|
||||
self.analyze_directory(transformers_directory, identifier=files, ignore_files=ignore_files)
|
||||
|
||||
def test_tokenization_examples(self):
|
||||
transformers_directory = Path("src/transformers")
|
||||
files = "tokenization"
|
||||
self.analyze_directory(transformers_directory, identifier=files)
|
||||
|
||||
def test_configuration_examples(self):
|
||||
transformers_directory = Path("src/transformers")
|
||||
files = "configuration"
|
||||
self.analyze_directory(transformers_directory, identifier=files)
|
||||
|
||||
def test_remaining_examples(self):
|
||||
transformers_directory = Path("src/transformers")
|
||||
n_identifiers = ["configuration", "modeling", "tokenization"]
|
||||
self.analyze_directory(transformers_directory, n_identifier=n_identifiers)
|
||||
|
||||
def test_doc_sources(self):
|
||||
doc_source_directory = Path("docs/source")
|
||||
ignore_files = ["favicon.ico"]
|
||||
self.analyze_directory(doc_source_directory, ignore_files=ignore_files, only_modules=False)
|
||||
129
transformers/tests/utils/test_dynamic_module_utils.py
Normal file
129
transformers/tests/utils/test_dynamic_module_utils.py
Normal file
@@ -0,0 +1,129 @@
|
||||
# Copyright 2023 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 os
|
||||
|
||||
import pytest
|
||||
|
||||
from transformers.dynamic_module_utils import get_imports
|
||||
|
||||
|
||||
TOP_LEVEL_IMPORT = """
|
||||
import os
|
||||
"""
|
||||
|
||||
IMPORT_IN_FUNCTION = """
|
||||
def foo():
|
||||
import os
|
||||
return False
|
||||
"""
|
||||
|
||||
DEEPLY_NESTED_IMPORT = """
|
||||
def foo():
|
||||
def bar():
|
||||
if True:
|
||||
import os
|
||||
return False
|
||||
return bar()
|
||||
"""
|
||||
|
||||
TOP_LEVEL_TRY_IMPORT = """
|
||||
import os
|
||||
|
||||
try:
|
||||
import bar
|
||||
except ImportError:
|
||||
raise ValueError()
|
||||
"""
|
||||
|
||||
TRY_IMPORT_IN_FUNCTION = """
|
||||
import os
|
||||
|
||||
def foo():
|
||||
try:
|
||||
import bar
|
||||
except ImportError:
|
||||
raise ValueError()
|
||||
"""
|
||||
|
||||
MULTIPLE_EXCEPTS_IMPORT = """
|
||||
import os
|
||||
|
||||
try:
|
||||
import bar
|
||||
except (ImportError, AttributeError):
|
||||
raise ValueError()
|
||||
"""
|
||||
|
||||
EXCEPT_AS_IMPORT = """
|
||||
import os
|
||||
|
||||
try:
|
||||
import bar
|
||||
except ImportError as e:
|
||||
raise ValueError()
|
||||
"""
|
||||
|
||||
GENERIC_EXCEPT_IMPORT = """
|
||||
import os
|
||||
|
||||
try:
|
||||
import bar
|
||||
except:
|
||||
raise ValueError()
|
||||
"""
|
||||
|
||||
MULTILINE_TRY_IMPORT = """
|
||||
import os
|
||||
|
||||
try:
|
||||
import bar
|
||||
import baz
|
||||
except ImportError:
|
||||
raise ValueError()
|
||||
"""
|
||||
|
||||
MULTILINE_BOTH_IMPORT = """
|
||||
import os
|
||||
|
||||
try:
|
||||
import bar
|
||||
import baz
|
||||
except ImportError:
|
||||
x = 1
|
||||
raise ValueError()
|
||||
"""
|
||||
|
||||
CASES = [
|
||||
TOP_LEVEL_IMPORT,
|
||||
IMPORT_IN_FUNCTION,
|
||||
DEEPLY_NESTED_IMPORT,
|
||||
TOP_LEVEL_TRY_IMPORT,
|
||||
GENERIC_EXCEPT_IMPORT,
|
||||
MULTILINE_TRY_IMPORT,
|
||||
MULTILINE_BOTH_IMPORT,
|
||||
MULTIPLE_EXCEPTS_IMPORT,
|
||||
EXCEPT_AS_IMPORT,
|
||||
TRY_IMPORT_IN_FUNCTION,
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("case", CASES)
|
||||
def test_import_parsing(tmp_path, case):
|
||||
tmp_file_path = os.path.join(tmp_path, "test_file.py")
|
||||
with open(tmp_file_path, "w") as _tmp_file:
|
||||
_tmp_file.write(case)
|
||||
|
||||
parsed_imports = get_imports(tmp_file_path)
|
||||
assert parsed_imports == ["os"]
|
||||
38
transformers/tests/utils/test_expectations.py
Normal file
38
transformers/tests/utils/test_expectations.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import unittest
|
||||
|
||||
from transformers.testing_utils import Expectations
|
||||
|
||||
|
||||
class ExpectationsTest(unittest.TestCase):
|
||||
def test_expectations(self):
|
||||
# We use the expectations below to make sure the right expectations are found for the right devices.
|
||||
# Each value is just a unique ID.
|
||||
expectations = Expectations(
|
||||
{
|
||||
(None, None): 1,
|
||||
("cuda", 8): 2,
|
||||
("cuda", 7): 3,
|
||||
("rocm", 8): 4,
|
||||
("rocm", None): 5,
|
||||
("cpu", None): 6,
|
||||
("xpu", 3): 7,
|
||||
}
|
||||
)
|
||||
|
||||
def check(expected_id, device_prop):
|
||||
found_id = expectations.find_expectation(device_prop)
|
||||
assert found_id == expected_id, f"Expected {expected_id} for {device_prop}, found {found_id}"
|
||||
|
||||
# npu has no matches so should find default expectation
|
||||
check(1, ("npu", None, None))
|
||||
check(7, ("xpu", 3, None))
|
||||
check(2, ("cuda", 8, None))
|
||||
check(3, ("cuda", 7, None))
|
||||
check(4, ("rocm", 9, None))
|
||||
check(4, ("rocm", None, None))
|
||||
check(2, ("cuda", 2, None))
|
||||
|
||||
# We also test that if there is no default excpectation and no match is found, a ValueError is raised.
|
||||
expectations = Expectations({("cuda", 8): 1})
|
||||
with self.assertRaises(ValueError):
|
||||
expectations.find_expectation(("xpu", None))
|
||||
121
transformers/tests/utils/test_feature_extraction_utils.py
Normal file
121
transformers/tests/utils/test_feature_extraction_utils.py
Normal file
@@ -0,0 +1,121 @@
|
||||
# Copyright 2021 HuggingFace Inc.
|
||||
#
|
||||
# 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 sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
|
||||
from transformers import AutoFeatureExtractor, Wav2Vec2FeatureExtractor
|
||||
from transformers.testing_utils import TOKEN, TemporaryHubRepo, get_tests_dir, is_staging_test
|
||||
|
||||
|
||||
sys.path.append(str(Path(__file__).parent.parent.parent / "utils"))
|
||||
|
||||
from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402
|
||||
|
||||
|
||||
SAMPLE_FEATURE_EXTRACTION_CONFIG_DIR = get_tests_dir("fixtures")
|
||||
|
||||
|
||||
class FeatureExtractorUtilTester(unittest.TestCase):
|
||||
def test_cached_files_are_used_when_internet_is_down(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 = httpx.HTTPStatusError(
|
||||
"failed", request=mock.Mock(), response=mock.Mock()
|
||||
)
|
||||
response_mock.json.return_value = {}
|
||||
|
||||
# Download this model to make sure it's in the cache.
|
||||
_ = Wav2Vec2FeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2")
|
||||
# Under the mock environment we get a 500 error when trying to reach the model.
|
||||
with mock.patch("httpx.Client.request", return_value=response_mock) as mock_head:
|
||||
_ = Wav2Vec2FeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2")
|
||||
# This check we did call the fake head request
|
||||
mock_head.assert_called()
|
||||
|
||||
|
||||
@is_staging_test
|
||||
class FeatureExtractorPushToHubTester(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls._token = TOKEN
|
||||
|
||||
def test_push_to_hub(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(SAMPLE_FEATURE_EXTRACTION_CONFIG_DIR)
|
||||
feature_extractor.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
new_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in feature_extractor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_feature_extractor, k))
|
||||
|
||||
def test_push_to_hub_via_save_pretrained(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(SAMPLE_FEATURE_EXTRACTION_CONFIG_DIR)
|
||||
# Push to hub via save_pretrained
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
feature_extractor.save_pretrained(
|
||||
tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token
|
||||
)
|
||||
|
||||
new_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in feature_extractor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_feature_extractor, k))
|
||||
|
||||
def test_push_to_hub_in_organization(self):
|
||||
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
|
||||
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(SAMPLE_FEATURE_EXTRACTION_CONFIG_DIR)
|
||||
feature_extractor.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
new_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in feature_extractor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_feature_extractor, k))
|
||||
|
||||
def test_push_to_hub_in_organization_via_save_pretrained(self):
|
||||
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
|
||||
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(SAMPLE_FEATURE_EXTRACTION_CONFIG_DIR)
|
||||
# Push to hub via save_pretrained
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
feature_extractor.save_pretrained(
|
||||
tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token
|
||||
)
|
||||
|
||||
new_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in feature_extractor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_feature_extractor, k))
|
||||
|
||||
def test_push_to_hub_dynamic_feature_extractor(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
CustomFeatureExtractor.register_for_auto_class()
|
||||
feature_extractor = CustomFeatureExtractor.from_pretrained(SAMPLE_FEATURE_EXTRACTION_CONFIG_DIR)
|
||||
|
||||
feature_extractor.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
# This has added the proper auto_map field to the config
|
||||
self.assertDictEqual(
|
||||
feature_extractor.auto_map,
|
||||
{"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"},
|
||||
)
|
||||
|
||||
new_feature_extractor = AutoFeatureExtractor.from_pretrained(tmp_repo.repo_id, trust_remote_code=True)
|
||||
# Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
|
||||
self.assertEqual(new_feature_extractor.__class__.__name__, "CustomFeatureExtractor")
|
||||
102
transformers/tests/utils/test_file_utils.py
Normal file
102
transformers/tests/utils/test_file_utils.py
Normal file
@@ -0,0 +1,102 @@
|
||||
# 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 contextlib
|
||||
import importlib
|
||||
import io
|
||||
import unittest
|
||||
|
||||
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, require_torch
|
||||
from transformers.utils import ContextManagers, find_labels, is_torch_available
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification
|
||||
|
||||
|
||||
MODEL_ID = DUMMY_UNKNOWN_IDENTIFIER
|
||||
# An actual model hosted on huggingface.co
|
||||
|
||||
REVISION_ID_DEFAULT = "main"
|
||||
# Default branch name
|
||||
REVISION_ID_ONE_SPECIFIC_COMMIT = "f2c752cfc5c0ab6f4bdec59acea69eefbee381c2"
|
||||
# One particular commit (not the top of `main`)
|
||||
REVISION_ID_INVALID = "aaaaaaa"
|
||||
# This commit does not exist, so we should 404.
|
||||
|
||||
PINNED_SHA1 = "d9e9f15bc825e4b2c9249e9578f884bbcb5e3684"
|
||||
# Sha-1 of config.json on the top of `main`, for checking purposes
|
||||
PINNED_SHA256 = "4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3"
|
||||
# Sha-256 of pytorch_model.bin on the top of `main`, for checking purposes
|
||||
|
||||
|
||||
# Dummy contexts to test `ContextManagers`
|
||||
@contextlib.contextmanager
|
||||
def context_en():
|
||||
print("Welcome!")
|
||||
yield
|
||||
print("Bye!")
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def context_fr():
|
||||
print("Bonjour!")
|
||||
yield
|
||||
print("Au revoir!")
|
||||
|
||||
|
||||
class TestImportMechanisms(unittest.TestCase):
|
||||
def test_module_spec_available(self):
|
||||
# If the spec is missing, importlib would not be able to import the module dynamically.
|
||||
assert transformers.__spec__ is not None
|
||||
assert importlib.util.find_spec("transformers") is not None
|
||||
|
||||
|
||||
class GenericUtilTests(unittest.TestCase):
|
||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_context_managers_no_context(self, mock_stdout):
|
||||
with ContextManagers([]):
|
||||
print("Transformers are awesome!")
|
||||
# The print statement adds a new line at the end of the output
|
||||
self.assertEqual(mock_stdout.getvalue(), "Transformers are awesome!\n")
|
||||
|
||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_context_managers_one_context(self, mock_stdout):
|
||||
with ContextManagers([context_en()]):
|
||||
print("Transformers are awesome!")
|
||||
# The output should be wrapped with an English welcome and goodbye
|
||||
self.assertEqual(mock_stdout.getvalue(), "Welcome!\nTransformers are awesome!\nBye!\n")
|
||||
|
||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_context_managers_two_context(self, mock_stdout):
|
||||
with ContextManagers([context_fr(), context_en()]):
|
||||
print("Transformers are awesome!")
|
||||
# The output should be wrapped with an English and French welcome and goodbye
|
||||
self.assertEqual(mock_stdout.getvalue(), "Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n")
|
||||
|
||||
@require_torch
|
||||
def test_find_labels_pt(self):
|
||||
self.assertEqual(find_labels(BertForSequenceClassification), ["labels"])
|
||||
self.assertEqual(find_labels(BertForPreTraining), ["labels", "next_sentence_label"])
|
||||
self.assertEqual(find_labels(BertForQuestionAnswering), ["start_positions", "end_positions"])
|
||||
|
||||
# find_labels works regardless of the class name (it detects the framework through inheritance)
|
||||
class DummyModel(BertForSequenceClassification):
|
||||
pass
|
||||
|
||||
self.assertEqual(find_labels(DummyModel), ["labels"])
|
||||
348
transformers/tests/utils/test_generic.py
Normal file
348
transformers/tests/utils/test_generic.py
Normal file
@@ -0,0 +1,348 @@
|
||||
# Copyright 2019-present, the HuggingFace Inc. team.
|
||||
#
|
||||
# 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 unittest
|
||||
import warnings
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from transformers.configuration_utils import PretrainedConfig
|
||||
from transformers.modeling_outputs import BaseModelOutput, CausalLMOutputWithPast
|
||||
from transformers.testing_utils import require_torch
|
||||
from transformers.utils import (
|
||||
can_return_tuple,
|
||||
expand_dims,
|
||||
filter_out_non_signature_kwargs,
|
||||
flatten_dict,
|
||||
is_torch_available,
|
||||
reshape,
|
||||
squeeze,
|
||||
to_py_obj,
|
||||
transpose,
|
||||
)
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
|
||||
class GenericTester(unittest.TestCase):
|
||||
def test_flatten_dict(self):
|
||||
input_dict = {
|
||||
"task_specific_params": {
|
||||
"summarization": {"length_penalty": 1.0, "max_length": 128, "min_length": 12, "num_beams": 4},
|
||||
"summarization_cnn": {"length_penalty": 2.0, "max_length": 142, "min_length": 56, "num_beams": 4},
|
||||
"summarization_xsum": {"length_penalty": 1.0, "max_length": 62, "min_length": 11, "num_beams": 6},
|
||||
}
|
||||
}
|
||||
expected_dict = {
|
||||
"task_specific_params.summarization.length_penalty": 1.0,
|
||||
"task_specific_params.summarization.max_length": 128,
|
||||
"task_specific_params.summarization.min_length": 12,
|
||||
"task_specific_params.summarization.num_beams": 4,
|
||||
"task_specific_params.summarization_cnn.length_penalty": 2.0,
|
||||
"task_specific_params.summarization_cnn.max_length": 142,
|
||||
"task_specific_params.summarization_cnn.min_length": 56,
|
||||
"task_specific_params.summarization_cnn.num_beams": 4,
|
||||
"task_specific_params.summarization_xsum.length_penalty": 1.0,
|
||||
"task_specific_params.summarization_xsum.max_length": 62,
|
||||
"task_specific_params.summarization_xsum.min_length": 11,
|
||||
"task_specific_params.summarization_xsum.num_beams": 6,
|
||||
}
|
||||
|
||||
self.assertEqual(flatten_dict(input_dict), expected_dict)
|
||||
|
||||
def test_transpose_numpy(self):
|
||||
x = np.random.randn(3, 4)
|
||||
self.assertTrue(np.allclose(transpose(x), x.transpose()))
|
||||
|
||||
x = np.random.randn(3, 4, 5)
|
||||
self.assertTrue(np.allclose(transpose(x, axes=(1, 2, 0)), x.transpose((1, 2, 0))))
|
||||
|
||||
@require_torch
|
||||
def test_transpose_torch(self):
|
||||
x = np.random.randn(3, 4)
|
||||
t = torch.tensor(x)
|
||||
self.assertTrue(np.allclose(transpose(x), transpose(t).numpy()))
|
||||
|
||||
x = np.random.randn(3, 4, 5)
|
||||
t = torch.tensor(x)
|
||||
self.assertTrue(np.allclose(transpose(x, axes=(1, 2, 0)), transpose(t, axes=(1, 2, 0)).numpy()))
|
||||
|
||||
@require_torch
|
||||
def test_reshape_torch(self):
|
||||
x = np.random.randn(3, 4)
|
||||
t = torch.tensor(x)
|
||||
self.assertTrue(np.allclose(reshape(x, (4, 3)), reshape(t, (4, 3)).numpy()))
|
||||
|
||||
x = np.random.randn(3, 4, 5)
|
||||
t = torch.tensor(x)
|
||||
self.assertTrue(np.allclose(reshape(x, (12, 5)), reshape(t, (12, 5)).numpy()))
|
||||
|
||||
@require_torch
|
||||
def test_squeeze_torch(self):
|
||||
x = np.random.randn(1, 3, 4)
|
||||
t = torch.tensor(x)
|
||||
self.assertTrue(np.allclose(squeeze(x), squeeze(t).numpy()))
|
||||
|
||||
x = np.random.randn(1, 4, 1, 5)
|
||||
t = torch.tensor(x)
|
||||
self.assertTrue(np.allclose(squeeze(x, axis=2), squeeze(t, axis=2).numpy()))
|
||||
|
||||
def test_expand_dims_numpy(self):
|
||||
x = np.random.randn(3, 4)
|
||||
self.assertTrue(np.allclose(expand_dims(x, axis=1), np.expand_dims(x, axis=1)))
|
||||
|
||||
@require_torch
|
||||
def test_expand_dims_torch(self):
|
||||
x = np.random.randn(3, 4)
|
||||
t = torch.tensor(x)
|
||||
self.assertTrue(np.allclose(expand_dims(x, axis=1), expand_dims(t, axis=1).numpy()))
|
||||
|
||||
def test_to_py_obj_native(self):
|
||||
self.assertTrue(to_py_obj(1) == 1)
|
||||
self.assertTrue(to_py_obj([1, 2, 3]) == [1, 2, 3])
|
||||
self.assertTrue(to_py_obj([((1.0, 1.1), 1.2), (2, 3)]) == [[[1.0, 1.1], 1.2], [2, 3]])
|
||||
|
||||
def test_to_py_obj_numpy(self):
|
||||
x1 = [[1, 2, 3], [4, 5, 6]]
|
||||
t1 = np.array(x1)
|
||||
self.assertTrue(to_py_obj(t1) == x1)
|
||||
|
||||
x2 = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
|
||||
t2 = np.array(x2)
|
||||
self.assertTrue(to_py_obj(t2) == x2)
|
||||
|
||||
self.assertTrue(to_py_obj([t1, t2]) == [x1, x2])
|
||||
|
||||
@require_torch
|
||||
def test_to_py_obj_torch(self):
|
||||
x1 = [[1, 2, 3], [4, 5, 6]]
|
||||
t1 = torch.tensor(x1)
|
||||
self.assertTrue(to_py_obj(t1) == x1)
|
||||
|
||||
x2 = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
|
||||
t2 = torch.tensor(x2)
|
||||
self.assertTrue(to_py_obj(t2) == x2)
|
||||
|
||||
self.assertTrue(to_py_obj([t1, t2]) == [x1, x2])
|
||||
|
||||
def test_model_output_subclass(self):
|
||||
# testing with “dict-like init” case
|
||||
out = CausalLMOutputWithPast({"logits": torch.ones(2, 3, 4)})
|
||||
self.assertTrue(out["logits"] is not None)
|
||||
self.assertTrue(out.loss is None)
|
||||
self.assertTrue(len(out.to_tuple()) == 1)
|
||||
|
||||
# testing with dataclass init case
|
||||
out = CausalLMOutputWithPast(logits=torch.ones(2, 3, 4))
|
||||
self.assertTrue(out["logits"] is not None)
|
||||
self.assertTrue(out.loss is None)
|
||||
self.assertTrue(len(out.to_tuple()) == 1)
|
||||
|
||||
|
||||
class ValidationDecoratorTester(unittest.TestCase):
|
||||
def test_cases_no_warning(self):
|
||||
with warnings.catch_warnings(record=True) as raised_warnings:
|
||||
warnings.simplefilter("always")
|
||||
|
||||
# basic test
|
||||
@filter_out_non_signature_kwargs()
|
||||
def func1(a):
|
||||
return a
|
||||
|
||||
result = func1(1)
|
||||
self.assertEqual(result, 1)
|
||||
|
||||
# include extra kwarg
|
||||
@filter_out_non_signature_kwargs(extra=["extra_arg"])
|
||||
def func2(a, **kwargs):
|
||||
return a, kwargs
|
||||
|
||||
a, kwargs = func2(1)
|
||||
self.assertEqual(a, 1)
|
||||
self.assertEqual(kwargs, {})
|
||||
|
||||
a, kwargs = func2(1, extra_arg=2)
|
||||
self.assertEqual(a, 1)
|
||||
self.assertEqual(kwargs, {"extra_arg": 2})
|
||||
|
||||
# multiple extra kwargs
|
||||
@filter_out_non_signature_kwargs(extra=["extra_arg", "extra_arg2"])
|
||||
def func3(a, **kwargs):
|
||||
return a, kwargs
|
||||
|
||||
a, kwargs = func3(2)
|
||||
self.assertEqual(a, 2)
|
||||
self.assertEqual(kwargs, {})
|
||||
|
||||
a, kwargs = func3(3, extra_arg2=3)
|
||||
self.assertEqual(a, 3)
|
||||
self.assertEqual(kwargs, {"extra_arg2": 3})
|
||||
|
||||
a, kwargs = func3(1, extra_arg=2, extra_arg2=3)
|
||||
self.assertEqual(a, 1)
|
||||
self.assertEqual(kwargs, {"extra_arg": 2, "extra_arg2": 3})
|
||||
|
||||
# Check that no warnings were raised
|
||||
self.assertEqual(len(raised_warnings), 0, f"Warning raised: {[w.message for w in raised_warnings]}")
|
||||
|
||||
def test_cases_with_warnings(self):
|
||||
@filter_out_non_signature_kwargs()
|
||||
def func1(a):
|
||||
return a
|
||||
|
||||
with self.assertWarns(UserWarning):
|
||||
func1(1, extra_arg=2)
|
||||
|
||||
@filter_out_non_signature_kwargs(extra=["extra_arg"])
|
||||
def func2(a, **kwargs):
|
||||
return kwargs
|
||||
|
||||
with self.assertWarns(UserWarning):
|
||||
kwargs = func2(1, extra_arg=2, extra_arg2=3)
|
||||
self.assertEqual(kwargs, {"extra_arg": 2})
|
||||
|
||||
@filter_out_non_signature_kwargs(extra=["extra_arg", "extra_arg2"])
|
||||
def func3(a, **kwargs):
|
||||
return kwargs
|
||||
|
||||
with self.assertWarns(UserWarning):
|
||||
kwargs = func3(1, extra_arg=2, extra_arg2=3, extra_arg3=4)
|
||||
self.assertEqual(kwargs, {"extra_arg": 2, "extra_arg2": 3})
|
||||
|
||||
|
||||
@require_torch
|
||||
class CanReturnTupleDecoratorTester(unittest.TestCase):
|
||||
def _get_model(self, config, store_config=True, raise_in_forward=False):
|
||||
# Simple model class for testing can_return_tuple decorator.
|
||||
class SimpleTestModel(torch.nn.Module):
|
||||
def __init__(self, config):
|
||||
super().__init__()
|
||||
if store_config:
|
||||
self.config = config
|
||||
|
||||
@can_return_tuple
|
||||
def forward(self, x):
|
||||
if raise_in_forward:
|
||||
raise ValueError("Test error")
|
||||
return BaseModelOutput(
|
||||
last_hidden_state=x,
|
||||
hidden_states=None,
|
||||
attentions=None,
|
||||
)
|
||||
|
||||
return SimpleTestModel(config)
|
||||
|
||||
def test_decorator_eager(self):
|
||||
"""Test that the can_return_tuple decorator works with eager mode."""
|
||||
|
||||
# test nothing is set
|
||||
config = PretrainedConfig()
|
||||
model = self._get_model(config)
|
||||
inputs = torch.tensor(10)
|
||||
output = model(inputs)
|
||||
self.assertIsInstance(
|
||||
output, BaseModelOutput, "output should be a BaseModelOutput when return_dict is not set"
|
||||
)
|
||||
|
||||
# test all explicit cases
|
||||
for config_return_dict in [True, False, None]:
|
||||
for return_dict in [True, False, None]:
|
||||
config = PretrainedConfig(return_dict=config_return_dict)
|
||||
model = self._get_model(config)
|
||||
output = model(torch.tensor(10), return_dict=return_dict)
|
||||
|
||||
expected_type = (
|
||||
tuple
|
||||
if return_dict is False
|
||||
else (tuple if config_return_dict is False and return_dict is None else BaseModelOutput)
|
||||
)
|
||||
if config_return_dict is None and return_dict is None:
|
||||
expected_type = tuple
|
||||
message = f"output should be a {expected_type.__name__} when config.use_return_dict={config_return_dict} and return_dict={return_dict}"
|
||||
self.assertIsInstance(output, expected_type, message)
|
||||
|
||||
@pytest.mark.torch_compile_test
|
||||
def test_decorator_compiled(self):
|
||||
"""Test that the can_return_tuple decorator works with compiled mode."""
|
||||
config = PretrainedConfig()
|
||||
|
||||
# Output object
|
||||
model = self._get_model(config)
|
||||
compiled_model = torch.compile(model)
|
||||
output = compiled_model(torch.tensor(10))
|
||||
self.assertIsInstance(output, BaseModelOutput)
|
||||
|
||||
# Tuple output
|
||||
model = self._get_model(config)
|
||||
compiled_model = torch.compile(model)
|
||||
output = compiled_model(torch.tensor(10), return_dict=False)
|
||||
self.assertIsInstance(output, tuple)
|
||||
|
||||
@pytest.mark.torch_export_test
|
||||
def test_decorator_torch_export(self):
|
||||
"""Test that the can_return_tuple decorator works with torch.export."""
|
||||
config = PretrainedConfig()
|
||||
model = self._get_model(config)
|
||||
torch.export.export(model, args=(torch.tensor(10),))
|
||||
|
||||
def test_decorator_torchscript(self):
|
||||
"""Test that the can_return_tuple decorator works with torch.jit.trace."""
|
||||
config = PretrainedConfig(return_dict=False)
|
||||
model = self._get_model(config)
|
||||
inputs = torch.tensor(10)
|
||||
traced_module = torch.jit.trace(model, inputs)
|
||||
output = traced_module(inputs)
|
||||
self.assertIsInstance(output, tuple)
|
||||
|
||||
def test_attribute_cleanup(self):
|
||||
"""Test that the `_is_top_level_module` attribute is removed after the forward call."""
|
||||
|
||||
config = PretrainedConfig(return_dict=False)
|
||||
inputs = torch.tensor(10)
|
||||
|
||||
# working case
|
||||
model = self._get_model(config)
|
||||
output = model(inputs)
|
||||
|
||||
self.assertIsInstance(output, tuple)
|
||||
for name, module in model.named_modules():
|
||||
self.assertFalse(
|
||||
hasattr(module, "_is_top_level_module"),
|
||||
f"Module `{name}` should not have `_is_top_level_module` attribute",
|
||||
)
|
||||
|
||||
# model without config
|
||||
no_config_model = self._get_model(config, store_config=False)
|
||||
output = no_config_model(inputs)
|
||||
|
||||
self.assertIsInstance(output, BaseModelOutput)
|
||||
for name, module in no_config_model.named_modules():
|
||||
self.assertFalse(
|
||||
hasattr(module, "_is_top_level_module"),
|
||||
f"Module `{name}` should not have `_is_top_level_module` attribute",
|
||||
)
|
||||
|
||||
# model with raise in forward
|
||||
model_with_raise = self._get_model(config, raise_in_forward=True)
|
||||
with self.assertRaises(ValueError):
|
||||
model_with_raise(inputs)
|
||||
|
||||
for name, module in model_with_raise.named_modules():
|
||||
self.assertFalse(
|
||||
hasattr(module, "_is_top_level_module"),
|
||||
f"Module `{name}` should not have `_is_top_level_module` attribute",
|
||||
)
|
||||
492
transformers/tests/utils/test_hf_argparser.py
Normal file
492
transformers/tests/utils/test_hf_argparser.py
Normal file
@@ -0,0 +1,492 @@
|
||||
# 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 argparse
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from argparse import Namespace
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import Literal, Optional, Union, get_args, get_origin
|
||||
from unittest.mock import patch
|
||||
|
||||
import yaml
|
||||
|
||||
from transformers import HfArgumentParser, TrainingArguments
|
||||
from transformers.hf_argparser import make_choice_type_function, string_to_bool
|
||||
from transformers.testing_utils import require_torch
|
||||
|
||||
|
||||
# Since Python 3.10, we can use the builtin `|` operator for Union types
|
||||
# See PEP 604: https://peps.python.org/pep-0604
|
||||
is_python_no_less_than_3_10 = sys.version_info >= (3, 10)
|
||||
|
||||
|
||||
def list_field(default=None, metadata=None):
|
||||
return field(default_factory=lambda: default, metadata=metadata)
|
||||
|
||||
|
||||
@dataclass
|
||||
class BasicExample:
|
||||
foo: int
|
||||
bar: float
|
||||
baz: str
|
||||
flag: bool
|
||||
|
||||
|
||||
@dataclass
|
||||
class WithDefaultExample:
|
||||
foo: int = 42
|
||||
baz: str = field(default="toto", metadata={"help": "help message"})
|
||||
|
||||
|
||||
@dataclass
|
||||
class WithDefaultBoolExample:
|
||||
foo: bool = False
|
||||
baz: bool = True
|
||||
opt: Optional[bool] = None
|
||||
|
||||
|
||||
class BasicEnum(Enum):
|
||||
titi = "titi"
|
||||
toto = "toto"
|
||||
|
||||
|
||||
class MixedTypeEnum(Enum):
|
||||
titi = "titi"
|
||||
toto = "toto"
|
||||
fourtytwo = 42
|
||||
|
||||
|
||||
@dataclass
|
||||
class EnumExample:
|
||||
foo: BasicEnum = "toto"
|
||||
|
||||
def __post_init__(self):
|
||||
self.foo = BasicEnum(self.foo)
|
||||
|
||||
|
||||
@dataclass
|
||||
class MixedTypeEnumExample:
|
||||
foo: MixedTypeEnum = "toto"
|
||||
|
||||
def __post_init__(self):
|
||||
self.foo = MixedTypeEnum(self.foo)
|
||||
|
||||
|
||||
@dataclass
|
||||
class OptionalExample:
|
||||
foo: Optional[int] = None
|
||||
bar: Optional[float] = field(default=None, metadata={"help": "help message"})
|
||||
baz: Optional[str] = None
|
||||
ces: Optional[list[str]] = list_field(default=[])
|
||||
des: Optional[list[int]] = list_field(default=[])
|
||||
|
||||
|
||||
@dataclass
|
||||
class ListExample:
|
||||
foo_int: list[int] = list_field(default=[])
|
||||
bar_int: list[int] = list_field(default=[1, 2, 3])
|
||||
foo_str: list[str] = list_field(default=["Hallo", "Bonjour", "Hello"])
|
||||
foo_float: list[float] = list_field(default=[0.1, 0.2, 0.3])
|
||||
|
||||
|
||||
@dataclass
|
||||
class RequiredExample:
|
||||
required_list: list[int] = field()
|
||||
required_str: str = field()
|
||||
required_enum: BasicEnum = field()
|
||||
|
||||
def __post_init__(self):
|
||||
self.required_enum = BasicEnum(self.required_enum)
|
||||
|
||||
|
||||
@dataclass
|
||||
class StringLiteralAnnotationExample:
|
||||
foo: int
|
||||
required_enum: "BasicEnum" = field()
|
||||
opt: "Optional[bool]" = None
|
||||
baz: "str" = field(default="toto", metadata={"help": "help message"})
|
||||
foo_str: "list[str]" = list_field(default=["Hallo", "Bonjour", "Hello"])
|
||||
|
||||
|
||||
if is_python_no_less_than_3_10:
|
||||
|
||||
@dataclass
|
||||
class WithDefaultBoolExamplePep604:
|
||||
foo: bool = False
|
||||
baz: bool = True
|
||||
opt: bool | None = None
|
||||
|
||||
@dataclass
|
||||
class OptionalExamplePep604:
|
||||
foo: int | None = None
|
||||
bar: float | None = field(default=None, metadata={"help": "help message"})
|
||||
baz: str | None = None
|
||||
ces: list[str] | None = list_field(default=[])
|
||||
des: list[int] | None = list_field(default=[])
|
||||
|
||||
|
||||
class HfArgumentParserTest(unittest.TestCase):
|
||||
def argparsersEqual(self, a: argparse.ArgumentParser, b: argparse.ArgumentParser):
|
||||
"""
|
||||
Small helper to check pseudo-equality of parsed arguments on `ArgumentParser` instances.
|
||||
"""
|
||||
self.assertEqual(len(a._actions), len(b._actions))
|
||||
for x, y in zip(a._actions, b._actions):
|
||||
xx = {k: v for k, v in vars(x).items() if k != "container"}
|
||||
yy = {k: v for k, v in vars(y).items() if k != "container"}
|
||||
|
||||
# Choices with mixed type have custom function as "type"
|
||||
# So we need to compare results directly for equality
|
||||
if xx.get("choices") and yy.get("choices"):
|
||||
for expected_choice in yy["choices"] + xx["choices"]:
|
||||
self.assertEqual(xx["type"](expected_choice), yy["type"](expected_choice))
|
||||
del xx["type"], yy["type"]
|
||||
|
||||
self.assertEqual(xx, yy)
|
||||
|
||||
def test_00_basic(self):
|
||||
parser = HfArgumentParser(BasicExample)
|
||||
|
||||
expected = argparse.ArgumentParser()
|
||||
expected.add_argument("--foo", type=int, required=True)
|
||||
expected.add_argument("--bar", type=float, required=True)
|
||||
expected.add_argument("--baz", type=str, required=True)
|
||||
expected.add_argument("--flag", type=string_to_bool, default=False, const=True, nargs="?")
|
||||
self.argparsersEqual(parser, expected)
|
||||
|
||||
args = ["--foo", "1", "--baz", "quux", "--bar", "0.5"]
|
||||
(example,) = parser.parse_args_into_dataclasses(args, look_for_args_file=False)
|
||||
self.assertFalse(example.flag)
|
||||
|
||||
def test_01_with_default(self):
|
||||
parser = HfArgumentParser(WithDefaultExample)
|
||||
|
||||
expected = argparse.ArgumentParser()
|
||||
expected.add_argument("--foo", default=42, type=int)
|
||||
expected.add_argument("--baz", default="toto", type=str, help="help message")
|
||||
self.argparsersEqual(parser, expected)
|
||||
|
||||
def test_02_with_default_bool(self):
|
||||
expected = argparse.ArgumentParser()
|
||||
expected.add_argument("--foo", type=string_to_bool, default=False, const=True, nargs="?")
|
||||
expected.add_argument("--baz", type=string_to_bool, default=True, const=True, nargs="?")
|
||||
# A boolean no_* argument always has to come after its "default: True" regular counter-part
|
||||
# and its default must be set to False
|
||||
expected.add_argument("--no_baz", "--no-baz", action="store_false", default=False, dest="baz")
|
||||
expected.add_argument("--opt", type=string_to_bool, default=None)
|
||||
|
||||
dataclass_types = [WithDefaultBoolExample]
|
||||
if is_python_no_less_than_3_10:
|
||||
dataclass_types.append(WithDefaultBoolExamplePep604)
|
||||
|
||||
for dataclass_type in dataclass_types:
|
||||
parser = HfArgumentParser(dataclass_type)
|
||||
self.argparsersEqual(parser, expected)
|
||||
|
||||
args = parser.parse_args([])
|
||||
self.assertEqual(args, Namespace(foo=False, baz=True, opt=None))
|
||||
|
||||
args = parser.parse_args(["--foo", "--no_baz"])
|
||||
self.assertEqual(args, Namespace(foo=True, baz=False, opt=None))
|
||||
|
||||
args = parser.parse_args(["--foo", "--no-baz"])
|
||||
self.assertEqual(args, Namespace(foo=True, baz=False, opt=None))
|
||||
|
||||
args = parser.parse_args(["--foo", "--baz"])
|
||||
self.assertEqual(args, Namespace(foo=True, baz=True, opt=None))
|
||||
|
||||
args = parser.parse_args(["--foo", "True", "--baz", "True", "--opt", "True"])
|
||||
self.assertEqual(args, Namespace(foo=True, baz=True, opt=True))
|
||||
|
||||
args = parser.parse_args(["--foo", "False", "--baz", "False", "--opt", "False"])
|
||||
self.assertEqual(args, Namespace(foo=False, baz=False, opt=False))
|
||||
|
||||
def test_03_with_enum(self):
|
||||
parser = HfArgumentParser(MixedTypeEnumExample)
|
||||
|
||||
expected = argparse.ArgumentParser()
|
||||
expected.add_argument(
|
||||
"--foo",
|
||||
default="toto",
|
||||
choices=["titi", "toto", 42],
|
||||
type=make_choice_type_function(["titi", "toto", 42]),
|
||||
)
|
||||
self.argparsersEqual(parser, expected)
|
||||
|
||||
args = parser.parse_args([])
|
||||
self.assertEqual(args.foo, "toto")
|
||||
enum_ex = parser.parse_args_into_dataclasses([])[0]
|
||||
self.assertEqual(enum_ex.foo, MixedTypeEnum.toto)
|
||||
|
||||
args = parser.parse_args(["--foo", "titi"])
|
||||
self.assertEqual(args.foo, "titi")
|
||||
enum_ex = parser.parse_args_into_dataclasses(["--foo", "titi"])[0]
|
||||
self.assertEqual(enum_ex.foo, MixedTypeEnum.titi)
|
||||
|
||||
args = parser.parse_args(["--foo", "42"])
|
||||
self.assertEqual(args.foo, 42)
|
||||
enum_ex = parser.parse_args_into_dataclasses(["--foo", "42"])[0]
|
||||
self.assertEqual(enum_ex.foo, MixedTypeEnum.fourtytwo)
|
||||
|
||||
def test_04_with_literal(self):
|
||||
@dataclass
|
||||
class LiteralExample:
|
||||
foo: Literal["titi", "toto", 42] = "toto"
|
||||
|
||||
parser = HfArgumentParser(LiteralExample)
|
||||
|
||||
expected = argparse.ArgumentParser()
|
||||
expected.add_argument(
|
||||
"--foo",
|
||||
default="toto",
|
||||
choices=("titi", "toto", 42),
|
||||
type=make_choice_type_function(["titi", "toto", 42]),
|
||||
)
|
||||
self.argparsersEqual(parser, expected)
|
||||
|
||||
args = parser.parse_args([])
|
||||
self.assertEqual(args.foo, "toto")
|
||||
|
||||
args = parser.parse_args(["--foo", "titi"])
|
||||
self.assertEqual(args.foo, "titi")
|
||||
|
||||
args = parser.parse_args(["--foo", "42"])
|
||||
self.assertEqual(args.foo, 42)
|
||||
|
||||
def test_05_with_list(self):
|
||||
parser = HfArgumentParser(ListExample)
|
||||
|
||||
expected = argparse.ArgumentParser()
|
||||
expected.add_argument("--foo_int", "--foo-int", nargs="+", default=[], type=int)
|
||||
expected.add_argument("--bar_int", "--bar-int", nargs="+", default=[1, 2, 3], type=int)
|
||||
expected.add_argument("--foo_str", "--foo-str", nargs="+", default=["Hallo", "Bonjour", "Hello"], type=str)
|
||||
expected.add_argument("--foo_float", "--foo-float", nargs="+", default=[0.1, 0.2, 0.3], type=float)
|
||||
|
||||
self.argparsersEqual(parser, expected)
|
||||
|
||||
args = parser.parse_args([])
|
||||
self.assertEqual(
|
||||
args,
|
||||
Namespace(foo_int=[], bar_int=[1, 2, 3], foo_str=["Hallo", "Bonjour", "Hello"], foo_float=[0.1, 0.2, 0.3]),
|
||||
)
|
||||
|
||||
args = parser.parse_args("--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7".split())
|
||||
self.assertEqual(args, Namespace(foo_int=[1], bar_int=[2, 3], foo_str=["a", "b", "c"], foo_float=[0.1, 0.7]))
|
||||
|
||||
args = parser.parse_args("--foo-int 1 --bar-int 2 3 --foo-str a b c --foo-float 0.1 0.7".split())
|
||||
self.assertEqual(args, Namespace(foo_int=[1], bar_int=[2, 3], foo_str=["a", "b", "c"], foo_float=[0.1, 0.7]))
|
||||
|
||||
def test_06_with_optional(self):
|
||||
expected = argparse.ArgumentParser()
|
||||
expected.add_argument("--foo", default=None, type=int)
|
||||
expected.add_argument("--bar", default=None, type=float, help="help message")
|
||||
expected.add_argument("--baz", default=None, type=str)
|
||||
expected.add_argument("--ces", nargs="+", default=[], type=str)
|
||||
expected.add_argument("--des", nargs="+", default=[], type=int)
|
||||
|
||||
dataclass_types = [OptionalExample]
|
||||
if is_python_no_less_than_3_10:
|
||||
dataclass_types.append(OptionalExamplePep604)
|
||||
|
||||
for dataclass_type in dataclass_types:
|
||||
parser = HfArgumentParser(dataclass_type)
|
||||
|
||||
self.argparsersEqual(parser, expected)
|
||||
|
||||
args = parser.parse_args([])
|
||||
self.assertEqual(args, Namespace(foo=None, bar=None, baz=None, ces=[], des=[]))
|
||||
|
||||
args = parser.parse_args("--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3".split())
|
||||
self.assertEqual(args, Namespace(foo=12, bar=3.14, baz="42", ces=["a", "b", "c"], des=[1, 2, 3]))
|
||||
|
||||
def test_07_with_required(self):
|
||||
parser = HfArgumentParser(RequiredExample)
|
||||
|
||||
expected = argparse.ArgumentParser()
|
||||
expected.add_argument("--required_list", "--required-list", nargs="+", type=int, required=True)
|
||||
expected.add_argument("--required_str", "--required-str", type=str, required=True)
|
||||
expected.add_argument(
|
||||
"--required_enum",
|
||||
"--required-enum",
|
||||
type=make_choice_type_function(["titi", "toto"]),
|
||||
choices=["titi", "toto"],
|
||||
required=True,
|
||||
)
|
||||
self.argparsersEqual(parser, expected)
|
||||
|
||||
def test_08_with_string_literal_annotation(self):
|
||||
parser = HfArgumentParser(StringLiteralAnnotationExample)
|
||||
|
||||
expected = argparse.ArgumentParser()
|
||||
expected.add_argument("--foo", type=int, required=True)
|
||||
expected.add_argument(
|
||||
"--required_enum",
|
||||
"--required-enum",
|
||||
type=make_choice_type_function(["titi", "toto"]),
|
||||
choices=["titi", "toto"],
|
||||
required=True,
|
||||
)
|
||||
expected.add_argument("--opt", type=string_to_bool, default=None)
|
||||
expected.add_argument("--baz", default="toto", type=str, help="help message")
|
||||
expected.add_argument("--foo_str", "--foo-str", nargs="+", default=["Hallo", "Bonjour", "Hello"], type=str)
|
||||
self.argparsersEqual(parser, expected)
|
||||
|
||||
def test_09_parse_dict(self):
|
||||
parser = HfArgumentParser(BasicExample)
|
||||
|
||||
args_dict = {
|
||||
"foo": 12,
|
||||
"bar": 3.14,
|
||||
"baz": "42",
|
||||
"flag": True,
|
||||
}
|
||||
|
||||
parsed_args = parser.parse_dict(args_dict)[0]
|
||||
args = BasicExample(**args_dict)
|
||||
self.assertEqual(parsed_args, args)
|
||||
|
||||
def test_10_parse_dict_extra_key(self):
|
||||
parser = HfArgumentParser(BasicExample)
|
||||
|
||||
args_dict = {
|
||||
"foo": 12,
|
||||
"bar": 3.14,
|
||||
"baz": "42",
|
||||
"flag": True,
|
||||
"extra": 42,
|
||||
}
|
||||
|
||||
self.assertRaises(ValueError, parser.parse_dict, args_dict, allow_extra_keys=False)
|
||||
|
||||
def test_11_parse_json(self):
|
||||
parser = HfArgumentParser(BasicExample)
|
||||
|
||||
args_dict_for_json = {
|
||||
"foo": 12,
|
||||
"bar": 3.14,
|
||||
"baz": "42",
|
||||
"flag": True,
|
||||
}
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
temp_local_path = os.path.join(tmp_dir, "temp_json")
|
||||
os.mkdir(temp_local_path)
|
||||
with open(temp_local_path + ".json", "w+") as f:
|
||||
json.dump(args_dict_for_json, f)
|
||||
parsed_args = parser.parse_json_file(Path(temp_local_path + ".json"))[0]
|
||||
|
||||
args = BasicExample(**args_dict_for_json)
|
||||
self.assertEqual(parsed_args, args)
|
||||
|
||||
def test_12_parse_yaml(self):
|
||||
parser = HfArgumentParser(BasicExample)
|
||||
|
||||
args_dict_for_yaml = {
|
||||
"foo": 12,
|
||||
"bar": 3.14,
|
||||
"baz": "42",
|
||||
"flag": True,
|
||||
}
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
temp_local_path = os.path.join(tmp_dir, "temp_yaml")
|
||||
os.mkdir(temp_local_path)
|
||||
with open(temp_local_path + ".yaml", "w+") as f:
|
||||
yaml.dump(args_dict_for_yaml, f)
|
||||
parsed_args = parser.parse_yaml_file(Path(temp_local_path + ".yaml"))[0]
|
||||
args = BasicExample(**args_dict_for_yaml)
|
||||
self.assertEqual(parsed_args, args)
|
||||
|
||||
def test_13_valid_dict_annotation(self):
|
||||
"""
|
||||
Tests to make sure that `dict` based annotations
|
||||
are correctly made in the `TrainingArguments`.
|
||||
|
||||
If this fails, a type annotation change is
|
||||
needed on a new input
|
||||
"""
|
||||
base_list = TrainingArguments._VALID_DICT_FIELDS.copy()
|
||||
args = TrainingArguments
|
||||
|
||||
# First find any annotations that contain `dict`
|
||||
fields = args.__dataclass_fields__
|
||||
|
||||
raw_dict_fields = []
|
||||
optional_dict_fields = []
|
||||
|
||||
for field in fields.values():
|
||||
# First verify raw dict
|
||||
if field.type is dict:
|
||||
raw_dict_fields.append(field)
|
||||
# Next check for `Union` or `Optional`
|
||||
elif get_origin(field.type) == Union:
|
||||
if any(arg is dict for arg in get_args(field.type)):
|
||||
optional_dict_fields.append(field)
|
||||
|
||||
# First check: anything in `raw_dict_fields` is very bad
|
||||
self.assertEqual(
|
||||
len(raw_dict_fields),
|
||||
0,
|
||||
f"Found invalid raw `dict` types in the `TrainingArgument` typings, which are {raw_dict_fields}. "
|
||||
"This leads to issues with the CLI. Please turn this into `typing.Optional[dict,str]`",
|
||||
)
|
||||
|
||||
# Next check raw annotations
|
||||
for field in optional_dict_fields:
|
||||
args = get_args(field.type)
|
||||
# These should be returned as `dict`, `str`, ...
|
||||
# we only care about the first two
|
||||
self.assertIn(
|
||||
dict,
|
||||
args,
|
||||
f"Expected field `{field.name}` to have a type signature of at least `typing.Union[dict,str,...]` for CLI compatibility, but `dict` not found. Please fix this.",
|
||||
)
|
||||
self.assertIn(
|
||||
str,
|
||||
args,
|
||||
f"Expected field `{field.name}` to have a type signature of at least `typing.Union[dict,str,...]` for CLI compatibility, but `str` not found. Please fix this.",
|
||||
)
|
||||
|
||||
# Second check: anything in `optional_dict_fields` is bad if it's not in `base_list`
|
||||
for field in optional_dict_fields:
|
||||
self.assertIn(
|
||||
field.name,
|
||||
base_list,
|
||||
f"Optional dict field `{field.name}` is not in the base list of valid fields. Please add it to `TrainingArguments._VALID_DICT_FIELDS`",
|
||||
)
|
||||
|
||||
@require_torch
|
||||
def test_14_valid_dict_input_parsing(self):
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
args = TrainingArguments(
|
||||
output_dir=tmp_dir,
|
||||
accelerator_config='{"split_batches": "True", "gradient_accumulation_kwargs": {"num_steps": 2}}',
|
||||
)
|
||||
self.assertEqual(args.accelerator_config.split_batches, True)
|
||||
self.assertEqual(args.accelerator_config.gradient_accumulation_kwargs["num_steps"], 2)
|
||||
|
||||
def test_15_integration_training_args(self):
|
||||
parser = HfArgumentParser(TrainingArguments)
|
||||
self.assertIsNotNone(parser)
|
||||
|
||||
@require_torch
|
||||
@patch("sys.argv", ["test.py", "--accelerator_config", '{"gradient_accumulation_kwargs": {"num_steps": 2}}'])
|
||||
def test_16_cli_input_parsing(self):
|
||||
parser = HfArgumentParser(TrainingArguments)
|
||||
training_args = parser.parse_args_into_dataclasses()[0]
|
||||
self.assertEqual(training_args.accelerator_config.gradient_accumulation_kwargs["num_steps"], 2)
|
||||
211
transformers/tests/utils/test_hub_utils.py
Normal file
211
transformers/tests/utils/test_hub_utils.py
Normal file
@@ -0,0 +1,211 @@
|
||||
# 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 huggingface_hub import hf_hub_download
|
||||
from huggingface_hub.errors import HfHubHTTPError, LocalEntryNotFoundError, OfflineModeIsEnabled
|
||||
|
||||
from transformers.utils import (
|
||||
CONFIG_NAME,
|
||||
TRANSFORMERS_CACHE,
|
||||
WEIGHTS_NAME,
|
||||
cached_file,
|
||||
has_file,
|
||||
list_repo_templates,
|
||||
)
|
||||
|
||||
|
||||
RANDOM_BERT = "hf-internal-testing/tiny-random-bert"
|
||||
TINY_BERT_PT_ONLY = "hf-internal-testing/tiny-bert-pt-only"
|
||||
CACHE_DIR = os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
|
||||
FULL_COMMIT_HASH = "9b8c223d42b2188cb49d29af482996f9d0f3e5a6"
|
||||
|
||||
GATED_REPO = "hf-internal-testing/dummy-gated-model"
|
||||
README_FILE = "README.md"
|
||||
|
||||
|
||||
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)
|
||||
|
||||
# Under the mock environment, hf_hub_download will always raise an HTTPError
|
||||
with mock.patch(
|
||||
"transformers.utils.hub.hf_hub_download",
|
||||
side_effect=HfHubHTTPError("failed", response=mock.Mock(status_code=404)),
|
||||
) 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(TINY_BERT_PT_ONLY, WEIGHTS_NAME))
|
||||
self.assertFalse(has_file(TINY_BERT_PT_ONLY, "tf_model.h5"))
|
||||
self.assertFalse(has_file(TINY_BERT_PT_ONLY, "flax_model.msgpack"))
|
||||
|
||||
def test_has_file_in_cache(self):
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
# Empty cache dir + offline mode => return False
|
||||
assert not has_file(TINY_BERT_PT_ONLY, WEIGHTS_NAME, local_files_only=True, cache_dir=tmp_dir)
|
||||
|
||||
# Populate cache dir
|
||||
hf_hub_download(TINY_BERT_PT_ONLY, WEIGHTS_NAME, cache_dir=tmp_dir)
|
||||
|
||||
# Cache dir + offline mode => return True
|
||||
assert has_file(TINY_BERT_PT_ONLY, WEIGHTS_NAME, local_files_only=True, cache_dir=tmp_dir)
|
||||
|
||||
def test_get_file_from_repo_distant(self):
|
||||
# should return None if the file does not exist
|
||||
self.assertIsNone(
|
||||
cached_file(
|
||||
"google-bert/bert-base-cased",
|
||||
"ahah.txt",
|
||||
_raise_exceptions_for_gated_repo=False,
|
||||
_raise_exceptions_for_missing_entries=False,
|
||||
_raise_exceptions_for_connection_errors=False,
|
||||
)
|
||||
)
|
||||
|
||||
# The function raises if the repository does not exist.
|
||||
with self.assertRaisesRegex(EnvironmentError, "is not a valid model identifier"):
|
||||
cached_file(
|
||||
"bert-base-case",
|
||||
CONFIG_NAME,
|
||||
_raise_exceptions_for_gated_repo=False,
|
||||
_raise_exceptions_for_missing_entries=False,
|
||||
_raise_exceptions_for_connection_errors=False,
|
||||
)
|
||||
|
||||
# The function raises if the revision does not exist.
|
||||
with self.assertRaisesRegex(EnvironmentError, "is not a valid git identifier"):
|
||||
cached_file(
|
||||
"google-bert/bert-base-cased",
|
||||
CONFIG_NAME,
|
||||
revision="ahaha",
|
||||
_raise_exceptions_for_gated_repo=False,
|
||||
_raise_exceptions_for_missing_entries=False,
|
||||
_raise_exceptions_for_connection_errors=False,
|
||||
)
|
||||
|
||||
resolved_file = cached_file(
|
||||
"google-bert/bert-base-cased",
|
||||
CONFIG_NAME,
|
||||
_raise_exceptions_for_gated_repo=False,
|
||||
_raise_exceptions_for_missing_entries=False,
|
||||
_raise_exceptions_for_connection_errors=False,
|
||||
)
|
||||
# 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).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(
|
||||
cached_file(
|
||||
tmp_dir,
|
||||
"a.txt",
|
||||
_raise_exceptions_for_gated_repo=False,
|
||||
_raise_exceptions_for_missing_entries=False,
|
||||
_raise_exceptions_for_connection_errors=False,
|
||||
),
|
||||
str(filename),
|
||||
)
|
||||
|
||||
self.assertIsNone(
|
||||
cached_file(
|
||||
tmp_dir,
|
||||
"b.txt",
|
||||
_raise_exceptions_for_gated_repo=False,
|
||||
_raise_exceptions_for_missing_entries=False,
|
||||
_raise_exceptions_for_connection_errors=False,
|
||||
)
|
||||
)
|
||||
|
||||
def test_get_file_gated_repo(self):
|
||||
"""Test download file from a gated repo fails with correct message when not authenticated."""
|
||||
with self.assertRaisesRegex(EnvironmentError, "You are trying to access a gated repo."):
|
||||
# All files except README.md are protected on a gated repo.
|
||||
cached_file(GATED_REPO, "gated_file.txt", token=False)
|
||||
|
||||
def test_has_file_gated_repo(self):
|
||||
"""Test check file existence from a gated repo fails with correct message when not authenticated."""
|
||||
with self.assertRaisesRegex(EnvironmentError, "is a gated repository"):
|
||||
# All files except README.md are protected on a gated repo.
|
||||
has_file(GATED_REPO, "gated_file.txt", token=False)
|
||||
|
||||
def test_cached_files_exception_raised(self):
|
||||
"""Test that unhadled exceptions, e.g. ModuleNotFoundError, is properly re-raised by cached_files when hf_hub_download fails."""
|
||||
with mock.patch(
|
||||
"transformers.utils.hub.hf_hub_download", side_effect=ModuleNotFoundError("No module named 'MockModule'")
|
||||
):
|
||||
with self.assertRaises(ModuleNotFoundError):
|
||||
# The error should be re-raised by cached_files, not caught in the exception handling block
|
||||
cached_file(RANDOM_BERT, "nonexistent.json")
|
||||
|
||||
|
||||
class OfflineModeTests(unittest.TestCase):
|
||||
def test_list_repo_templates_w_offline(self):
|
||||
with mock.patch("transformers.utils.hub.list_repo_tree", side_effect=OfflineModeIsEnabled()):
|
||||
with mock.patch(
|
||||
"transformers.utils.hub.snapshot_download", side_effect=LocalEntryNotFoundError("no snapshot found")
|
||||
):
|
||||
self.assertEqual(list_repo_templates(RANDOM_BERT, local_files_only=False), [])
|
||||
224
transformers/tests/utils/test_image_processing_utils.py
Normal file
224
transformers/tests/utils/test_image_processing_utils.py
Normal file
@@ -0,0 +1,224 @@
|
||||
# Copyright 2024 HuggingFace Inc.
|
||||
#
|
||||
# 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 sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
|
||||
from transformers import AutoImageProcessor, ViTImageProcessor, ViTImageProcessorFast
|
||||
from transformers.image_processing_utils import get_size_dict
|
||||
from transformers.testing_utils import TOKEN, TemporaryHubRepo, get_tests_dir, is_staging_test
|
||||
|
||||
|
||||
sys.path.append(str(Path(__file__).parent.parent.parent / "utils"))
|
||||
|
||||
from test_module.custom_image_processing import CustomImageProcessor # noqa E402
|
||||
|
||||
|
||||
SAMPLE_IMAGE_PROCESSING_CONFIG_DIR = get_tests_dir("fixtures")
|
||||
|
||||
|
||||
class ImageProcessorUtilTester(unittest.TestCase):
|
||||
def test_cached_files_are_used_when_internet_is_down(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 = httpx.HTTPStatusError(
|
||||
"failed", request=mock.Mock(), response=mock.Mock()
|
||||
)
|
||||
response_mock.json.return_value = {}
|
||||
|
||||
# Download this model to make sure it's in the cache.
|
||||
_ = ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit")
|
||||
_ = ViTImageProcessorFast.from_pretrained("hf-internal-testing/tiny-random-vit")
|
||||
|
||||
# Under the mock environment we get a 500 error when trying to reach the model.
|
||||
with mock.patch("httpx.Client.request", return_value=response_mock) as mock_head:
|
||||
_ = ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit")
|
||||
_ = ViTImageProcessorFast.from_pretrained("hf-internal-testing/tiny-random-vit")
|
||||
# This check we did call the fake head request
|
||||
mock_head.assert_called()
|
||||
|
||||
def test_image_processor_from_pretrained_subfolder(self):
|
||||
with self.assertRaises(OSError):
|
||||
# config is in subfolder, the following should not work without specifying the subfolder
|
||||
_ = AutoImageProcessor.from_pretrained("hf-internal-testing/stable-diffusion-all-variants")
|
||||
|
||||
config = AutoImageProcessor.from_pretrained(
|
||||
"hf-internal-testing/stable-diffusion-all-variants", subfolder="feature_extractor"
|
||||
)
|
||||
|
||||
self.assertIsNotNone(config)
|
||||
|
||||
|
||||
@is_staging_test
|
||||
class ImageProcessorPushToHubTester(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls._token = TOKEN
|
||||
|
||||
def test_push_to_hub(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
image_processor = ViTImageProcessor.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
|
||||
image_processor.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
new_image_processor = ViTImageProcessor.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in image_processor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_image_processor, k))
|
||||
|
||||
def test_push_to_hub_fast(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
image_processor = ViTImageProcessorFast.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
|
||||
image_processor.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
new_image_processor = ViTImageProcessorFast.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in image_processor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_image_processor, k))
|
||||
|
||||
def test_push_to_hub_via_save_pretrained(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
image_processor = ViTImageProcessor.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
|
||||
# Push to hub via save_pretrained
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
image_processor.save_pretrained(tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token)
|
||||
|
||||
new_image_processor = ViTImageProcessor.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in image_processor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_image_processor, k))
|
||||
|
||||
def test_push_to_hub_via_save_pretrained_fast(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
image_processor = ViTImageProcessorFast.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
|
||||
# Push to hub via save_pretrained
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
image_processor.save_pretrained(tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token)
|
||||
|
||||
new_image_processor = ViTImageProcessorFast.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in image_processor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_image_processor, k))
|
||||
|
||||
def test_push_to_hub_in_organization(self):
|
||||
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
|
||||
image_processor = ViTImageProcessor.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
|
||||
image_processor.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
new_image_processor = ViTImageProcessor.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in image_processor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_image_processor, k))
|
||||
|
||||
def test_push_to_hub_in_organization_fast(self):
|
||||
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
|
||||
image_processor = ViTImageProcessorFast.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
|
||||
image_processor.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
new_image_processor = ViTImageProcessorFast.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in image_processor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_image_processor, k))
|
||||
|
||||
def test_push_to_hub_in_organization_via_save_pretrained(self):
|
||||
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
|
||||
image_processor = ViTImageProcessor.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
|
||||
# Push to hub via save_pretrained
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
image_processor.save_pretrained(tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token)
|
||||
|
||||
new_image_processor = ViTImageProcessor.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in image_processor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_image_processor, k))
|
||||
|
||||
def test_push_to_hub_in_organization_via_save_pretrained_fast(self):
|
||||
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
|
||||
image_processor = ViTImageProcessorFast.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
|
||||
# Push to hub via save_pretrained
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
image_processor.save_pretrained(tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token)
|
||||
|
||||
new_image_processor = ViTImageProcessorFast.from_pretrained(tmp_repo.repo_id)
|
||||
for k, v in image_processor.__dict__.items():
|
||||
self.assertEqual(v, getattr(new_image_processor, k))
|
||||
|
||||
def test_push_to_hub_dynamic_image_processor(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
CustomImageProcessor.register_for_auto_class()
|
||||
image_processor = CustomImageProcessor.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
|
||||
|
||||
image_processor.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
# This has added the proper auto_map field to the config
|
||||
self.assertDictEqual(
|
||||
image_processor.auto_map,
|
||||
{"AutoImageProcessor": "custom_image_processing.CustomImageProcessor"},
|
||||
)
|
||||
|
||||
new_image_processor = AutoImageProcessor.from_pretrained(tmp_repo.repo_id, trust_remote_code=True)
|
||||
# Can't make an isinstance check because the new_image_processor is from the CustomImageProcessor class of a dynamic module
|
||||
self.assertEqual(new_image_processor.__class__.__name__, "CustomImageProcessor")
|
||||
|
||||
|
||||
class ImageProcessingUtilsTester(unittest.TestCase):
|
||||
def test_get_size_dict(self):
|
||||
# Test a dict with the wrong keys raises an error
|
||||
inputs = {"wrong_key": 224}
|
||||
with self.assertRaises(ValueError):
|
||||
get_size_dict(inputs)
|
||||
|
||||
inputs = {"height": 224}
|
||||
with self.assertRaises(ValueError):
|
||||
get_size_dict(inputs)
|
||||
|
||||
inputs = {"width": 224, "shortest_edge": 224}
|
||||
with self.assertRaises(ValueError):
|
||||
get_size_dict(inputs)
|
||||
|
||||
# Test a dict with the correct keys is returned as is
|
||||
inputs = {"height": 224, "width": 224}
|
||||
outputs = get_size_dict(inputs)
|
||||
self.assertEqual(outputs, inputs)
|
||||
|
||||
inputs = {"shortest_edge": 224}
|
||||
outputs = get_size_dict(inputs)
|
||||
self.assertEqual(outputs, {"shortest_edge": 224})
|
||||
|
||||
inputs = {"longest_edge": 224, "shortest_edge": 224}
|
||||
outputs = get_size_dict(inputs)
|
||||
self.assertEqual(outputs, {"longest_edge": 224, "shortest_edge": 224})
|
||||
|
||||
# Test a single int value which represents (size, size)
|
||||
outputs = get_size_dict(224)
|
||||
self.assertEqual(outputs, {"height": 224, "width": 224})
|
||||
|
||||
# Test a single int value which represents the shortest edge
|
||||
outputs = get_size_dict(224, default_to_square=False)
|
||||
self.assertEqual(outputs, {"shortest_edge": 224})
|
||||
|
||||
# Test a tuple of ints which represents (height, width)
|
||||
outputs = get_size_dict((150, 200))
|
||||
self.assertEqual(outputs, {"height": 150, "width": 200})
|
||||
|
||||
# Test a tuple of ints which represents (width, height)
|
||||
outputs = get_size_dict((150, 200), height_width_order=False)
|
||||
self.assertEqual(outputs, {"height": 200, "width": 150})
|
||||
|
||||
# Test an int representing the shortest edge and max_size which represents the longest edge
|
||||
outputs = get_size_dict(224, max_size=256, default_to_square=False)
|
||||
self.assertEqual(outputs, {"shortest_edge": 224, "longest_edge": 256})
|
||||
|
||||
# Test int with default_to_square=True and max_size fails
|
||||
with self.assertRaises(ValueError):
|
||||
get_size_dict(224, max_size=256, default_to_square=True)
|
||||
932
transformers/tests/utils/test_image_utils.py
Normal file
932
transformers/tests/utils/test_image_utils.py
Normal file
@@ -0,0 +1,932 @@
|
||||
# Copyright 2021 HuggingFace Inc.
|
||||
#
|
||||
# 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 codecs
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from io import BytesIO
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
import numpy as np
|
||||
import pytest
|
||||
from huggingface_hub.file_download import hf_hub_url, http_get
|
||||
|
||||
from tests.pipelines.test_pipelines_document_question_answering import INVOICE_URL
|
||||
from transformers import is_torch_available, is_vision_available
|
||||
from transformers.image_utils import (
|
||||
ChannelDimension,
|
||||
get_channel_dimension_axis,
|
||||
make_flat_list_of_images,
|
||||
make_list_of_images,
|
||||
make_nested_list_of_images,
|
||||
)
|
||||
from transformers.testing_utils import is_flaky, require_torch, require_vision
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
if is_vision_available():
|
||||
import PIL.Image
|
||||
|
||||
from transformers import ImageFeatureExtractionMixin
|
||||
from transformers.image_utils import get_image_size, infer_channel_dimension_format, load_image
|
||||
|
||||
|
||||
def get_image_from_hub_dataset(dataset_id: str, filename: str, revision: Optional[str] = None) -> "PIL.Image.Image":
|
||||
url = hf_hub_url(dataset_id, filename, repo_type="dataset", revision=revision)
|
||||
return PIL.Image.open(BytesIO(httpx.get(url, follow_redirects=True).content))
|
||||
|
||||
|
||||
def get_random_image(height, width):
|
||||
random_array = np.random.randint(0, 256, (height, width, 3), dtype=np.uint8)
|
||||
return PIL.Image.fromarray(random_array)
|
||||
|
||||
|
||||
@require_vision
|
||||
class ImageFeatureExtractionTester(unittest.TestCase):
|
||||
def test_conversion_image_to_array(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
image = get_random_image(16, 32)
|
||||
|
||||
# Conversion with defaults (rescale + channel first)
|
||||
array1 = feature_extractor.to_numpy_array(image)
|
||||
self.assertTrue(array1.dtype, np.float32)
|
||||
self.assertEqual(array1.shape, (3, 16, 32))
|
||||
|
||||
# Conversion with rescale and not channel first
|
||||
array2 = feature_extractor.to_numpy_array(image, channel_first=False)
|
||||
self.assertTrue(array2.dtype, np.float32)
|
||||
self.assertEqual(array2.shape, (16, 32, 3))
|
||||
self.assertTrue(np.array_equal(array1, array2.transpose(2, 0, 1)))
|
||||
|
||||
# Conversion with no rescale and channel first
|
||||
array3 = feature_extractor.to_numpy_array(image, rescale=False)
|
||||
self.assertTrue(array3.dtype, np.uint8)
|
||||
self.assertEqual(array3.shape, (3, 16, 32))
|
||||
self.assertTrue(np.array_equal(array1, array3.astype(np.float32) * (1 / 255.0)))
|
||||
|
||||
# Conversion with no rescale and not channel first
|
||||
array4 = feature_extractor.to_numpy_array(image, rescale=False, channel_first=False)
|
||||
self.assertTrue(array4.dtype, np.uint8)
|
||||
self.assertEqual(array4.shape, (16, 32, 3))
|
||||
self.assertTrue(np.array_equal(array2, array4.astype(np.float32) * (1 / 255.0)))
|
||||
|
||||
def test_conversion_array_to_array(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
array = np.random.randint(0, 256, (16, 32, 3), dtype=np.uint8)
|
||||
|
||||
# By default, rescale (for an array of ints) and channel permute
|
||||
array1 = feature_extractor.to_numpy_array(array)
|
||||
self.assertTrue(array1.dtype, np.float32)
|
||||
self.assertEqual(array1.shape, (3, 16, 32))
|
||||
self.assertTrue(np.array_equal(array1, array.transpose(2, 0, 1).astype(np.float32) * (1 / 255.0)))
|
||||
|
||||
# Same with no permute
|
||||
array2 = feature_extractor.to_numpy_array(array, channel_first=False)
|
||||
self.assertTrue(array2.dtype, np.float32)
|
||||
self.assertEqual(array2.shape, (16, 32, 3))
|
||||
self.assertTrue(np.array_equal(array2, array.astype(np.float32) * (1 / 255.0)))
|
||||
|
||||
# Force rescale to False
|
||||
array3 = feature_extractor.to_numpy_array(array, rescale=False)
|
||||
self.assertTrue(array3.dtype, np.uint8)
|
||||
self.assertEqual(array3.shape, (3, 16, 32))
|
||||
self.assertTrue(np.array_equal(array3, array.transpose(2, 0, 1)))
|
||||
|
||||
# Force rescale to False and no channel permute
|
||||
array4 = feature_extractor.to_numpy_array(array, rescale=False, channel_first=False)
|
||||
self.assertTrue(array4.dtype, np.uint8)
|
||||
self.assertEqual(array4.shape, (16, 32, 3))
|
||||
self.assertTrue(np.array_equal(array4, array))
|
||||
|
||||
# Now test the default rescale for a float array (defaults to False)
|
||||
array5 = feature_extractor.to_numpy_array(array2)
|
||||
self.assertTrue(array5.dtype, np.float32)
|
||||
self.assertEqual(array5.shape, (3, 16, 32))
|
||||
self.assertTrue(np.array_equal(array5, array1))
|
||||
|
||||
def test_make_list_of_images_pil(self):
|
||||
# Test a single image is converted to a list of 1 image
|
||||
pil_image = get_random_image(16, 32)
|
||||
images_list = make_list_of_images(pil_image)
|
||||
self.assertIsInstance(images_list, list)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertIsInstance(images_list[0], PIL.Image.Image)
|
||||
|
||||
# Test a list of images is not modified
|
||||
images = [get_random_image(16, 32) for _ in range(4)]
|
||||
images_list = make_list_of_images(images)
|
||||
self.assertIsInstance(images_list, list)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertIsInstance(images_list[0], PIL.Image.Image)
|
||||
|
||||
def test_make_list_of_images_numpy(self):
|
||||
# Test a single image is converted to a list of 1 image
|
||||
images = np.random.randint(0, 256, (16, 32, 3))
|
||||
images_list = make_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertTrue(np.array_equal(images_list[0], images))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
# Test a batch of images is converted to a list of images
|
||||
images = np.random.randint(0, 256, (4, 16, 32, 3))
|
||||
images_list = make_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0]))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
# Test a list of images is not modified
|
||||
images = [np.random.randint(0, 256, (16, 32, 3)) for _ in range(4)]
|
||||
images_list = make_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0]))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
# Test batched masks with no channel dimension are converted to a list of masks
|
||||
masks = np.random.randint(0, 2, (4, 16, 32))
|
||||
masks_list = make_list_of_images(masks, expected_ndims=2)
|
||||
self.assertEqual(len(masks_list), 4)
|
||||
self.assertTrue(np.array_equal(masks_list[0], masks[0]))
|
||||
self.assertIsInstance(masks_list, list)
|
||||
|
||||
@require_torch
|
||||
def test_make_list_of_images_torch(self):
|
||||
# Test a single image is converted to a list of 1 image
|
||||
images = torch.randint(0, 256, (16, 32, 3))
|
||||
images_list = make_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertTrue(np.array_equal(images_list[0], images))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
# Test a batch of images is converted to a list of images
|
||||
images = torch.randint(0, 256, (4, 16, 32, 3))
|
||||
images_list = make_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0]))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
# Test a list of images is left unchanged
|
||||
images = [torch.randint(0, 256, (16, 32, 3)) for _ in range(4)]
|
||||
images_list = make_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0]))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
def test_make_flat_list_of_images_pil(self):
|
||||
# Test a single image is converted to a list of 1 image
|
||||
pil_image = get_random_image(16, 32)
|
||||
images_list = make_flat_list_of_images(pil_image)
|
||||
self.assertIsInstance(images_list, list)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertIsInstance(images_list[0], PIL.Image.Image)
|
||||
|
||||
# Test a list of images is not modified
|
||||
images = [get_random_image(16, 32) for _ in range(4)]
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertIsInstance(images_list, list)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertIsInstance(images_list[0], PIL.Image.Image)
|
||||
|
||||
# Test a nested list of images is flattened
|
||||
images = [[get_random_image(16, 32) for _ in range(2)] for _ in range(2)]
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertIsInstance(images_list, list)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertIsInstance(images_list[0], PIL.Image.Image)
|
||||
|
||||
def test_make_flat_list_of_images_numpy(self):
|
||||
# Test a single image is converted to a list of 1 image
|
||||
images = np.random.randint(0, 256, (16, 32, 3))
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertTrue(np.array_equal(images_list[0], images))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
# Test a 4d array of images is changed to a list of images
|
||||
images = np.random.randint(0, 256, (4, 16, 32, 3))
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertIsInstance(images_list, list)
|
||||
self.assertIsInstance(images_list[0], np.ndarray)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0]))
|
||||
|
||||
# Test a list of images is not modified
|
||||
images = [np.random.randint(0, 256, (16, 32, 3)) for _ in range(4)]
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0]))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
# Test list of 4d array images is flattened
|
||||
images = [np.random.randint(0, 256, (4, 16, 32, 3)) for _ in range(2)]
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 8)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0][0]))
|
||||
self.assertIsInstance(images_list, list)
|
||||
self.assertIsInstance(images_list[0], np.ndarray)
|
||||
|
||||
# Test nested list of images is flattened
|
||||
images = [[np.random.randint(0, 256, (16, 32, 3)) for _ in range(2)] for _ in range(2)]
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0][0]))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
@require_torch
|
||||
def test_make_flat_list_of_images_torch(self):
|
||||
# Test a single image is converted to a list of 1 image
|
||||
images = torch.randint(0, 256, (16, 32, 3))
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertTrue(np.array_equal(images_list[0], images))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
# Test a 4d tensors of images is changed to a list of images
|
||||
images = torch.randint(0, 256, (4, 16, 32, 3))
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertIsInstance(images_list, list)
|
||||
self.assertIsInstance(images_list[0], torch.Tensor)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0]))
|
||||
|
||||
# Test a list of images is not modified
|
||||
images = [torch.randint(0, 256, (16, 32, 3)) for _ in range(4)]
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0]))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
# Test list of 4d tensors of imagess is flattened
|
||||
images = [torch.randint(0, 256, (4, 16, 32, 3)) for _ in range(2)]
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 8)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0][0]))
|
||||
self.assertIsInstance(images_list, list)
|
||||
self.assertIsInstance(images_list[0], torch.Tensor)
|
||||
|
||||
# Test nested list of images is flattened
|
||||
images = [[torch.randint(0, 256, (16, 32, 3)) for _ in range(2)] for _ in range(2)]
|
||||
images_list = make_flat_list_of_images(images)
|
||||
self.assertEqual(len(images_list), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0], images[0][0]))
|
||||
self.assertIsInstance(images_list, list)
|
||||
|
||||
def test_make_nested_list_of_images_pil(self):
|
||||
# Test a single image is converted to a nested list of 1 image
|
||||
pil_image = get_random_image(16, 32)
|
||||
images_list = make_nested_list_of_images(pil_image)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertEqual(len(images_list[0]), 1)
|
||||
self.assertIsInstance(images_list[0][0], PIL.Image.Image)
|
||||
|
||||
# Test a list of images is converted to a nested list of images
|
||||
images = [get_random_image(16, 32) for _ in range(4)]
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertEqual(len(images_list[0]), 4)
|
||||
self.assertIsInstance(images_list[0][0], PIL.Image.Image)
|
||||
|
||||
# Test a nested list of images is not modified
|
||||
images = [[get_random_image(16, 32) for _ in range(2)] for _ in range(2)]
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertEqual(len(images_list), 2)
|
||||
self.assertEqual(len(images_list[0]), 2)
|
||||
self.assertIsInstance(images_list[0][0], PIL.Image.Image)
|
||||
|
||||
def test_make_nested_list_of_images_numpy(self):
|
||||
# Test a single image is converted to a nested list of 1 image
|
||||
images = np.random.randint(0, 256, (16, 32, 3))
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertTrue(np.array_equal(images_list[0][0], images))
|
||||
|
||||
# Test a 4d array of images is converted to a nested list of images
|
||||
images = np.random.randint(0, 256, (4, 16, 32, 3))
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertIsInstance(images_list[0][0], np.ndarray)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertEqual(len(images_list[0]), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0][0], images[0]))
|
||||
|
||||
# Test a list of images is converted to a nested list of images
|
||||
images = [np.random.randint(0, 256, (16, 32, 3)) for _ in range(4)]
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertEqual(len(images_list[0]), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0][0], images[0]))
|
||||
|
||||
# Test a nested list of images is left unchanged
|
||||
images = [[np.random.randint(0, 256, (16, 32, 3)) for _ in range(2)] for _ in range(2)]
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertEqual(len(images_list), 2)
|
||||
self.assertEqual(len(images_list[0]), 2)
|
||||
self.assertTrue(np.array_equal(images_list[0][0], images[0][0]))
|
||||
|
||||
# Test a list of 4d array images is converted to a nested list of images
|
||||
images = [np.random.randint(0, 256, (4, 16, 32, 3)) for _ in range(2)]
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertIsInstance(images_list[0][0], np.ndarray)
|
||||
self.assertEqual(len(images_list), 2)
|
||||
self.assertEqual(len(images_list[0]), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0][0], images[0][0]))
|
||||
|
||||
@require_torch
|
||||
def test_make_nested_list_of_images_torch(self):
|
||||
# Test a single image is converted to a nested list of 1 image
|
||||
images = torch.randint(0, 256, (16, 32, 3))
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertEqual(len(images_list[0]), 1)
|
||||
self.assertTrue(np.array_equal(images_list[0][0], images))
|
||||
|
||||
# Test a 4d tensor of images is converted to a nested list of images
|
||||
images = torch.randint(0, 256, (4, 16, 32, 3))
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertIsInstance(images_list[0][0], torch.Tensor)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertEqual(len(images_list[0]), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0][0], images[0]))
|
||||
|
||||
# Test a list of images is converted to a nested list of images
|
||||
images = [torch.randint(0, 256, (16, 32, 3)) for _ in range(4)]
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertEqual(len(images_list), 1)
|
||||
self.assertEqual(len(images_list[0]), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0][0], images[0]))
|
||||
|
||||
# Test a nested list of images is left unchanged
|
||||
images = [[torch.randint(0, 256, (16, 32, 3)) for _ in range(2)] for _ in range(2)]
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertEqual(len(images_list), 2)
|
||||
self.assertEqual(len(images_list[0]), 2)
|
||||
self.assertTrue(np.array_equal(images_list[0][0], images[0][0]))
|
||||
|
||||
# Test a list of 4d tensor images is converted to a nested list of images
|
||||
images = [torch.randint(0, 256, (4, 16, 32, 3)) for _ in range(2)]
|
||||
images_list = make_nested_list_of_images(images)
|
||||
self.assertIsInstance(images_list[0], list)
|
||||
self.assertIsInstance(images_list[0][0], torch.Tensor)
|
||||
self.assertEqual(len(images_list), 2)
|
||||
self.assertEqual(len(images_list[0]), 4)
|
||||
self.assertTrue(np.array_equal(images_list[0][0], images[0][0]))
|
||||
|
||||
@require_torch
|
||||
def test_conversion_torch_to_array(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
tensor = torch.randint(0, 256, (16, 32, 3))
|
||||
array = tensor.numpy()
|
||||
|
||||
# By default, rescale (for a tensor of ints) and channel permute
|
||||
array1 = feature_extractor.to_numpy_array(array)
|
||||
self.assertTrue(array1.dtype, np.float32)
|
||||
self.assertEqual(array1.shape, (3, 16, 32))
|
||||
self.assertTrue(np.array_equal(array1, array.transpose(2, 0, 1).astype(np.float32) * (1 / 255.0)))
|
||||
|
||||
# Same with no permute
|
||||
array2 = feature_extractor.to_numpy_array(array, channel_first=False)
|
||||
self.assertTrue(array2.dtype, np.float32)
|
||||
self.assertEqual(array2.shape, (16, 32, 3))
|
||||
self.assertTrue(np.array_equal(array2, array.astype(np.float32) * (1 / 255.0)))
|
||||
|
||||
# Force rescale to False
|
||||
array3 = feature_extractor.to_numpy_array(array, rescale=False)
|
||||
self.assertTrue(array3.dtype, np.uint8)
|
||||
self.assertEqual(array3.shape, (3, 16, 32))
|
||||
self.assertTrue(np.array_equal(array3, array.transpose(2, 0, 1)))
|
||||
|
||||
# Force rescale to False and no channel permute
|
||||
array4 = feature_extractor.to_numpy_array(array, rescale=False, channel_first=False)
|
||||
self.assertTrue(array4.dtype, np.uint8)
|
||||
self.assertEqual(array4.shape, (16, 32, 3))
|
||||
self.assertTrue(np.array_equal(array4, array))
|
||||
|
||||
# Now test the default rescale for a float tensor (defaults to False)
|
||||
array5 = feature_extractor.to_numpy_array(array2)
|
||||
self.assertTrue(array5.dtype, np.float32)
|
||||
self.assertEqual(array5.shape, (3, 16, 32))
|
||||
self.assertTrue(np.array_equal(array5, array1))
|
||||
|
||||
def test_conversion_image_to_image(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
image = get_random_image(16, 32)
|
||||
|
||||
# On an image, `to_pil_image1` is a noop.
|
||||
image1 = feature_extractor.to_pil_image(image)
|
||||
self.assertTrue(isinstance(image, PIL.Image.Image))
|
||||
self.assertTrue(np.array_equal(np.array(image), np.array(image1)))
|
||||
|
||||
def test_conversion_array_to_image(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
array = np.random.randint(0, 256, (16, 32, 3), dtype=np.uint8)
|
||||
|
||||
# By default, no rescale (for an array of ints)
|
||||
image1 = feature_extractor.to_pil_image(array)
|
||||
self.assertTrue(isinstance(image1, PIL.Image.Image))
|
||||
self.assertTrue(np.array_equal(np.array(image1), array))
|
||||
|
||||
# If the array is channel-first, proper reordering of the channels is done.
|
||||
image2 = feature_extractor.to_pil_image(array.transpose(2, 0, 1))
|
||||
self.assertTrue(isinstance(image2, PIL.Image.Image))
|
||||
self.assertTrue(np.array_equal(np.array(image2), array))
|
||||
|
||||
# If the array has floating type, it's rescaled by default.
|
||||
image3 = feature_extractor.to_pil_image(array.astype(np.float32) * (1 / 255.0))
|
||||
self.assertTrue(isinstance(image3, PIL.Image.Image))
|
||||
self.assertTrue(np.array_equal(np.array(image3), array))
|
||||
|
||||
# You can override the default to rescale.
|
||||
image4 = feature_extractor.to_pil_image(array.astype(np.float32), rescale=False)
|
||||
self.assertTrue(isinstance(image4, PIL.Image.Image))
|
||||
self.assertTrue(np.array_equal(np.array(image4), array))
|
||||
|
||||
# And with floats + channel first.
|
||||
image5 = feature_extractor.to_pil_image(array.transpose(2, 0, 1).astype(np.float32) * (1 / 255.0))
|
||||
self.assertTrue(isinstance(image5, PIL.Image.Image))
|
||||
self.assertTrue(np.array_equal(np.array(image5), array))
|
||||
|
||||
@require_torch
|
||||
def test_conversion_tensor_to_image(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
tensor = torch.randint(0, 256, (16, 32, 3))
|
||||
array = tensor.numpy()
|
||||
|
||||
# By default, no rescale (for a tensor of ints)
|
||||
image1 = feature_extractor.to_pil_image(tensor)
|
||||
self.assertTrue(isinstance(image1, PIL.Image.Image))
|
||||
self.assertTrue(np.array_equal(np.array(image1), array))
|
||||
|
||||
# If the tensor is channel-first, proper reordering of the channels is done.
|
||||
image2 = feature_extractor.to_pil_image(tensor.permute(2, 0, 1))
|
||||
self.assertTrue(isinstance(image2, PIL.Image.Image))
|
||||
self.assertTrue(np.array_equal(np.array(image2), array))
|
||||
|
||||
# If the tensor has floating type, it's rescaled by default.
|
||||
image3 = feature_extractor.to_pil_image(tensor.float() / 255.0)
|
||||
self.assertTrue(isinstance(image3, PIL.Image.Image))
|
||||
self.assertTrue(np.array_equal(np.array(image3), array))
|
||||
|
||||
# You can override the default to rescale.
|
||||
image4 = feature_extractor.to_pil_image(tensor.float(), rescale=False)
|
||||
self.assertTrue(isinstance(image4, PIL.Image.Image))
|
||||
self.assertTrue(np.array_equal(np.array(image4), array))
|
||||
|
||||
# And with floats + channel first.
|
||||
image5 = feature_extractor.to_pil_image(tensor.permute(2, 0, 1).float() * (1 / 255.0))
|
||||
self.assertTrue(isinstance(image5, PIL.Image.Image))
|
||||
self.assertTrue(np.array_equal(np.array(image5), array))
|
||||
|
||||
def test_resize_image_and_array(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
image = get_random_image(16, 32)
|
||||
array = np.array(image)
|
||||
|
||||
# Size can be an int or a tuple of ints.
|
||||
resized_image = feature_extractor.resize(image, 8)
|
||||
self.assertTrue(isinstance(resized_image, PIL.Image.Image))
|
||||
self.assertEqual(resized_image.size, (8, 8))
|
||||
|
||||
resized_image1 = feature_extractor.resize(image, (8, 16))
|
||||
self.assertTrue(isinstance(resized_image1, PIL.Image.Image))
|
||||
self.assertEqual(resized_image1.size, (8, 16))
|
||||
|
||||
# Passing an array converts it to a PIL Image.
|
||||
resized_image2 = feature_extractor.resize(array, 8)
|
||||
self.assertTrue(isinstance(resized_image2, PIL.Image.Image))
|
||||
self.assertEqual(resized_image2.size, (8, 8))
|
||||
self.assertTrue(np.array_equal(np.array(resized_image), np.array(resized_image2)))
|
||||
|
||||
resized_image3 = feature_extractor.resize(image, (8, 16))
|
||||
self.assertTrue(isinstance(resized_image3, PIL.Image.Image))
|
||||
self.assertEqual(resized_image3.size, (8, 16))
|
||||
self.assertTrue(np.array_equal(np.array(resized_image1), np.array(resized_image3)))
|
||||
|
||||
def test_resize_image_and_array_non_default_to_square(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
|
||||
heights_widths = [
|
||||
# height, width
|
||||
# square image
|
||||
(28, 28),
|
||||
(27, 27),
|
||||
# rectangular image: h < w
|
||||
(28, 34),
|
||||
(29, 35),
|
||||
# rectangular image: h > w
|
||||
(34, 28),
|
||||
(35, 29),
|
||||
]
|
||||
|
||||
# single integer or single integer in tuple/list
|
||||
sizes = [22, 27, 28, 36, [22], (27,)]
|
||||
|
||||
for (height, width), size in zip(heights_widths, sizes):
|
||||
for max_size in (None, 37, 1000):
|
||||
image = get_random_image(height, width)
|
||||
array = np.array(image)
|
||||
|
||||
size = size[0] if isinstance(size, (list, tuple)) else size
|
||||
# Size can be an int or a tuple of ints.
|
||||
# If size is an int, smaller edge of the image will be matched to this number.
|
||||
# i.e, if height > width, then image will be rescaled to (size * height / width, size).
|
||||
if height < width:
|
||||
exp_w, exp_h = (int(size * width / height), size)
|
||||
if max_size is not None and max_size < exp_w:
|
||||
exp_w, exp_h = max_size, int(max_size * exp_h / exp_w)
|
||||
elif width < height:
|
||||
exp_w, exp_h = (size, int(size * height / width))
|
||||
if max_size is not None and max_size < exp_h:
|
||||
exp_w, exp_h = int(max_size * exp_w / exp_h), max_size
|
||||
else:
|
||||
exp_w, exp_h = (size, size)
|
||||
if max_size is not None and max_size < size:
|
||||
exp_w, exp_h = max_size, max_size
|
||||
|
||||
resized_image = feature_extractor.resize(image, size=size, default_to_square=False, max_size=max_size)
|
||||
self.assertTrue(isinstance(resized_image, PIL.Image.Image))
|
||||
self.assertEqual(resized_image.size, (exp_w, exp_h))
|
||||
|
||||
# Passing an array converts it to a PIL Image.
|
||||
resized_image2 = feature_extractor.resize(array, size=size, default_to_square=False, max_size=max_size)
|
||||
self.assertTrue(isinstance(resized_image2, PIL.Image.Image))
|
||||
self.assertEqual(resized_image2.size, (exp_w, exp_h))
|
||||
self.assertTrue(np.array_equal(np.array(resized_image), np.array(resized_image2)))
|
||||
|
||||
@require_torch
|
||||
def test_resize_tensor(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
tensor = torch.randint(0, 256, (16, 32, 3))
|
||||
array = tensor.numpy()
|
||||
|
||||
# Size can be an int or a tuple of ints.
|
||||
resized_image = feature_extractor.resize(tensor, 8)
|
||||
self.assertTrue(isinstance(resized_image, PIL.Image.Image))
|
||||
self.assertEqual(resized_image.size, (8, 8))
|
||||
|
||||
resized_image1 = feature_extractor.resize(tensor, (8, 16))
|
||||
self.assertTrue(isinstance(resized_image1, PIL.Image.Image))
|
||||
self.assertEqual(resized_image1.size, (8, 16))
|
||||
|
||||
# Check we get the same results as with NumPy arrays.
|
||||
resized_image2 = feature_extractor.resize(array, 8)
|
||||
self.assertTrue(np.array_equal(np.array(resized_image), np.array(resized_image2)))
|
||||
|
||||
resized_image3 = feature_extractor.resize(array, (8, 16))
|
||||
self.assertTrue(np.array_equal(np.array(resized_image1), np.array(resized_image3)))
|
||||
|
||||
def test_normalize_image(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
image = get_random_image(16, 32)
|
||||
array = np.array(image)
|
||||
mean = [0.1, 0.5, 0.9]
|
||||
std = [0.2, 0.4, 0.6]
|
||||
|
||||
# PIL Image are converted to NumPy arrays for the normalization
|
||||
normalized_image = feature_extractor.normalize(image, mean, std)
|
||||
self.assertTrue(isinstance(normalized_image, np.ndarray))
|
||||
self.assertEqual(normalized_image.shape, (3, 16, 32))
|
||||
|
||||
# During the conversion rescale and channel first will be applied.
|
||||
expected = array.transpose(2, 0, 1).astype(np.float32) * (1 / 255.0)
|
||||
np_mean = np.array(mean).astype(np.float32)[:, None, None]
|
||||
np_std = np.array(std).astype(np.float32)[:, None, None]
|
||||
expected = (expected - np_mean) / np_std
|
||||
self.assertTrue(np.array_equal(normalized_image, expected))
|
||||
|
||||
def test_normalize_array(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
array = np.random.random((16, 32, 3))
|
||||
mean = [0.1, 0.5, 0.9]
|
||||
std = [0.2, 0.4, 0.6]
|
||||
|
||||
# mean and std can be passed as lists or NumPy arrays.
|
||||
expected = (array - np.array(mean)) / np.array(std)
|
||||
normalized_array = feature_extractor.normalize(array, mean, std)
|
||||
self.assertTrue(np.array_equal(normalized_array, expected))
|
||||
|
||||
normalized_array = feature_extractor.normalize(array, np.array(mean), np.array(std))
|
||||
self.assertTrue(np.array_equal(normalized_array, expected))
|
||||
|
||||
# Normalize will detect automatically if channel first or channel last is used.
|
||||
array = np.random.random((3, 16, 32))
|
||||
expected = (array - np.array(mean)[:, None, None]) / np.array(std)[:, None, None]
|
||||
normalized_array = feature_extractor.normalize(array, mean, std)
|
||||
self.assertTrue(np.array_equal(normalized_array, expected))
|
||||
|
||||
normalized_array = feature_extractor.normalize(array, np.array(mean), np.array(std))
|
||||
self.assertTrue(np.array_equal(normalized_array, expected))
|
||||
|
||||
@require_torch
|
||||
def test_normalize_tensor(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
tensor = torch.rand(16, 32, 3)
|
||||
mean = [0.1, 0.5, 0.9]
|
||||
std = [0.2, 0.4, 0.6]
|
||||
|
||||
# mean and std can be passed as lists or tensors.
|
||||
expected = (tensor - torch.tensor(mean)) / torch.tensor(std)
|
||||
normalized_tensor = feature_extractor.normalize(tensor, mean, std)
|
||||
self.assertTrue(torch.equal(normalized_tensor, expected))
|
||||
|
||||
normalized_tensor = feature_extractor.normalize(tensor, torch.tensor(mean), torch.tensor(std))
|
||||
self.assertTrue(torch.equal(normalized_tensor, expected))
|
||||
|
||||
# Normalize will detect automatically if channel first or channel last is used.
|
||||
tensor = torch.rand(3, 16, 32)
|
||||
expected = (tensor - torch.tensor(mean)[:, None, None]) / torch.tensor(std)[:, None, None]
|
||||
normalized_tensor = feature_extractor.normalize(tensor, mean, std)
|
||||
self.assertTrue(torch.equal(normalized_tensor, expected))
|
||||
|
||||
normalized_tensor = feature_extractor.normalize(tensor, torch.tensor(mean), torch.tensor(std))
|
||||
self.assertTrue(torch.equal(normalized_tensor, expected))
|
||||
|
||||
def test_center_crop_image(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
image = get_random_image(16, 32)
|
||||
|
||||
# Test various crop sizes: bigger on all dimensions, on one of the dimensions only and on both dimensions.
|
||||
crop_sizes = [8, (8, 64), 20, (32, 64)]
|
||||
for size in crop_sizes:
|
||||
cropped_image = feature_extractor.center_crop(image, size)
|
||||
self.assertTrue(isinstance(cropped_image, PIL.Image.Image))
|
||||
|
||||
# PIL Image.size is transposed compared to NumPy or PyTorch (width first instead of height first).
|
||||
expected_size = (size, size) if isinstance(size, int) else (size[1], size[0])
|
||||
self.assertEqual(cropped_image.size, expected_size)
|
||||
|
||||
def test_center_crop_array(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
image = get_random_image(16, 32)
|
||||
array = feature_extractor.to_numpy_array(image)
|
||||
|
||||
# Test various crop sizes: bigger on all dimensions, on one of the dimensions only and on both dimensions.
|
||||
crop_sizes = [8, (8, 64), 20, (32, 64)]
|
||||
for size in crop_sizes:
|
||||
cropped_array = feature_extractor.center_crop(array, size)
|
||||
self.assertTrue(isinstance(cropped_array, np.ndarray))
|
||||
|
||||
expected_size = (size, size) if isinstance(size, int) else size
|
||||
self.assertEqual(cropped_array.shape[-2:], expected_size)
|
||||
|
||||
# Check result is consistent with PIL.Image.crop
|
||||
cropped_image = feature_extractor.center_crop(image, size)
|
||||
self.assertTrue(np.array_equal(cropped_array, feature_extractor.to_numpy_array(cropped_image)))
|
||||
|
||||
@require_torch
|
||||
def test_center_crop_tensor(self):
|
||||
feature_extractor = ImageFeatureExtractionMixin()
|
||||
image = get_random_image(16, 32)
|
||||
array = feature_extractor.to_numpy_array(image)
|
||||
tensor = torch.tensor(array)
|
||||
|
||||
# Test various crop sizes: bigger on all dimensions, on one of the dimensions only and on both dimensions.
|
||||
crop_sizes = [8, (8, 64), 20, (32, 64)]
|
||||
for size in crop_sizes:
|
||||
cropped_tensor = feature_extractor.center_crop(tensor, size)
|
||||
self.assertTrue(isinstance(cropped_tensor, torch.Tensor))
|
||||
|
||||
expected_size = (size, size) if isinstance(size, int) else size
|
||||
self.assertEqual(cropped_tensor.shape[-2:], expected_size)
|
||||
|
||||
# Check result is consistent with PIL.Image.crop
|
||||
cropped_image = feature_extractor.center_crop(image, size)
|
||||
self.assertTrue(torch.equal(cropped_tensor, torch.tensor(feature_extractor.to_numpy_array(cropped_image))))
|
||||
|
||||
|
||||
@require_vision
|
||||
class LoadImageTester(unittest.TestCase):
|
||||
def test_load_img_url(self):
|
||||
img = load_image(INVOICE_URL)
|
||||
img_arr = np.array(img)
|
||||
|
||||
self.assertEqual(img_arr.shape, (1061, 750, 3))
|
||||
|
||||
@is_flaky()
|
||||
def test_load_img_url_timeout(self):
|
||||
with self.assertRaises(httpx.ConnectTimeout):
|
||||
load_image(INVOICE_URL, timeout=0.001)
|
||||
|
||||
def test_load_img_local(self):
|
||||
img = load_image("./tests/fixtures/tests_samples/COCO/000000039769.png")
|
||||
img_arr = np.array(img)
|
||||
|
||||
self.assertEqual(
|
||||
img_arr.shape,
|
||||
(480, 640, 3),
|
||||
)
|
||||
|
||||
def test_load_img_base64_prefix(self):
|
||||
try:
|
||||
tmp_file = tempfile.NamedTemporaryFile(delete=False).name
|
||||
with open(tmp_file, "wb") as f:
|
||||
http_get(
|
||||
"https://huggingface.co/datasets/hf-internal-testing/dummy-base64-images/raw/main/image_0.txt", f
|
||||
)
|
||||
|
||||
with open(tmp_file, encoding="utf-8") as b64:
|
||||
img = load_image(b64.read())
|
||||
img_arr = np.array(img)
|
||||
|
||||
finally:
|
||||
os.remove(tmp_file)
|
||||
|
||||
self.assertEqual(img_arr.shape, (64, 32, 3))
|
||||
|
||||
def test_load_img_base64(self):
|
||||
try:
|
||||
tmp_file = tempfile.NamedTemporaryFile(delete=False).name
|
||||
with open(tmp_file, "wb") as f:
|
||||
http_get(
|
||||
"https://huggingface.co/datasets/hf-internal-testing/dummy-base64-images/raw/main/image_1.txt", f
|
||||
)
|
||||
|
||||
with open(tmp_file, encoding="utf-8") as b64:
|
||||
img = load_image(b64.read())
|
||||
img_arr = np.array(img)
|
||||
|
||||
finally:
|
||||
os.remove(tmp_file)
|
||||
|
||||
self.assertEqual(img_arr.shape, (64, 32, 3))
|
||||
|
||||
def test_load_img_base64_encoded_bytes(self):
|
||||
try:
|
||||
tmp_file = tempfile.NamedTemporaryFile(delete=False).name
|
||||
with open(tmp_file, "wb") as f:
|
||||
http_get(
|
||||
"https://huggingface.co/datasets/hf-internal-testing/dummy-base64-images/raw/main/image_2.txt", f
|
||||
)
|
||||
|
||||
with codecs.open(tmp_file, encoding="unicode_escape") as b64:
|
||||
img = load_image(b64.read())
|
||||
img_arr = np.array(img)
|
||||
|
||||
finally:
|
||||
os.remove(tmp_file)
|
||||
|
||||
self.assertEqual(img_arr.shape, (256, 256, 3))
|
||||
|
||||
def test_load_img_rgba(self):
|
||||
# we use revision="refs/pr/1" until the PR is merged
|
||||
# https://hf.co/datasets/hf-internal-testing/fixtures_image_utils/discussions/1
|
||||
img = get_image_from_hub_dataset(
|
||||
"hf-internal-testing/fixtures_image_utils", "0-test-lena.png", revision="refs/pr/1"
|
||||
)
|
||||
|
||||
img = load_image(img) # img with mode RGBA
|
||||
img_arr = np.array(img)
|
||||
|
||||
self.assertEqual(
|
||||
img_arr.shape,
|
||||
(512, 512, 3),
|
||||
)
|
||||
|
||||
def test_load_img_la(self):
|
||||
# we use revision="refs/pr/1" until the PR is merged
|
||||
# https://hf.co/datasets/hf-internal-testing/fixtures_image_utils/discussions/1
|
||||
img = get_image_from_hub_dataset(
|
||||
"hf-internal-testing/fixtures_image_utils", "1-test-parrots.png", revision="refs/pr/1"
|
||||
)
|
||||
|
||||
img = load_image(img) # img with mode LA
|
||||
img_arr = np.array(img)
|
||||
|
||||
self.assertEqual(
|
||||
img_arr.shape,
|
||||
(512, 768, 3),
|
||||
)
|
||||
|
||||
def test_load_img_l(self):
|
||||
# we use revision="refs/pr/1" until the PR is merged
|
||||
# https://hf.co/datasets/hf-internal-testing/fixtures_image_utils/discussions/1
|
||||
img = get_image_from_hub_dataset(
|
||||
"hf-internal-testing/fixtures_image_utils", "2-test-tree.png", revision="refs/pr/1"
|
||||
)
|
||||
|
||||
img = load_image(img) # img with mode L
|
||||
img_arr = np.array(img)
|
||||
|
||||
self.assertEqual(
|
||||
img_arr.shape,
|
||||
(381, 225, 3),
|
||||
)
|
||||
|
||||
def test_load_img_exif_transpose(self):
|
||||
# we use revision="refs/pr/1" until the PR is merged
|
||||
# https://hf.co/datasets/hf-internal-testing/fixtures_image_utils/discussions/1
|
||||
|
||||
img_without_exif_transpose = get_image_from_hub_dataset(
|
||||
"hf-internal-testing/fixtures_image_utils", "3-test-cat-rotated.jpg", revision="refs/pr/1"
|
||||
)
|
||||
img_arr_without_exif_transpose = np.array(img_without_exif_transpose)
|
||||
|
||||
self.assertEqual(
|
||||
img_arr_without_exif_transpose.shape,
|
||||
(333, 500, 3),
|
||||
)
|
||||
|
||||
img_with_exif_transpose = load_image(img_without_exif_transpose)
|
||||
img_arr_with_exif_transpose = np.array(img_with_exif_transpose)
|
||||
|
||||
self.assertEqual(
|
||||
img_arr_with_exif_transpose.shape,
|
||||
(500, 333, 3),
|
||||
)
|
||||
|
||||
|
||||
class UtilFunctionTester(unittest.TestCase):
|
||||
def test_get_image_size(self):
|
||||
# Test we can infer the size and channel dimension of an image.
|
||||
image = np.random.randint(0, 256, (32, 64, 3))
|
||||
self.assertEqual(get_image_size(image), (32, 64))
|
||||
|
||||
image = np.random.randint(0, 256, (3, 32, 64))
|
||||
self.assertEqual(get_image_size(image), (32, 64))
|
||||
|
||||
# Test the channel dimension can be overridden
|
||||
image = np.random.randint(0, 256, (3, 32, 64))
|
||||
self.assertEqual(get_image_size(image, channel_dim=ChannelDimension.LAST), (3, 32))
|
||||
|
||||
def test_infer_channel_dimension(self):
|
||||
# Test we fail with invalid input
|
||||
with pytest.raises(ValueError):
|
||||
infer_channel_dimension_format(np.random.randint(0, 256, (10, 10)))
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
infer_channel_dimension_format(np.random.randint(0, 256, (10, 10, 10, 10, 10)))
|
||||
|
||||
# Test we fail if neither first not last dimension is of size 3 or 1
|
||||
with pytest.raises(ValueError):
|
||||
infer_channel_dimension_format(np.random.randint(0, 256, (10, 1, 50)))
|
||||
|
||||
# But if we explicitly set one of the number of channels to 50 it works
|
||||
inferred_dim = infer_channel_dimension_format(np.random.randint(0, 256, (10, 1, 50)), num_channels=50)
|
||||
self.assertEqual(inferred_dim, ChannelDimension.LAST)
|
||||
|
||||
# Test we correctly identify the channel dimension
|
||||
image = np.random.randint(0, 256, (3, 4, 5))
|
||||
inferred_dim = infer_channel_dimension_format(image)
|
||||
self.assertEqual(inferred_dim, ChannelDimension.FIRST)
|
||||
|
||||
image = np.random.randint(0, 256, (1, 4, 5))
|
||||
inferred_dim = infer_channel_dimension_format(image)
|
||||
self.assertEqual(inferred_dim, ChannelDimension.FIRST)
|
||||
|
||||
image = np.random.randint(0, 256, (4, 5, 3))
|
||||
inferred_dim = infer_channel_dimension_format(image)
|
||||
self.assertEqual(inferred_dim, ChannelDimension.LAST)
|
||||
|
||||
image = np.random.randint(0, 256, (4, 5, 1))
|
||||
inferred_dim = infer_channel_dimension_format(image)
|
||||
self.assertEqual(inferred_dim, ChannelDimension.LAST)
|
||||
|
||||
# We can take a batched array of images and find the dimension
|
||||
image = np.random.randint(0, 256, (1, 3, 4, 5))
|
||||
inferred_dim = infer_channel_dimension_format(image)
|
||||
self.assertEqual(inferred_dim, ChannelDimension.FIRST)
|
||||
|
||||
def test_get_channel_dimension_axis(self):
|
||||
# Test we correctly identify the channel dimension
|
||||
image = np.random.randint(0, 256, (3, 4, 5))
|
||||
inferred_axis = get_channel_dimension_axis(image)
|
||||
self.assertEqual(inferred_axis, 0)
|
||||
|
||||
image = np.random.randint(0, 256, (1, 4, 5))
|
||||
inferred_axis = get_channel_dimension_axis(image)
|
||||
self.assertEqual(inferred_axis, 0)
|
||||
|
||||
image = np.random.randint(0, 256, (4, 5, 3))
|
||||
inferred_axis = get_channel_dimension_axis(image)
|
||||
self.assertEqual(inferred_axis, 2)
|
||||
|
||||
image = np.random.randint(0, 256, (4, 5, 1))
|
||||
inferred_axis = get_channel_dimension_axis(image)
|
||||
self.assertEqual(inferred_axis, 2)
|
||||
|
||||
# We can take a batched array of images and find the dimension
|
||||
image = np.random.randint(0, 256, (1, 3, 4, 5))
|
||||
inferred_axis = get_channel_dimension_axis(image)
|
||||
self.assertEqual(inferred_axis, 1)
|
||||
207
transformers/tests/utils/test_import_structure.py
Normal file
207
transformers/tests/utils/test_import_structure.py
Normal file
@@ -0,0 +1,207 @@
|
||||
import os
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from typing import Callable
|
||||
|
||||
import pytest
|
||||
|
||||
from transformers.utils.import_utils import (
|
||||
Backend,
|
||||
VersionComparison,
|
||||
define_import_structure,
|
||||
spread_import_structure,
|
||||
)
|
||||
|
||||
|
||||
import_structures = Path(__file__).parent / "import_structures"
|
||||
|
||||
|
||||
def fetch__all__(file_content):
|
||||
"""
|
||||
Returns the content of the __all__ variable in the file content.
|
||||
Returns None if not defined, otherwise returns a list of strings.
|
||||
"""
|
||||
lines = file_content.split("\n")
|
||||
for line_index in range(len(lines)):
|
||||
line = lines[line_index]
|
||||
if line.startswith("__all__ = "):
|
||||
# __all__ is defined on a single line
|
||||
if line.endswith("]"):
|
||||
return [obj.strip("\"' ") for obj in line.split("=")[1].strip(" []").split(",")]
|
||||
|
||||
# __all__ is defined on multiple lines
|
||||
else:
|
||||
_all = []
|
||||
for __all__line_index in range(line_index + 1, len(lines)):
|
||||
if lines[__all__line_index].strip() == "]":
|
||||
return _all
|
||||
else:
|
||||
_all.append(lines[__all__line_index].strip("\"', "))
|
||||
|
||||
|
||||
class TestImportStructures(unittest.TestCase):
|
||||
base_transformers_path = Path(__file__).parent.parent.parent
|
||||
models_path = base_transformers_path / "src" / "transformers" / "models"
|
||||
models_import_structure = spread_import_structure(define_import_structure(models_path))
|
||||
|
||||
def test_definition(self):
|
||||
import_structure = define_import_structure(import_structures)
|
||||
valid_frozensets: dict[frozenset | frozenset[str], dict[str, set[str]]] = {
|
||||
frozenset(): {
|
||||
"import_structure_raw_register": {"A0", "A4", "a0"},
|
||||
"import_structure_register_with_comments": {"B0", "b0"},
|
||||
},
|
||||
frozenset({"random_item_that_should_not_exist"}): {"failing_export": {"A0"}},
|
||||
frozenset({"torch"}): {
|
||||
"import_structure_raw_register": {"A1", "A2", "A3", "a1", "a2", "a3"},
|
||||
"import_structure_register_with_duplicates": {"C0", "C1", "C2", "C3", "c0", "c1", "c2", "c3"},
|
||||
"import_structure_register_with_comments": {"B1", "B2", "B3", "b1", "b2", "b3"},
|
||||
},
|
||||
frozenset({"torch>=2.5"}): {"import_structure_raw_register_with_versions": {"D0", "d0"}},
|
||||
frozenset({"torch>2.5"}): {"import_structure_raw_register_with_versions": {"D1", "d1"}},
|
||||
frozenset({"torch<=2.5"}): {"import_structure_raw_register_with_versions": {"D2", "d2"}},
|
||||
frozenset({"torch<2.5"}): {"import_structure_raw_register_with_versions": {"D3", "d3"}},
|
||||
frozenset({"torch==2.5"}): {"import_structure_raw_register_with_versions": {"D4", "d4"}},
|
||||
frozenset({"torch!=2.5"}): {"import_structure_raw_register_with_versions": {"D5", "d5"}},
|
||||
frozenset({"torch>=2.5", "accelerate<0.20"}): {
|
||||
"import_structure_raw_register_with_versions": {"D6", "d6"}
|
||||
},
|
||||
}
|
||||
|
||||
self.assertEqual(len(import_structure.keys()), len(valid_frozensets.keys()))
|
||||
for _frozenset in valid_frozensets:
|
||||
self.assertTrue(_frozenset in import_structure)
|
||||
self.assertListEqual(
|
||||
sorted(import_structure[_frozenset].keys()), sorted(valid_frozensets[_frozenset].keys())
|
||||
)
|
||||
for module, objects in valid_frozensets[_frozenset].items():
|
||||
self.assertTrue(module in import_structure[_frozenset])
|
||||
self.assertSetEqual(objects, import_structure[_frozenset][module])
|
||||
|
||||
def test_transformers_specific_model_import(self):
|
||||
"""
|
||||
This test ensures that there is equivalence between what is written down in __all__ and what is
|
||||
written down with register().
|
||||
|
||||
It doesn't test the backends attributed to register().
|
||||
"""
|
||||
for architecture in os.listdir(self.models_path):
|
||||
if (
|
||||
os.path.isfile(self.models_path / architecture)
|
||||
or architecture.startswith("_")
|
||||
or architecture == "deprecated"
|
||||
):
|
||||
continue
|
||||
|
||||
with self.subTest(f"Testing arch {architecture}"):
|
||||
import_structure = define_import_structure(self.models_path / architecture)
|
||||
backend_agnostic_import_structure = {}
|
||||
for module_object_mapping in import_structure.values():
|
||||
for module, objects in module_object_mapping.items():
|
||||
if module not in backend_agnostic_import_structure:
|
||||
backend_agnostic_import_structure[module] = []
|
||||
|
||||
backend_agnostic_import_structure[module].extend(objects)
|
||||
|
||||
for module, objects in backend_agnostic_import_structure.items():
|
||||
with open(self.models_path / architecture / f"{module}.py") as f:
|
||||
content = f.read()
|
||||
_all = fetch__all__(content)
|
||||
|
||||
if _all is None:
|
||||
raise ValueError(f"{module} doesn't have __all__ defined.")
|
||||
|
||||
error_message = (
|
||||
f"self.models_path / architecture / f'{module}.py doesn't seem to be defined correctly:\n"
|
||||
f"Defined in __all__: {sorted(_all)}\nDefined with register: {sorted(objects)}"
|
||||
)
|
||||
self.assertListEqual(sorted(objects), sorted(_all), msg=error_message)
|
||||
|
||||
def test_import_spread(self):
|
||||
"""
|
||||
This test is specifically designed to test that varying levels of depth across import structures are
|
||||
respected.
|
||||
|
||||
In this instance, frozensets are at respective depths of 1, 2 and 3, for example:
|
||||
- models.{frozensets}
|
||||
- models.albert.{frozensets}
|
||||
- models.deprecated.transfo_xl.{frozensets}
|
||||
"""
|
||||
initial_import_structure = {
|
||||
frozenset(): {"dummy_non_model": {"DummyObject"}},
|
||||
"models": {
|
||||
frozenset(): {"dummy_config": {"DummyConfig"}},
|
||||
"albert": {
|
||||
frozenset(): {"configuration_albert": {"AlbertConfig", "AlbertOnnxConfig"}},
|
||||
frozenset({"torch"}): {
|
||||
"modeling_albert": {
|
||||
"AlbertForMaskedLM",
|
||||
}
|
||||
},
|
||||
},
|
||||
"llama": {
|
||||
frozenset(): {"configuration_llama": {"LlamaConfig"}},
|
||||
frozenset({"torch"}): {
|
||||
"modeling_llama": {
|
||||
"LlamaForCausalLM",
|
||||
}
|
||||
},
|
||||
},
|
||||
"deprecated": {
|
||||
"transfo_xl": {
|
||||
frozenset({"torch"}): {
|
||||
"modeling_transfo_xl": {
|
||||
"TransfoXLModel",
|
||||
}
|
||||
},
|
||||
frozenset(): {
|
||||
"configuration_transfo_xl": {"TransfoXLConfig"},
|
||||
"tokenization_transfo_xl": {"TransfoXLCorpus", "TransfoXLTokenizer"},
|
||||
},
|
||||
},
|
||||
"deta": {
|
||||
frozenset({"torch"}): {
|
||||
"modeling_deta": {"DetaForObjectDetection", "DetaModel", "DetaPreTrainedModel"}
|
||||
},
|
||||
frozenset(): {"configuration_deta": {"DetaConfig"}},
|
||||
frozenset({"vision"}): {"image_processing_deta": {"DetaImageProcessor"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ground_truth_spread_import_structure = {
|
||||
frozenset(): {
|
||||
"dummy_non_model": {"DummyObject"},
|
||||
"models.dummy_config": {"DummyConfig"},
|
||||
"models.albert.configuration_albert": {"AlbertConfig", "AlbertOnnxConfig"},
|
||||
"models.llama.configuration_llama": {"LlamaConfig"},
|
||||
"models.deprecated.transfo_xl.configuration_transfo_xl": {"TransfoXLConfig"},
|
||||
"models.deprecated.transfo_xl.tokenization_transfo_xl": {"TransfoXLCorpus", "TransfoXLTokenizer"},
|
||||
"models.deprecated.deta.configuration_deta": {"DetaConfig"},
|
||||
},
|
||||
frozenset({"torch"}): {
|
||||
"models.albert.modeling_albert": {"AlbertForMaskedLM"},
|
||||
"models.llama.modeling_llama": {"LlamaForCausalLM"},
|
||||
"models.deprecated.transfo_xl.modeling_transfo_xl": {"TransfoXLModel"},
|
||||
"models.deprecated.deta.modeling_deta": {"DetaForObjectDetection", "DetaModel", "DetaPreTrainedModel"},
|
||||
},
|
||||
frozenset({"vision"}): {"models.deprecated.deta.image_processing_deta": {"DetaImageProcessor"}},
|
||||
}
|
||||
|
||||
newly_spread_import_structure = spread_import_structure(initial_import_structure)
|
||||
|
||||
self.assertEqual(ground_truth_spread_import_structure, newly_spread_import_structure)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"backend,package_name,version_comparison,version",
|
||||
[
|
||||
pytest.param(Backend("torch>=2.5 "), "torch", VersionComparison.GREATER_THAN_OR_EQUAL.value, "2.5"),
|
||||
pytest.param(Backend("torchvision==0.19.1"), "torchvision", VersionComparison.EQUAL.value, "0.19.1"),
|
||||
],
|
||||
)
|
||||
def test_backend_specification(backend: Backend, package_name: str, version_comparison: Callable, version: str):
|
||||
assert backend.package_name == package_name
|
||||
assert VersionComparison.from_string(backend.version_comparison) == version_comparison
|
||||
assert backend.version == version
|
||||
26
transformers/tests/utils/test_import_utils.py
Normal file
26
transformers/tests/utils/test_import_utils.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import sys
|
||||
|
||||
from transformers.testing_utils import run_test_using_subprocess
|
||||
from transformers.utils.import_utils import clear_import_cache
|
||||
|
||||
|
||||
@run_test_using_subprocess
|
||||
def test_clear_import_cache():
|
||||
"""Test the clear_import_cache function."""
|
||||
|
||||
# Save initial state
|
||||
initial_modules = {name: mod for name, mod in sys.modules.items() if name.startswith("transformers.")}
|
||||
assert len(initial_modules) > 0, "No transformers modules loaded before test"
|
||||
|
||||
# Execute clear_import_cache() function
|
||||
clear_import_cache()
|
||||
|
||||
# Verify modules were removed
|
||||
remaining_modules = {name: mod for name, mod in sys.modules.items() if name.startswith("transformers.")}
|
||||
assert len(remaining_modules) < len(initial_modules), "No modules were removed"
|
||||
|
||||
# Import and verify module exists
|
||||
from transformers.models.auto import modeling_auto
|
||||
|
||||
assert "transformers.models.auto.modeling_auto" in sys.modules
|
||||
assert modeling_auto.__name__ == "transformers.models.auto.modeling_auto"
|
||||
135
transformers/tests/utils/test_logging.py
Normal file
135
transformers/tests/utils/test_logging.py
Normal file
@@ -0,0 +1,135 @@
|
||||
# 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 os
|
||||
import unittest
|
||||
|
||||
from huggingface_hub.utils import are_progress_bars_disabled
|
||||
|
||||
import transformers.models.bart.tokenization_bart
|
||||
from transformers import logging
|
||||
from transformers.testing_utils import CaptureLogger, mockenv, mockenv_context
|
||||
from transformers.utils.logging import disable_progress_bar, enable_progress_bar
|
||||
|
||||
|
||||
class HfArgumentParserTest(unittest.TestCase):
|
||||
def test_set_level(self):
|
||||
logger = logging.get_logger()
|
||||
|
||||
# the current default level is logging.WARNING
|
||||
level_origin = logging.get_verbosity()
|
||||
|
||||
logging.set_verbosity_error()
|
||||
self.assertEqual(logger.getEffectiveLevel(), logging.get_verbosity())
|
||||
|
||||
logging.set_verbosity_warning()
|
||||
self.assertEqual(logger.getEffectiveLevel(), logging.get_verbosity())
|
||||
|
||||
logging.set_verbosity_info()
|
||||
self.assertEqual(logger.getEffectiveLevel(), logging.get_verbosity())
|
||||
|
||||
logging.set_verbosity_debug()
|
||||
self.assertEqual(logger.getEffectiveLevel(), logging.get_verbosity())
|
||||
|
||||
# restore to the original level
|
||||
logging.set_verbosity(level_origin)
|
||||
|
||||
def test_integration(self):
|
||||
level_origin = logging.get_verbosity()
|
||||
|
||||
logger = logging.get_logger("transformers.models.bart.tokenization_bart")
|
||||
msg = "Testing 1, 2, 3"
|
||||
|
||||
# should be able to log warnings (if default settings weren't overridden by `pytest --log-level-all`)
|
||||
if level_origin <= logging.WARNING:
|
||||
with CaptureLogger(logger) as cl:
|
||||
logger.warning(msg)
|
||||
self.assertEqual(cl.out, msg + "\n")
|
||||
|
||||
# this is setting the level for all of `transformers.*` loggers
|
||||
logging.set_verbosity_error()
|
||||
|
||||
# should not be able to log warnings
|
||||
with CaptureLogger(logger) as cl:
|
||||
logger.warning(msg)
|
||||
self.assertEqual(cl.out, "")
|
||||
|
||||
# should be able to log warnings again
|
||||
logging.set_verbosity_warning()
|
||||
with CaptureLogger(logger) as cl:
|
||||
logger.warning(msg)
|
||||
self.assertEqual(cl.out, msg + "\n")
|
||||
|
||||
# restore to the original level
|
||||
logging.set_verbosity(level_origin)
|
||||
|
||||
@mockenv(TRANSFORMERS_VERBOSITY="error")
|
||||
def test_env_override(self):
|
||||
# reset for the env var to take effect, next time some logger call is made
|
||||
transformers.utils.logging._reset_library_root_logger()
|
||||
# this action activates the env var
|
||||
_ = logging.get_logger("transformers.models.bart.tokenization_bart")
|
||||
|
||||
env_level_str = os.getenv("TRANSFORMERS_VERBOSITY", None)
|
||||
env_level = logging.log_levels[env_level_str]
|
||||
|
||||
current_level = logging.get_verbosity()
|
||||
self.assertEqual(
|
||||
env_level,
|
||||
current_level,
|
||||
f"TRANSFORMERS_VERBOSITY={env_level_str}/{env_level}, but internal verbosity is {current_level}",
|
||||
)
|
||||
|
||||
# restore to the original level
|
||||
os.environ["TRANSFORMERS_VERBOSITY"] = ""
|
||||
transformers.utils.logging._reset_library_root_logger()
|
||||
|
||||
@mockenv(TRANSFORMERS_VERBOSITY="super-error")
|
||||
def test_env_invalid_override(self):
|
||||
# reset for the env var to take effect, next time some logger call is made
|
||||
transformers.utils.logging._reset_library_root_logger()
|
||||
logger = logging.logging.getLogger()
|
||||
with CaptureLogger(logger) as cl:
|
||||
# this action activates the env var
|
||||
logging.get_logger("transformers.models.bart.tokenization_bart")
|
||||
self.assertIn("Unknown option TRANSFORMERS_VERBOSITY=super-error", cl.out)
|
||||
|
||||
# no need to restore as nothing was changed
|
||||
|
||||
def test_advisory_warnings(self):
|
||||
# testing `logger.warning_advice()`
|
||||
transformers.utils.logging._reset_library_root_logger()
|
||||
|
||||
logger = logging.get_logger("transformers.models.bart.tokenization_bart")
|
||||
msg = "Testing 1, 2, 3"
|
||||
|
||||
with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS="1"):
|
||||
# nothing should be logged as env var disables this method
|
||||
with CaptureLogger(logger) as cl:
|
||||
logger.warning_advice(msg)
|
||||
self.assertEqual(cl.out, "")
|
||||
|
||||
with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS=""):
|
||||
# should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset
|
||||
with CaptureLogger(logger) as cl:
|
||||
logger.warning_advice(msg)
|
||||
self.assertEqual(cl.out, msg + "\n")
|
||||
|
||||
|
||||
def test_set_progress_bar_enabled():
|
||||
disable_progress_bar()
|
||||
assert are_progress_bars_disabled()
|
||||
|
||||
enable_progress_bar()
|
||||
assert not are_progress_bars_disabled()
|
||||
246
transformers/tests/utils/test_masking_utils.py
Normal file
246
transformers/tests/utils/test_masking_utils.py
Normal file
@@ -0,0 +1,246 @@
|
||||
# Copyright 2025 HuggingFace Inc.
|
||||
#
|
||||
# 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 unittest
|
||||
|
||||
from transformers.testing_utils import is_torch_available, require_torch
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
from torch.nn.attention.flex_attention import create_block_mask
|
||||
|
||||
from transformers import DynamicCache, LlamaConfig
|
||||
from transformers.cache_utils import DynamicSlidingWindowLayer
|
||||
from transformers.masking_utils import create_causal_mask, create_chunked_causal_mask, find_packed_sequence_indices
|
||||
|
||||
|
||||
# fmt: off
|
||||
EXPECTED_PACKED_MASK = torch.tensor([[[
|
||||
[ True, False, False, False, False, False, False, False, False, False],
|
||||
[ True, True, False, False, False, False, False, False, False, False],
|
||||
[ True, True, True, False, False, False, False, False, False, False],
|
||||
[ True, True, True, True, False, False, False, False, False, False],
|
||||
[False, False, False, False, True, False, False, False, False, False],
|
||||
[False, False, False, False, True, True, False, False, False, False],
|
||||
[False, False, False, False, False, False, True, False, False, False],
|
||||
[False, False, False, False, False, False, True, True, False, False],
|
||||
[False, False, False, False, False, False, True, True, True, False],
|
||||
[False, False, False, False, False, False, True, True, True, True]]],
|
||||
|
||||
|
||||
[[[ True, False, False, False, False, False, False, False, False, False],
|
||||
[ True, True, False, False, False, False, False, False, False, False],
|
||||
[ True, True, True, False, False, False, False, False, False, False],
|
||||
[ True, True, True, True, False, False, False, False, False, False],
|
||||
[ True, True, True, True, True, False, False, False, False, False],
|
||||
[ True, True, True, True, True, True, False, False, False, False],
|
||||
[False, False, False, False, False, False, True, False, False, False],
|
||||
[False, False, False, False, False, False, True, True, False, False],
|
||||
[False, False, False, False, False, False, True, True, True, False],
|
||||
[False, False, False, False, False, False, True, True, True, True]
|
||||
]]], dtype=torch.bool)
|
||||
# fmt: on
|
||||
|
||||
|
||||
@require_torch
|
||||
class MaskTest(unittest.TestCase):
|
||||
def test_packed_sequence_mask_sdpa(self):
|
||||
config = LlamaConfig()
|
||||
config._attn_implementation = "sdpa"
|
||||
|
||||
batch_size = 2
|
||||
sequence_length = 10
|
||||
cache_position = torch.arange(sequence_length)
|
||||
|
||||
# First batch has 3 packed sequences of 4, 2 and 4 tokens respectively, second has 2 of 6 and 4 tokens
|
||||
position_ids = torch.tensor([[0, 1, 2, 3, 0, 1, 0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 0, 1, 2, 3]])
|
||||
|
||||
causal_mask = create_causal_mask(
|
||||
config=config,
|
||||
# we only need batch size, seq_length and dtype here - we don't care about the values of the embeddings
|
||||
input_embeds=torch.empty((batch_size, sequence_length), dtype=torch.float16),
|
||||
attention_mask=None,
|
||||
cache_position=cache_position,
|
||||
past_key_values=None,
|
||||
position_ids=position_ids,
|
||||
)
|
||||
|
||||
self.assertTrue((causal_mask == EXPECTED_PACKED_MASK).all())
|
||||
|
||||
def test_packed_sequence_mask_eager(self):
|
||||
config = LlamaConfig()
|
||||
config._attn_implementation = "eager"
|
||||
|
||||
batch_size = 2
|
||||
sequence_length = 10
|
||||
cache_position = torch.arange(sequence_length)
|
||||
|
||||
# First batch has 3 packed sequences of 4, 2 and 4 tokens respectively, second has 2 of 6 and 4 tokens
|
||||
position_ids = torch.tensor([[0, 1, 2, 3, 0, 1, 0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 0, 1, 2, 3]])
|
||||
|
||||
causal_mask = create_causal_mask(
|
||||
config=config,
|
||||
# we only need batch size, seq_length and dtype here - we don't care about the values of the embeddings
|
||||
input_embeds=torch.empty((batch_size, sequence_length), dtype=torch.float16),
|
||||
attention_mask=None,
|
||||
cache_position=cache_position,
|
||||
past_key_values=None,
|
||||
position_ids=position_ids,
|
||||
)
|
||||
|
||||
min_dtype = torch.finfo(torch.float16).min
|
||||
self.assertTrue((causal_mask == torch.where(EXPECTED_PACKED_MASK, 0.0, min_dtype)).all())
|
||||
|
||||
def test_packed_sequence_mask_flex_attention(self):
|
||||
config = LlamaConfig()
|
||||
config._attn_implementation = "flex_attention"
|
||||
|
||||
batch_size = 2
|
||||
sequence_length = 10
|
||||
cache_position = torch.arange(sequence_length)
|
||||
|
||||
# First batch has 3 packed sequences of 4, 2 and 4 tokens respectively, second has 2 of 6 and 4 tokens
|
||||
position_ids = torch.tensor([[0, 1, 2, 3, 0, 1, 0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 0, 1, 2, 3]])
|
||||
|
||||
causal_mask = create_causal_mask(
|
||||
config=config,
|
||||
# we only need batch size, seq_length and dtype here - we don't care about the values of the embeddings
|
||||
input_embeds=torch.empty((batch_size, sequence_length), dtype=torch.float16),
|
||||
attention_mask=None,
|
||||
cache_position=cache_position,
|
||||
past_key_values=None,
|
||||
position_ids=position_ids,
|
||||
)
|
||||
|
||||
def dummy_mask_mod(b, h, q, kv):
|
||||
return EXPECTED_PACKED_MASK[b, h, q, kv]
|
||||
|
||||
EXPECTED_BLOCK_MASK = create_block_mask(dummy_mask_mod, 2, None, 10, 10, device="cpu")
|
||||
|
||||
# We compatre the str representations, as the BlockMask objects themselves cannot easily be compared
|
||||
self.assertEqual(causal_mask.to_string(), EXPECTED_BLOCK_MASK.to_string())
|
||||
|
||||
def test_find_packed_sequence_indices(self):
|
||||
position_ids = torch.tensor([[0, 1, 2, 3, 0, 1, 0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 0, 1, 2, 3]])
|
||||
EXPECTED_SEQUENCE_INDICES = torch.tensor([[0, 0, 0, 0, 1, 1, 2, 2, 2, 2], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1]])
|
||||
self.assertTrue((find_packed_sequence_indices(position_ids) == EXPECTED_SEQUENCE_INDICES).all())
|
||||
|
||||
def test_chunked_mask_with_left_padding_and_large_prefill(self):
|
||||
# Make sure we have an attention_chunk_size in the config
|
||||
config = LlamaConfig(attention_chunk_size=3, attn_implementation="sdpa")
|
||||
|
||||
batch_size = 2
|
||||
sequence_length = 8
|
||||
pad_tokens = 4
|
||||
|
||||
input_ids = torch.randint(100, 200, (batch_size, sequence_length))
|
||||
attention_mask = torch.tensor(
|
||||
[[0 if i < pad_tokens else 1 for i in range(sequence_length)], [1] * sequence_length]
|
||||
)
|
||||
inputs_embeds = torch.empty_like(input_ids, dtype=torch.float16)
|
||||
cache_position = torch.arange(sequence_length)
|
||||
position_ids = torch.empty(batch_size, sequence_length, dtype=cache_position.dtype)
|
||||
position_ids[0, :pad_tokens] = 1
|
||||
position_ids[0, pad_tokens:] = torch.arange(sequence_length - pad_tokens)
|
||||
position_ids[1, :] = cache_position
|
||||
|
||||
chunked_attention_mask = create_chunked_causal_mask(
|
||||
config=config,
|
||||
input_embeds=inputs_embeds,
|
||||
attention_mask=attention_mask,
|
||||
cache_position=cache_position,
|
||||
past_key_values=None,
|
||||
position_ids=position_ids,
|
||||
)
|
||||
|
||||
# fmt: off
|
||||
EXPECTED_CHUNKED_MASK = torch.tensor(
|
||||
# Here, for the padded sequence, the chunk size should start correctly at index 4 (otherwise, with 4 padding
|
||||
# tokens are chunk_size=3, the first chunk is from indices 0-2, then 3-6 if we don't account for the padding correctly)
|
||||
[[[[False, False, False, False, False, False, False, False],
|
||||
[False, False, False, False, False, False, False, False],
|
||||
[False, False, False, False, False, False, False, False],
|
||||
[False, False, False, False, False, False, False, False],
|
||||
[False, False, False, False, True, False, False, False],
|
||||
[False, False, False, False, True, True, False, False],
|
||||
[False, False, False, False, True, True, True, False],
|
||||
[False, False, False, False, False, False, False, True]]],
|
||||
|
||||
|
||||
[[[ True, False, False, False, False, False, False, False],
|
||||
[ True, True, False, False, False, False, False, False],
|
||||
[ True, True, True, False, False, False, False, False],
|
||||
[False, False, False, True, False, False, False, False],
|
||||
[False, False, False, True, True, False, False, False],
|
||||
[False, False, False, True, True, True, False, False],
|
||||
[False, False, False, False, False, False, True, False],
|
||||
[False, False, False, False, False, False, True, True]]]],
|
||||
dtype=torch.bool)
|
||||
# fmt: on
|
||||
|
||||
self.assertTrue((chunked_attention_mask == EXPECTED_CHUNKED_MASK).all())
|
||||
|
||||
def test_chunked_mask_with_left_padding_decoding(self):
|
||||
# Make sure we have an attention_chunk_size in the config
|
||||
config = LlamaConfig(attention_chunk_size=4, attn_implementation="sdpa", num_hidden_layers=1)
|
||||
|
||||
cache = DynamicCache(config=config)
|
||||
# Sanity check
|
||||
self.assertEqual(len(cache), 1)
|
||||
self.assertTrue(isinstance(cache.layers[0], DynamicSlidingWindowLayer))
|
||||
|
||||
# Fill-in the Cache (sequence length is bigger than chunk size here)
|
||||
batch_size = 2
|
||||
prefill_size = 8
|
||||
pad_tokens = 7
|
||||
fake_kv = torch.rand(batch_size, 32, prefill_size, 32)
|
||||
cache.update(fake_kv, fake_kv, 0, torch.arange(prefill_size))
|
||||
|
||||
# Create a new input after the prefill
|
||||
input_ids = torch.randint(100, 200, (batch_size, 1))
|
||||
attention_mask = torch.tensor(
|
||||
[[0 if i < pad_tokens else 1 for i in range(prefill_size + 1)], [1] * (prefill_size + 1)]
|
||||
)
|
||||
inputs_embeds = torch.empty_like(input_ids, dtype=torch.float16)
|
||||
cache_position = torch.tensor([prefill_size], dtype=int)
|
||||
position_ids = torch.tensor([[prefill_size - pad_tokens], [prefill_size]])
|
||||
|
||||
chunked_attention_mask = create_chunked_causal_mask(
|
||||
config=config,
|
||||
input_embeds=inputs_embeds,
|
||||
attention_mask=attention_mask,
|
||||
cache_position=cache_position,
|
||||
past_key_values=cache,
|
||||
position_ids=position_ids,
|
||||
)
|
||||
|
||||
# To understand a bit more the following expected mask, here is the full 2d mask, where the "|" characters are the chunk
|
||||
# separators (where the tokens should stop seeing each other)
|
||||
# [0, 0, 0, 0, 0, 0, 0, | 1, 1], -> due to left padding, the first chunk only starts after the padding tokens
|
||||
# [| 1, 1, 1, 1, | 1, 1, 1, 1, | 1]]) -> easy case, each 4 tokens is a new chunk
|
||||
|
||||
# fmt: off
|
||||
EXPECTED_CHUNKED_MASK = torch.tensor(
|
||||
# Here, for the padded sequence, the chunk size should start correctly at index 7 (the first unpadded
|
||||
# index), and so only indices 7 and 8 should be True
|
||||
[[[[False, False, True, True]]],
|
||||
|
||||
# Here, for the unpadded sequence, the chunks start at index 0. Since we have 9 tokens in total, the last
|
||||
# token (index 8) will only see itself (we have 2 full chunks before)
|
||||
[[[False, False, False, True]]]],
|
||||
dtype=torch.bool)
|
||||
# fmt: on
|
||||
|
||||
self.assertTrue((chunked_attention_mask == EXPECTED_CHUNKED_MASK).all())
|
||||
88
transformers/tests/utils/test_model_card.py
Normal file
88
transformers/tests/utils/test_model_card.py
Normal file
@@ -0,0 +1,88 @@
|
||||
# Copyright 2019 HuggingFace Inc.
|
||||
#
|
||||
# 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
|
||||
|
||||
from transformers.modelcard import ModelCard, TrainingSummary
|
||||
|
||||
|
||||
class ModelCardTester(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.inputs_dict = {
|
||||
"model_details": {
|
||||
"Organization": "testing",
|
||||
"Model date": "today",
|
||||
"Model version": "v2.1, Developed by Test Corp in 2019.",
|
||||
"Architecture": "Convolutional Neural Network.",
|
||||
},
|
||||
"metrics": "BLEU and ROUGE-1",
|
||||
"evaluation_data": {
|
||||
"Datasets": {"BLEU": "My-great-dataset-v1", "ROUGE-1": "My-short-dataset-v2.1"},
|
||||
"Preprocessing": "See details on https://huggingface.co/papers/1810.03993",
|
||||
},
|
||||
"training_data": {
|
||||
"Dataset": "English Wikipedia dump dated 2018-12-01",
|
||||
"Preprocessing": (
|
||||
"Using SentencePiece vocabulary of size 52k tokens. See details on"
|
||||
" https://huggingface.co/papers/1810.03993"
|
||||
),
|
||||
},
|
||||
"quantitative_analyses": {"BLEU": 55.1, "ROUGE-1": 76},
|
||||
}
|
||||
|
||||
def test_model_card_common_properties(self):
|
||||
modelcard = ModelCard.from_dict(self.inputs_dict)
|
||||
self.assertTrue(hasattr(modelcard, "model_details"))
|
||||
self.assertTrue(hasattr(modelcard, "intended_use"))
|
||||
self.assertTrue(hasattr(modelcard, "factors"))
|
||||
self.assertTrue(hasattr(modelcard, "metrics"))
|
||||
self.assertTrue(hasattr(modelcard, "evaluation_data"))
|
||||
self.assertTrue(hasattr(modelcard, "training_data"))
|
||||
self.assertTrue(hasattr(modelcard, "quantitative_analyses"))
|
||||
self.assertTrue(hasattr(modelcard, "ethical_considerations"))
|
||||
self.assertTrue(hasattr(modelcard, "caveats_and_recommendations"))
|
||||
|
||||
def test_model_card_to_json_string(self):
|
||||
modelcard = ModelCard.from_dict(self.inputs_dict)
|
||||
obj = json.loads(modelcard.to_json_string())
|
||||
for key, value in self.inputs_dict.items():
|
||||
self.assertEqual(obj[key], value)
|
||||
|
||||
def test_model_card_to_json_file(self):
|
||||
model_card_first = ModelCard.from_dict(self.inputs_dict)
|
||||
|
||||
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)
|
||||
|
||||
self.assertEqual(model_card_second.to_dict(), model_card_first.to_dict())
|
||||
|
||||
def test_model_card_from_and_save_pretrained(self):
|
||||
model_card_first = ModelCard.from_dict(self.inputs_dict)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
model_card_first.save_pretrained(tmpdirname)
|
||||
model_card_second = ModelCard.from_pretrained(tmpdirname)
|
||||
|
||||
self.assertEqual(model_card_second.to_dict(), model_card_first.to_dict())
|
||||
|
||||
def test_model_summary_modelcard_base_metadata(self):
|
||||
metadata = TrainingSummary("Model name").create_metadata()
|
||||
self.assertTrue("library_name" in metadata)
|
||||
self.assertTrue(metadata["library_name"] == "transformers")
|
||||
122
transformers/tests/utils/test_model_debugging_utils.py
Normal file
122
transformers/tests/utils/test_model_debugging_utils.py
Normal file
@@ -0,0 +1,122 @@
|
||||
# coding=utf-8
|
||||
# Copyright 2025 The HuggingFace Inc. team.
|
||||
#
|
||||
# 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 gc
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from transformers import is_torch_available
|
||||
from transformers.model_debugging_utils import model_addition_debugger_context
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
class ToyModel(nn.Module):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.embed = nn.Embedding(10, 4)
|
||||
self.linear_1 = nn.Linear(4, 8)
|
||||
self.linear_2 = nn.Linear(8, 2)
|
||||
self.act = nn.ReLU()
|
||||
|
||||
def forward(self, input_ids: str):
|
||||
hidden_states = self.embed(input_ids).mean(dim=1)
|
||||
hidden_states = self.act(self.linear_1(hidden_states))
|
||||
return self.linear_2(hidden_states)
|
||||
|
||||
class TestModelAdditionDebugger(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.model = ToyModel()
|
||||
self.inputs = {"input_ids": torch.randint(0, 10, (1, 3))}
|
||||
|
||||
def tearDown(self):
|
||||
gc.collect()
|
||||
|
||||
def test_debugger_outputs(self):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
with model_addition_debugger_context(self.model, debug_path=str(tmpdir)):
|
||||
_ = self.model.forward(**self.inputs)
|
||||
|
||||
base = f"{self.model.__class__.__name__}_debug_tree"
|
||||
summary = Path(os.path.join(tmpdir, f"{base}_SUMMARY.json"))
|
||||
full = Path(os.path.join(tmpdir, f"{base}_FULL_TENSORS.json"))
|
||||
self.assertTrue(os.path.isfile(summary) and os.path.isfile(full))
|
||||
data = json.loads(summary.read_text())
|
||||
self.assertTrue({"module_path", "inputs", "children"} <= data.keys())
|
||||
self.assertTrue(data["children"])
|
||||
|
||||
class ToyLayer(nn.Module):
|
||||
def __init__(self, layer_index):
|
||||
super().__init__()
|
||||
self.layer_index = layer_index
|
||||
self.layer_operation = nn.Linear(4, 4)
|
||||
|
||||
def forward(self, hidden_states):
|
||||
return self.layer_operation(hidden_states)
|
||||
|
||||
class ToyModelWithLayers(nn.Module):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.input_proj = nn.Linear(4, 4)
|
||||
self.layers = nn.ModuleList([ToyLayer(layer_index) for layer_index in range(6)])
|
||||
self.output_proj = nn.Linear(4, 2)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.input_proj(x)
|
||||
for layer in self.layers:
|
||||
x = layer(x)
|
||||
return self.output_proj(x)
|
||||
|
||||
class TestModelWithLayers(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.inputs = {"input_ids": torch.randint(0, 10, (1, 3))}
|
||||
self.model_with_layers = ToyModelWithLayers()
|
||||
self.dense_input = {"x": torch.randn(1, 4)}
|
||||
|
||||
def tearDown(self):
|
||||
gc.collect()
|
||||
|
||||
def test_layer_pruning_behavior(self):
|
||||
# No pruning: expect all 6 layers
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
with model_addition_debugger_context(self.model_with_layers, debug_path=tmpdir, do_prune_layers=False):
|
||||
_ = self.model_with_layers(**self.dense_input)
|
||||
|
||||
summary_path = os.path.join(tmpdir, "ToyModelWithLayers_debug_tree_SUMMARY.json")
|
||||
with open(summary_path) as f:
|
||||
data = json.load(f)
|
||||
self.assertEqual(set(data.keys()), {"module_path", "inputs", "children"})
|
||||
for layer_index in range(6):
|
||||
self.assertEqual(
|
||||
data["children"][layer_index + 1]["module_path"],
|
||||
f"ToyModelWithLayers.layers.{int(layer_index)}",
|
||||
)
|
||||
|
||||
# Pruning: expect only 2 layers (0 and 5)
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
with model_addition_debugger_context(self.model_with_layers, debug_path=tmpdir, do_prune_layers=True):
|
||||
_ = self.model_with_layers(**self.dense_input)
|
||||
|
||||
summary_path = os.path.join(tmpdir, "ToyModelWithLayers_debug_tree_SUMMARY.json")
|
||||
with open(summary_path) as f:
|
||||
data = json.load(f)
|
||||
self.assertEqual(set(data.keys()), {"module_path", "inputs", "children"})
|
||||
self.assertEqual(data["children"][1]["module_path"], "ToyModelWithLayers.layers.0")
|
||||
self.assertEqual(data["children"][2]["module_path"], "ToyModelWithLayers.layers.5")
|
||||
198
transformers/tests/utils/test_model_output.py
Normal file
198
transformers/tests/utils/test_model_output.py
Normal file
@@ -0,0 +1,198 @@
|
||||
# Copyright 2020 The Hugging Face Team.
|
||||
#
|
||||
# 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 io
|
||||
import unittest
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
import pytest
|
||||
|
||||
from transformers import AlbertForMaskedLM
|
||||
from transformers.testing_utils import require_torch
|
||||
from transformers.utils import ModelOutput, is_torch_available
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
|
||||
@dataclass
|
||||
class ModelOutputTest(ModelOutput):
|
||||
a: float
|
||||
b: Optional[float] = None
|
||||
c: Optional[float] = None
|
||||
|
||||
|
||||
class ModelOutputTester(unittest.TestCase):
|
||||
def test_get_attributes(self):
|
||||
x = ModelOutputTest(a=30)
|
||||
self.assertEqual(x.a, 30)
|
||||
self.assertIsNone(x.b)
|
||||
self.assertIsNone(x.c)
|
||||
with self.assertRaises(AttributeError):
|
||||
_ = x.d
|
||||
|
||||
def test_index_with_ints_and_slices(self):
|
||||
x = ModelOutputTest(a=30, b=10)
|
||||
self.assertEqual(x[0], 30)
|
||||
self.assertEqual(x[1], 10)
|
||||
self.assertEqual(x[:2], (30, 10))
|
||||
self.assertEqual(x[:], (30, 10))
|
||||
|
||||
x = ModelOutputTest(a=30, c=10)
|
||||
self.assertEqual(x[0], 30)
|
||||
self.assertEqual(x[1], 10)
|
||||
self.assertEqual(x[:2], (30, 10))
|
||||
self.assertEqual(x[:], (30, 10))
|
||||
|
||||
def test_index_with_strings(self):
|
||||
x = ModelOutputTest(a=30, b=10)
|
||||
self.assertEqual(x["a"], 30)
|
||||
self.assertEqual(x["b"], 10)
|
||||
with self.assertRaises(KeyError):
|
||||
_ = x["c"]
|
||||
|
||||
x = ModelOutputTest(a=30, c=10)
|
||||
self.assertEqual(x["a"], 30)
|
||||
self.assertEqual(x["c"], 10)
|
||||
with self.assertRaises(KeyError):
|
||||
_ = x["b"]
|
||||
|
||||
def test_dict_like_properties(self):
|
||||
x = ModelOutputTest(a=30)
|
||||
self.assertEqual(list(x.keys()), ["a"])
|
||||
self.assertEqual(list(x.values()), [30])
|
||||
self.assertEqual(list(x.items()), [("a", 30)])
|
||||
self.assertEqual(list(x), ["a"])
|
||||
|
||||
x = ModelOutputTest(a=30, b=10)
|
||||
self.assertEqual(list(x.keys()), ["a", "b"])
|
||||
self.assertEqual(list(x.values()), [30, 10])
|
||||
self.assertEqual(list(x.items()), [("a", 30), ("b", 10)])
|
||||
self.assertEqual(list(x), ["a", "b"])
|
||||
|
||||
x = ModelOutputTest(a=30, c=10)
|
||||
self.assertEqual(list(x.keys()), ["a", "c"])
|
||||
self.assertEqual(list(x.values()), [30, 10])
|
||||
self.assertEqual(list(x.items()), [("a", 30), ("c", 10)])
|
||||
self.assertEqual(list(x), ["a", "c"])
|
||||
|
||||
with self.assertRaises(Exception):
|
||||
x = x.update({"d": 20})
|
||||
with self.assertRaises(Exception):
|
||||
del x["a"]
|
||||
with self.assertRaises(Exception):
|
||||
_ = x.pop("a")
|
||||
with self.assertRaises(Exception):
|
||||
_ = x.setdefault("d", 32)
|
||||
|
||||
def test_set_attributes(self):
|
||||
x = ModelOutputTest(a=30)
|
||||
x.a = 10
|
||||
self.assertEqual(x.a, 10)
|
||||
self.assertEqual(x["a"], 10)
|
||||
|
||||
def test_set_keys(self):
|
||||
x = ModelOutputTest(a=30)
|
||||
x["a"] = 10
|
||||
self.assertEqual(x.a, 10)
|
||||
self.assertEqual(x["a"], 10)
|
||||
|
||||
def test_instantiate_from_dict(self):
|
||||
x = ModelOutputTest({"a": 30, "b": 10})
|
||||
self.assertEqual(list(x.keys()), ["a", "b"])
|
||||
self.assertEqual(x.a, 30)
|
||||
self.assertEqual(x.b, 10)
|
||||
|
||||
def test_instantiate_from_iterator(self):
|
||||
x = ModelOutputTest([("a", 30), ("b", 10)])
|
||||
self.assertEqual(list(x.keys()), ["a", "b"])
|
||||
self.assertEqual(x.a, 30)
|
||||
self.assertEqual(x.b, 10)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
_ = ModelOutputTest([("a", 30), (10, 10)])
|
||||
|
||||
x = ModelOutputTest(a=(30, 30))
|
||||
self.assertEqual(list(x.keys()), ["a"])
|
||||
self.assertEqual(x.a, (30, 30))
|
||||
|
||||
@require_torch
|
||||
def test_torch_pytree(self):
|
||||
# ensure torch.utils._pytree treats ModelOutput subclasses as nodes (and not leaves)
|
||||
# this is important for DistributedDataParallel gradient synchronization with static_graph=True
|
||||
import torch.utils._pytree as pytree
|
||||
|
||||
x = ModelOutput({"a": 1.0, "c": 2.0})
|
||||
self.assertFalse(pytree._is_leaf(x))
|
||||
|
||||
x = ModelOutputTest(a=1.0, c=2.0)
|
||||
self.assertFalse(pytree._is_leaf(x))
|
||||
|
||||
expected_flat_outs = [1.0, 2.0]
|
||||
expected_tree_spec = pytree.TreeSpec(ModelOutputTest, ["a", "c"], [pytree.LeafSpec(), pytree.LeafSpec()])
|
||||
|
||||
actual_flat_outs, actual_tree_spec = pytree.tree_flatten(x)
|
||||
self.assertEqual(expected_flat_outs, actual_flat_outs)
|
||||
self.assertEqual(expected_tree_spec, actual_tree_spec)
|
||||
|
||||
unflattened_x = pytree.tree_unflatten(actual_flat_outs, actual_tree_spec)
|
||||
self.assertEqual(x, unflattened_x)
|
||||
|
||||
self.assertEqual(
|
||||
pytree.treespec_dumps(actual_tree_spec),
|
||||
'[1, {"type": "tests.utils.test_model_output.ModelOutputTest", "context": "[\\"a\\", \\"c\\"]", "children_spec": [{"type": null, "context": null, "children_spec": []}, {"type": null, "context": null, "children_spec": []}]}]',
|
||||
)
|
||||
|
||||
# TODO: @ydshieh
|
||||
@unittest.skip(reason="CPU OOM")
|
||||
@require_torch
|
||||
@pytest.mark.torch_export_test
|
||||
def test_export_serialization(self):
|
||||
model_cls = AlbertForMaskedLM
|
||||
model_config = model_cls.config_class()
|
||||
model = model_cls(model_config)
|
||||
|
||||
input_dict = {"input_ids": torch.randint(0, 30000, (1, 512), dtype=torch.int64, requires_grad=False)}
|
||||
|
||||
ep = torch.export.export(model, (), input_dict)
|
||||
|
||||
buffer = io.BytesIO()
|
||||
torch.export.save(ep, buffer)
|
||||
buffer.seek(0)
|
||||
loaded_ep = torch.export.load(buffer)
|
||||
|
||||
input_dict = {"input_ids": torch.randint(0, 30000, (1, 512), dtype=torch.int64, requires_grad=False)}
|
||||
assert torch.allclose(model(**input_dict).logits, loaded_ep(**input_dict).logits)
|
||||
|
||||
|
||||
class ModelOutputTestNoDataclass(ModelOutput):
|
||||
"""Invalid test subclass of ModelOutput where @dataclass decorator is not used"""
|
||||
|
||||
a: float
|
||||
b: Optional[float] = None
|
||||
c: Optional[float] = None
|
||||
|
||||
|
||||
class ModelOutputSubclassTester(unittest.TestCase):
|
||||
def test_direct_model_output(self):
|
||||
# Check that direct usage of ModelOutput instantiates without errors
|
||||
ModelOutput({"a": 1.1})
|
||||
|
||||
def test_subclass_no_dataclass(self):
|
||||
# Check that a subclass of ModelOutput without @dataclass is invalid
|
||||
# A valid subclass is inherently tested other unit tests above.
|
||||
with self.assertRaises(TypeError):
|
||||
ModelOutputTestNoDataclass(a=1.1, b=2.2, c=3.3)
|
||||
442
transformers/tests/utils/test_modeling_rope_utils.py
Normal file
442
transformers/tests/utils/test_modeling_rope_utils.py
Normal file
@@ -0,0 +1,442 @@
|
||||
# Copyright 2024 HuggingFace Inc.
|
||||
#
|
||||
# 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 math
|
||||
import unittest
|
||||
|
||||
from transformers import LlamaConfig
|
||||
from transformers.testing_utils import is_torch_available, require_torch, torch_device
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
from transformers import ROPE_INIT_FUNCTIONS
|
||||
from transformers.modeling_rope_utils import rope_config_validation
|
||||
|
||||
|
||||
@require_torch
|
||||
class RopeTest(unittest.TestCase):
|
||||
def test_rope_validation(self):
|
||||
config = LlamaConfig()
|
||||
all_rope_types = ROPE_INIT_FUNCTIONS.keys()
|
||||
|
||||
# The base config is always valid (default RoPE)
|
||||
rope_config_validation(config)
|
||||
|
||||
# If we explicitly set the other RoPE types, then validation should fail
|
||||
for rope_type in all_rope_types:
|
||||
if rope_type != "default":
|
||||
config.rope_scaling = {"rope_type": rope_type}
|
||||
with self.assertRaises(KeyError):
|
||||
rope_config_validation(config)
|
||||
|
||||
# Parameters are exclusive to their own RoPE type, and should raise an exception if incorrectly passed
|
||||
valid_param_mapping = {
|
||||
"factor": ["linear", "dynamic", "yarn", "longrope"],
|
||||
"attention_factor": ["yarn", "longrope"],
|
||||
"beta_fast": ["yarn"],
|
||||
"beta_slow": ["yarn"],
|
||||
"short_factor": ["longrope"],
|
||||
"long_factor": ["longrope"],
|
||||
}
|
||||
for rope_type in all_rope_types:
|
||||
if rope_type == "default":
|
||||
continue # checked above
|
||||
for param, valid_rope_types in valid_param_mapping.items():
|
||||
# Set `param` with a dummy value -- we want to test the dict key
|
||||
config.rope_scaling = {"rope_type": rope_type, param: True}
|
||||
if rope_type in valid_rope_types:
|
||||
continue
|
||||
else:
|
||||
with self.assertRaises(KeyError):
|
||||
rope_config_validation(config)
|
||||
|
||||
# Any other parameters passed to RoPE will raise a warning that a particular key is not used
|
||||
# But sometimes we can have model-specific RoPE kwargs and bypass warning with `ignore_keys`
|
||||
model_specific_kwarg = "mrope_sections" # e,g in Qwen2-VL
|
||||
|
||||
for rope_type in all_rope_types:
|
||||
if rope_type == "default":
|
||||
config.rope_scaling = {"rope_type": rope_type, model_specific_kwarg: True}
|
||||
rope_config_validation(config, ignore_keys={model_specific_kwarg})
|
||||
with self.assertLogs("transformers.modeling_rope_utils", level="WARNING") as logs:
|
||||
rope_config_validation(config)
|
||||
self.assertEqual(len(logs.output), 1)
|
||||
self.assertIn(model_specific_kwarg, logs.output[0])
|
||||
|
||||
def test_yarn_original_original_max_position_embeddings_validation(self):
|
||||
"""Tests that models with no/bad `original_max_position_embeddings` raise a warning"""
|
||||
config = LlamaConfig()
|
||||
|
||||
# good rope config: has a factor AND original_max_position_embeddings -> no warnings
|
||||
rope_config = {
|
||||
"rope_type": "yarn",
|
||||
"factor": 2.0,
|
||||
"original_max_position_embeddings": int(config.max_position_embeddings / 2.0),
|
||||
}
|
||||
config.rope_scaling = rope_config
|
||||
with self.assertRaises(AssertionError): # confirm that no warnings are thrown
|
||||
with self.assertLogs("transformers.modeling_rope_utils", level="WARNING") as logs:
|
||||
rope_config_validation(config)
|
||||
|
||||
# bad rope config, no `original_max_position_embeddings` -> warning
|
||||
rope_config = {
|
||||
"rope_type": "yarn",
|
||||
"factor": 2.0,
|
||||
}
|
||||
config.rope_scaling = rope_config
|
||||
with self.assertLogs("transformers.modeling_rope_utils", level="WARNING") as logs:
|
||||
rope_config_validation(config)
|
||||
self.assertEqual(len(logs.output), 1)
|
||||
self.assertIn("is unset", logs.output[0])
|
||||
|
||||
# bad rope config, bad implicit fator -> warning
|
||||
rope_config = {
|
||||
"rope_type": "yarn",
|
||||
"factor": 2.0,
|
||||
"original_max_position_embeddings": 1,
|
||||
}
|
||||
config.rope_scaling = rope_config
|
||||
with self.assertLogs("transformers.modeling_rope_utils", level="WARNING") as logs:
|
||||
rope_config_validation(config)
|
||||
self.assertEqual(len(logs.output), 1)
|
||||
self.assertIn("implicit factor", logs.output[0])
|
||||
|
||||
def test_default_rope_numerically(self):
|
||||
# Note: some RoPE scaling methods start off by calling the default RoPE frequencies. If this test fails, then
|
||||
# multiple RoPE strategies will fail.
|
||||
# fmt: off
|
||||
EXPECTED_INV_FREQ = torch.tensor(
|
||||
[
|
||||
1.0000e+00, 8.6596e-01, 7.4989e-01, 6.4938e-01, 5.6234e-01, 4.8697e-01,
|
||||
4.2170e-01, 3.6517e-01, 3.1623e-01, 2.7384e-01, 2.3714e-01, 2.0535e-01,
|
||||
1.7783e-01, 1.5399e-01, 1.3335e-01, 1.1548e-01, 1.0000e-01, 8.6596e-02,
|
||||
7.4989e-02, 6.4938e-02, 5.6234e-02, 4.8697e-02, 4.2170e-02, 3.6517e-02,
|
||||
3.1623e-02, 2.7384e-02, 2.3714e-02, 2.0535e-02, 1.7783e-02, 1.5399e-02,
|
||||
1.3335e-02, 1.1548e-02, 1.0000e-02, 8.6596e-03, 7.4989e-03, 6.4938e-03,
|
||||
5.6234e-03, 4.8697e-03, 4.2170e-03, 3.6517e-03, 3.1623e-03, 2.7384e-03,
|
||||
2.3714e-03, 2.0535e-03, 1.7783e-03, 1.5399e-03, 1.3335e-03, 1.1548e-03,
|
||||
1.0000e-03, 8.6596e-04, 7.4989e-04, 6.4938e-04, 5.6234e-04, 4.8697e-04,
|
||||
4.2170e-04, 3.6517e-04, 3.1623e-04, 2.7384e-04, 2.3714e-04, 2.0535e-04,
|
||||
1.7783e-04, 1.5399e-04, 1.3335e-04, 1.1548e-04
|
||||
], device=torch_device
|
||||
)
|
||||
# fmt: on
|
||||
|
||||
# input sanity checks: if these change, the output will also change
|
||||
config = LlamaConfig()
|
||||
self.assertEqual(config.rope_scaling, None)
|
||||
self.assertEqual(config.hidden_size, 4096)
|
||||
self.assertEqual(config.num_attention_heads, 32)
|
||||
self.assertEqual(config.rope_theta, 10000.0)
|
||||
self.assertFalse(hasattr(config, "partial_rotary_factor"))
|
||||
|
||||
rope_fn = ROPE_INIT_FUNCTIONS["default"]
|
||||
inv_freq, attention_scale = rope_fn(config=config, device=torch_device)
|
||||
|
||||
self.assertEqual(attention_scale, 1.0) # attention scale is always 1 for default RoPE
|
||||
torch.testing.assert_close(inv_freq, EXPECTED_INV_FREQ)
|
||||
|
||||
def test_linear_rope_numerically(self):
|
||||
# This is a linear scaling strategy, the **frequencies** are scaled linearly with respect to the default
|
||||
# frequencies (= the inverse frequencies are scaled **inversely**)
|
||||
config = LlamaConfig()
|
||||
default_rope_fn = ROPE_INIT_FUNCTIONS["default"]
|
||||
default_inv_freq, _ = default_rope_fn(config=config, device=torch_device)
|
||||
|
||||
rope_fn = ROPE_INIT_FUNCTIONS["linear"]
|
||||
for factor in (2.0, 10.0, 20.0):
|
||||
config.rope_scaling = {"rope_type": "linear", "factor": factor}
|
||||
inv_freq, attention_scale = rope_fn(config=config, device=torch_device)
|
||||
self.assertEqual(attention_scale, 1.0) # attention scale is always 1 for linear RoPE
|
||||
torch.testing.assert_close(inv_freq, default_inv_freq / factor)
|
||||
|
||||
def test_dynamic_rope_numerically(self):
|
||||
# fmt: off
|
||||
EXPECTED_INV_FREQ = torch.tensor(
|
||||
[
|
||||
1.0000e+00, 8.0931e-01, 6.5498e-01, 5.3008e-01, 4.2900e-01, 3.4720e-01,
|
||||
2.8099e-01, 2.2741e-01, 1.8404e-01, 1.4895e-01, 1.2055e-01, 9.7558e-02,
|
||||
7.8955e-02, 6.3899e-02, 5.1714e-02, 4.1853e-02, 3.3872e-02, 2.7413e-02,
|
||||
2.2185e-02, 1.7955e-02, 1.4531e-02, 1.1760e-02, 9.5176e-03, 7.7027e-03,
|
||||
6.2339e-03, 5.0451e-03, 4.0831e-03, 3.3045e-03, 2.6744e-03, 2.1644e-03,
|
||||
1.7517e-03, 1.4176e-03, 1.1473e-03, 9.2852e-04, 7.5146e-04, 6.0817e-04,
|
||||
4.9220e-04, 3.9834e-04, 3.2238e-04, 2.6091e-04, 2.1115e-04, 1.7089e-04,
|
||||
1.3830e-04, 1.1193e-04, 9.0585e-05, 7.3312e-05, 5.9332e-05, 4.8018e-05,
|
||||
3.8861e-05, 3.1451e-05, 2.5453e-05, 2.0600e-05, 1.6672e-05, 1.3492e-05,
|
||||
1.0920e-05, 8.8374e-06, 7.1522e-06, 5.7883e-06, 4.6845e-06, 3.7912e-06,
|
||||
3.0683e-06, 2.4832e-06, 2.0097e-06, 1.6265e-06
|
||||
], device=torch_device
|
||||
)
|
||||
# fmt: on
|
||||
|
||||
# input sanity checks: if these change, the output will also change
|
||||
config = LlamaConfig()
|
||||
self.assertEqual(config.rope_scaling, None)
|
||||
self.assertEqual(config.hidden_size, 4096)
|
||||
self.assertEqual(config.num_attention_heads, 32)
|
||||
self.assertEqual(config.rope_theta, 10000.0)
|
||||
self.assertFalse(hasattr(config, "partial_rotary_factor"))
|
||||
|
||||
rope_fn = ROPE_INIT_FUNCTIONS["default"]
|
||||
default_inv_freq, _ = rope_fn(config=config, device=torch_device)
|
||||
|
||||
# Check 1: this is a dynamic scaling strategy, it will not scale unless we provide `seq_len` larger than the
|
||||
# model's original training sequence length
|
||||
rope_fn = ROPE_INIT_FUNCTIONS["dynamic"]
|
||||
for factor in (2.0, 10.0, 20.0):
|
||||
config.rope_scaling = {"rope_type": "dynamic", "factor": factor}
|
||||
inv_freq, attention_scale = rope_fn(config=config, device=torch_device)
|
||||
self.assertEqual(attention_scale, 1.0) # attention scale is always 1 for dynamic RoPE
|
||||
torch.testing.assert_close(inv_freq, default_inv_freq)
|
||||
|
||||
inv_freq, _ = rope_fn(config=config, device=torch_device, seq_len=1)
|
||||
torch.testing.assert_close(inv_freq, default_inv_freq)
|
||||
|
||||
inv_freq, _ = rope_fn(config=config, device=torch_device, seq_len=torch.tensor(1, dtype=torch.int64))
|
||||
torch.testing.assert_close(inv_freq, default_inv_freq)
|
||||
|
||||
# Check 2: if we provide `seq_len` larger than the model's original training sequence length, the frequencies
|
||||
# will scale up (i.e., the inverse frequencies will scale down).
|
||||
factor = 10.0
|
||||
config.rope_scaling = {"rope_type": "dynamic", "factor": factor}
|
||||
inv_freq, _ = rope_fn(config=config, device=torch_device, seq_len=16384)
|
||||
with self.assertRaises(AssertionError): # It is NOT a linear factor
|
||||
torch.testing.assert_close(inv_freq, default_inv_freq / factor)
|
||||
torch.testing.assert_close(inv_freq, EXPECTED_INV_FREQ)
|
||||
|
||||
def test_yarn_rope_numerically(self):
|
||||
# fmt: off
|
||||
EXPECTED_INV_FREQ = torch.tensor(
|
||||
[
|
||||
1.0000e+00, 8.6596e-01, 7.4989e-01, 6.4938e-01, 5.6234e-01, 4.8697e-01,
|
||||
4.2170e-01, 3.6517e-01, 3.1623e-01, 2.7384e-01, 2.3714e-01, 2.0535e-01,
|
||||
1.7783e-01, 1.5399e-01, 1.3335e-01, 1.1548e-01, 1.0000e-01, 8.3479e-02,
|
||||
6.9590e-02, 5.7925e-02, 4.8136e-02, 3.9931e-02, 3.3061e-02, 2.7315e-02,
|
||||
2.2515e-02, 1.8512e-02, 1.5177e-02, 1.2403e-02, 1.0101e-02, 8.1924e-03,
|
||||
6.6143e-03, 5.3120e-03, 4.2400e-03, 3.3599e-03, 2.6396e-03, 2.0520e-03,
|
||||
1.5746e-03, 1.1882e-03, 8.7713e-04, 6.2810e-04, 4.3007e-04, 2.7384e-04,
|
||||
2.3714e-04, 2.0535e-04, 1.7783e-04, 1.5399e-04, 1.3335e-04, 1.1548e-04,
|
||||
1.0000e-04, 8.6596e-05, 7.4989e-05, 6.4938e-05, 5.6234e-05, 4.8697e-05,
|
||||
4.2170e-05, 3.6517e-05, 3.1623e-05, 2.7384e-05, 2.3714e-05, 2.0535e-05,
|
||||
1.7783e-05, 1.5399e-05, 1.3335e-05, 1.1548e-05
|
||||
], device=torch_device
|
||||
)
|
||||
# fmt: on
|
||||
|
||||
# input sanity checks: if these change, the output will also change
|
||||
config = LlamaConfig()
|
||||
self.assertEqual(config.rope_scaling, None)
|
||||
self.assertEqual(config.hidden_size, 4096)
|
||||
self.assertEqual(config.num_attention_heads, 32)
|
||||
self.assertEqual(config.rope_theta, 10000.0)
|
||||
self.assertFalse(hasattr(config, "partial_rotary_factor"))
|
||||
|
||||
rope_fn = ROPE_INIT_FUNCTIONS["default"]
|
||||
default_inv_freq, _ = rope_fn(config=config, device=torch_device)
|
||||
|
||||
# Check 1: according to the paper, if `attention_factor` is not specified, then it has a specific default --
|
||||
# `0.1 * math.log(factor) + 1.0`
|
||||
rope_fn = ROPE_INIT_FUNCTIONS["yarn"]
|
||||
for factor in (2.0, 10.0, 20.0):
|
||||
config.rope_scaling = {"rope_type": "yarn", "factor": factor}
|
||||
_, attention_scale = rope_fn(config=config, device=torch_device)
|
||||
self.assertEqual(attention_scale, 0.1 * math.log(factor) + 1.0)
|
||||
|
||||
config.rope_scaling = {"rope_type": "yarn", "factor": factor, "attention_factor": 0.5}
|
||||
_, attention_scale = rope_fn(config=config, device=torch_device, seq_len=1)
|
||||
self.assertEqual(attention_scale, 0.5)
|
||||
|
||||
# Check 2: based on `beta_fast` and `beta_slow`, the frequencies will be scaled between 1 and `factor`.
|
||||
# Increasing `beta_fast` will make RoPE more interpolative (apply scaling), and the other way around.
|
||||
# `beta_slow` behaves the opposite way. Remember: `beta_fast` > `beta_slow`
|
||||
# (note: adds a margin to the test for numerical stability)
|
||||
factor = 10.0
|
||||
margin = 1e-8
|
||||
config.rope_scaling = {"rope_type": "yarn", "factor": factor, "beta_fast": 32, "beta_slow": 1}
|
||||
inv_freq, _ = rope_fn(config=config, device=torch_device)
|
||||
is_bounded_by_factor = [
|
||||
((default_inv_freq[idx] / factor) - margin) <= yarn_inv_freq_value <= (default_inv_freq[idx] + margin)
|
||||
for idx, yarn_inv_freq_value in enumerate(inv_freq)
|
||||
]
|
||||
self.assertTrue(all(is_bounded_by_factor))
|
||||
|
||||
# super high beta_fast = interpolation (i.e. scaling) in all but the first inverse frequency. The last ~20
|
||||
# values (empirically checked for `beta_fast` = 1000) should be very small to linear scaling
|
||||
config.rope_scaling = {"rope_type": "yarn", "factor": factor, "beta_fast": 1000, "beta_slow": 1}
|
||||
inv_freq, _ = rope_fn(config=config, device=torch_device)
|
||||
is_interpolating = [
|
||||
yarn_inv_freq_value < (default_inv_freq[idx] + margin) for idx, yarn_inv_freq_value in enumerate(inv_freq)
|
||||
]
|
||||
self.assertFalse(is_interpolating[0])
|
||||
self.assertTrue(all(is_interpolating[1:]))
|
||||
torch.testing.assert_close(inv_freq[-20:], default_inv_freq[-20:] / factor)
|
||||
|
||||
# Check 3: numerical snapshot to avoid regressions
|
||||
config.rope_scaling = {"rope_type": "yarn", "factor": factor, "beta_fast": 32, "beta_slow": 1}
|
||||
inv_freq, _ = rope_fn(config=config, device=torch_device)
|
||||
torch.testing.assert_close(inv_freq, EXPECTED_INV_FREQ)
|
||||
|
||||
def test_longrope_rope_numerically(self):
|
||||
# input sanity checks: if these change, the output will also change
|
||||
config = LlamaConfig()
|
||||
self.assertEqual(config.rope_scaling, None)
|
||||
self.assertEqual(config.hidden_size, 4096)
|
||||
self.assertEqual(config.num_attention_heads, 32)
|
||||
self.assertEqual(config.rope_theta, 10000.0)
|
||||
self.assertFalse(hasattr(config, "partial_rotary_factor"))
|
||||
|
||||
# longrope applies scaling on EACH inv frequency, `short_factor` or `long_factor`, depending on the seq_len
|
||||
dim = config.hidden_size // config.num_attention_heads
|
||||
short_factor = [2.0] * (dim // 2) # scaling applied when seq_len <= max_position_embeddings
|
||||
long_factor = torch.ones(dim // 2).cumsum(0).tolist() # scaling applied when seq_len > max_position_embeddings
|
||||
|
||||
rope_fn = ROPE_INIT_FUNCTIONS["default"]
|
||||
default_inv_freq, _ = rope_fn(config=config, device=torch_device)
|
||||
|
||||
# Check 1: according to the paper, if `attention_factor` is not specified, then it has a specific default --
|
||||
# `math.sqrt(1 + math.log(factor) / math.log(max_position_embeddings))`
|
||||
rope_fn = ROPE_INIT_FUNCTIONS["longrope"]
|
||||
max_position_embeddings = config.max_position_embeddings
|
||||
for factor in (2.0, 10.0, 20.0):
|
||||
config.rope_scaling = {
|
||||
"rope_type": "longrope",
|
||||
"factor": factor,
|
||||
"short_factor": short_factor,
|
||||
"long_factor": long_factor,
|
||||
}
|
||||
_, attention_scale = rope_fn(config=config, device=torch_device)
|
||||
self.assertEqual(attention_scale, math.sqrt(1 + math.log(factor) / math.log(max_position_embeddings)))
|
||||
|
||||
config.rope_scaling = {
|
||||
"rope_type": "longrope",
|
||||
"factor": factor,
|
||||
"short_factor": short_factor,
|
||||
"long_factor": long_factor,
|
||||
"attention_factor": 0.5,
|
||||
}
|
||||
_, attention_scale = rope_fn(config=config, device=torch_device, seq_len=1)
|
||||
self.assertEqual(attention_scale, 0.5)
|
||||
|
||||
config.rope_scaling = {
|
||||
"rope_type": "longrope",
|
||||
"factor": factor,
|
||||
"short_factor": short_factor,
|
||||
"long_factor": long_factor,
|
||||
}
|
||||
self.assertEqual(config.rope_scaling.get("attention_factor"), None)
|
||||
# Verify that "TypeError: '<' not supported between instances of 'NoneType' and 'int'" is not raised.
|
||||
rope_config_validation(config)
|
||||
|
||||
# Check 2: seq_len == 0 -> short factor is applied to the default frequencies
|
||||
config.rope_scaling = {
|
||||
"rope_type": "longrope",
|
||||
"factor": 1.0,
|
||||
"short_factor": short_factor,
|
||||
"long_factor": long_factor,
|
||||
}
|
||||
inv_freq, _ = rope_fn(config=config, device=torch_device, seq_len=0)
|
||||
torch.testing.assert_close(inv_freq, default_inv_freq / torch.tensor(short_factor).to(torch_device))
|
||||
|
||||
# Check 3: seq_len > max_position_embeddings -> long factor is applied to the default frequencies
|
||||
inv_freq, _ = rope_fn(config=config, device=torch_device, seq_len=config.max_position_embeddings + 1)
|
||||
torch.testing.assert_close(inv_freq, default_inv_freq / torch.tensor(long_factor).to(torch_device))
|
||||
|
||||
def test_llama3_rope_numerically(self):
|
||||
# fmt: off
|
||||
EXPECTED_INV_FREQ = torch.tensor(
|
||||
[
|
||||
1.0000e+00, 8.6596e-01, 7.4989e-01, 6.4938e-01, 5.6234e-01, 4.8697e-01,
|
||||
4.2170e-01, 3.6517e-01, 3.1623e-01, 2.7384e-01, 2.3714e-01, 2.0535e-01,
|
||||
1.7783e-01, 1.5399e-01, 1.3335e-01, 1.1548e-01, 1.0000e-01, 8.6596e-02,
|
||||
7.4989e-02, 6.4938e-02, 5.6234e-02, 4.8697e-02, 4.2170e-02, 3.6517e-02,
|
||||
3.1623e-02, 2.7384e-02, 2.3714e-02, 2.0535e-02, 1.7783e-02, 1.5399e-02,
|
||||
1.3335e-02, 1.0730e-02, 7.7785e-03, 5.6009e-03, 3.9991e-03, 2.8248e-03,
|
||||
1.9675e-03, 1.3449e-03, 8.9549e-04, 5.7363e-04, 3.4539e-04, 2.7384e-04,
|
||||
2.3714e-04, 2.0535e-04, 1.7783e-04, 1.5399e-04, 1.3335e-04, 1.1548e-04,
|
||||
1.0000e-04, 8.6596e-05, 7.4989e-05, 6.4938e-05, 5.6234e-05, 4.8697e-05,
|
||||
4.2170e-05, 3.6517e-05, 3.1623e-05, 2.7384e-05, 2.3714e-05, 2.0535e-05,
|
||||
1.7783e-05, 1.5399e-05, 1.3335e-05, 1.1548e-05
|
||||
], device=torch_device
|
||||
)
|
||||
# fmt: on
|
||||
|
||||
# input sanity checks: if these change, the output will also change
|
||||
config = LlamaConfig()
|
||||
self.assertEqual(config.rope_scaling, None)
|
||||
self.assertEqual(config.hidden_size, 4096)
|
||||
self.assertEqual(config.num_attention_heads, 32)
|
||||
self.assertEqual(config.rope_theta, 10000.0)
|
||||
self.assertFalse(hasattr(config, "partial_rotary_factor"))
|
||||
|
||||
rope_fn = ROPE_INIT_FUNCTIONS["default"]
|
||||
default_inv_freq, _ = rope_fn(config=config, device=torch_device)
|
||||
|
||||
# Check 1: `attention_factor` is always 1
|
||||
rope_fn = ROPE_INIT_FUNCTIONS["llama3"]
|
||||
for factor in (2.0, 10.0, 20.0):
|
||||
config.rope_scaling = {
|
||||
"rope_type": "llama3",
|
||||
"factor": factor,
|
||||
"original_max_position_embeddings": 2048,
|
||||
"low_freq_factor": 1,
|
||||
"high_freq_factor": 4,
|
||||
}
|
||||
_, attention_scale = rope_fn(config=config, device=torch_device)
|
||||
self.assertEqual(attention_scale, 1.0)
|
||||
|
||||
# Check 2: based on `low_freq_factor` and `high_freq_factor`, the frequencies will be scaled between 1 and
|
||||
# `factor` (similar to yarn). Low frequencies get scaled by `factor`, high frequencies see no change, medium
|
||||
# frequencies are scaled by a value in between. Changing `low_freq_factor` and `high_freq_factor` changes what
|
||||
# is considered low, medium, and high frequencies.
|
||||
factor = 10.0
|
||||
config.rope_scaling = {
|
||||
"rope_type": "llama3",
|
||||
"factor": factor,
|
||||
"original_max_position_embeddings": 2048,
|
||||
"low_freq_factor": 1,
|
||||
"high_freq_factor": 4,
|
||||
}
|
||||
inv_freq, _ = rope_fn(config=config, device=torch_device)
|
||||
is_bounded_by_factor = [
|
||||
(default_inv_freq[idx] / factor) <= llama3_inv_freq_value <= default_inv_freq[idx]
|
||||
for idx, llama3_inv_freq_value in enumerate(inv_freq)
|
||||
]
|
||||
self.assertTrue(all(is_bounded_by_factor))
|
||||
|
||||
# if we change `high_freq_factor` to a very high value, none is considered high-frequency -> ALL values will be
|
||||
# scaled
|
||||
config.rope_scaling = config.rope_scaling = {
|
||||
"rope_type": "llama3",
|
||||
"factor": factor,
|
||||
"original_max_position_embeddings": 2048,
|
||||
"low_freq_factor": 1,
|
||||
"high_freq_factor": 1000,
|
||||
}
|
||||
inv_freq, _ = rope_fn(config=config, device=torch_device)
|
||||
is_scaled = [yarn_inv_freq_value < default_inv_freq[idx] for idx, yarn_inv_freq_value in enumerate(inv_freq)]
|
||||
self.assertTrue(all(is_scaled))
|
||||
|
||||
# Check 3: numerical snapshot to avoid regressions
|
||||
config.rope_scaling = {
|
||||
"rope_type": "llama3",
|
||||
"factor": factor,
|
||||
"original_max_position_embeddings": 2048,
|
||||
"low_freq_factor": 1,
|
||||
"high_freq_factor": 4,
|
||||
}
|
||||
inv_freq, _ = rope_fn(config=config, device=torch_device)
|
||||
torch.testing.assert_close(inv_freq, EXPECTED_INV_FREQ)
|
||||
3160
transformers/tests/utils/test_modeling_utils.py
Normal file
3160
transformers/tests/utils/test_modeling_utils.py
Normal file
File diff suppressed because it is too large
Load Diff
220
transformers/tests/utils/test_offline.py
Normal file
220
transformers/tests/utils/test_offline.py
Normal file
@@ -0,0 +1,220 @@
|
||||
# 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 subprocess
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
|
||||
from transformers.testing_utils import TestCasePlus, require_torch
|
||||
|
||||
|
||||
class OfflineTests(TestCasePlus):
|
||||
@require_torch
|
||||
@unittest.skip("This test is failing on main") # TODO matt/ydshieh, this test needs to be fixed
|
||||
def test_offline_mode(self):
|
||||
# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
|
||||
# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
|
||||
# while running an external program
|
||||
|
||||
# python one-liner segments
|
||||
|
||||
# this must be loaded before socket.socket is monkey-patched
|
||||
load = """
|
||||
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
|
||||
"""
|
||||
|
||||
run = """
|
||||
mname = "hf-internal-testing/tiny-random-bert"
|
||||
BertConfig.from_pretrained(mname)
|
||||
BertModel.from_pretrained(mname)
|
||||
BertTokenizer.from_pretrained(mname)
|
||||
pipe = pipeline(task="fill-mask", model=mname)
|
||||
print("success")
|
||||
"""
|
||||
|
||||
mock = """
|
||||
import socket
|
||||
def offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn't access internet")
|
||||
socket.socket = offline_socket
|
||||
"""
|
||||
|
||||
# Force fetching the files so that we can use the cache
|
||||
mname = "hf-internal-testing/tiny-random-bert"
|
||||
BertConfig.from_pretrained(mname)
|
||||
BertModel.from_pretrained(mname)
|
||||
BertTokenizer.from_pretrained(mname)
|
||||
pipeline(task="fill-mask", model=mname)
|
||||
|
||||
# baseline - just load from_pretrained with normal network
|
||||
# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
|
||||
stdout, _ = self._execute_with_env(load, run, mock, TRANSFORMERS_OFFLINE="1")
|
||||
self.assertIn("success", stdout)
|
||||
|
||||
@require_torch
|
||||
def test_offline_mode_no_internet(self):
|
||||
# python one-liner segments
|
||||
# this must be loaded before socket.socket is monkey-patched
|
||||
load = """
|
||||
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
|
||||
"""
|
||||
|
||||
run = """
|
||||
mname = "hf-internal-testing/tiny-random-bert"
|
||||
BertConfig.from_pretrained(mname)
|
||||
BertModel.from_pretrained(mname)
|
||||
BertTokenizer.from_pretrained(mname)
|
||||
pipe = pipeline(task="fill-mask", model=mname)
|
||||
print("success")
|
||||
"""
|
||||
|
||||
mock = """
|
||||
import socket
|
||||
def offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")
|
||||
socket.socket = offline_socket
|
||||
"""
|
||||
|
||||
# Force fetching the files so that we can use the cache
|
||||
mname = "hf-internal-testing/tiny-random-bert"
|
||||
BertConfig.from_pretrained(mname)
|
||||
BertModel.from_pretrained(mname)
|
||||
BertTokenizer.from_pretrained(mname)
|
||||
pipeline(task="fill-mask", model=mname)
|
||||
|
||||
# baseline - just load from_pretrained with normal network
|
||||
# should succeed
|
||||
stdout, _ = self._execute_with_env(load, run, mock)
|
||||
self.assertIn("success", stdout)
|
||||
|
||||
@require_torch
|
||||
def test_offline_mode_sharded_checkpoint(self):
|
||||
# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
|
||||
# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
|
||||
# while running an external program
|
||||
|
||||
# python one-liner segments
|
||||
|
||||
# this must be loaded before socket.socket is monkey-patched
|
||||
load = """
|
||||
from transformers import BertConfig, BertModel, BertTokenizer
|
||||
"""
|
||||
|
||||
run = """
|
||||
mname = "hf-internal-testing/tiny-random-bert-sharded"
|
||||
BertConfig.from_pretrained(mname)
|
||||
BertModel.from_pretrained(mname)
|
||||
print("success")
|
||||
"""
|
||||
|
||||
mock = """
|
||||
import socket
|
||||
def offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")
|
||||
socket.socket = offline_socket
|
||||
"""
|
||||
|
||||
# baseline - just load from_pretrained with normal network
|
||||
# should succeed
|
||||
stdout, _ = self._execute_with_env(load, run)
|
||||
self.assertIn("success", stdout)
|
||||
|
||||
# next emulate no network
|
||||
# Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
|
||||
# self._execute_with_env(load, mock, run, should_fail=True, TRANSFORMERS_OFFLINE="0")
|
||||
|
||||
# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
|
||||
stdout, _ = self._execute_with_env(load, mock, run, TRANSFORMERS_OFFLINE="1")
|
||||
self.assertIn("success", stdout)
|
||||
|
||||
@require_torch
|
||||
def test_offline_mode_pipeline_exception(self):
|
||||
load = """
|
||||
from transformers import pipeline
|
||||
"""
|
||||
run = """
|
||||
mname = "hf-internal-testing/tiny-random-bert"
|
||||
pipe = pipeline(model=mname)
|
||||
"""
|
||||
|
||||
mock = """
|
||||
import socket
|
||||
def offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")
|
||||
socket.socket = offline_socket
|
||||
"""
|
||||
|
||||
_, stderr = self._execute_with_env(load, mock, run, should_fail=True, TRANSFORMERS_OFFLINE="1")
|
||||
self.assertIn(
|
||||
"You cannot infer task automatically within `pipeline` when using offline mode",
|
||||
stderr.replace("\n", ""),
|
||||
)
|
||||
|
||||
@require_torch
|
||||
def test_offline_model_dynamic_model(self):
|
||||
load = """
|
||||
from transformers import AutoModel
|
||||
"""
|
||||
run = """
|
||||
mname = "hf-internal-testing/test_dynamic_model"
|
||||
AutoModel.from_pretrained(mname, trust_remote_code=True)
|
||||
print("success")
|
||||
"""
|
||||
|
||||
# baseline - just load from_pretrained with normal network
|
||||
# should succeed
|
||||
stdout, _ = self._execute_with_env(load, run)
|
||||
self.assertIn("success", stdout)
|
||||
|
||||
# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
|
||||
stdout, _ = self._execute_with_env(load, run, TRANSFORMERS_OFFLINE="1")
|
||||
self.assertIn("success", stdout)
|
||||
|
||||
def test_is_offline_mode(self):
|
||||
"""
|
||||
Test `_is_offline_mode` helper (should respect both HF_HUB_OFFLINE and legacy TRANSFORMERS_OFFLINE env vars)
|
||||
"""
|
||||
load = "from transformers.utils import is_offline_mode"
|
||||
run = "print(is_offline_mode())"
|
||||
|
||||
stdout, _ = self._execute_with_env(load, run)
|
||||
self.assertIn("False", stdout)
|
||||
|
||||
stdout, _ = self._execute_with_env(load, run, TRANSFORMERS_OFFLINE="1")
|
||||
self.assertIn("True", stdout)
|
||||
|
||||
stdout, _ = self._execute_with_env(load, run, HF_HUB_OFFLINE="1")
|
||||
self.assertIn("True", stdout)
|
||||
|
||||
def _execute_with_env(self, *commands: tuple[str, ...], should_fail: bool = False, **env) -> tuple[str, str]:
|
||||
"""Execute Python code with a given environment and return the stdout/stderr as strings.
|
||||
|
||||
If `should_fail=True`, the command is expected to fail. Otherwise, it should succeed.
|
||||
Environment variables can be passed as keyword arguments.
|
||||
"""
|
||||
# Build command
|
||||
cmd = [sys.executable, "-c", "\n".join(commands)]
|
||||
|
||||
# Configure env
|
||||
new_env = self.get_env()
|
||||
new_env.update(env)
|
||||
|
||||
# Run command
|
||||
result = subprocess.run(cmd, env=new_env, check=False, capture_output=True)
|
||||
|
||||
# Check execution
|
||||
if should_fail:
|
||||
self.assertNotEqual(result.returncode, 0, result.stderr)
|
||||
else:
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
|
||||
# Return output
|
||||
return result.stdout.decode(), result.stderr.decode()
|
||||
124
transformers/tests/utils/test_skip_decorators.py
Normal file
124
transformers/tests/utils/test_skip_decorators.py
Normal file
@@ -0,0 +1,124 @@
|
||||
# Copyright 2019-present, the HuggingFace Inc. team.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
#
|
||||
#
|
||||
# this test validates that we can stack skip decorators in groups and whether
|
||||
# they work correctly with other decorators
|
||||
#
|
||||
# since the decorators have already built their decision params (like checking
|
||||
# env[], we can't mock the env and test each of the combinations), so ideally
|
||||
# the following 4 should be run. But since we have different CI jobs running
|
||||
# different configs, all combinations should get covered
|
||||
#
|
||||
# RUN_SLOW=1 pytest -rA tests/test_skip_decorators.py
|
||||
# RUN_SLOW=1 CUDA_VISIBLE_DEVICES="" pytest -rA tests/test_skip_decorators.py
|
||||
# RUN_SLOW=0 pytest -rA tests/test_skip_decorators.py
|
||||
# RUN_SLOW=0 CUDA_VISIBLE_DEVICES="" pytest -rA tests/test_skip_decorators.py
|
||||
|
||||
import os
|
||||
import unittest
|
||||
|
||||
import pytest
|
||||
from parameterized import parameterized
|
||||
|
||||
from transformers.testing_utils import require_torch, require_torch_accelerator, slow, torch_device
|
||||
|
||||
|
||||
# skipping in unittest tests
|
||||
|
||||
params = [(1,)]
|
||||
|
||||
|
||||
# test that we can stack our skip decorators with 3rd party decorators
|
||||
def check_slow():
|
||||
run_slow = bool(os.getenv("RUN_SLOW", "0"))
|
||||
if run_slow:
|
||||
assert True
|
||||
else:
|
||||
assert False, "should have been skipped"
|
||||
|
||||
|
||||
# test that we can stack our skip decorators
|
||||
def check_slow_torch_cuda():
|
||||
run_slow = bool(os.getenv("RUN_SLOW", "0"))
|
||||
if run_slow and torch_device == "cuda":
|
||||
assert True
|
||||
else:
|
||||
assert False, "should have been skipped"
|
||||
|
||||
|
||||
def check_slow_torch_accelerator():
|
||||
run_slow = bool(os.getenv("RUN_SLOW", "0"))
|
||||
assert run_slow and torch_device in ["cuda", "xpu"], "should have been skipped"
|
||||
|
||||
|
||||
@require_torch
|
||||
class SkipTester(unittest.TestCase):
|
||||
@slow
|
||||
@require_torch_accelerator
|
||||
def test_2_skips_slow_first(self):
|
||||
check_slow_torch_accelerator()
|
||||
|
||||
@require_torch_accelerator
|
||||
@slow
|
||||
def test_2_skips_slow_last(self):
|
||||
check_slow_torch_accelerator()
|
||||
|
||||
# The combination of any skip decorator, followed by parameterized fails to skip the tests
|
||||
# 1. @slow manages to correctly skip `test_param_slow_first`
|
||||
# 2. but then `parameterized` creates new tests, with a unique name for each parameter groups.
|
||||
# It has no idea that they are to be skipped and so they all run, ignoring @slow
|
||||
# Therefore skip decorators must come after `parameterized`
|
||||
#
|
||||
# @slow
|
||||
# @parameterized.expand(params)
|
||||
# def test_param_slow_first(self, param=None):
|
||||
# check_slow()
|
||||
|
||||
# This works as expected:
|
||||
# 1. `parameterized` creates new tests with unique names
|
||||
# 2. each of them gets an opportunity to be skipped
|
||||
@parameterized.expand(params)
|
||||
@slow
|
||||
def test_param_slow_last(self, param=None):
|
||||
check_slow()
|
||||
|
||||
|
||||
# skipping in non-unittest tests
|
||||
# no problem at all here
|
||||
|
||||
|
||||
@slow
|
||||
@require_torch_accelerator
|
||||
def test_pytest_2_skips_slow_first():
|
||||
check_slow_torch_accelerator()
|
||||
|
||||
|
||||
@require_torch_accelerator
|
||||
@slow
|
||||
def test_pytest_2_skips_slow_last():
|
||||
check_slow_torch_accelerator()
|
||||
|
||||
|
||||
@slow
|
||||
@pytest.mark.parametrize("param", [1])
|
||||
def test_pytest_param_slow_first(param):
|
||||
check_slow()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("param", [1])
|
||||
@slow
|
||||
def test_pytest_param_slow_last(param):
|
||||
check_slow()
|
||||
305
transformers/tests/utils/test_tokenization_utils.py
Normal file
305
transformers/tests/utils/test_tokenization_utils.py
Normal file
@@ -0,0 +1,305 @@
|
||||
# Copyright 2019 HuggingFace Inc.
|
||||
#
|
||||
# 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 os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
from huggingface_hub.file_download import http_get
|
||||
|
||||
from transformers import (
|
||||
AlbertTokenizer,
|
||||
AutoTokenizer,
|
||||
BertTokenizer,
|
||||
BertTokenizerFast,
|
||||
GPT2TokenizerFast,
|
||||
is_tokenizers_available,
|
||||
)
|
||||
from transformers.testing_utils import TOKEN, TemporaryHubRepo, is_staging_test, require_tokenizers
|
||||
from transformers.tokenization_utils import ExtensionsTrie, Trie
|
||||
|
||||
|
||||
sys.path.append(str(Path(__file__).parent.parent.parent / "utils"))
|
||||
|
||||
from test_module.custom_tokenization import CustomTokenizer # noqa E402
|
||||
|
||||
|
||||
if is_tokenizers_available():
|
||||
from test_module.custom_tokenization_fast import CustomTokenizerFast
|
||||
|
||||
|
||||
class TokenizerUtilTester(unittest.TestCase):
|
||||
def test_cached_files_are_used_when_internet_is_down(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 = httpx.HTTPStatusError(
|
||||
"failed", request=mock.Mock(), response=mock.Mock()
|
||||
)
|
||||
response_mock.json.return_value = {}
|
||||
|
||||
# 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 tokenizer.
|
||||
with mock.patch("httpx.Client.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 = httpx.HTTPStatusError(
|
||||
"failed", request=mock.Mock(), response=mock.Mock()
|
||||
)
|
||||
response_mock.json.return_value = {}
|
||||
|
||||
# Download this model to make sure it's in the cache.
|
||||
_ = GPT2TokenizerFast.from_pretrained("openai-community/gpt2")
|
||||
|
||||
# Under the mock environment we get a 500 error when trying to reach the tokenizer.
|
||||
with mock.patch("httpx.Client.request", return_value=response_mock) as mock_head:
|
||||
_ = GPT2TokenizerFast.from_pretrained("openai-community/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.NamedTemporaryFile(delete=False).name
|
||||
with open(tmp_file, "wb") as f:
|
||||
http_get("https://huggingface.co/albert/albert-base-v1/resolve/main/spiece.model", f)
|
||||
|
||||
_ = AlbertTokenizer.from_pretrained(tmp_file)
|
||||
finally:
|
||||
os.remove(tmp_file)
|
||||
|
||||
# Supporting this legacy load introduced a weird bug where the tokenizer would load local files if they are in
|
||||
# the current folder and have the right name.
|
||||
if os.path.isfile("tokenizer.json"):
|
||||
# We skip the test if the user has a `tokenizer.json` in this folder to avoid deleting it.
|
||||
self.skipTest(reason="Skipping test as there is a `tokenizer.json` file in the current folder.")
|
||||
try:
|
||||
with open("tokenizer.json", "wb") as f:
|
||||
http_get("https://huggingface.co/hf-internal-testing/tiny-random-bert/blob/main/tokenizer.json", f)
|
||||
tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2")
|
||||
# The tiny random BERT has a vocab size of 1024, tiny openai-community/gpt2 as a vocab size of 1000
|
||||
self.assertEqual(tokenizer.vocab_size, 1000)
|
||||
# Tokenizer should depend on the remote checkpoint, not the local tokenizer.json file.
|
||||
|
||||
finally:
|
||||
os.remove("tokenizer.json")
|
||||
|
||||
|
||||
@is_staging_test
|
||||
class TokenizerPushToHubTester(unittest.TestCase):
|
||||
vocab_tokens = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"]
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls._token = TOKEN
|
||||
|
||||
def test_push_to_hub(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
vocab_file = os.path.join(tmp_dir, "vocab.txt")
|
||||
with open(vocab_file, "w", encoding="utf-8") as vocab_writer:
|
||||
vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]))
|
||||
tokenizer = BertTokenizer(vocab_file)
|
||||
|
||||
tokenizer.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
new_tokenizer = BertTokenizer.from_pretrained(tmp_repo.repo_id)
|
||||
self.assertDictEqual(new_tokenizer.vocab, tokenizer.vocab)
|
||||
|
||||
def test_push_to_hub_via_save_pretrained(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
vocab_file = os.path.join(tmp_dir, "vocab.txt")
|
||||
with open(vocab_file, "w", encoding="utf-8") as vocab_writer:
|
||||
vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]))
|
||||
tokenizer = BertTokenizer(vocab_file)
|
||||
|
||||
# Push to hub via save_pretrained
|
||||
tokenizer.save_pretrained(tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token)
|
||||
|
||||
new_tokenizer = BertTokenizer.from_pretrained(tmp_repo.repo_id)
|
||||
self.assertDictEqual(new_tokenizer.vocab, tokenizer.vocab)
|
||||
|
||||
def test_push_to_hub_in_organization(self):
|
||||
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
vocab_file = os.path.join(tmp_dir, "vocab.txt")
|
||||
with open(vocab_file, "w", encoding="utf-8") as vocab_writer:
|
||||
vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]))
|
||||
tokenizer = BertTokenizer(vocab_file)
|
||||
|
||||
tokenizer.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
new_tokenizer = BertTokenizer.from_pretrained(tmp_repo.repo_id)
|
||||
self.assertDictEqual(new_tokenizer.vocab, tokenizer.vocab)
|
||||
|
||||
def test_push_to_hub_in_organization_via_save_pretrained(self):
|
||||
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
vocab_file = os.path.join(tmp_dir, "vocab.txt")
|
||||
with open(vocab_file, "w", encoding="utf-8") as vocab_writer:
|
||||
vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]))
|
||||
tokenizer = BertTokenizer(vocab_file)
|
||||
|
||||
# Push to hub via save_pretrained
|
||||
tokenizer.save_pretrained(tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token)
|
||||
|
||||
new_tokenizer = BertTokenizer.from_pretrained(tmp_repo.repo_id)
|
||||
self.assertDictEqual(new_tokenizer.vocab, tokenizer.vocab)
|
||||
|
||||
@require_tokenizers
|
||||
def test_push_to_hub_dynamic_tokenizer(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
CustomTokenizer.register_for_auto_class()
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
vocab_file = os.path.join(tmp_dir, "vocab.txt")
|
||||
with open(vocab_file, "w", encoding="utf-8") as vocab_writer:
|
||||
vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]))
|
||||
tokenizer = CustomTokenizer(vocab_file)
|
||||
|
||||
# No fast custom tokenizer
|
||||
tokenizer.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
tokenizer = AutoTokenizer.from_pretrained(tmp_repo.repo_id, trust_remote_code=True)
|
||||
# Can't make an isinstance check because the new_model.config is from the CustomTokenizer class of a dynamic module
|
||||
self.assertEqual(tokenizer.__class__.__name__, "CustomTokenizer")
|
||||
|
||||
@require_tokenizers
|
||||
def test_push_to_hub_dynamic_tokenizer_with_both_slow_and_fast_classes(self):
|
||||
with TemporaryHubRepo(token=self._token) as tmp_repo:
|
||||
CustomTokenizer.register_for_auto_class()
|
||||
|
||||
# Fast and slow custom tokenizer
|
||||
CustomTokenizerFast.register_for_auto_class()
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
vocab_file = os.path.join(tmp_dir, "vocab.txt")
|
||||
with open(vocab_file, "w", encoding="utf-8") as vocab_writer:
|
||||
vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]))
|
||||
|
||||
bert_tokenizer = BertTokenizerFast.from_pretrained(tmp_dir)
|
||||
bert_tokenizer.save_pretrained(tmp_dir)
|
||||
tokenizer = CustomTokenizerFast.from_pretrained(tmp_dir)
|
||||
|
||||
tokenizer.push_to_hub(tmp_repo.repo_id, token=self._token)
|
||||
|
||||
tokenizer = AutoTokenizer.from_pretrained(tmp_repo.repo_id, trust_remote_code=True)
|
||||
# Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module
|
||||
self.assertEqual(tokenizer.__class__.__name__, "CustomTokenizerFast")
|
||||
tokenizer = AutoTokenizer.from_pretrained(tmp_repo.repo_id, use_fast=False, trust_remote_code=True)
|
||||
# Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module
|
||||
self.assertEqual(tokenizer.__class__.__name__, "CustomTokenizer")
|
||||
|
||||
|
||||
class TrieTest(unittest.TestCase):
|
||||
def test_trie(self):
|
||||
trie = Trie()
|
||||
trie.add("Hello 友達")
|
||||
self.assertEqual(trie.data, {"H": {"e": {"l": {"l": {"o": {" ": {"友": {"達": {"": 1}}}}}}}}})
|
||||
trie.add("Hello")
|
||||
self.assertEqual(trie.data, {"H": {"e": {"l": {"l": {"o": {"": 1, " ": {"友": {"達": {"": 1}}}}}}}}})
|
||||
|
||||
def test_trie_split(self):
|
||||
trie = Trie()
|
||||
self.assertEqual(trie.split("[CLS] This is a extra_id_100"), ["[CLS] This is a extra_id_100"])
|
||||
trie.add("[CLS]")
|
||||
trie.add("extra_id_1")
|
||||
trie.add("extra_id_100")
|
||||
self.assertEqual(trie.split("[CLS] This is a extra_id_100"), ["[CLS]", " This is a ", "extra_id_100"])
|
||||
|
||||
def test_trie_single(self):
|
||||
trie = Trie()
|
||||
trie.add("A")
|
||||
self.assertEqual(trie.split("ABC"), ["A", "BC"])
|
||||
self.assertEqual(trie.split("BCA"), ["BC", "A"])
|
||||
|
||||
def test_trie_final(self):
|
||||
trie = Trie()
|
||||
trie.add("TOKEN]")
|
||||
trie.add("[SPECIAL_TOKEN]")
|
||||
self.assertEqual(trie.split("This is something [SPECIAL_TOKEN]"), ["This is something ", "[SPECIAL_TOKEN]"])
|
||||
|
||||
def test_trie_subtokens(self):
|
||||
trie = Trie()
|
||||
trie.add("A")
|
||||
trie.add("P")
|
||||
trie.add("[SPECIAL_TOKEN]")
|
||||
self.assertEqual(trie.split("This is something [SPECIAL_TOKEN]"), ["This is something ", "[SPECIAL_TOKEN]"])
|
||||
|
||||
def test_trie_suffix_tokens(self):
|
||||
trie = Trie()
|
||||
trie.add("AB")
|
||||
trie.add("B")
|
||||
trie.add("C")
|
||||
self.assertEqual(trie.split("ABC"), ["AB", "C"])
|
||||
|
||||
def test_trie_skip(self):
|
||||
trie = Trie()
|
||||
trie.add("ABC")
|
||||
trie.add("B")
|
||||
trie.add("CD")
|
||||
self.assertEqual(trie.split("ABCD"), ["ABC", "D"])
|
||||
|
||||
def test_cut_text_hardening(self):
|
||||
# Even if the offsets are wrong, we necessarily output correct string
|
||||
# parts.
|
||||
trie = Trie()
|
||||
parts = trie.cut_text("ABC", [0, 0, 2, 1, 2, 3])
|
||||
self.assertEqual(parts, ["AB", "C"])
|
||||
|
||||
|
||||
class ExtensionsTrieTest(unittest.TestCase):
|
||||
def test_extensions(self):
|
||||
# Test searching by prefix
|
||||
trie = ExtensionsTrie()
|
||||
trie.add("foo")
|
||||
trie.add("food")
|
||||
trie.add("foodie")
|
||||
trie.add("helium")
|
||||
self.assertEqual(trie.extensions("foo"), ["foo", "food", "foodie"])
|
||||
self.assertEqual(trie.extensions("helium"), ["helium"])
|
||||
|
||||
def test_empty_prefix(self):
|
||||
trie = ExtensionsTrie()
|
||||
# Test searching with an empty prefix returns all values
|
||||
trie.add("hello")
|
||||
trie.add("bye")
|
||||
self.assertEqual(trie.extensions(""), ["hello", "bye"])
|
||||
|
||||
def test_no_extension_match(self):
|
||||
trie = ExtensionsTrie()
|
||||
# Test searching for a prefix that doesn't match any key
|
||||
values = trie.extensions("unknown")
|
||||
|
||||
self.assertEqual(len(values), 0)
|
||||
|
||||
def test_update_value(self):
|
||||
trie = ExtensionsTrie()
|
||||
# Test updating the value of an existing key
|
||||
trie.add("hi")
|
||||
trie.add("hi")
|
||||
self.assertEqual(trie.extensions("hi"), ["hi"])
|
||||
97
transformers/tests/utils/test_versions_utils.py
Normal file
97
transformers/tests/utils/test_versions_utils.py
Normal file
@@ -0,0 +1,97 @@
|
||||
# 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 importlib.metadata
|
||||
import sys
|
||||
|
||||
from transformers.testing_utils import TestCasePlus
|
||||
from transformers.utils.versions import require_version, require_version_core
|
||||
|
||||
|
||||
numpy_ver = importlib.metadata.version("numpy")
|
||||
python_ver = ".".join([str(x) for x in sys.version_info[:3]])
|
||||
|
||||
|
||||
class DependencyVersionCheckTest(TestCasePlus):
|
||||
def test_core(self):
|
||||
# lt + different version strings
|
||||
require_version_core("numpy<1000.4.5")
|
||||
require_version_core("numpy<1000.4")
|
||||
require_version_core("numpy<1000")
|
||||
|
||||
# le
|
||||
require_version_core("numpy<=1000.4.5")
|
||||
require_version_core(f"numpy<={numpy_ver}")
|
||||
|
||||
# eq
|
||||
require_version_core(f"numpy=={numpy_ver}")
|
||||
|
||||
# ne
|
||||
require_version_core("numpy!=1000.4.5")
|
||||
|
||||
# ge
|
||||
require_version_core("numpy>=1.0")
|
||||
require_version_core("numpy>=1.0.0")
|
||||
require_version_core(f"numpy>={numpy_ver}")
|
||||
|
||||
# gt
|
||||
require_version_core("numpy>1.0.0")
|
||||
|
||||
# mix
|
||||
require_version_core("numpy>1.0.0,<1000")
|
||||
|
||||
# requirement w/o version
|
||||
require_version_core("numpy")
|
||||
|
||||
# unmet requirements due to version conflict
|
||||
for req in ["numpy==1.0.0", "numpy>=1000.0.0", f"numpy<{numpy_ver}"]:
|
||||
try:
|
||||
require_version_core(req)
|
||||
except ImportError as e:
|
||||
self.assertIn(f"{req} is required", str(e))
|
||||
self.assertIn("but found", str(e))
|
||||
|
||||
# unmet requirements due to missing module
|
||||
for req in ["numpipypie>1", "numpipypie2"]:
|
||||
try:
|
||||
require_version_core(req)
|
||||
except importlib.metadata.PackageNotFoundError as e:
|
||||
self.assertIn(f"The '{req}' distribution was not found and is required by this application", str(e))
|
||||
self.assertIn("Try: `pip install transformers -U`", str(e))
|
||||
|
||||
# bogus requirements formats:
|
||||
# 1. whole thing
|
||||
for req in ["numpy??1.0.0", "numpy1.0.0"]:
|
||||
try:
|
||||
require_version_core(req)
|
||||
except ValueError as e:
|
||||
self.assertIn("requirement needs to be in the pip package format", str(e))
|
||||
# 2. only operators
|
||||
for req in ["numpy=1.0.0", "numpy == 1.00", "numpy<>1.0.0", "numpy><1.00", "numpy>>1.0.0"]:
|
||||
try:
|
||||
require_version_core(req)
|
||||
except ValueError as e:
|
||||
self.assertIn("need one of ", str(e))
|
||||
|
||||
def test_python(self):
|
||||
# matching requirement
|
||||
require_version("python>=3.9.0")
|
||||
|
||||
# not matching requirements
|
||||
for req in ["python>9.9.9", "python<3.0.0"]:
|
||||
try:
|
||||
require_version_core(req)
|
||||
except ImportError as e:
|
||||
self.assertIn(f"{req} is required", str(e))
|
||||
self.assertIn(f"but found python=={python_ver}", str(e))
|
||||
346
transformers/tests/utils/test_video_utils.py
Normal file
346
transformers/tests/utils/test_video_utils.py
Normal file
@@ -0,0 +1,346 @@
|
||||
# coding=utf-8
|
||||
# Copyright 2025 HuggingFace Inc.
|
||||
#
|
||||
# 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 unittest
|
||||
|
||||
import numpy as np
|
||||
from huggingface_hub import hf_hub_download
|
||||
|
||||
from transformers import is_torch_available, is_vision_available
|
||||
from transformers.image_processing_utils import get_size_dict
|
||||
from transformers.image_utils import SizeDict
|
||||
from transformers.processing_utils import VideosKwargs
|
||||
from transformers.testing_utils import (
|
||||
require_av,
|
||||
require_cv2,
|
||||
require_decord,
|
||||
require_torch,
|
||||
require_torchcodec,
|
||||
require_torchvision,
|
||||
require_vision,
|
||||
)
|
||||
from transformers.video_utils import group_videos_by_shape, make_batched_videos, reorder_videos
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
if is_vision_available():
|
||||
import PIL
|
||||
|
||||
from transformers import BaseVideoProcessor
|
||||
from transformers.video_utils import VideoMetadata, load_video
|
||||
|
||||
|
||||
def get_random_video(height, width, num_frames=8, return_torch=False):
|
||||
random_frame = np.random.randint(0, 256, (height, width, 3), dtype=np.uint8)
|
||||
video = np.array([random_frame] * num_frames)
|
||||
if return_torch:
|
||||
# move channel first
|
||||
return torch.from_numpy(video).permute(0, 3, 1, 2)
|
||||
return video
|
||||
|
||||
|
||||
@require_vision
|
||||
@require_torchvision
|
||||
class BaseVideoProcessorTester(unittest.TestCase):
|
||||
"""
|
||||
Tests that the `transforms` can be applied to a 4-dim array directly, i.e. to a whole video.
|
||||
"""
|
||||
|
||||
def test_make_batched_videos_pil(self):
|
||||
# Test a single image is converted to a list of 1 video with 1 frame
|
||||
video = get_random_video(16, 32)
|
||||
pil_image = PIL.Image.fromarray(video[0])
|
||||
videos_list = make_batched_videos(pil_image)
|
||||
self.assertIsInstance(videos_list, list)
|
||||
self.assertIsInstance(videos_list[0], np.ndarray)
|
||||
self.assertEqual(videos_list[0].shape, (1, 16, 32, 3))
|
||||
self.assertTrue(np.array_equal(videos_list[0][0], np.array(pil_image)))
|
||||
|
||||
# Test a list of videos is converted to a list of 1 video
|
||||
video = get_random_video(16, 32)
|
||||
pil_video = [PIL.Image.fromarray(frame) for frame in video]
|
||||
videos_list = make_batched_videos(pil_video)
|
||||
self.assertIsInstance(videos_list, list)
|
||||
self.assertIsInstance(videos_list[0], np.ndarray)
|
||||
self.assertEqual(videos_list[0].shape, (8, 16, 32, 3))
|
||||
self.assertTrue(np.array_equal(videos_list[0], video))
|
||||
|
||||
# Test a nested list of videos is not modified
|
||||
video = get_random_video(16, 32)
|
||||
pil_video = [PIL.Image.fromarray(frame) for frame in video]
|
||||
videos = [pil_video, pil_video]
|
||||
videos_list = make_batched_videos(videos)
|
||||
self.assertIsInstance(videos_list, list)
|
||||
self.assertIsInstance(videos_list[0], np.ndarray)
|
||||
self.assertEqual(videos_list[0].shape, (8, 16, 32, 3))
|
||||
self.assertTrue(np.array_equal(videos_list[0], video))
|
||||
|
||||
def test_make_batched_videos_numpy(self):
|
||||
# Test a single image is converted to a list of 1 video with 1 frame
|
||||
video = get_random_video(16, 32)[0]
|
||||
videos_list = make_batched_videos(video)
|
||||
self.assertIsInstance(videos_list, list)
|
||||
self.assertIsInstance(videos_list[0], np.ndarray)
|
||||
self.assertEqual(videos_list[0].shape, (1, 16, 32, 3))
|
||||
self.assertTrue(np.array_equal(videos_list[0][0], video))
|
||||
|
||||
# Test a 4d array of videos is converted to a a list of 1 video
|
||||
video = get_random_video(16, 32)
|
||||
videos_list = make_batched_videos(video)
|
||||
self.assertIsInstance(videos_list, list)
|
||||
self.assertIsInstance(videos_list[0], np.ndarray)
|
||||
self.assertEqual(videos_list[0].shape, (8, 16, 32, 3))
|
||||
self.assertTrue(np.array_equal(videos_list[0], video))
|
||||
|
||||
# Test a list of videos is converted to a list of videos
|
||||
video = get_random_video(16, 32)
|
||||
videos = [video, video]
|
||||
videos_list = make_batched_videos(videos)
|
||||
self.assertIsInstance(videos_list, list)
|
||||
self.assertIsInstance(videos_list[0], np.ndarray)
|
||||
self.assertEqual(videos_list[0].shape, (8, 16, 32, 3))
|
||||
self.assertTrue(np.array_equal(videos_list[0], video))
|
||||
|
||||
@require_torch
|
||||
def test_make_batched_videos_torch(self):
|
||||
# Test a single image is converted to a list of 1 video with 1 frame
|
||||
video = get_random_video(16, 32)[0]
|
||||
torch_video = torch.from_numpy(video)
|
||||
videos_list = make_batched_videos(torch_video)
|
||||
self.assertIsInstance(videos_list, list)
|
||||
self.assertIsInstance(videos_list[0], np.ndarray)
|
||||
self.assertEqual(videos_list[0].shape, (1, 16, 32, 3))
|
||||
self.assertTrue(np.array_equal(videos_list[0][0], video))
|
||||
|
||||
# Test a 4d array of videos is converted to a a list of 1 video
|
||||
video = get_random_video(16, 32)
|
||||
torch_video = torch.from_numpy(video)
|
||||
videos_list = make_batched_videos(torch_video)
|
||||
self.assertIsInstance(videos_list, list)
|
||||
self.assertIsInstance(videos_list[0], torch.Tensor)
|
||||
self.assertEqual(videos_list[0].shape, (8, 16, 32, 3))
|
||||
self.assertTrue(np.array_equal(videos_list[0], video))
|
||||
|
||||
# Test a list of videos is converted to a list of videos
|
||||
video = get_random_video(16, 32)
|
||||
torch_video = torch.from_numpy(video)
|
||||
videos = [torch_video, torch_video]
|
||||
videos_list = make_batched_videos(videos)
|
||||
self.assertIsInstance(videos_list, list)
|
||||
self.assertIsInstance(videos_list[0], torch.Tensor)
|
||||
self.assertEqual(videos_list[0].shape, (8, 16, 32, 3))
|
||||
self.assertTrue(np.array_equal(videos_list[0], video))
|
||||
|
||||
def test_resize(self):
|
||||
video_processor = BaseVideoProcessor(model_init_kwargs=VideosKwargs)
|
||||
video = get_random_video(16, 32, return_torch=True)
|
||||
|
||||
# Size can be an int or a tuple of ints.
|
||||
size_dict = SizeDict(**get_size_dict((8, 8), param_name="size"))
|
||||
resized_video = video_processor.resize(video, size=size_dict)
|
||||
self.assertIsInstance(resized_video, torch.Tensor)
|
||||
self.assertEqual(resized_video.shape, (8, 3, 8, 8))
|
||||
|
||||
def test_normalize(self):
|
||||
video_processor = BaseVideoProcessor(model_init_kwargs=VideosKwargs)
|
||||
array = torch.randn(4, 3, 16, 32)
|
||||
mean = [0.1, 0.5, 0.9]
|
||||
std = [0.2, 0.4, 0.6]
|
||||
|
||||
# mean and std can be passed as lists or NumPy arrays.
|
||||
expected = (array - torch.tensor(mean)[:, None, None]) / torch.tensor(std)[:, None, None]
|
||||
normalized_array = video_processor.normalize(array, mean, std)
|
||||
torch.testing.assert_close(normalized_array, expected)
|
||||
|
||||
def test_center_crop(self):
|
||||
video_processor = BaseVideoProcessor(model_init_kwargs=VideosKwargs)
|
||||
video = get_random_video(16, 32, return_torch=True)
|
||||
|
||||
# Test various crop sizes: bigger on all dimensions, on one of the dimensions only and on both dimensions.
|
||||
crop_sizes = [8, (8, 64), 20, (32, 64)]
|
||||
for size in crop_sizes:
|
||||
size_dict = SizeDict(**get_size_dict(size, default_to_square=True, param_name="crop_size"))
|
||||
cropped_video = video_processor.center_crop(video, size_dict)
|
||||
self.assertIsInstance(cropped_video, torch.Tensor)
|
||||
|
||||
expected_size = (size, size) if isinstance(size, int) else size
|
||||
self.assertEqual(cropped_video.shape, (8, 3, *expected_size))
|
||||
|
||||
def test_convert_to_rgb(self):
|
||||
video_processor = BaseVideoProcessor(model_init_kwargs=VideosKwargs)
|
||||
video = get_random_video(20, 20, return_torch=True)
|
||||
|
||||
rgb_video = video_processor.convert_to_rgb(video[:, :1])
|
||||
self.assertEqual(rgb_video.shape, (8, 3, 20, 20))
|
||||
|
||||
rgb_video = video_processor.convert_to_rgb(torch.cat([video, video[:, :1]], dim=1))
|
||||
self.assertEqual(rgb_video.shape, (8, 3, 20, 20))
|
||||
|
||||
def test_group_and_reorder_videos(self):
|
||||
"""Tests that videos can be grouped by frame size and number of frames"""
|
||||
video_1 = get_random_video(20, 20, num_frames=3, return_torch=True)
|
||||
video_2 = get_random_video(20, 20, num_frames=5, return_torch=True)
|
||||
|
||||
# Group two videos of same size but different number of frames
|
||||
grouped_videos, grouped_videos_index = group_videos_by_shape([video_1, video_2])
|
||||
self.assertEqual(len(grouped_videos), 2)
|
||||
|
||||
regrouped_videos = reorder_videos(grouped_videos, grouped_videos_index)
|
||||
self.assertTrue(len(regrouped_videos), 2)
|
||||
self.assertEqual(video_1.shape, regrouped_videos[0].shape)
|
||||
|
||||
# Group two videos of different size but same number of frames
|
||||
video_3 = get_random_video(15, 20, num_frames=3, return_torch=True)
|
||||
grouped_videos, grouped_videos_index = group_videos_by_shape([video_1, video_3])
|
||||
self.assertEqual(len(grouped_videos), 2)
|
||||
|
||||
regrouped_videos = reorder_videos(grouped_videos, grouped_videos_index)
|
||||
self.assertTrue(len(regrouped_videos), 2)
|
||||
self.assertEqual(video_1.shape, regrouped_videos[0].shape)
|
||||
|
||||
# Group all three videos where some have same size or same frame count
|
||||
# But since none have frames and sizes identical, we'll have 3 groups
|
||||
grouped_videos, grouped_videos_index = group_videos_by_shape([video_1, video_2, video_3])
|
||||
self.assertEqual(len(grouped_videos), 3)
|
||||
|
||||
regrouped_videos = reorder_videos(grouped_videos, grouped_videos_index)
|
||||
self.assertTrue(len(regrouped_videos), 3)
|
||||
self.assertEqual(video_1.shape, regrouped_videos[0].shape)
|
||||
|
||||
# Group if we had some videos with identical shapes
|
||||
grouped_videos, grouped_videos_index = group_videos_by_shape([video_1, video_1, video_3])
|
||||
self.assertEqual(len(grouped_videos), 2)
|
||||
|
||||
regrouped_videos = reorder_videos(grouped_videos, grouped_videos_index)
|
||||
self.assertTrue(len(regrouped_videos), 2)
|
||||
self.assertEqual(video_1.shape, regrouped_videos[0].shape)
|
||||
|
||||
# Group if we had all videos with identical shapes
|
||||
grouped_videos, grouped_videos_index = group_videos_by_shape([video_1, video_1, video_1])
|
||||
self.assertEqual(len(grouped_videos), 1)
|
||||
|
||||
regrouped_videos = reorder_videos(grouped_videos, grouped_videos_index)
|
||||
self.assertTrue(len(regrouped_videos), 1)
|
||||
self.assertEqual(video_1.shape, regrouped_videos[0].shape)
|
||||
|
||||
|
||||
@require_vision
|
||||
@require_av
|
||||
class LoadVideoTester(unittest.TestCase):
|
||||
def test_load_video_url(self):
|
||||
video, _ = load_video(
|
||||
"https://huggingface.co/datasets/raushan-testing-hf/videos-test/resolve/main/sample_demo_1.mp4",
|
||||
)
|
||||
self.assertEqual(video.shape, (243, 360, 640, 3)) # 243 frames is the whole video, no sampling applied
|
||||
|
||||
def test_load_video_local(self):
|
||||
video_file_path = hf_hub_download(
|
||||
repo_id="raushan-testing-hf/videos-test", filename="sample_demo_1.mp4", repo_type="dataset"
|
||||
)
|
||||
video, _ = load_video(video_file_path)
|
||||
self.assertEqual(video.shape, (243, 360, 640, 3)) # 243 frames is the whole video, no sampling applied
|
||||
|
||||
# FIXME: @raushan, yt-dlp downloading works for for some reason it cannot redirect to out buffer?
|
||||
# @requires_yt_dlp
|
||||
# def test_load_video_youtube(self):
|
||||
# video = load_video("https://www.youtube.com/watch?v=QC8iQqtG0hg")
|
||||
# self.assertEqual(video.shape, (243, 360, 640, 3)) # 243 frames is the whole video, no sampling applied
|
||||
|
||||
@require_decord
|
||||
@require_torchvision
|
||||
@require_torchcodec
|
||||
@require_cv2
|
||||
def test_load_video_backend_url(self):
|
||||
video, _ = load_video(
|
||||
"https://huggingface.co/datasets/raushan-testing-hf/videos-test/resolve/main/sample_demo_1.mp4",
|
||||
backend="decord",
|
||||
)
|
||||
self.assertEqual(video.shape, (243, 360, 640, 3))
|
||||
|
||||
video, _ = load_video(
|
||||
"https://huggingface.co/datasets/raushan-testing-hf/videos-test/resolve/main/sample_demo_1.mp4",
|
||||
backend="torchcodec",
|
||||
)
|
||||
self.assertEqual(video.shape, (243, 360, 640, 3))
|
||||
|
||||
# Can't use certain backends with url
|
||||
with self.assertRaises(ValueError):
|
||||
video, _ = load_video(
|
||||
"https://huggingface.co/datasets/raushan-testing-hf/videos-test/resolve/main/sample_demo_1.mp4",
|
||||
backend="opencv",
|
||||
)
|
||||
with self.assertRaises(ValueError):
|
||||
video, _ = load_video(
|
||||
"https://huggingface.co/datasets/raushan-testing-hf/videos-test/resolve/main/sample_demo_1.mp4",
|
||||
backend="torchvision",
|
||||
)
|
||||
|
||||
@require_decord
|
||||
@require_torchvision
|
||||
@require_torchcodec
|
||||
@require_cv2
|
||||
def test_load_video_backend_local(self):
|
||||
video_file_path = hf_hub_download(
|
||||
repo_id="raushan-testing-hf/videos-test", filename="sample_demo_1.mp4", repo_type="dataset"
|
||||
)
|
||||
video, metadata = load_video(video_file_path, backend="decord")
|
||||
self.assertEqual(video.shape, (243, 360, 640, 3))
|
||||
self.assertIsInstance(metadata, VideoMetadata)
|
||||
|
||||
video, metadata = load_video(video_file_path, backend="opencv")
|
||||
self.assertEqual(video.shape, (243, 360, 640, 3))
|
||||
self.assertIsInstance(metadata, VideoMetadata)
|
||||
|
||||
video, metadata = load_video(video_file_path, backend="torchvision")
|
||||
self.assertEqual(video.shape, (243, 360, 640, 3))
|
||||
self.assertIsInstance(metadata, VideoMetadata)
|
||||
|
||||
video, metadata = load_video(video_file_path, backend="torchcodec")
|
||||
self.assertEqual(video.shape, (243, 360, 640, 3))
|
||||
self.assertIsInstance(metadata, VideoMetadata)
|
||||
|
||||
def test_load_video_num_frames(self):
|
||||
video, _ = load_video(
|
||||
"https://huggingface.co/datasets/raushan-testing-hf/videos-test/resolve/main/sample_demo_1.mp4",
|
||||
num_frames=16,
|
||||
)
|
||||
self.assertEqual(video.shape, (16, 360, 640, 3))
|
||||
|
||||
video, _ = load_video(
|
||||
"https://huggingface.co/datasets/raushan-testing-hf/videos-test/resolve/main/sample_demo_1.mp4",
|
||||
num_frames=22,
|
||||
)
|
||||
self.assertEqual(video.shape, (22, 360, 640, 3))
|
||||
|
||||
def test_load_video_fps(self):
|
||||
video, _ = load_video(
|
||||
"https://huggingface.co/datasets/raushan-testing-hf/videos-test/resolve/main/sample_demo_1.mp4", fps=1
|
||||
)
|
||||
self.assertEqual(video.shape, (9, 360, 640, 3))
|
||||
|
||||
video, _ = load_video(
|
||||
"https://huggingface.co/datasets/raushan-testing-hf/videos-test/resolve/main/sample_demo_1.mp4", fps=2
|
||||
)
|
||||
self.assertEqual(video.shape, (19, 360, 640, 3))
|
||||
|
||||
# `num_frames` is mutually exclusive with `video_fps`
|
||||
with self.assertRaises(ValueError):
|
||||
video, _ = load_video(
|
||||
"https://huggingface.co/datasets/raushan-testing-hf/videos-test/resolve/main/sample_demo_1.mp4",
|
||||
fps=1,
|
||||
num_frames=10,
|
||||
)
|
||||
7246
transformers/tests/utils/tiny_model_summary.json
Normal file
7246
transformers/tests/utils/tiny_model_summary.json
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user