Skip to content

[Compile] Fix capture outputs during compile - #44614

Closed
vasqu wants to merge 5 commits into
mainfrom
fix-compile-capture
Closed

vasqu wants to merge 5 commits into
mainfrom
fix-compile-capture

Conversation

@vasqu

@vasqu vasqu commented Mar 11, 2026 •

Copy link
Copy Markdown
Collaborator

Only detected in #43590, so it can only be detected there for test_sdpa_can_compile_dynamic (lw_detr). Core issue: Dynamo can cache the attribute and ignore it across frames which means that updates/reads are not working as expected.

This PR changes the logic to force dynamo to read compile state instead of relying on the attribute


Note

Medium Risk
Changes how output-capture state is stored/read during torch.compile, which can affect correctness in compiled vs eager regions and has limited thread-safety while compiling. Also broadens CI triggering by marking the file as core, potentially increasing test load but reducing missed regressions.

Overview
Fixes output capturing under torch.compile by reworking CompileableContextVar to always query Dynamo’s current compiling state and to keep a mirrored global_var in sync, avoiding stale cached flags across graph breaks.

Updates CI test selection by adding src/transformers/utils/output_capturing.py to CORE_FILES, so changes to output capturing trigger full test runs.

Written by Cursor Bugbot for commit 04569d0. This will update automatically on new commits. Configure here.

else:
# Set was maybe never called, so still check it here
if is_torchdynamo_compiling():
self.is_compiling = True

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Side note: This was even a typo 👀 self.compiling <-> self.is_compiling

@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.

@huggingface huggingface deleted a comment from github-actions Bot Mar 11, 2026
Cyrilvallez
Cyrilvallez previously approved these changes Mar 12, 2026

@Cyrilvallez Cyrilvallez left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was actually always checking like this in the beginning, then introduced the flag but I cannot remember if there was a specific reason test-wise... 😅 I guess if the test_generate_compile_model_forward_fullgraph and test_generate_compilation_all_outputs all pass, then it's good!

EDIT: Removed my approval as the CI actually did not trigger those tests at all (test fetcher will not run any model test if only output_capturing is modified I believe)! Will add the file to the all-trigger list actually, as it's an important feature that will soon touch all files with your coming PR!

Comment on lines +84 to +85
self.global_var = value
# Same, check the current state dynamically rather than relying on cached attributes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we always set the global here? Let's only do when it's compiling!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is essentially the core change, it seems somewhere along the way, some small graph break happens and when we do not set it in general the state between compiled regions and non-compiled eager regions is messed up

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is concerning, it means some modules are fully skipped by dynamo and not compiled??

Comment on lines +74 to +78
# NOTE: Local attributes/flags may be interpreted once during tracing
# and effectively treated as constant across compiled frames. If we
# cached such a flag, eager frames could incorrectly fall back to the
# shared global value, breaking visibility between eager wrappers and
# compiled hooks

@Cyrilvallez Cyrilvallez Mar 12, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't really understand this comment (the final part), so not helpful IMO haha - let's either entirely remove or rewrite!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Will update in a second

self.global_var = None
self.compiling = False
else:
self.global_var = self.default

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's just reset to None alway no? Instead of saving the default as internal attr

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I don't mind, but why do we even provide a default in the init at all 😅

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only because ContextVar requires it! Though indeed we could simply explicitly do it in our wrapper's init instead of exposing it

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yea changed the init, and forced none in any case

@Cyrilvallez
Cyrilvallez dismissed their stale review March 12, 2026 09:05

Oh wait actually the tests did not trigger!

@github-actions

Copy link
Copy Markdown
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=44614&sha=04569d

@Cyrilvallez

Copy link
Copy Markdown
Member

Superseded by #44638 after more investigation!

@vasqu
vasqu deleted the fix-compile-capture branch March 13, 2026 02:15
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.

3 participants