Skip to content

[simplefsdp auto-bucketing] add ir node bucket helper function - #158097

Closed
ruisizhang123 wants to merge 7 commits into
gh/ruisizhang123/4/basefrom
gh/ruisizhang123/4/head
Closed

ruisizhang123 wants to merge 7 commits into
gh/ruisizhang123/4/basefrom
gh/ruisizhang123/4/head

Conversation

@ruisizhang123

@ruisizhang123 ruisizhang123 commented Jul 11, 2025 •

Copy link
Copy Markdown
Contributor

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 11, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit db832b0 with merge base 5dad5b4 (image):

NEW FAILURE - The following job has failed:

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

@pytorch-bot pytorch-bot Bot added ciflow/inductor module: inductor oncall: distributed Add this issue/PR to distributed oncall triage queue release notes: distributed (fsdp) release notes category labels Jul 11, 2025
@ruisizhang123 ruisizhang123 changed the title add ir node bucket helper function [simplefsdp auto-bucketing] add ir node bucket helper function Jul 11, 2025
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
…tion"

This pr is based on Diff D67292294 from yf225.

Major changes are:

- Change the function structure to be compatible with auto-bucketing
- Group bucketed nodes & dependencies with GroupedSchedulerNodes for easier reordering.

Stack from [ghstack](https://github.com/ezyang/ghstack) (oldest at bottom):

* #160282
* #158609
* #158321
* #158098
*  __->__ #158097
* #157572



cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy chenyang78 kadeng muchulee8 amjames chauhang aakhundov

[ghstack-poisoned]
ruisizhang123 added a commit that referenced this pull request Aug 11, 2025
…cket helper function"

This pr is based on Diff D67292294 from yf225.

Major changes are:

- Change the function structure to be compatible with auto-bucketing
- Group bucketed nodes & dependencies with GroupedSchedulerNodes for easier reordering.

Stack from [ghstack](https://github.com/ezyang/ghstack) (oldest at bottom):

* #160282
* #158609
* #158321
* #158098
*  __->__ #158097
* #157572



cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy chenyang78 kadeng muchulee8 amjames chauhang aakhundov

[ghstack-poisoned]
ruisizhang123 added a commit that referenced this pull request Aug 11, 2025
… with greedy algorithm"

## Greedy Algorithm Design

### Runtime estimation

estimation is done in  [__->__ #157572]. 

#### Communication estimation:

- Optimization: While realize communication data into realtensor gives us good estimation of a node, it could incur significant overhead in auto-bucketing.
- Calibration-based estimation: We sample 20 samples evenly in a range of [min(fwd_ag_tensor_list), 0.3* sum(fwd_ag_tensor_list)], and benchmark its communication time. This also applies to backward reduce scatter tensors.
- When a new IR comes, it will search the saved communication dictionary for the closest tensor size, and use the closest tensor runtime as its predicted runtime. 

#### Cache estimated runtime

Here, we add a cache to save pre-estimated results. Leverage a comm cache dictionary & comp cache dictionary to save pre-estimated inductor ir. If it meets a new IR node with the same key, it will skip the estimation for the new IR node.

- For comm, the key is communication type, input tensor size & output tensor size.
- For comp, the key is (i) generated triton code for BaseSchedulerNode/FusedSchedulerNode; (ii) Extern Kernel args

### Greedy Algorithm Implementation

Core idea: the greedy algorithm decide if the node will be bucketed with the previous one based on several criteria below. The reordering will by itself reorder with the previous computation.

bucketing is done in  [__->__ #158097]
reordering is done in  [__->__ #158098]

#### FWD Pass:

- (i) the bucketed AG communication could be overlapped by the previous computation;
- (ii) the bucketed AG copy-in/out memory doesn’t exceed peak memory; 
- (iii) bucketed AG communication size doesn’t exceed 0.3* sum(fwd_ag_tensor_list), such that the estimated AG communication time is always in the calibration bound.

#### BWD Pass:

- (i) the bucketed AG + RS communication could be overlapped by the previous computation; 
- (ii)  the bucketed AG+RS copy-in/out memory doesn’t exceed peak memory; 
- (iii) RS always have future compute to overlap it, such that its final exposed communication is small; 
- (iv) bucketed AG/RS communication size doesn’t exceed 0.3* sum(fwd_ag_tensor_list)/0.3* sum(bwd_rs_tensor_list), such that  the estimated AG/RS communication time is always in the calibration bound.





cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k pragupta voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy chenyang78 kadeng muchulee8 amjames chauhang aakhundov coconutruben

[ghstack-poisoned]
ruisizhang123 added a commit that referenced this pull request Aug 11, 2025
…orithm"

## Greedy Algorithm Design

### Runtime estimation

estimation is done in  [__->__ #157572]. 

#### Communication estimation:

- Optimization: While realize communication data into realtensor gives us good estimation of a node, it could incur significant overhead in auto-bucketing.
- Calibration-based estimation: We sample 20 samples evenly in a range of [min(fwd_ag_tensor_list), 0.3* sum(fwd_ag_tensor_list)], and benchmark its communication time. This also applies to backward reduce scatter tensors.
- When a new IR comes, it will search the saved communication dictionary for the closest tensor size, and use the closest tensor runtime as its predicted runtime. 

#### Cache estimated runtime

Here, we add a cache to save pre-estimated results. Leverage a comm cache dictionary & comp cache dictionary to save pre-estimated inductor ir. If it meets a new IR node with the same key, it will skip the estimation for the new IR node.

- For comm, the key is communication type, input tensor size & output tensor size.
- For comp, the key is (i) generated triton code for BaseSchedulerNode/FusedSchedulerNode; (ii) Extern Kernel args

### Greedy Algorithm Implementation

Core idea: the greedy algorithm decide if the node will be bucketed with the previous one based on several criteria below. The reordering will by itself reorder with the previous computation.

bucketing is done in  [__->__ #158097]
reordering is done in  [__->__ #158098]

#### FWD Pass:

- (i) the bucketed AG communication could be overlapped by the previous computation;
- (ii) the bucketed AG copy-in/out memory doesn’t exceed peak memory; 
- (iii) bucketed AG communication size doesn’t exceed 0.3* sum(fwd_ag_tensor_list), such that the estimated AG communication time is always in the calibration bound.

#### BWD Pass:

- (i) the bucketed AG + RS communication could be overlapped by the previous computation; 
- (ii)  the bucketed AG+RS copy-in/out memory doesn’t exceed peak memory; 
- (iii) RS always have future compute to overlap it, such that its final exposed communication is small; 
- (iv) bucketed AG/RS communication size doesn’t exceed 0.3* sum(fwd_ag_tensor_list)/0.3* sum(bwd_rs_tensor_list), such that  the estimated AG/RS communication time is always in the calibration bound.





cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k pragupta voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy chenyang78 kadeng muchulee8 amjames chauhang aakhundov coconutruben

[ghstack-poisoned]
[ghstack-poisoned]
) -> torch.Tensor:
chunk_cat_out = torch._chunk_cat(tensors, dim, num_chunks)
return chunk_cat_out

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 sure why we have this function lol

"bucketing algorithm requires torch._inductor.config.allow_buffer_reuse to be False"
)
self.nodes = bucket.bucket_fsdp_all_gather_concat_on_scheduler_ir(
self, self.nodes, self.name_to_buf, self.name_to_fused_node, [[]]

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.

Bucket plan is empty here, so this is a no op!

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 actually no-op, this does the default plan

snodes: list["scheduler.BaseSchedulerNode"],
name_to_buf: Dict[str, "scheduler.SchedulerBuffer"],
name_to_fused_node: Dict[str, "scheduler.BaseSchedulerNode"],
all_gather_bucket_plan: list[list["scheduler.BaseSchedulerNode"]],

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 exactly sure what the exact format of all_gather_bucket_plan is. Maybe it is a list of lists of all gather nodes to bucket together

)


def bucket_fsdp_all_gather_concat_on_scheduler_ir(

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.

It's a little irritating that we need two structurally very similar functions, although I guess there probably is a good reason they can't be deduped.

@github-actions

github-actions Bot commented Nov 8, 2025

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Nov 8, 2025
Khanaksahu pushed a commit to Khanaksahu/pytorch that referenced this pull request Nov 17, 2025
@github-actions github-actions Bot closed this Dec 8, 2025
@github-actions
github-actions Bot deleted the gh/ruisizhang123/4/head branch January 8, 2026 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor module: inductor oncall: distributed Add this issue/PR to distributed oncall triage queue release notes: distributed (fsdp) release notes category Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants