Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions kernels/norm/layernorm_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,8 @@ def block_reduce_max(val):
lane = tid % WARP_SIZE
wave = tid // WARP_SIZE
w = wave_reduce_max(val)
# Other waves may still be reading the previous block reduction's result from s_sum[0].
gpu.barrier()

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.

This PR adds a gpu.barrier() to block_reduce_max in all four quant builders. It fixes the race caused by reusing the same LDS slot for the sum and max reductions.
However, this PR is based on the main branch as of September 29. Commit 6487994 in #848 has already fixed the same issue, and the current main already contains the following code in rmsnorm_kernel.py (lines 1133–1140):

# s_red still holds the sum reduction's result, which every wave
# broadcast-reads on exit. Only this barrier orders that read
# against the store below; without it a late wave reads this max.
gpu.barrier()
w = wave_reduce_max(val)
if lane == 0:
    fx.memref_store(w, s_red, wave)
gpu.barrier()

I used git merge-tree to simulate the merge. This PR merges cleanly with main, and the redundant barrier could be merged into main without noticing. Therefore, I recommend removing one of duplicative gpu.barrier().

if lane == 0:
fx.memref_store(w, s_sum, wave)
gpu.barrier()
Expand Down Expand Up @@ -1226,6 +1228,8 @@ def block_reduce_max(val):
lane = tid % WARP_SIZE
wave = tid // WARP_SIZE
w = wave_reduce_max(val)
# Other waves may still be reading the previous block reduction's result from s_sum[0].
gpu.barrier()
if lane == 0:
fx.memref_store(w, s_sum, wave)
gpu.barrier()
Expand Down
4 changes: 4 additions & 0 deletions kernels/norm/rmsnorm_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ def block_reduce_max(val):
wave = tid // WARP_SIZE

w = wave_reduce_max(val)
# Other waves may still be reading the previous block reduction's result from s_red[0].
gpu.barrier()
if lane == 0:
fx.memref_store(w, s_red, wave)
gpu.barrier()
Expand Down Expand Up @@ -1211,6 +1213,8 @@ def block_reduce_max(val):
wave = tid // WARP_SIZE

w = wave_reduce_max(val)
# Other waves may still be reading the previous block reduction's result from s_red[0].
gpu.barrier()
if lane == 0:
fx.memref_store(w, s_red, wave)
gpu.barrier()
Expand Down
Loading