Skip some export tests on torch 2.7 (#38677)

* skip

* fix

* better check

* Update import_utils.py

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
Co-authored-by: Cyril Vallez <cyril.vallez@gmail.com>
This commit is contained in:
Yih-Dar
2025-06-12 12:47:15 +02:00
committed by GitHub
parent 27459025b8
commit 89c46b648d
9 changed files with 30 additions and 7 deletions

View File

@@ -393,6 +393,13 @@ def get_torch_version():
return _torch_version return _torch_version
def get_torch_major_and_minor_version() -> str:
if _torch_version == "N/A":
return "N/A"
parsed_version = version.parse(_torch_version)
return str(parsed_version.major) + "." + str(parsed_version.minor)
def is_torch_sdpa_available(): def is_torch_sdpa_available():
if not is_torch_available(): if not is_torch_available():
return False return False

View File

@@ -19,6 +19,7 @@ from transformers import DepthAnythingConfig, Dinov2Config
from transformers.file_utils import is_torch_available, is_vision_available from transformers.file_utils import is_torch_available, is_vision_available
from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_4 from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_4
from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils.import_utils import get_torch_major_and_minor_version
from ...test_configuration_common import ConfigTester from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
@@ -146,6 +147,7 @@ class DepthAnythingModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.Tes
test_resize_embeddings = False test_resize_embeddings = False
test_head_masking = False test_head_masking = False
test_torch_exportable = True test_torch_exportable = True
test_torch_exportable_strictly = not get_torch_major_and_minor_version() == "2.7"
def setUp(self): def setUp(self):
self.model_tester = DepthAnythingModelTester(self) self.model_tester = DepthAnythingModelTester(self)
@@ -285,8 +287,11 @@ class DepthAnythingModelIntegrationTest(unittest.TestCase):
torch.testing.assert_close(predicted_depth[0, :3, :3], expected_slice, rtol=1e-4, atol=1e-4) torch.testing.assert_close(predicted_depth[0, :3, :3], expected_slice, rtol=1e-4, atol=1e-4)
def test_export(self): def test_export(self):
for strict in [True, False]: for strict in [False, True]:
with self.subTest(strict=strict): with self.subTest(strict=strict):
if strict and get_torch_major_and_minor_version() == "2.7":
self.skipTest(reason="`strict=True` is currently failing with torch 2.7.")
if not is_torch_greater_or_equal_than_2_4: if not is_torch_greater_or_equal_than_2_4:
self.skipTest(reason="This test requires torch >= 2.4 to run.") self.skipTest(reason="This test requires torch >= 2.4 to run.")
model = ( model = (

View File

@@ -18,6 +18,7 @@ import unittest
from transformers import Dinov2Config, DPTConfig from transformers import Dinov2Config, DPTConfig
from transformers.file_utils import is_torch_available, is_vision_available from transformers.file_utils import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils.import_utils import get_torch_major_and_minor_version
from ...test_configuration_common import ConfigTester from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
@@ -140,6 +141,7 @@ class DPTModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
test_resize_embeddings = False test_resize_embeddings = False
test_head_masking = False test_head_masking = False
test_torch_exportable = True test_torch_exportable = True
test_torch_exportable_strictly = not get_torch_major_and_minor_version() == "2.7"
def setUp(self): def setUp(self):
self.model_tester = DPTModelTester(self) self.model_tester = DPTModelTester(self)

View File

@@ -21,6 +21,7 @@ from transformers import Dinov2Config, PromptDepthAnythingConfig
from transformers.file_utils import is_torch_available, is_vision_available from transformers.file_utils import is_torch_available, is_vision_available
from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_4 from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_4
from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils.import_utils import get_torch_major_and_minor_version
from ...test_configuration_common import ConfigTester from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
@@ -284,7 +285,10 @@ class PromptDepthAnythingModelIntegrationTest(unittest.TestCase):
self.assertTrue(torch.allclose(predicted_depth[0, :3, :3], expected_slice, atol=1e-3)) self.assertTrue(torch.allclose(predicted_depth[0, :3, :3], expected_slice, atol=1e-3))
def test_export(self): def test_export(self):
for strict in [True, False]: for strict in [False, True]:
if strict and get_torch_major_and_minor_version() == "2.7":
self.skipTest(reason="`strict=True` is currently failing with torch 2.7.")
with self.subTest(strict=strict): with self.subTest(strict=strict):
if not is_torch_greater_or_equal_than_2_4: if not is_torch_greater_or_equal_than_2_4:
self.skipTest(reason="This test requires torch >= 2.4 to run.") self.skipTest(reason="This test requires torch >= 2.4 to run.")

View File

@@ -27,6 +27,7 @@ from transformers.testing_utils import (
torch_device, torch_device,
) )
from transformers.utils import is_torch_available, is_vision_available from transformers.utils import is_torch_available, is_vision_available
from transformers.utils.import_utils import get_torch_major_and_minor_version
from ...test_configuration_common import ConfigTester from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
@@ -157,6 +158,7 @@ class UperNetModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase)
test_torchscript = False test_torchscript = False
has_attentions = False has_attentions = False
test_torch_exportable = True test_torch_exportable = True
test_torch_exportable_strictly = not get_torch_major_and_minor_version() == "2.7"
def setUp(self): def setUp(self):
self.model_tester = UperNetModelTester(self) self.model_tester = UperNetModelTester(self)

View File

@@ -25,6 +25,7 @@ from transformers.testing_utils import (
torch_device, torch_device,
) )
from transformers.utils import is_torch_available, is_vision_available from transformers.utils import is_torch_available, is_vision_available
from transformers.utils.import_utils import get_torch_major_and_minor_version
from ...test_configuration_common import ConfigTester from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor from ...test_modeling_common import ModelTesterMixin, floats_tensor
@@ -143,6 +144,7 @@ class VitMatteModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase
test_resize_embeddings = False test_resize_embeddings = False
test_head_masking = False test_head_masking = False
test_torch_exportable = True test_torch_exportable = True
test_torch_exportable_strictly = not get_torch_major_and_minor_version() == "2.7"
def setUp(self): def setUp(self):
self.model_tester = VitMatteModelTester(self) self.model_tester = VitMatteModelTester(self)

View File

@@ -21,6 +21,7 @@ import requests
from transformers import VitPoseBackboneConfig, VitPoseConfig from transformers import VitPoseBackboneConfig, VitPoseConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available from transformers.utils import cached_property, is_torch_available, is_vision_available
from transformers.utils.import_utils import get_torch_major_and_minor_version
from ...test_configuration_common import ConfigTester from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
@@ -154,6 +155,7 @@ class VitPoseModelTest(ModelTesterMixin, unittest.TestCase):
test_resize_embeddings = False test_resize_embeddings = False
test_head_masking = False test_head_masking = False
test_torch_exportable = True test_torch_exportable = True
test_torch_exportable_strictly = not get_torch_major_and_minor_version() == "2.7"
def setUp(self): def setUp(self):
self.model_tester = VitPoseModelTester(self) self.model_tester = VitPoseModelTester(self)

View File

@@ -20,6 +20,7 @@ import numpy as np
from transformers import Dinov2Config, ZoeDepthConfig from transformers import Dinov2Config, ZoeDepthConfig
from transformers.file_utils import is_torch_available, is_vision_available from transformers.file_utils import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils.import_utils import get_torch_major_and_minor_version
from ...test_configuration_common import ConfigTester from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
@@ -146,7 +147,8 @@ class ZoeDepthModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase
test_pruning = False test_pruning = False
test_resize_embeddings = False test_resize_embeddings = False
test_head_masking = False test_head_masking = False
test_torch_exportable = True # `strict=True/False` are both failing with torch 2.7, see #38677
test_torch_exportable = not get_torch_major_and_minor_version() == "2.7"
def setUp(self): def setUp(self):
self.model_tester = ZoeDepthModelTester(self) self.model_tester = ZoeDepthModelTester(self)

View File

@@ -4569,10 +4569,7 @@ class ModelTesterMixin:
# Export model # Export model
exported_model = torch.export.export( exported_model = torch.export.export(
model, model, args=(), kwargs=inputs_dict, strict=getattr(self, "test_torch_exportable_strictly", True)
args=(),
kwargs=inputs_dict,
strict=True,
) )
# Run exported model and eager model # Run exported model and eager model