Skip to content

Fix data race between two writes on a shared memory in soft_max_autoregressive_backward_kernel - #847

Open
ziqiaozhou wants to merge 1 commit into
karpathy:masterfrom
ziqiaozhou:fix-race
Open

Fix data race between two writes on a shared memory in soft_max_autoregressive_backward_kernel#847
ziqiaozhou wants to merge 1 commit into
karpathy:masterfrom
ziqiaozhou:fix-race

Conversation

@ziqiaozhou

Copy link
Copy Markdown

…ax_autoregressive_backward_kernel

There are three access sites to block_acc in softmax_autoregressive_backward_kernel:

A. Initialization write
B. Per-warp local sum write
C. Block-wide read of accumulated values

The indexing differs across these accesses. The initialization (A) is performed by threads in warp 0 and indexed by the thread’s lane ID. The per-warp writes (B) are indexed by the warp ID, while the read (C) is indexed again by thread ID.

Although there is a synchronization barrier between (B) and (C), there is no synchronization between (A) and (B). As a result, the relative ordering between initialization and per-warp writes is not guaranteed.

In particular, if warp 1 executes its write (B) before warp 0 completes initialization (A), warp 0 may overwrite a previously written local sum with zero. This creates a data race, and the subsequent block-wide read (C) may observe incorrect values.

Thus, it may cause wrong result due to data race.

…ax_autoregressive_backward_kernel

There are three locations in softmax_autoregressive_backward_kernel to access block_acc: 1. write to init; 2. write local sum per warp; 3. read local sum per block.

The first write is indexed by thread id inside warp 0, the second and future writes are indexed by wrap id.

In theory, if warp 1 starts before warp 0, warp 0 may set that element to zero between the warp-wide local sum write and the block-wide sum read.

Thus, it may cause wrong result due to data race.
@ziqiaozhou
ziqiaozhou marked this pull request as ready for review April 22, 2026 20:11
@ziqiaozhou ziqiaozhou changed the title Fix possible data race between two writes on a shared memory in softm… Fix data race between two writes on a shared memory in softm… Apr 22, 2026
@ziqiaozhou ziqiaozhou changed the title Fix data race between two writes on a shared memory in softm… Fix data race between two writes on a shared memory in soft_max_autoregressive_backward_kernel Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant