Skip to content

Support custom callback functions in schedule - #162016

Closed
H-Huang wants to merge 7 commits into
gh/H-Huang/214/basefrom
gh/H-Huang/214/head
Closed

H-Huang wants to merge 7 commits into
gh/H-Huang/214/basefrom
gh/H-Huang/214/head

Conversation

@H-Huang

@H-Huang H-Huang commented Sep 2, 2025 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

This is going to be used in pytorch/torchtitan#1682

Add a register_custom_function to the _PipelineScheduleRuntime which allows users to implement any custom function to replace the runtime operation dynamically.

The signature of the callback should look like:

class _CustomFunctionProtocol(Protocol):
    def __call__(self, action: _Action, ctx: _PipelineContext) -> None: ...

_PipelineContext contains a reference to the schedule which is executing the operations.

Testing

Added a test which adds custom methods for FORWARD and OVERLAP_F_B which are just the same implementations as those used in the default schedule runtime. Check that the schedule can still run, numerics are correct, and the callbacks are executed the correct number of times.

cc @awgu @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @pragupta @msaroufim @dcci @ezyang

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 2, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 83839cf with merge base 322091d (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot pytorch-bot Bot added the oncall: distributed Add this issue/PR to distributed oncall triage queue label Sep 2, 2025
H-Huang added a commit that referenced this pull request Sep 2, 2025
@H-Huang H-Huang added the release notes: distributed (pipeline) release notes category label Sep 2, 2025
[ghstack-poisoned]
H-Huang added a commit that referenced this pull request Sep 3, 2025
[ghstack-poisoned]
H-Huang added a commit that referenced this pull request Sep 3, 2025
[ghstack-poisoned]
H-Huang added a commit that referenced this pull request Sep 8, 2025
@H-Huang H-Huang added the module: pipelining Pipeline Parallelism label Sep 8, 2025
[ghstack-poisoned]
H-Huang added a commit that referenced this pull request Oct 7, 2025

base_schedule.register_custom_function(FORWARD, forward_callback)
base_schedule.register_custom_function(OVERLAP_F_B, overlap_callback)

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.

I'm confused by this test. We didn't update callback_calls in overlap_callback. Do we expect overlap_callback to be called or not? And if yes, what's the test for overlap_callback?

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.

It is because overlap_callback calls forward_callback in its implementation. To make this less confusing I will use two counters and test that both are being called.

[ghstack-poisoned]
H-Huang added a commit that referenced this pull request Oct 7, 2025
@H-Huang H-Huang added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 7, 2025

@fegin fegin 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.

LGTM


# Check if computation_type is already registered
if computation_type in self._comp_type_to_function_map:
logger.warning(

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.

Should we error out or warning? This seems to be an unexpected mistake from users.

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.

I'm thinking about whether users would want to change their custom function between steps, doesn't seem like this would be the case so maybe error-ing out is better. Let me think about it more

@H-Huang

H-Huang commented Oct 8, 2025

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: Command git -C /home/runner/work/pytorch/pytorch cherry-pick -x 02a533fc48944188cc3fdb1b37fa5376282506ea returned non-zero exit code 1

Auto-merging test/distributed/pipelining/test_schedule_multiproc.py
Auto-merging torch/distributed/pipelining/schedules.py
CONFLICT (content): Merge conflict in torch/distributed/pipelining/schedules.py
error: could not apply 02a533fc489... Support custom callback functions in schedule
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Details for Dev Infra team Raised by workflow job

[ghstack-poisoned]
H-Huang added a commit that referenced this pull request Oct 8, 2025
@H-Huang

H-Huang commented Oct 8, 2025

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

pytorchmergebot pushed a commit that referenced this pull request Oct 8, 2025
BE cleaning up dead code since we migrated the Multi-stage schedules to use schedule execution runtime

Pull Request resolved: #164962
Approved by: https://github.com/Skylion007
ghstack dependencies: #162016
pytorchmergebot pushed a commit that referenced this pull request Oct 10, 2025
Chao1Han pushed a commit to Chao1Han/pytorch that referenced this pull request Oct 21, 2025
This is going to be used in pytorch/torchtitan#1682

Add a `register_custom_function` to the `_PipelineScheduleRuntime` which allows users to implement any custom function to replace the runtime operation dynamically.

The signature of the callback should look like:

```python
class _CustomFunctionProtocol(Protocol):
    def __call__(self, action: _Action, ctx: _PipelineContext) -> None: ...
```

`_PipelineContext` contains a reference to the schedule which is executing the operations.

### Testing

Added a test which adds custom methods for `FORWARD` and `OVERLAP_F_B` which are just the same implementations as those used in the default schedule runtime. Check that the schedule can still run, numerics are correct, and the callbacks are executed the correct number of times.

Pull Request resolved: pytorch#162016
Approved by: https://github.com/fegin
Chao1Han pushed a commit to Chao1Han/pytorch that referenced this pull request Oct 21, 2025
BE cleaning up dead code since we migrated the Multi-stage schedules to use schedule execution runtime

Pull Request resolved: pytorch#164962
Approved by: https://github.com/Skylion007
ghstack dependencies: pytorch#162016
Chao1Han pushed a commit to Chao1Han/pytorch that referenced this pull request Oct 21, 2025
@github-actions
github-actions Bot deleted the gh/H-Huang/214/head branch November 8, 2025 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged module: pipelining Pipeline Parallelism oncall: distributed Add this issue/PR to distributed oncall triage queue release notes: distributed (pipeline) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants