Skip to content

[DTensor] PQ search: fast path, cost cache, and reachability tests - #175313

Closed
wconstab wants to merge 1 commit into
gh/wconstab/536/basefrom
gh/wconstab/536/head
Closed

wconstab wants to merge 1 commit into
gh/wconstab/536/basefrom
gh/wconstab/536/head

Conversation

@wconstab

@wconstab wconstab commented Feb 19, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Add three optimizations to _find_lowest_cost_sharding and two
reachability tests that prove the PQ transition graph covers all valid
strategies.

The fast path checks whether the initial input placements already match
a strategy rule on every mesh dim, returning immediately without
constructing MeshTopoInfo or the PQ. The per-input cost cache avoids
redundant _compute_redistribute_cost calls by keying on destination
placement tuples (at most 64 unique keys per input on a 3D mesh).
_build_output_spec is extracted as a shared helper between the fast path
and PQ match block.

_collect_all_matches is a testing-only parameter that forces the PQ to
exhaustively explore the full transition graph, collecting every
shardable match into a set while still returning the optimal result.

Authored with Claude.

Add three optimizations to _find_lowest_cost_sharding and two
reachability tests that prove the PQ transition graph covers all valid
strategies.

The fast path checks whether the initial input placements already match
a strategy rule on every mesh dim, returning immediately without
constructing MeshTopoInfo or the PQ. The per-input cost cache avoids
redundant _compute_redistribute_cost calls by keying on destination
placement tuples (at most 64 unique keys per input on a 3D mesh).
_build_output_spec is extracted as a shared helper between the fast path
and PQ match block.

_collect_all_matches is a testing-only parameter that forces the PQ to
exhaustively explore the full transition graph, collecting every
shardable match into a set while still returning the optimal result.

Authored with Claude.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Feb 19, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/175313

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 3 Unrelated Failures

As of commit d2fd078 with merge base 003e05b (image):

NEW FAILURE - The following job has failed:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

BROKEN TRUNK - The following job failed but was present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

wconstab added a commit that referenced this pull request Feb 19, 2026
Add three optimizations to _find_lowest_cost_sharding and two
reachability tests that prove the PQ transition graph covers all valid
strategies.

The fast path checks whether the initial input placements already match
a strategy rule on every mesh dim, returning immediately without
constructing MeshTopoInfo or the PQ. The per-input cost cache avoids
redundant _compute_redistribute_cost calls by keying on destination
placement tuples (at most 64 unique keys per input on a 3D mesh).
_build_output_spec is extracted as a shared helper between the fast path
and PQ match block.

_collect_all_matches is a testing-only parameter that forces the PQ to
exhaustively explore the full transition graph, collecting every
shardable match into a set while still returning the optimal result.

Authored with Claude.

ghstack-source-id: 966a427
Pull Request resolved: #175313
mesh, selected_output_placements, num_outputs, output_metas
)
redistribute_costs = [
generate_redistribute_costs(OpStrategy([OpSpec(input_spec)]), arg_spec)

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.

shouldn't redistribute_costs be zeros? maybe calling this is OK though, it should be short-circuiting inside?

]
op_spec = OpSpec(
output_specs=output_spec,
input_specs=arg_specs,

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.

we should probably refactor a helper fn that tries to do shard prop without redistribute.

for now, we can make one that is specific to single-dim strategies, since that's all we need from here. later we might want to have a similar API that works for any op, and falls back to some other path for non-single-dim strategies

) -> DTensorSpec | tuple[DTensorSpec | None, ...]:
"""Build multi-dim output spec from per-mesh-dim output placements."""
if num_outputs == 1:
output_placements_tuple = tuple(out[0] for out in selected_output_placements)

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.

confusing why we have multiple outputs in num_outputs==1 case?

)
return tuple(multi_output_specs)
else:
return DTensorSpec(mesh, (Replicate(),) * mesh.ndim)

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.

what is the else case for?

[OpOverload, ArgsType, KwargsType], list[list[Placement | _ShardingPlaceholder]]
],
output_tensor_meta: TensorMeta | Sequence[TensorMeta | None] | None = None,
_collect_all_matches: set[tuple[tuple[Placement, ...], ...]] | None = None,

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.

docstring

@wconstab wconstab closed this Feb 20, 2026
@wconstab
wconstab deleted the gh/wconstab/536/head branch February 20, 2026 02:34
sandy-gags pushed a commit to sandy-gags/pytorch that referenced this pull request Mar 12, 2026
very WIP

ghstack-source-id: bbd81da
Pull Request resolved: pytorch/pytorch#169438

[DTensor] PQ search: fast path, cost cache, and reachability tests

Add three optimizations to _find_lowest_cost_sharding and two
reachability tests that prove the PQ transition graph covers all valid
strategies.

The fast path checks whether the initial input placements already match
a strategy rule on every mesh dim, returning immediately without
constructing MeshTopoInfo or the PQ. The per-input cost cache avoids
redundant _compute_redistribute_cost calls by keying on destination
placement tuples (at most 64 unique keys per input on a 3D mesh).
_build_output_spec is extracted as a shared helper between the fast path
and PQ match block.

_collect_all_matches is a testing-only parameter that forces the PQ to
exhaustively explore the full transition graph, collecting every
shardable match into a set while still returning the optimal result.

Authored with Claude.

ghstack-source-id: bbd81da
Pull Request resolved: pytorch/pytorch#175313
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant