[CUDA] Fix performance bug in DecoderMaskedMultiheadAttention for BeamSearch #17613
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.
Description
Minor change of moving a bounds checking
if
outside the some loopsMove out a global memory read outside an unrolled for loop. Reading in a loop is redundant as
ti
never changes in the loop. This seems to be a case for compiler optimization to move it outside the loop but the compiler doesn't seem to move it out and instead adds instructions for multiple redundant global memory readsIllustrative profiling from HugingFace GPT-2
Before instructions:
There are more such redundant reads than shown in the snippet as they are interleaved with other independently executing instructions based on compiler re-arrangement of instructions
Before metrics:
After instructions:
We have one load instead of the many above (not pasting for conciseness)
After metrics:
Commentary about metrics:
As you can see, the number of cycles have decreased and latency has decreased (about 8.x micro-seconds to 7.x micro-seconds) and the memory throughput has gone down because we reduce the global memory read(s) mani-fold. This bug didn't impact perf much beause the redundant reads mostly happen in parallel (once the loop is unrolled) and I think the kernel wasn't nearing the global memory bandwidth ceiling yet. In any case, the ultimate perf impact will vary from model to model (based on head size, number of layers)
Motivation and Context
Fix perf bug in decoder masked multihead attention