Avoid cudaStreamSync at the end of Forward/Backward#9470
Merged
SherlockNoMad merged 2 commits intomasterfrom Oct 21, 2021
Merged
Avoid cudaStreamSync at the end of Forward/Backward#9470SherlockNoMad merged 2 commits intomasterfrom
SherlockNoMad merged 2 commits intomasterfrom
Conversation
weixingzhang
approved these changes
Oct 21, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As ORTModule should match the behavior of nn.module, we don't need to explicitly introduce a cudaStreamSync at the end of each subgraph execution.
Addition cudaStreamSync at the end of forward
As shown in the profiling result below, ORTModule run has an extra “cudaStreamSync” call at the end of forward section. This was introduced as the finalizing step for InferenceSession::PartialRun(). This behavior is copied from original InferenceSession::Run() code when we implemented PartialRun executor.
However, PyTorch would automatically introduce “cudaStreamSync” if following CPU computation has dependency on a GPU tensor. In another word, ORT doesn’t need to introduce this call explicitly.
Warmup Patterns after cudaStreamSync
As we zoom in to the time segment following cudaStreamSync call, we can see a time window lasting ~4ms that GPU is barely utilized. As the tasks in compute stream are depleted with cudaStreamSync call, CPU needs to refill the compute stream from scratch. This resulted in the GPU starvation, as CPU is not able to launch the kernels fast enough, worsen by the fact that the scheduled kernels are short to complete (<10us). The starving situation is eventually relieved when a larger kernel kicks in, taking up >100 us, giving time for CPU to catch up with the scheduling work.