Skip to content

Mirror the registered pytree flatten when building automatic dynamic shapes - #48578

Merged
IlyasMoutawwakil merged 2 commits into
mainfrom
fix-auto-dynamic-shapes-nested-outputs
Sep 10, 2026
Merged

IlyasMoutawwakil merged 2 commits into
mainfrom
fix-auto-dynamic-shapes-nested-outputs

Conversation

@IlyasMoutawwakil

@IlyasMoutawwakil IlyasMoutawwakil commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

CPU CI GPU run-slow

What does this PR do?

get_auto_dynamic_shapes treated every object with a __dict__ as flattening to a flat list of tensors, which is true only for the Cache subclasses the exporter registers itself. A ModelOutput is registered by transformers with the standard dict-like flatten, so torch.export expects one child per field -- and a field holding a tuple keeps that tuple in the tree spec.

Passing a ModelOutput as a generate kwarg therefore failed with Node arity mismatch; expected 2, but got 3. Ask the pytree registry for the flatten instead of assuming one: caches still yield a flat list, outputs keep their nesting.

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.

…shapes

`get_auto_dynamic_shapes` treated every object with a `__dict__` as flattening to a
flat list of tensors, which is true only for the `Cache` subclasses the exporter
registers itself. A `ModelOutput` is registered by transformers with the standard
dict-like flatten, so `torch.export` expects one child per field -- and a field
holding a tuple keeps that tuple in the tree spec.

Passing a `ModelOutput` as a generate kwarg therefore failed with `Node arity
mismatch; expected 2, but got 3`. Ask the pytree registry for the flatten instead
of assuming one: caches still yield a flat list, outputs keep their nesting.

Co-Authored-By: Claude Opus 5 <[email protected]>
@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.

@IlyasMoutawwakil

IlyasMoutawwakil commented Sep 10, 2026 •

Copy link
Copy Markdown
Member Author

tested locally over all models, it doesn't change much here because model outputs are never passed as inputs during export.

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 34052829173:1
Result: success | Jobs: 16 | Tests: 185,596 | Failures: 0 | Duration: 18h 56m

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

Thanks, it works with the other PR that added ModeOutput in generate. Can you just double-check that changes in test_export are expected?

"GroundingDinoForObjectDetection": "Same `timeout` failure as `Mask2FormerModel`.",
"MMGroundingDinoModel": "Same `timeout` failure as `Mask2FormerModel`.",
"MMGroundingDinoForObjectDetection": "Same `timeout` failure as `Mask2FormerModel`.",
"Sam2VisionModel": "Same `timeout` failure as `Mask2FormerModel`.",

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.

no timeout anymore, or was the real issue in ModelOutput?

@IlyasMoutawwakil IlyasMoutawwakil Sep 10, 2026 •

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.

we skip sam2 altogether (see above it), when testing locally i found that torch 2.13+ broke it

@IlyasMoutawwakil
IlyasMoutawwakil added this pull request to the merge queue Sep 10, 2026
Merged via the queue into main with commit c583a3a Sep 10, 2026
116 checks passed
@IlyasMoutawwakil
IlyasMoutawwakil deleted the fix-auto-dynamic-shapes-nested-outputs branch September 10, 2026 08:53
sbucaille pushed a commit to sbucaille/transformers that referenced this pull request Sep 16, 2026
…shapes (huggingface#48578)

* Mirror the registered pytree flatten when building automatic dynamic shapes

`get_auto_dynamic_shapes` treated every object with a `__dict__` as flattening to a
flat list of tensors, which is true only for the `Cache` subclasses the exporter
registers itself. A `ModelOutput` is registered by transformers with the standard
dict-like flatten, so `torch.export` expects one child per field -- and a field
holding a tuple keeps that tuple in the tree spec.

Passing a `ModelOutput` as a generate kwarg therefore failed with `Node arity
mismatch; expected 2, but got 3`. Ask the pytree registry for the flatten instead
of assuming one: caches still yield a flat list, outputs keep their nesting.

Co-Authored-By: Claude Opus 5 <[email protected]>

* skip

---------

Co-authored-by: Claude Opus 5 <[email protected]>
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