Skip to content

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

Merged
ArthurZucker merged 1 commit intohuggingface:mainfrom
harshaljanjani:fix/torch-compilable-check-export
Feb 25, 2026
Merged

fix(utils): Make torch_compilable_check compatible with torch.export strict mode#44266
ArthurZucker merged 1 commit intohuggingface:mainfrom
harshaljanjani:fix/torch-compilable-check-export

Conversation

@harshaljanjani
Copy link
Copy Markdown
Contributor

@harshaljanjani harshaljanjani commented Feb 24, 2026

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):

1

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

3

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?

@github-actions
Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: deformable_detr

@harshaljanjani harshaljanjani marked this pull request as ready for review February 24, 2026 20:12
Copy link
Copy Markdown
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot for fixing!

@ArthurZucker ArthurZucker merged commit a344fed into huggingface:main Feb 25, 2026
25 checks passed
@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@harshaljanjani harshaljanjani deleted the fix/torch-compilable-check-export branch February 25, 2026 17:03
@harshaljanjani
Copy link
Copy Markdown
Contributor Author

Thank you so much for your time @ArthurZucker! I was wondering if I may ask you to check in on this PR as it has been pending review for some time now; at your convenience and sorry if this is a bother :)

@IlyasMoutawwakil
Copy link
Copy Markdown
Member

IlyasMoutawwakil commented Feb 26, 2026

Hi ! the test you mention in the PR uses strict mode which we don't wanna support in general because it's too constraining, non-strict mode is generally enough for model export (even onnx export doesn't require strict mode).
Do you have any specific use case for it or was the intent to just fix the failing test ?

@harshaljanjani
Copy link
Copy Markdown
Contributor Author

harshaljanjani commented Feb 26, 2026

Good day @IlyasMoutawwakil!
Since Mask2FormerModelIntegrationTest::test_export used the pattern, I read it as intentional; did some RCA, noticed a broader issue, and posted the fix. The merged fix itself should be harmless either way; the only clarification I'd ask for is if it's aligned with the project's stance. Since the project stance is to not support strict mode as you mentioned, I'm happy to open a follow-up PR to switch both the existing Mask2Former test and the new DeformableDetr test to strict=False, but I'm missing a reason to revert the change I made to import_utils.py which preserves the original semantics in eager and non-strict export, while also being compatible with strict export.
Would love to know your thoughts :)

@IlyasMoutawwakil
Copy link
Copy Markdown
Member

IlyasMoutawwakil commented Feb 26, 2026

unfortunately the change actually does break a couple models 😅, we have a more general test for torch.export (it's marked as slow so it doesn't appear on fast ci) and some models started failing after this change because we also use torch._check to give dynamo compiler hints, basically telling dynamo that it should expect a condition to be true at compile time instead of evaluating it. I document that in the function's docstring but it might be easy to miss there.

the failing tests can be seen in here for example https://github.com/huggingface/transformers/actions/runs/22426245229/job/64935308903
i'm reverting that line and cleaning up the strict mode exports in #44293

@IlyasMoutawwakil
Copy link
Copy Markdown
Member

IlyasMoutawwakil commented Feb 26, 2026

to sum it up:

  • torch._check is the most compatible way with torch.compile and torch.export non-strict mode
  • torch._check seems to fail with torch.export strict mode (not sure how frequently).

I would argue that this requires fixing in pytorch because the torch._check api is what torch.export suggests using when you get data dependency failures, so it's weird for it to not be compatible with strict mode.

see this traceback from a failing export due to datat dep:

FAILED tests/models/vits/test_modeling_vits.py::VitsModelTest::test_torch_export - torch.fx.experimental.symbolic_shapes.GuardOnDataDependentSymNode: Could not guard on data-dependent expression u4 < 1 (unhinted: u4 < 1).  (Size-like symbols: none)

consider using data-dependent friendly APIs such as guard_or_false, guard_or_true and statically_known_true.
Caused by: (_subclasses/fake_impls.py:1135 in conv)
For more information, run with TORCH_LOGS="dynamic"
For extended logs when we create symbols, also add TORCHDYNAMO_EXTENDED_DEBUG_CREATE_SYMBOL="u4"
If you suspect the guard was triggered from C++, add TORCHDYNAMO_EXTENDED_DEBUG_CPP=1
For more debugging help, see https://docs.google.com/document/d/1HSuTTVvYH1pTew89Rtpeu84Ht3nQEFTYhAX3Ypa_xJs/edit?usp=sharing

For C++ stack trace, run with TORCHDYNAMO_EXTENDED_DEBUG_CPP=1

The following call raised this error:
  File "/home/ilyas/transformers/src/transformers/models/vits/modeling_vits.py", line 565, in forward
    hidden_states = self.conv_pre(first_half) * padding_mask

To fix the error, insert one of the following checks before this call:
  1. torch._check(inputs.shape[2] < 1)
  2. torch._check(inputs.shape[2] >= 1)

(These suggested fixes were derived by replacing `u4` with inputs.shape[2] or padding_mask.shape[2] or first_half.shape[2] or second_half.shape[2] in u4 < 1 and its negation.)

The error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.

@harshaljanjani
Copy link
Copy Markdown
Contributor Author

That makes sense to me; thanks for the explanation of the revert and the context on the torch._check traceback as well :)

@IlyasMoutawwakil IlyasMoutawwakil mentioned this pull request Feb 27, 2026
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] torch.export.export fails for models using torch_compilable_check (Mask2Former, DeformableDetr, etc.)

5 participants