Skip to content

Commit 67a959c

Browse files
CI Handle 2 errors with MacOS and transformers
Changes in transformers introduced 2 errors in the MacOS CI, which are 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 1st error was introduced by huggingface/transformers#37785, which results in torch.load failing when using torch < 2.6. The 2nd error was introduced by huggingface/transformers#37919, which results in a DTensor import being triggered when calling save_pretrained, which fails with MacOS and torch 2.2 (possibly also later MacOS versions, I haven't checked). The proposed solution is to plug into pytest, intercept the test report, check for these specific errors, and turn them into skips. 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 b3130c9 commit 67a959c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/conftest.py

Lines changed: 41 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,41 @@ 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+
Failure 0 was introduced by https://github.com/huggingface/transformers/pull/37785, which results in torch.load
48+
failing when using torch < 2.6.
49+
50+
Failure 1 was introduced by https://github.com/huggingface/transformers/pull/37919, which results in a DTensor
51+
import being triggered in the save_pretrained, which fails with MacOS and torch 2.2 (possibly also later MacOS
52+
versions).
53+
54+
Since the MacOS x86 runners need to use an older torch version, those steps are necessary to get the CI green.
55+
"""
56+
outcome = yield
57+
rep = outcome.get_result()
58+
# ref:
59+
# https://github.com/huggingface/transformers/blob/858ce6879a4aa7fa76a7c4e2ac20388e087ace26/src/transformers/utils/import_utils.py#L1418
60+
error_msg_0 = re.compile(r"Due to a serious vulnerability issue in `torch.load`")
61+
error_msg_1 = re.compile(r"cannot import name 'DTensor' from 'torch.distributed.tensor'")
62+
63+
# notes:
64+
# - pytest uses hard-coded strings, we cannot import and use constants
65+
# https://docs.pytest.org/en/stable/reference/reference.html#pytest.TestReport
66+
# - errors can happen during call (running the test) but also setup (e.g. in fixtures)
67+
if rep.failed and (rep.when in ("setup", "call")) and (platform.system() == "Darwin"):
68+
exc_msg = str(call.excinfo.value)
69+
if error_msg_0.search(exc_msg) or error_msg_1.search(exc_msg):
70+
# turn this failure into an xfail:
71+
rep.outcome = "skipped"
72+
# for this attribute, see:
73+
# https://github.com/pytest-dev/pytest/blob/bd6877e5874b50ee57d0f63b342a67298ee9a1c3/src/_pytest/reports.py#L266C5-L266C13
74+
rep.wasxfail = "Errors known to occur on MacOS with older torch versions, won't be fixed"

0 commit comments

Comments
 (0)