Skip to content

Enable profiler schedule to early-exit on problems - #180698

Closed
scotts wants to merge 21 commits into
pytorch:mainfrom
scotts:profiler_scheduler_early_stop
Closed

scotts wants to merge 21 commits into
pytorch:mainfrom
scotts:profiler_scheduler_early_stop

Conversation

@scotts

@scotts scotts commented Apr 17, 2026 •

Copy link
Copy Markdown
Contributor

If Kineto, in the C++ layer, hit errors that caused the Kineto side to exit profiling early, the PyTorch side in Python never learned of this. In particular, the torch.profiler.schedule object would still step through its entire schedule, never having learned that Kineto tried to exit early. This is particularly bad if Kineto wanted to exit early during warmup, and the schedule still tries to go through more warmup and then a bunch of active phases.

This PR checks the newly exposed (pytorch/kineto#1367) isStopped() API in Kineto during the torch.profiler.schedule state transitions. If it has been stopped, we enter a new schedule state: DEVICE_STOPPED.

Earlier implementations avoided adding a new state to the scheduling state machine, but they ran into the problem that the intended profiling behavior would no longer line up with the schedule the user specified. That is, we tried effectively fast-forwarding to NONE or RECORD_AND_SAVE. But if the user has a repeating schedule, we'd actually then end up with more profiling.

Now, if torch.autogra._is_kineto_stopped() is true, we'll enter the state DEVICE_STOPPED. Transitioning into DEVICE_STOPPED shuts down profiling. We stay in that state until the schedule would ordinarily go into either NONE or WARMUP. This behavior ensures:

  • Users get a partial trace--which is particularly useful since there was enough of a problem during tracing to shut it down.
  • Their specified schedule behaves as requested.
  • We still appropriately shut down profiling when Kineto has told us cannot continue.

@pytorch-bot

pytorch-bot Bot commented Apr 17, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 9c74855 with merge base 4e3940c (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 commented Apr 17, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@scotts scotts added topic: bug fixes topic category and removed topic: bug fixes topic category labels Apr 17, 2026
@pytorch-bot

pytorch-bot Bot commented Apr 17, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@scotts scotts added the release notes: profiler release notes category label Apr 17, 2026
@scotts
scotts marked this pull request as ready for review April 20, 2026 13:50
@scotts
scotts requested review from albanD and soulitzer as code owners April 20, 2026 13:50
Comment thread torch/profiler/profiler.py Resolved
Comment thread torch/profiler/profiler.py Resolved
Comment thread torch/profiler/profiler.py Outdated
ProfilerAction.RECORD_AND_SAVE,
)
and self.current_action != ProfilerAction.NONE
and torch.autograd._is_stopped()

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: imo _is_kineto_stopped or a similar name would make it more clear that this is a kineto-only concern.

Related q: what happens if Kineto is available but the user only requested CPU activities?

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.

On _is_kineto_stopped: we're not consistent with using the _kineto prefix on calls down into it. We do it for _kineto_step(), but not for _add_metadata_json() and _toggle_collection_dynamic(). I think it's probably better to use _is_kineto_stopped(), but I want to explore the code more. We should probably be more consistent here; I don't think this layer is clear on what should be known where.

On only requesting CPU activities: I think you're right that the following sequence would be a problem in the current implementation:

  1. User requests a trace with GPU. (CPU could be involved, but not needed.)
  2. Kineto hits error, sets to stop the trace.
  3. PyTorch exits schedule early.
  4. User requests a new, CPU only trace.
  5. Schedule would erroneously not work.

So I think we'll need to do some check on if we're collecting from GPUs. But I don't see us doing that kind of check yet, so I'll need to investigate 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.

@ryanzhang22, agreed on the name, updated. I think the boundary between the PyTorch profiler and Kineto could probably use a refactor. At the least, I'm not sure what the abstraction layers are or even should be. I think that ideally we would have a layer in PyTorch that does not know "kineto" as a thing, it just knows there is a layer below that does device profiling. But I'm not sure where that layer should be. My current change, I think, is going in the wrong direction for that, but I also think it's the best thing to do until we can rethink how to abstract these layers.

@scotts
scotts force-pushed the profiler_scheduler_early_stop branch from 84e3b60 to c8f84c2 Compare April 24, 2026 20:45
Comment thread torch/profiler/profiler.py Resolved
Comment thread torch/profiler/profiler.py Outdated
# stopping us (e.g. RECORD_AND_SAVE -> NONE), respect that rather
# than entering DEVICE_STOPPED.
if prev_action == ProfilerAction.DEVICE_STOPPED:
if self.current_action not in (

@ryanzhang22 ryanzhang22 Apr 29, 2026 •

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 think this could be a problem if warmup=0? If we have repeats it looks like the action would stay in DEVICE_STOPPED forever.

We might want to release one step after the raw schedule reaches RECORD_AND_SAVE but that would require a third action variable to track the scheduled action before we overrode it in the last iteration.

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.

@ryanzhang22, yes, good call. We need to explicitly detect the cycle boundaries, which we now do now. And we do indeed another internal state variable to track what the scheduler actually told us (self._prev_schedule_action).

Added warmup=0 and wait=0 tests as well.

Comment thread torch/profiler/profiler.py Outdated
# than entering DEVICE_STOPPED.
if prev_action == ProfilerAction.DEVICE_STOPPED:
at_cycle_boundary = (
self.current_action in (ProfilerAction.WARMUP, ProfilerAction.NONE)

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 think we want to remove ProfilerAction.WARMUP here, otherwise we'd get back into the 'WARMUP -> DEVICE_STOPPED -> WARMUP` loop in consecutive iterations (on the same profiling cycle).

prev_schedule_action == ProfilerAction.RECORD_AND_SAVE is probably a good enough check, though self.current_action == ProfilerAction.NONE does help with weird schedules that don't use ProfilerAction.RECORD_AND_SAVE for some reason.

Comment thread torch/profiler/profiler.py Outdated
in (
ProfilerAction.WARMUP,
ProfilerAction.RECORD,
ProfilerAction.RECORD_AND_SAVE,

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.

When RECORD_AND_SAVE transitions to any other state (except None) we reset Kineto, so we can probably don't need to force RECORD_AND_SAVE to transition to DEVICE_STOPPED. Otherwise the cycle after would never be able to exit DEVICE_STOPPED.

Comment thread torch/profiler/profiler.py Outdated
# current_action to DEVICE_STOPPED; if a user calls stop() then
# start() again, that stale DS would no-op the (NONE, DS)
# transition and leave the profiler running with no Kineto session.
schedule_action = self.schedule(self.step_num)

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: I think a more intuitive way to achieve this would be to reset self.current_action to self._prev_schedule_action in stop(). That way we don't need to call schedule twice.

@scotts

scotts commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label May 1, 2026
@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

Alokksinha00 pushed a commit to Alokksinha00/pytorch that referenced this pull request May 15, 2026
If Kineto, in the C++ layer, hit errors that caused the Kineto side to exit profiling early, the PyTorch side in Python never learned of this. In particular, the `torch.profiler.schedule` object would still step through its entire schedule, never having learned that Kineto tried to exit early. This is particularly bad if Kineto wanted to exit early during warmup, and the schedule still tries to go through more warmup and then a bunch of active phases.

This PR checks the newly exposed (pytorch/kineto#1367) `isStopped()` API in Kineto during the `torch.profiler.schedule` state transitions. If it has been stopped, we enter a new schedule state: `DEVICE_STOPPED`.

Earlier implementations avoided adding a new state to the scheduling state machine, but they ran into the problem that the intended profiling behavior would no longer line up with the schedule the user specified. That is, we tried effectively fast-forwarding to `NONE` or `RECORD_AND_SAVE`. But if the user has a repeating schedule, we'd actually then end up with _more_ profiling.

Now, if `torch.autogra._is_kineto_stopped()` is true, we'll enter the state `DEVICE_STOPPED`. Transitioning _into_ `DEVICE_STOPPED` shuts down profiling. We stay in that state until the schedule would ordinarily go into either `NONE` or `WARMUP`. This behavior ensures:

* Users get a partial trace--which is particularly useful since there was enough of a problem during tracing to shut it down.
* Their specified schedule behaves as requested.
* We still appropriately shut down profiling when Kineto has told us cannot continue.
Pull Request resolved: pytorch#180698
Approved by: https://github.com/ryanzhang22
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
If Kineto, in the C++ layer, hit errors that caused the Kineto side to exit profiling early, the PyTorch side in Python never learned of this. In particular, the `torch.profiler.schedule` object would still step through its entire schedule, never having learned that Kineto tried to exit early. This is particularly bad if Kineto wanted to exit early during warmup, and the schedule still tries to go through more warmup and then a bunch of active phases.

This PR checks the newly exposed (pytorch/kineto#1367) `isStopped()` API in Kineto during the `torch.profiler.schedule` state transitions. If it has been stopped, we enter a new schedule state: `DEVICE_STOPPED`.

Earlier implementations avoided adding a new state to the scheduling state machine, but they ran into the problem that the intended profiling behavior would no longer line up with the schedule the user specified. That is, we tried effectively fast-forwarding to `NONE` or `RECORD_AND_SAVE`. But if the user has a repeating schedule, we'd actually then end up with _more_ profiling.

Now, if `torch.autogra._is_kineto_stopped()` is true, we'll enter the state `DEVICE_STOPPED`. Transitioning _into_ `DEVICE_STOPPED` shuts down profiling. We stay in that state until the schedule would ordinarily go into either `NONE` or `WARMUP`. This behavior ensures:

* Users get a partial trace--which is particularly useful since there was enough of a problem during tracing to shut it down.
* Their specified schedule behaves as requested.
* We still appropriately shut down profiling when Kineto has told us cannot continue.
Pull Request resolved: pytorch#180698
Approved by: https://github.com/ryanzhang22
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
If Kineto, in the C++ layer, hit errors that caused the Kineto side to exit profiling early, the PyTorch side in Python never learned of this. In particular, the `torch.profiler.schedule` object would still step through its entire schedule, never having learned that Kineto tried to exit early. This is particularly bad if Kineto wanted to exit early during warmup, and the schedule still tries to go through more warmup and then a bunch of active phases.

This PR checks the newly exposed (pytorch/kineto#1367) `isStopped()` API in Kineto during the `torch.profiler.schedule` state transitions. If it has been stopped, we enter a new schedule state: `DEVICE_STOPPED`.

Earlier implementations avoided adding a new state to the scheduling state machine, but they ran into the problem that the intended profiling behavior would no longer line up with the schedule the user specified. That is, we tried effectively fast-forwarding to `NONE` or `RECORD_AND_SAVE`. But if the user has a repeating schedule, we'd actually then end up with _more_ profiling.

Now, if `torch.autogra._is_kineto_stopped()` is true, we'll enter the state `DEVICE_STOPPED`. Transitioning _into_ `DEVICE_STOPPED` shuts down profiling. We stay in that state until the schedule would ordinarily go into either `NONE` or `WARMUP`. This behavior ensures:

* Users get a partial trace--which is particularly useful since there was enough of a problem during tracing to shut it down.
* Their specified schedule behaves as requested.
* We still appropriately shut down profiling when Kineto has told us cannot continue.
Pull Request resolved: pytorch#180698
Approved by: https://github.com/ryanzhang22
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
If Kineto, in the C++ layer, hit errors that caused the Kineto side to exit profiling early, the PyTorch side in Python never learned of this. In particular, the `torch.profiler.schedule` object would still step through its entire schedule, never having learned that Kineto tried to exit early. This is particularly bad if Kineto wanted to exit early during warmup, and the schedule still tries to go through more warmup and then a bunch of active phases.

This PR checks the newly exposed (pytorch/kineto#1367) `isStopped()` API in Kineto during the `torch.profiler.schedule` state transitions. If it has been stopped, we enter a new schedule state: `DEVICE_STOPPED`.

Earlier implementations avoided adding a new state to the scheduling state machine, but they ran into the problem that the intended profiling behavior would no longer line up with the schedule the user specified. That is, we tried effectively fast-forwarding to `NONE` or `RECORD_AND_SAVE`. But if the user has a repeating schedule, we'd actually then end up with _more_ profiling.

Now, if `torch.autogra._is_kineto_stopped()` is true, we'll enter the state `DEVICE_STOPPED`. Transitioning _into_ `DEVICE_STOPPED` shuts down profiling. We stay in that state until the schedule would ordinarily go into either `NONE` or `WARMUP`. This behavior ensures:

* Users get a partial trace--which is particularly useful since there was enough of a problem during tracing to shut it down.
* Their specified schedule behaves as requested.
* We still appropriately shut down profiling when Kineto has told us cannot continue.
Pull Request resolved: pytorch#180698
Approved by: https://github.com/ryanzhang22
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 release notes: profiler release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants