Add common test for torch.export and fix some vision models (#35124)

* Add is_torch_greater_or_equal test decorator

* Add common test for torch.export

* Fix bit

* Fix focalnet

* Fix imagegpt

* Fix seggpt

* Fix swin2sr

* Enable torch.export test for vision models

* Enable test for video models

* Remove json

* Enable for hiera

* Enable for ijepa

* Fix detr

* Fic conditional_detr

* Fix maskformer

* Enable test maskformer

* Fix test for deformable detr

* Fix custom kernels for export in rt-detr and deformable-detr

* Enable test for all DPT

* Remove custom test for deformable detr

* Simplify test to use only kwargs for export

* Add comment

* Move compile_compatible_method_lru_cache to utils

* Fix beit export

* Fix deformable detr

* Fix copies data2vec<->beit

* Fix typos, update test to work with dict

* Add seed to the test

* Enable test for vit_mae

* Fix beit tests

* [run-slow] beit, bit, conditional_detr, data2vec, deformable_detr, detr, focalnet, imagegpt, maskformer, rt_detr, seggpt, swin2sr

* Add vitpose test

* Add textnet test

* Add dinov2 with registers

* Update tests/test_modeling_common.py

* Switch to torch.testing.assert_close

* Fix masformer

* Remove save-load from test

* Add dab_detr

* Add depth_pro

* Fix and test RT-DETRv2

* Fix dab_detr
This commit is contained in:
Pavel Iakubovskii
2025-02-11 11:37:31 +00:00
committed by GitHub
parent 1779f5180e
commit f42d46ccb4
77 changed files with 305 additions and 151 deletions

View File

@@ -86,6 +86,7 @@ from transformers.testing_utils import (
require_torch,
require_torch_accelerator,
require_torch_gpu,
require_torch_greater_or_equal,
require_torch_multi_accelerator,
require_torch_multi_gpu,
require_torch_sdpa,
@@ -221,6 +222,7 @@ class ModelTesterMixin:
test_mismatched_shapes = True
test_missing_keys = True
test_model_parallel = False
test_torch_exportable = False
# Used in `check_training_gradient_checkpointing` to NOT check all params having gradient (e.g. for some MOE models)
test_all_params_have_gradient = True
is_encoder_decoder = False
@@ -4865,6 +4867,72 @@ class ModelTesterMixin:
# Assert the last tokens are actually the same (except for the natural fluctuation due to order of FP ops)
torch.testing.assert_close(all_logits[:, -1:, :], last_token_logits, rtol=1e-5, atol=1e-5)
@slow
@require_torch_greater_or_equal("2.5")
def test_torch_export(self, config=None, inputs_dict=None, tolerance=1e-4):
"""
Test if model can be exported with torch.export.export()
Args:
config (PretrainedConfig):
Config to use for the model, if None, use default config from model_tester
inputs_dict (dict):
Inputs to use for the model, if None, use default inputs from model_tester
tolerance (float):
`atol` for torch.allclose(), defined in signature for test overriding
"""
if not self.test_torch_exportable:
self.skipTest(reason="test_torch_exportable=False for this model.")
def recursively_check(eager_outputs, exported_outputs):
is_tested = False
if isinstance(eager_outputs, torch.Tensor):
torch.testing.assert_close(eager_outputs, exported_outputs, atol=tolerance, rtol=tolerance)
return True
elif isinstance(eager_outputs, (tuple, list)):
for eager_output, exported_output in zip(eager_outputs, exported_outputs):
is_tested = is_tested or recursively_check(eager_output, exported_output)
return is_tested
elif isinstance(eager_outputs, dict):
for key in eager_outputs:
is_tested = is_tested or recursively_check(eager_outputs[key], exported_outputs[key])
return is_tested
return is_tested
default_config, default_inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
config = config or default_config
inputs_dict = inputs_dict or default_inputs_dict
for model_class in self.all_model_classes:
if model_class.__name__.endswith("ForPreTraining"):
continue
with self.subTest(model_class.__name__):
model = model_class(config).eval().to(torch_device)
# Export model
exported_model = torch.export.export(
model,
args=(),
kwargs=inputs_dict,
strict=True,
)
# Run exported model and eager model
with torch.no_grad():
# set seed in case anything is not deterministic in model (e.g. vit_mae noise)
torch.manual_seed(1234)
eager_outputs = model(**inputs_dict)
torch.manual_seed(1234)
exported_outputs = exported_model.module().forward(**inputs_dict)
# Check if outputs are close:
# is_tested is a boolean flag idicating if we comapre any outputs,
# e.g. there might be a situation when outputs are empty list, then is_tested will be False.
# In case of outputs are different the error will be rasied in `recursively_check` function.
is_tested = recursively_check(eager_outputs, exported_outputs)
self.assertTrue(is_tested, msg=f"No outputs were compared for {model_class.__name__}")
@require_torch_gpu
def test_flex_attention_with_grads(self):
for model_class in self.all_model_classes: