Skip to content

Import utils compilation fixes - #47726

Merged
vasqu merged 8 commits into
mainfrom
fix-import-helpers-compilation
Aug 4, 2026
Merged

Import utils compilation fixes#47726
vasqu merged 8 commits into
mainfrom
fix-import-helpers-compilation

Conversation

@IlyasMoutawwakil

@IlyasMoutawwakil IlyasMoutawwakil commented Aug 3, 2026

Copy link
Copy Markdown
Member

CI

What does this PR do?

Import utils are very sneaky and break compile (distributed util to_local breaks compile bcz of distributed torch check).

Code Agent Policy

The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. These often are low-quality, or fix extremely minor issues that occur rarely or never in practice.
As a result, we're instituting a rule that first-time contributors should not use code agents to submit PRs or issues.
We'd also ask autonomous "OpenClaw"-like agents not to open any PRs or issues.

Issues/PRs from first-time contributors that violate this rule will probably just be closed without review, and we
might block you, especially if you open more than one or appear to be deliberately ignoring this. We especially do not
want new contributors to jump in on random issues to contribute an agent-written fix. This creates lots of noise
for reviewers and other users and will almost certainly get you blocked.

For more information, please read CONTRIBUTING.md.

  • (First-time contributors only): I confirm that this PR description and code is not written by an LLM or code agent

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 and the
    Pull Request checks?
  • 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 according to the guidelines?
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@IlyasMoutawwakil
IlyasMoutawwakil requested a review from vasqu August 3, 2026 07:07
@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.

Comment on lines -164 to -165
@torch._dynamo.allow_in_graph
def _sonicmoe_wrapper(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

dropping this since we removed the "not torch compiling" assertions in huggingface/kernels-community#1056

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Got some questions first tbh, not opposed just want the full picture

Comment thread src/transformers/utils/import_utils.py Outdated
capability, an environment variable. Never for a runtime query such as `is_cuda_stream_capturing`,
where inlining a value that legitimately changes would silently bake a transient into the graph.
"""
fn._dynamo_marked_constant = True

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  • Should we use settatr instead then?
  • Also let's move it a bit below so when the actual import utils start for each package
  • this seems like something we want for most utils here then? at which boundary do we decide if it's important or not?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Iiuc then it's mostly appearing in torch distributed where the calls are not friendly if not marked as constant?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

basically any util that might make it into the forward path, the problem is that in many of our tests / compilation scenarios, the util might have been called before being compiled so it gets cached and tests don't catch it (for example it gets called in prefill before being compiled for decode). but it can break when compiled from the get go.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Lets move and use setattr pls

Comment thread src/transformers/utils/import_utils.py Outdated
return _is_package_available("smdistributed")[0]


@_compile_constant

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For anything env related, it could also switch at runtime theoretically no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeh but i guess you could say that when you compile, you should expect those values to freeze in the graph. even a library/package availability can change at runtime actually 🫠

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hmm, but I then feel like maybe we should not make this a default and allow making it a constant when explicitly requested? It's a bit brittle because we don't know what users use which functionality outside of this and we go against bc behavior then

Any option we make this optional and mark it under our runtime requirements where needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

allow making it a constant when explicitly requested

it's only a constant when compiled tho, we don't make it a constant with this flag, we only make it assumed a constant by the compiler (the only way for it to compile).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah sorry, yea the docstrings even mention it.. wouldnt it kind of make sense to attach to all utils we have in the file

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Only potentially not necessary ^

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

adding it to as many as possible, but there are some that should be left without

"""
These helpers get called from inside `torch.compile`d regions — e.g. `is_dtensor`, which every MoE
kernel integration reaches through `to_local`. Each carries `@_compile_constant`, so dynamo evaluates
it once at trace time and never enters the body; this checks the marker actually takes effect.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yea exactly where it would be useful to know how to determine this list

Comment thread tests/utils/test_import_utils.py

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ok sorry for all the back and forth, just small comments left imo

Comment thread src/transformers/utils/import_utils.py Outdated
Comment thread src/transformers/utils/import_utils.py Outdated
capability, an environment variable. Never for a runtime query such as `is_cuda_stream_capturing`,
where inlining a value that legitimately changes would silently bake a transient into the graph.
"""
fn._dynamo_marked_constant = True

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Lets move and use setattr pls

Comment thread src/transformers/utils/import_utils.py Outdated
return _is_package_available("smdistributed")[0]


@_compile_constant

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Only potentially not necessary ^

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 30896562992:1
Result: failure | Jobs: 16 | Tests: 179,379 | Failures: 1 | Duration: 15h 18m

@vasqu
vasqu added this pull request to the merge queue Aug 4, 2026
Merged via the queue into main with commit 410c4b1 Aug 4, 2026
213 of 215 checks passed
@vasqu
vasqu deleted the fix-import-helpers-compilation branch August 4, 2026 11:37
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