transformers

Форк
0
/
conftest.py 
132 строки · 4.8 Кб
1
# Copyright 2020 The HuggingFace Team. All rights reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

15
# tests directory-specific settings - this file is run automatically
16
# by pytest before any tests are run
17

18
import doctest
19
import sys
20
import warnings
21
from os.path import abspath, dirname, join
22

23
import _pytest
24
import pytest
25

26
from transformers.testing_utils import HfDoctestModule, HfDocTestParser
27

28

29
NOT_DEVICE_TESTS = {
30
    "test_tokenization",
31
    "test_processor",
32
    "test_processing",
33
    "test_feature_extraction",
34
    "test_image_processing",
35
    "test_image_processor",
36
    "test_retrieval",
37
    "test_config",
38
    "test_from_pretrained_no_checkpoint",
39
    "test_keep_in_fp32_modules",
40
    "test_gradient_checkpointing_backward_compatibility",
41
    "test_gradient_checkpointing_enable_disable",
42
    "test_save_load_fast_init_from_base",
43
    "test_fast_init_context_manager",
44
    "test_fast_init_tied_embeddings",
45
    "test_save_load_fast_init_to_base",
46
    "test_torch_save_load",
47
    "test_initialization",
48
    "test_forward_signature",
49
    "test_model_common_attributes",
50
    "test_model_main_input_name",
51
    "test_correct_missing_keys",
52
    "test_tie_model_weights",
53
    "test_can_use_safetensors",
54
    "test_load_save_without_tied_weights",
55
    "test_tied_weights_keys",
56
    "test_model_weights_reload_no_missing_tied_weights",
57
    "test_pt_tf_model_equivalence",
58
    "test_mismatched_shapes_have_properly_initialized_weights",
59
    "test_matched_shapes_have_loaded_weights_when_some_mismatched_shapes_exist",
60
    "test_model_is_small",
61
    "test_tf_from_pt_safetensors",
62
    "test_flax_from_pt_safetensors",
63
    "ModelTest::test_pipeline_",  # None of the pipeline tests from PipelineTesterMixin (of which XxxModelTest inherits from) are running on device
64
    "ModelTester::test_pipeline_",
65
}
66

67
# allow having multiple repository checkouts and not needing to remember to rerun
68
# `pip install -e '.[dev]'` when switching between checkouts and running tests.
69
git_repo_path = abspath(join(dirname(__file__), "src"))
70
sys.path.insert(1, git_repo_path)
71

72
# silence FutureWarning warnings in tests since often we can't act on them until
73
# they become normal warnings - i.e. the tests still need to test the current functionality
74
warnings.simplefilter(action="ignore", category=FutureWarning)
75

76

77
def pytest_configure(config):
78
    config.addinivalue_line(
79
        "markers", "is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested"
80
    )
81
    config.addinivalue_line(
82
        "markers", "is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested"
83
    )
84
    config.addinivalue_line("markers", "is_pipeline_test: mark test to run only when pipelines are tested")
85
    config.addinivalue_line("markers", "is_staging_test: mark test to run only in the staging environment")
86
    config.addinivalue_line("markers", "accelerate_tests: mark test that require accelerate")
87
    config.addinivalue_line("markers", "tool_tests: mark the tool tests that are run on their specific schedule")
88
    config.addinivalue_line("markers", "not_device_test: mark the tests always running on cpu")
89

90

91
def pytest_collection_modifyitems(items):
92
    for item in items:
93
        if any(test_name in item.nodeid for test_name in NOT_DEVICE_TESTS):
94
            item.add_marker(pytest.mark.not_device_test)
95

96

97
def pytest_addoption(parser):
98
    from transformers.testing_utils import pytest_addoption_shared
99

100
    pytest_addoption_shared(parser)
101

102

103
def pytest_terminal_summary(terminalreporter):
104
    from transformers.testing_utils import pytest_terminal_summary_main
105

106
    make_reports = terminalreporter.config.getoption("--make-reports")
107
    if make_reports:
108
        pytest_terminal_summary_main(terminalreporter, id=make_reports)
109

110

111
def pytest_sessionfinish(session, exitstatus):
112
    # If no tests are collected, pytest exists with code 5, which makes the CI fail.
113
    if exitstatus == 5:
114
        session.exitstatus = 0
115

116

117
# Doctest custom flag to ignore output.
118
IGNORE_RESULT = doctest.register_optionflag("IGNORE_RESULT")
119

120
OutputChecker = doctest.OutputChecker
121

122

123
class CustomOutputChecker(OutputChecker):
124
    def check_output(self, want, got, optionflags):
125
        if IGNORE_RESULT & optionflags:
126
            return True
127
        return OutputChecker.check_output(self, want, got, optionflags)
128

129

130
doctest.OutputChecker = CustomOutputChecker
131
_pytest.doctest.DoctestModule = HfDoctestModule
132
doctest.DocTestParser = HfDocTestParser
133

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.