Skip to content

[graph_trainer] Support marked symbolic input dims in tracing - #3362

Merged
sanketpurandare merged 1 commit into
mainfrom
sanketpurandare/stack/16
Jun 26, 2026
Merged

sanketpurandare merged 1 commit into
mainfrom
sanketpurandare/stack/16

Conversation

@sanketpurandare

@sanketpurandare sanketpurandare commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[graph_trainer] Support marked symbolic input dims in tracing

Add the graph_trainer tracing prerequisites needed by later EP chunking work without introducing the chunking passes themselves. minimal_fx_tracer now accepts mark_unbacked and mark_dynamic metadata on plain tensor inputs, preserves Dynamo dynamic-range annotations during fakeification, and keeps wrapper-subclass inputs rejected so DTensor-style layouts do not silently lose their metadata.

Make RoPE singleton-position handling symbolic-shape friendly without adding tracer-specific runtime checks, make no-op cleanup passes guard symbolic comparisons, and materialize traced gradients to the parameter layout before assigning param.grad.

Move AutoParallel CI checks after the graph_trainer checks in the default and H100 workflows so a stale AutoParallel package cannot hide the graph_trainer signal from this stack.

Test Plan:\n- conda run -n pt-dev pytest -q torchtitan/experiments/graph_trainer/tests/test_trace_module.py -k 'fakeify_input_copies_only_shape_annotations or mark_dynamic_min_max_preserves_range or mark_dynamic_batch_and_seq_dims_with_rope or mark_unbacked_positions_batch_dim_with_rope or rope_symbolic_positions_compile_fullgraph or materialize_grad_for_param_layout_restores_param_strides or nested_wrapper_subclass_marked_inner_rejected or mark_dynamic_wrapper_subclass_rejected or dtensor_mark_unbacked_rejected'\n- conda run -n pt-dev python -m pytest -q tests/unit_tests/test_loss.py::TestChunkedCELoss::test_equal_split_chunks_match_torch_chunk_for_supported_shapes tests/unit_tests/test_loss.py::TestChunkedCELoss::test_equal_split_chunks_backward_matches_torch_chunk tests/unit_tests/test_loss.py::TestChunkedCELoss::test_symbolic_seq_len_traces_chunking tests/unit_tests/test_loss.py::TestChunkedCELoss::test_rejects_non_divisible_sequence_length tests/unit_tests/test_loss.py::TestChunkedCELoss::test_single_token_chunks_match_torch_chunk_reference torchtitan/experiments/graph_trainer/tests/test_chunked_loss.py::TestChunkedCELossWithParamGrads::test_bitwise_equal_with_chunked_celoss

@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/15 branch from 07f3629 to 1ee37b9 Compare May 15, 2026 03:03
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/16 branch from 60ac042 to 3178797 Compare May 15, 2026 03:03
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 15, 2026
@sanketpurandare
sanketpurandare marked this pull request as draft May 15, 2026 09:36
@sanketpurandare
sanketpurandare changed the base branch from sanketpurandare/stack/15 to main May 15, 2026 09:36
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/16 branch 2 times, most recently from d7fa96e to 0fb48ed Compare May 15, 2026 09:36
@sanketpurandare
sanketpurandare changed the base branch from main to sanketpurandare/stack/18 May 15, 2026 09:36
@sanketpurandare
sanketpurandare marked this pull request as ready for review May 15, 2026 09:37
Comment thread torchtitan/models/common/rope.py Resolved
@sanketpurandare
sanketpurandare marked this pull request as draft May 22, 2026 19:02
@sanketpurandare
sanketpurandare changed the base branch from sanketpurandare/stack/18 to main May 22, 2026 19:02
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/16 branch from 0fb48ed to b243510 Compare May 22, 2026 19:03
@sanketpurandare
sanketpurandare changed the base branch from main to sanketpurandare/stack/18 May 22, 2026 19:03
@sanketpurandare
sanketpurandare marked this pull request as ready for review May 22, 2026 19:03
@aditvenk
aditvenk requested a review from ydwu4 June 22, 2026 18:28
@sanketpurandare
sanketpurandare marked this pull request as draft June 22, 2026 18:30
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/16 branch from 1734678 to 01f60ba Compare June 22, 2026 18:31
@sanketpurandare
sanketpurandare marked this pull request as ready for review June 22, 2026 18:31
Comment thread torchtitan/models/common/rope.py Resolved
@sanketpurandare
sanketpurandare marked this pull request as draft June 23, 2026 04:18
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/16 branch from 01f60ba to c9c815c Compare June 23, 2026 04:18
@sanketpurandare sanketpurandare changed the title [graph_trainer] Support hinted symbolic input dims in tracing [graph_trainer] Support marked symbolic input dims in tracing Jun 23, 2026
@sanketpurandare
sanketpurandare marked this pull request as ready for review June 23, 2026 04:18
@sanketpurandare
sanketpurandare marked this pull request as draft June 23, 2026 04:47
Comment thread torchtitan/components/loss.py Resolved
}


def _can_guard_true(expr) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: why not just use guard_or_false?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the _can_guard_true() wrapper and used guard_or_false directly.

return fwd_bwd_step


def _materialize_grad_for_param_layout(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is there a test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Existing coverage is TestMinimalFXTracerDynamicShapes.test_materialize_grad_for_param_layout_restores_param_strides.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The test checks that a dense grad with a different stride is materialized to the parameter stride and that the helper is a no-op when the grad already satisfies the parameter layout contract.

@sanketpurandare

Copy link
Copy Markdown
Contributor Author

There are some coupling between tracer and EP_overlap passes...

This needs to be called out in readme.md / claude.md ... together with clear instruction on how to use ep overlap, and document it's composability and limitations.

@SherlockNoMad Added an EP-overlap section to the GraphTrainer README and a shorter developer
note in the graph_trainer guidance. The docs now make the intended coupling explicit: EP-overlap graph chunking has a trace-input preparer that marks token-grid dims before minimal_fx_tracer fakeifies inputs, and the graph passes consume those symbolic dims. The docs also list the supported flags, supported chunk_dim/module_fqn combinations, and current composability
limits.

# D2H scalar reads (token dispatcher split sizes). Each
# recomputation triggers a CUDA synchronize; saving the tiny
# CPU scalar avoids re-executing the counts all-to-all chain.
torch.ops.aten._local_scalar_dense.default,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

where is this op called?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not answered

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But anyways, now that Simon has already added routed input shape to be the output size of repeat_interleave and torch.arange I no longer need it so removing it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also FYI, when you use to_list() on a integer tensor these ops are generated or when you create a cpu scalar from gpu scalar

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@pianpwk could you help review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as of recently, we already assert this chunk divisibility in init time right ? otherwise checks look fine to me

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

init time asserts are fine for me they don't get traced

Comment thread torchtitan/models/common/rope.py Resolved

@wwwjn wwwjn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stamp to unblock

Comment thread torchtitan/models/common/rope.py Resolved
# D2H scalar reads (token dispatcher split sizes). Each
# recomputation triggers a CUDA synchronize; saving the tiny
# CPU scalar avoids re-executing the counts all-to-all chain.
torch.ops.aten._local_scalar_dense.default,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not answered

Comment thread torchtitan/models/common/rope.py Resolved
Comment thread torchtitan/components/loss.py Resolved
Comment thread torchtitan/models/common/rope.py Resolved
Comment thread torchtitan/models/common/rope.py Resolved
Comment thread torchtitan/models/common/rope.py Resolved

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please address remaining comments

if positions is None:
# No explicit positions: use the prefix cache and broadcast it over batch.
rope_cache = rope_cache[0:seqlen]
assert rope_cache.shape == (seqlen, cache_width)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please move to comment, not delete


# Concrete/provable singleton positions can use the cheaper prefix-shaped
# view path. If singleton-ness is symbolic, fall through to gather below.
if guard_or_false(positions.size(0) == 1):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This branch was removed, but added back in #3750 due to not compatible with vllm during inference

Add a TODO for @pianpwk to remove it

Comment thread torchtitan/components/loss.py Outdated
# writes, which use one chunk length for each write offset.
def _chunk_local(t):
return tuple(c.contiguous() for c in torch.chunk(t, num_chunks, dim=1))
return ChunkedCELoss._split_equal_chunks(t, num_chunks)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

using a static method inside an inline function sounds strange -- can we also inline _split_equal_chunks?

Add the graph_trainer tracing prerequisites needed by later EP chunking work without introducing the chunking passes themselves. minimal_fx_tracer now accepts mark_unbacked and mark_dynamic metadata on plain tensor inputs, preserves Dynamo dynamic-range annotations during fakeification, and keeps wrapper-subclass inputs rejected so DTensor-style layouts do not silently lose their metadata.

Make RoPE singleton-position handling symbolic-shape friendly without adding tracer-specific runtime checks, make no-op cleanup passes guard symbolic comparisons, and materialize traced gradients to the parameter layout before assigning param.grad.

Move AutoParallel CI checks after the graph_trainer checks in the default and H100 workflows so a stale AutoParallel package cannot hide the graph_trainer signal from this stack.

Test Plan:\n- conda run -n pt-dev pytest -q torchtitan/experiments/graph_trainer/tests/test_trace_module.py -k 'fakeify_input_copies_only_shape_annotations or mark_dynamic_min_max_preserves_range or mark_dynamic_batch_and_seq_dims_with_rope or mark_unbacked_positions_batch_dim_with_rope or rope_symbolic_positions_compile_fullgraph or materialize_grad_for_param_layout_restores_param_strides or nested_wrapper_subclass_marked_inner_rejected or mark_dynamic_wrapper_subclass_rejected or dtensor_mark_unbacked_rejected'\n- conda run -n pt-dev python -m pytest -q tests/unit_tests/test_loss.py::TestChunkedCELoss::test_equal_split_chunks_match_torch_chunk_for_supported_shapes tests/unit_tests/test_loss.py::TestChunkedCELoss::test_equal_split_chunks_backward_matches_torch_chunk tests/unit_tests/test_loss.py::TestChunkedCELoss::test_symbolic_seq_len_traces_chunking tests/unit_tests/test_loss.py::TestChunkedCELoss::test_rejects_non_divisible_sequence_length tests/unit_tests/test_loss.py::TestChunkedCELoss::test_single_token_chunks_match_torch_chunk_reference torchtitan/experiments/graph_trainer/tests/test_chunked_loss.py::TestChunkedCELossWithParamGrads::test_bitwise_equal_with_chunked_celoss

stack-info: PR: #3362, branch: sanketpurandare/stack/16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants