Skip to content

Commit 9acba80

Browse files
BenjaminBossanefraimdahl
authored andcommitted
CI: Handle errors with MacOS and transformers (huggingface#2561)
CI Handle error with MacOS and transformers A change in transformers introduced an error in the MacOS CI, which is handled in this PR. Context For context on why we use torch 2.2 for MacOS, check huggingface#2431. Unfortunately, as of today, the available GH workers for MacOS still haven't improved. Description The error was introduced by huggingface/transformers#37785, which results in torch.load failing when using torch < 2.6. The proposed solution is to plug into pytest, intercept the test report, check for the specific error, and mark the test as skipped instead. Alternative solutions The proposed solution is obviously an ugly hack. However, these are errors we cannot fix directly, as they're caused by a dependency and are caused by the old torch version we're forced to use (thus fixing them in transformers is probably not an option). Instead of altering the test report, the individual tests that fail could get an explicit skip marker when MacOS is detected. However, since the amount of affected tests are several hundreds, this is very impractical and leads to a lot of noise in the tests. Alternatively, we could move forward with the proposal in huggingface#2431 and remove MacOS completely from the CI. I do, however, still have the faint hope that GH will provide arm64 workers with more RAM in the future, allowing us to switch.
1 parent e8c7d79 commit 9acba80

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import platform
16+
import re
17+
1518
import pytest
1619

1720

@@ -31,3 +34,36 @@ def pytest_collection_modifyitems(config, items):
3134
for item in items:
3235
if "regression" in item.keywords:
3336
item.add_marker(skip_regression)
37+
38+
39+
# TODO: remove this once support for PyTorch 2.2 (the latest one still supported by GitHub MacOS x86_64 runners) is
40+
# dropped, or if MacOS is removed from the test matrix, see https://github.com/huggingface/peft/issues/2431.
41+
# Note: the function name is fixed by the pytest plugin system, don't change it
42+
@pytest.hookimpl(hookwrapper=True)
43+
def pytest_runtest_makereport(item, call):
44+
"""
45+
Plug into the pytest test report generation to skip a specific MacOS failure caused by transformers.
46+
47+
The error was introduced by https://github.com/huggingface/transformers/pull/37785, which results in torch.load
48+
failing when using torch < 2.6.
49+
50+
Since the MacOS x86 runners need to use an older torch version, those steps are necessary to get the CI green.
51+
"""
52+
outcome = yield
53+
rep = outcome.get_result()
54+
# ref:
55+
# https://github.com/huggingface/transformers/blob/858ce6879a4aa7fa76a7c4e2ac20388e087ace26/src/transformers/utils/import_utils.py#L1418
56+
error_msg = re.compile(r"Due to a serious vulnerability issue in `torch.load`")
57+
58+
# notes:
59+
# - pytest uses hard-coded strings, we cannot import and use constants
60+
# https://docs.pytest.org/en/stable/reference/reference.html#pytest.TestReport
61+
# - errors can happen during call (running the test) but also setup (e.g. in fixtures)
62+
if rep.failed and (rep.when in ("setup", "call")) and (platform.system() == "Darwin"):
63+
exc_msg = str(call.excinfo.value)
64+
if error_msg.search(exc_msg):
65+
# turn this failure into an xfail:
66+
rep.outcome = "skipped"
67+
# for this attribute, see:
68+
# https://github.com/pytest-dev/pytest/blob/bd6877e5874b50ee57d0f63b342a67298ee9a1c3/src/_pytest/reports.py#L266C5-L266C13
69+
rep.wasxfail = "Error known to occur on MacOS with older torch versions, won't be fixed"

0 commit comments

Comments
 (0)