transformers
/
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
18import doctest
19import sys
20import warnings
21from os.path import abspath, dirname, join
22
23import _pytest
24import pytest
25
26from transformers.testing_utils import HfDoctestModule, HfDocTestParser
27
28
29NOT_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.
69git_repo_path = abspath(join(dirname(__file__), "src"))
70sys.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
74warnings.simplefilter(action="ignore", category=FutureWarning)
75
76
77def pytest_configure(config):
78config.addinivalue_line(
79"markers", "is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested"
80)
81config.addinivalue_line(
82"markers", "is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested"
83)
84config.addinivalue_line("markers", "is_pipeline_test: mark test to run only when pipelines are tested")
85config.addinivalue_line("markers", "is_staging_test: mark test to run only in the staging environment")
86config.addinivalue_line("markers", "accelerate_tests: mark test that require accelerate")
87config.addinivalue_line("markers", "tool_tests: mark the tool tests that are run on their specific schedule")
88config.addinivalue_line("markers", "not_device_test: mark the tests always running on cpu")
89
90
91def pytest_collection_modifyitems(items):
92for item in items:
93if any(test_name in item.nodeid for test_name in NOT_DEVICE_TESTS):
94item.add_marker(pytest.mark.not_device_test)
95
96
97def pytest_addoption(parser):
98from transformers.testing_utils import pytest_addoption_shared
99
100pytest_addoption_shared(parser)
101
102
103def pytest_terminal_summary(terminalreporter):
104from transformers.testing_utils import pytest_terminal_summary_main
105
106make_reports = terminalreporter.config.getoption("--make-reports")
107if make_reports:
108pytest_terminal_summary_main(terminalreporter, id=make_reports)
109
110
111def pytest_sessionfinish(session, exitstatus):
112# If no tests are collected, pytest exists with code 5, which makes the CI fail.
113if exitstatus == 5:
114session.exitstatus = 0
115
116
117# Doctest custom flag to ignore output.
118IGNORE_RESULT = doctest.register_optionflag("IGNORE_RESULT")
119
120OutputChecker = doctest.OutputChecker
121
122
123class CustomOutputChecker(OutputChecker):
124def check_output(self, want, got, optionflags):
125if IGNORE_RESULT & optionflags:
126return True
127return OutputChecker.check_output(self, want, got, optionflags)
128
129
130doctest.OutputChecker = CustomOutputChecker
131_pytest.doctest.DoctestModule = HfDoctestModule
132doctest.DocTestParser = HfDocTestParser
133