Skip to content

Support auto_functionalized_v2 nodes in the sharding solver and apply pass - #596

Open
schmidt-jake wants to merge 1 commit into
meta-pytorch:mainfrom
schmidt-jake:jake/auto-functionalized-v2
Open

schmidt-jake wants to merge 1 commit into
meta-pytorch:mainfrom
schmidt-jake:jake/auto-functionalized-v2

Conversation

@schmidt-jake

Copy link
Copy Markdown

Problem

aot_export_module functionalizes a custom op that declares mutates_args into an auto_functionalized_v2 higher-order op. All of that node's tensors are kwargs; the mutated ones are indices into _all_bases. Graphs containing such nodes currently fail in the solver:

AttributeError: 'AutoFunctionalizedV2' object has no attribute '_can_decompose'

The failure is in get_op_strategy → _try_decomp_sharding, because a higher-order op is not an OpOverload. Beyond that, every input-node helper reads node.args only, so the solver and the apply pass never see the node's tensors.

Change

  • Inputs from args and kwargs. graph_utils.all_input_nodes, the solver's input lookup, build_sharding_metadata and the ordered-sharding helpers flatten (args, kwargs).
  • New shardings/auto_functionalize.py. It:
    • decodes the node from the wrapped op's schema and the view-info kwargs, with no per-op code;
    • rebuilds the op's call with each mutated argument as its base;
    • asks the DTensor registry (or AutoParallel's op rules) for the op's strategy;
    • maps that strategy onto the node's value (op returns..., updated bases...), each updated base taking its argument's placement.
  • Cases left conservative:
    • strict views of a base, and shared bases, get the all-replicated strategy;
    • out= variants and tensor-list mutated arguments raise NotImplementedError.
  • In-place filter. DTensor's trailing-underscore in-place filter is disabled while the strategy is generated, because the solver places every operand.
  • apply_sharding. It redistributes tensors found in kwargs and runs the node.
  • Compute estimation. Non-OpOverload targets were priced at zero compute. This PR prices the node as a memory-bound opaque kernel over its sharded inputs and outputs. It's a proxy, with no FLOP count. The zero-cost default also applies to other higher-order ops (e.g. flex attention), which this PR leaves unchanged.

Tests

tests/test_auto_functionalize.py (20 tests) covers:

  • toy mutating custom ops with register_sharding rules: plain, underscore-named, and a no-return op with two mutated arguments;
  • decoding and the replicate fallbacks;
  • solves on 1-D and 2-D fake meshes;
  • an end-to-end AutoParallel run;
  • the cost model;
  • apply against the eager op on 2 and 4 gloo ranks, including an operand that must be redistributed through kwargs.

We also validated it against real in-place custom ops (a top-k repair kernel and a routing scan) on 2 and 4 H200s, in a separate harness not included here. Lint passes with the repo's pinned black, isort and flake8. mypy was not run locally.

🤖 Generated with Claude Code

… pass

aot_export_module functionalizes a custom op with mutates_args into an
auto_functionalized_v2 higher-order op whose tensors are all kwargs (the mutated
ones as indices into _all_bases). The solver crashed on it in get_op_strategy
(`'AutoFunctionalizedV2' object has no attribute '_can_decompose'`), and every
input-node helper read node.args only.

* graph_utils.all_input_nodes, the solver's input lookup, build_sharding_metadata
  and the ordered-sharding helpers flatten (args, kwargs).
* shardings/auto_functionalize.py decodes the node from the wrapped op's schema
  and the view-info kwargs, rebuilds the op's call with each mutated argument as
  its base, asks the DTensor registry (or AutoParallel's op rules) for the op's
  strategy, and maps it back onto the node's value (op returns..., updated
  bases...), each updated base taking its argument's placement. Strict views of
  a base and shared bases get the all-replicated strategy; out= variants and
  tensor-list mutated arguments raise NotImplementedError. DTensor's
  trailing-underscore in-place filter is disabled while the strategy is
  generated, since the solver places every operand.
* apply_sharding redistributes tensors found in kwargs and runs the node.
* compute estimation: non-OpOverload targets were priced at zero; price the node
  as a memory-bound opaque kernel on its sharded inputs and outputs.
* tests: toy mutating ops with register_sharding rules (plain, underscore-named,
  a no-return op with two mutated arguments), decode and fallback cases, solves
  on 1-D and 2-D fake meshes, an end-to-end AutoParallel run, the cost model,
  and apply against the eager op on 2 and 4 gloo ranks, including an operand
  that must be redistributed through kwargs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@meta-cla

meta-cla Bot commented Nov 26, 2026

Copy link
Copy Markdown

Hi @schmidt-jake!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

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.

1 participant