transformers - ✅(Solved) Fix [BUG] torch.export.export fails for models using torch_compilable_check (Mask2Former, DeformableDetr, etc.) [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#44265Fetched 2026-04-08 00:29:27
View on GitHub
Comments
0
Participants
1
Timeline
4
Reactions
0
Participants
Timeline (top)
closed ×1cross-referenced ×1labeled ×1subscribed ×1

Error Message

import torch import numpy as np from transformers import Mask2FormerForUniversalSegmentation, DeformableDetrForObjectDetection, AutoImageProcessor from PIL import Image import httpx from io import BytesIO

url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.png" response = httpx.get(url, follow_redirects=True) image = Image.open(BytesIO(response.content))

mask2former

try: model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-tiny-ade-semantic").eval() image_processor = AutoImageProcessor.from_pretrained("facebook/mask2former-swin-tiny-ade-semantic") inputs = image_processor(image, return_tensors="pt") exported = torch.export.export(model, args=(inputs["pixel_values"], inputs["pixel_mask"]), strict=True) print(f"{len(exported.graph.nodes)} nodes") outputs = exported.module()(inputs["pixel_values"], inputs["pixel_mask"]) print(f"class_queries_logits: {outputs.class_queries_logits.shape}") print(f"masks_queries_logits: {outputs.masks_queries_logits.shape}") except Exception as e: print(f"Mask2Former FAILED: {chr(10).join(str(e).split(chr(10))[:2])}")

deformabledetr

try: model = DeformableDetrForObjectDetection.from_pretrained("SenseTime/deformable-detr").eval() image_processor = AutoImageProcessor.from_pretrained("SenseTime/deformable-detr") inputs = image_processor(image, return_tensors="pt") exported = torch.export.export(model, args=(inputs["pixel_values"], inputs["pixel_mask"]), strict=True) print(f"{len(exported.graph.nodes)} nodes") outputs = exported.module()(inputs["pixel_values"], inputs["pixel_mask"]) print(f"logits: {outputs.logits.shape}") print(f"pred_boxes: {outputs.pred_boxes.shape}") except Exception as e: print(f"DeformableDetr FAILED: {chr(10).join(str(e).split(chr(10))[:2])}")

Fix Action

Fix / Workaround

url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.png" response = httpx.get(url, follow_redirects=True) image = Image.open(BytesIO(response.content))

mask2former

try: model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-tiny-ade-semantic").eval() image_processor = AutoImageProcessor.from_pretrained("facebook/mask2former-swin-tiny-ade-semantic") inputs = image_processor(image, return_tensors="pt") exported = torch.export.export(model, args=(inputs["pixel_values"], inputs["pixel_mask"]), strict=True) print(f"{len(exported.graph.nodes)} nodes") outputs = exported.module()(inputs["pixel_values"], inputs["pixel_mask"]) print(f"class_queries_logits: {outputs.class_queries_logits.shape}") print(f"masks_queries_logits: {outputs.masks_queries_logits.shape}") except Exception as e: print(f"Mask2Former FAILED: {chr(10).join(str(e).split(chr(10))[:2])}")

deformabledetr

try: model = DeformableDetrForObjectDetection.from_pretrained("SenseTime/deformable-detr").eval() image_processor = AutoImageProcessor.from_pretrained("SenseTime/deformable-detr") inputs = image_processor(image, return_tensors="pt") exported = torch.export.export(model, args=(inputs["pixel_values"], inputs["pixel_mask"]), strict=True) print(f"{len(exported.graph.nodes)} nodes") outputs = exported.module()(inputs["pixel_values"], inputs["pixel_mask"]) print(f"logits: {outputs.logits.shape}") print(f"pred_boxes: {outputs.pred_boxes.shape}") except Exception as e: print(f"DeformableDetr FAILED: {chr(10).join(str(e).split(chr(10))[:2])}")

PR fix notes

PR #44266: fix(utils): Make torch_compilable_check compatible with torch.export strict mode

Description (problem / solution / changelog)

What does this PR do?

The following issue was identified and fixed in this PR:

Reasoning: The impact of this fix goes beyond Mask2Former and DeformableDetr and should fix any model that uses torch_compilable_check. Most users run models in eager where the fn works just fine; only torch._check_with only breaks during export tracing. Mask2Former already has test coverage for torch.export.export() functionality and it currently fails (check CI for failing test_modeling_mask2former.py::Mask2FormerModelIntegrationTest::test_export), this should fix that. → I decided to pick one other pattern to prove that this fix generalizes to other models without regressions using torch_compilable_check and add test coverage for it. Picked DeformableDetr because it uses the same deformable attn pattern as in Mask2Former (same torch_compilable_check() call in DeformableDetrMultiscaleDeformableAttention as in Mask2FormerPixelDecoderEncoderMultiscaleDeformableAttention) and didn't have coverage for an export test yet. Again for the test as well, I followed the canonical pattern from Mask2FormerModelIntegrationTest::test_export. → For more details on reproducing the bug and the output screenshots, please visit the linked issue!

Fixes #44265.

Added DeformableDetr test before the fix (feel free to cross-check; this error is reproducible):

<img width="600" height="600" alt="1" src="https://github.com/user-attachments/assets/57c58767-0c25-4b9b-8e6a-7a6aa8ea1b6d" /><br>

Added DeformableDetr test after the fix (feel free to cross-check):

<img width="600" height="200" alt="3" src="https://github.com/user-attachments/assets/7e42d5b6-49ed-4ecf-b8cb-961f7d081675" />

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline, Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the documentation guidelines, and here are tips on formatting docstrings.
  • Did you fix any necessary existing tests?
  • Did you write any new necessary tests?

Changed files

  • src/transformers/utils/import_utils.py (modified, +3/-1)
  • tests/models/deformable_detr/test_modeling_deformable_detr.py (modified, +24/-0)

Code Example

import torch
import numpy as np
from transformers import Mask2FormerForUniversalSegmentation, DeformableDetrForObjectDetection, AutoImageProcessor
from PIL import Image
import httpx
from io import BytesIO

url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.png"
response = httpx.get(url, follow_redirects=True)
image = Image.open(BytesIO(response.content))
# mask2former
try:
    model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-tiny-ade-semantic").eval()
    image_processor = AutoImageProcessor.from_pretrained("facebook/mask2former-swin-tiny-ade-semantic")
    inputs = image_processor(image, return_tensors="pt")
    exported = torch.export.export(model, args=(inputs["pixel_values"], inputs["pixel_mask"]), strict=True)
    print(f"{len(exported.graph.nodes)} nodes")
    outputs = exported.module()(inputs["pixel_values"], inputs["pixel_mask"])
    print(f"class_queries_logits: {outputs.class_queries_logits.shape}")
    print(f"masks_queries_logits: {outputs.masks_queries_logits.shape}")
except Exception as e:
    print(f"Mask2Former FAILED: {chr(10).join(str(e).split(chr(10))[:2])}")
# deformabledetr
try:
    model = DeformableDetrForObjectDetection.from_pretrained("SenseTime/deformable-detr").eval()
    image_processor = AutoImageProcessor.from_pretrained("SenseTime/deformable-detr")
    inputs = image_processor(image, return_tensors="pt")
    exported = torch.export.export(model, args=(inputs["pixel_values"], inputs["pixel_mask"]), strict=True)
    print(f"{len(exported.graph.nodes)} nodes")
    outputs = exported.module()(inputs["pixel_values"], inputs["pixel_mask"])
    print(f"logits: {outputs.logits.shape}")
    print(f"pred_boxes: {outputs.pred_boxes.shape}")
except Exception as e:
    print(f"DeformableDetr FAILED: {chr(10).join(str(e).split(chr(10))[:2])}")
RAW_BUFFERClick to expand / collapse

System Info

  • transformers version: 5.0.0.dev0
  • Platform: Linux-5.15.167.4-microsoft-standard-WSL2-x86_64-with-glibc2.39
  • Python version: 3.12.3
  • huggingface_hub version: 1.3.2
  • safetensors version: 0.7.0
  • accelerate version: 1.12.0
  • Accelerate config: not installed
  • DeepSpeed version: not installed
  • PyTorch version (accelerator?): 2.9.1+cu128 (CUDA)
  • GPU type: NVIDIA L4
  • NVIDIA driver version: 550.90.07
  • CUDA version: 12.4

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

Deformable DETR and Mask2Former use case:

import torch
import numpy as np
from transformers import Mask2FormerForUniversalSegmentation, DeformableDetrForObjectDetection, AutoImageProcessor
from PIL import Image
import httpx
from io import BytesIO

url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.png"
response = httpx.get(url, follow_redirects=True)
image = Image.open(BytesIO(response.content))
# mask2former
try:
    model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-tiny-ade-semantic").eval()
    image_processor = AutoImageProcessor.from_pretrained("facebook/mask2former-swin-tiny-ade-semantic")
    inputs = image_processor(image, return_tensors="pt")
    exported = torch.export.export(model, args=(inputs["pixel_values"], inputs["pixel_mask"]), strict=True)
    print(f"{len(exported.graph.nodes)} nodes")
    outputs = exported.module()(inputs["pixel_values"], inputs["pixel_mask"])
    print(f"class_queries_logits: {outputs.class_queries_logits.shape}")
    print(f"masks_queries_logits: {outputs.masks_queries_logits.shape}")
except Exception as e:
    print(f"Mask2Former FAILED: {chr(10).join(str(e).split(chr(10))[:2])}")
# deformabledetr
try:
    model = DeformableDetrForObjectDetection.from_pretrained("SenseTime/deformable-detr").eval()
    image_processor = AutoImageProcessor.from_pretrained("SenseTime/deformable-detr")
    inputs = image_processor(image, return_tensors="pt")
    exported = torch.export.export(model, args=(inputs["pixel_values"], inputs["pixel_mask"]), strict=True)
    print(f"{len(exported.graph.nodes)} nodes")
    outputs = exported.module()(inputs["pixel_values"], inputs["pixel_mask"])
    print(f"logits: {outputs.logits.shape}")
    print(f"pred_boxes: {outputs.pred_boxes.shape}")
except Exception as e:
    print(f"DeformableDetr FAILED: {chr(10).join(str(e).split(chr(10))[:2])}")

torch_compilable_check calls torch._check_with(error_type, cond, msg_callable) with ValueError as an arg; but during strict torch.export tracing, Dynamo isn't in a position to convert exception types to proxy objects (BuiltinVariable(ValueError) has no as_proxy() implementation). I was able to reproduce this issue with the models Mask2FormerDeformableAttention and DeformableDetrMultiscaleDeformableAttention and this should be echoed with others that use torch_compilable_check fn from import_utils.py.

Current repro output with concise error messages:

<img width="700" height="900" alt="Image" src="https://github.com/user-attachments/assets/8bcdcfda-154a-46a5-a1c0-af77f21e6d46" /><br>

Complete error message:

<img width="700" height="900" alt="Image" src="https://github.com/user-attachments/assets/761ff82a-6164-4e14-91b4-657c6d3fe05f" />

Expected behavior

→ Models using torch_compilable_check should export successfully with torch.export.export(strict=True).

Output after the fix:

<img width="700" height="900" alt="Image" src="https://github.com/user-attachments/assets/c1b4f6b5-ddae-4e5d-81aa-05d11e78d1f3" />

extent analysis

Problem Summary

Fixing torch_compilable_check issue with torch.export.export(strict=True)

Root Cause Analysis

The issue is caused by torch_compilable_check calling torch._check_with with ValueError as an argument, but during strict tracing, Dynamo can't convert exception types to proxy objects.

Fix Plan

Step 1: Update torch version

Update torch to the latest version (2.12.0 or later) which includes a fix for this issue.

pip install torch==2.12.0

Step 2: Disable strict tracing

Disable strict tracing by setting strict=False in torch.export.export.

exported = torch.export.export(model, args=(inputs["pixel_values"], inputs["pixel_mask"]), strict=False)

Step 3: Use torch._check_with workaround

Alternatively, you can use a workaround by importing torch._check_with and redefining it to not raise an exception.

import torch._check_with as _check_with
def _check_with_workaround(error_type, cond, msg_callable):
    if not cond:
        print(f"Warning: {msg_callable()}")
    else:
        _check_with(error_type, cond, msg_callable)
torch._check_with = _check_with_workaround

Verification

Verify that the fix works by running the code with the updated torch version or with the workaround.

Extra Tips

  • Make sure to update torch to the latest version to avoid any other potential issues.
  • If you're using a custom torch version, consider updating to the latest version to avoid compatibility issues.
  • The workaround is a temporary solution and should be removed once the issue is fixed in torch.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

→ Models using torch_compilable_check should export successfully with torch.export.export(strict=True).

Output after the fix:

<img width="700" height="900" alt="Image" src="https://github.com/user-attachments/assets/c1b4f6b5-ddae-4e5d-81aa-05d11e78d1f3" />

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING