Repository navigation
Conversation
🔗 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 FailuresAs of commit 9c74855 with merge base 4e3940c ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
This PR needs a
|
| ProfilerAction.RECORD_AND_SAVE, | ||
| ) | ||
| and self.current_action != ProfilerAction.NONE | ||
| and torch.autograd._is_stopped() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
- User requests a trace with GPU. (CPU could be involved, but not needed.)
- Kineto hits error, sets to stop the trace.
- PyTorch exits schedule early.
- User requests a new, CPU only trace.
- 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.
There was a problem hiding this comment.
@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.
84e3b60 to
c8f84c2
Compare
| # 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 ( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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.
| # than entering DEVICE_STOPPED. | ||
| if prev_action == ProfilerAction.DEVICE_STOPPED: | ||
| at_cycle_boundary = ( | ||
| self.current_action in (ProfilerAction.WARMUP, ProfilerAction.NONE) |
There was a problem hiding this comment.
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.
| in ( | ||
| ProfilerAction.WARMUP, | ||
| ProfilerAction.RECORD, | ||
| ProfilerAction.RECORD_AND_SAVE, |
There was a problem hiding this comment.
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.
| # 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) |
There was a problem hiding this comment.
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.
|
@pytorchbot merge |
Merge startedYour 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 |
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
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
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
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
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.scheduleobject 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 thetorch.profiler.schedulestate 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
NONEorRECORD_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 stateDEVICE_STOPPED. Transitioning intoDEVICE_STOPPEDshuts down profiling. We stay in that state until the schedule would ordinarily go into eitherNONEorWARMUP. This behavior ensures: