Optimizing writes in nobag inference kernel#157
Open
aryaman-gupta wants to merge 7 commits into
Open
Conversation
When D is an exact multiple of (kWarpSize * kOutputsPerThread), skip the half2 scale/bias header at row[0] via a +1 read offset (in scalar_t units) and drop the D_padding shift on output_d. This eliminates the mostly-empty tail iteration that the original loop runs, which on AMD wave-64 for D=256 wastes 63/64 lanes. The branch is hoisted out of the per-iter loop nest — the compiler hoists the predicate but not the branch itself, which would otherwise add ~3-5% per-iter overhead on non-triggering D values.
avbokovoy
approved these changes
May 20, 2026
avbokovoy
left a comment
There was a problem hiding this comment.
Overall LGTM with potential for further improvements
| {% else %} | ||
| using scalar_t = {{ emb_weight_type.cpp_type_name }}; | ||
| {% if emb_weight_type.primitive_type == "INT" and is_rocm %} | ||
| if (D % (kWarpSize * kOutputsPerThread) == 0 && D_padding > 0) { |
There was a problem hiding this comment.
Ain't always D_padding > 0 in case of quantization to INT?
| auto thread_local_max = std::numeric_limits<float>::lowest(); | ||
| float2 qparams; | ||
| // Pass 1: min/max scan | ||
| for (uint32_t j = 0; j < opt_iters; ++j) { |
There was a problem hiding this comment.
Loops with opt_iters worth be investigated as a candidate for manual loop unrolling
Author
There was a problem hiding this comment.
I did investigate that. I created a dynamic if-else ladder for different embedding dimensions (256, 512, 768 etc.) and manually unrolling the loops within each case. This negatively impacted the performance, though
… packedMode effects in nobag kernel
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.
Summary
Adds a shifted-index store fast path for the INT-weight nobag inference forward kernel (
{INT8,INT4,INT2}_split_embedding_nobag_codegen_forward_unweighted_kernel_small_L) on ROCm.When
D % (kWarpSize * kOutputsPerThread) == 0andD_padding > 0, the original store loop wastes one full iteration: the lastjiteration is fully masked out by theoutput_d < Dguard because the per-row header offset (-D_padding) shifts every lane belowD. The fast path iteratesD / (kWarpSize * kOutputsPerThread)times instead of(MaxNum128BRows + 1) / 2, drops the bounds check, and reads weights at[j * kWarpSize + threadIdx.x + kHeaderScalarOffset]to absorb the header offset on the load side.The runtime check is hoisted outside the per-
input_row_idx/ per-iloops to avoid ~3-5% per-iter branch overhead on non-triggering D values. Bagged path is unchanged.Scope
is_rocmJinja gate); CUDA path is byte-identical to upstream.Validation