Metadata-Version: 2.4
Name: syncsan
Version: 0.10.5
Summary: Finds data races in GPU megakernels and shows what each one does to your output.
Author: Shreshth Rajan
License: Apache-2.0
Project-URL: Homepage, https://github.com/ShreshthRajan/syncsan
Project-URL: Issues, https://github.com/ShreshthRajan/syncsan/issues
Keywords: cuda,gpu,race detection,megakernel,ptx,memory model,nvbit,sanitizer
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: GPU :: NVIDIA CUDA
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: C++
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Debuggers
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: cmake>=3.29
Dynamic: license-file

# syncsan

syncsan finds data races in GPU megakernels. It runs your CUDA program unchanged under
[NVBit](https://github.com/NVlabs/NVBit) and reports every pair of memory accesses that no barrier, fence,
release/acquire or async-copy wait puts in order. With `SYNCSAN_ADVERSARIAL=1` it also hands each racy load the stale
value the PTX memory model allows, so you can see what the race does to your output.

Megakernels need this more than most code. They run a whole model in one launch and pass activations between SMs
through counters in global memory. A missing fence in one of those handoffs rarely fails on today's GPUs, so it
survives testing.

## Quick start

You need Linux, an NVIDIA GPU of compute capability 7.5 or newer (T4, RTX 20 series and later), and CUDA 12. It runs
on Colab's free T4: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ShreshthRajan/syncsan/blob/main/examples/colab.ipynb)

```
pip install syncsan
syncsan build --arch 90          # once; your GPU's compute capability: 75 T4, 80 A100, 90 H100, 100 B200, 120 RTX 50xx
syncsan run -- python generate.py
```

To see it work before pointing it at your own code, try [examples/handoff.cu](examples/handoff.cu). It is the most
common megakernel bug in 30 lines: one block writes data, fences and bumps a counter, and another block spins on the
counter and reads the data with no fence after the spin.

```
$ nvcc -arch=sm_90 -lineinfo -o handoff examples/handoff.cu
$ ./handoff
sum = 32896 (expected 32896): correct
$ syncsan run -- ./handoff
syncsan: RACE (write then read) at 0x7ef8ffe00300
  block 1 warp 2: STG.E desc[UR6][R2.64], R5 ;  [handoff(unsigned int*, int*, int*)+0x1e0 handoff.cu:15]
  block 0 warp 2: LDG.E R2, desc[UR6][R2.64] ;  [handoff(unsigned int*, int*, int*)+0x120 handoff.cu:26]
sum = 32896 (expected 32896): correct
$ SYNCSAN_ADVERSARIAL=1 syncsan run -- ./handoff
sum = 0 (expected 32896): WRONG
```

The kernel gives the right sum on the GPU, syncsan points at the write on line 15 and the read on line 26, and
adversarial mode shows the wrong sum the memory model allows. Rebuild with `-DFIXED`, which adds the fence after the
spin, and syncsan reports nothing. Build your own kernels with `-lineinfo` so the reports name source lines.

`--kernel NAME` checks only the kernels whose name contains `NAME`, `--exitcode 1` makes a race fail your CI
([example workflow](examples/github-actions.yml)), and `--sample k` checks one in `k` plain accesses when the full
check is too slow. `syncsan run --help` lists the rest.

## What it has found

The clearest case is SuperLU_DIST. A commit that fixed a dropped fence in its GPU triangular solve restored the
consumer's acquire but left out the producer's release. syncsan flagged the producer's decrement, and in adversarial
mode the solve came back with an error of 0.7. On the fixed commit the error is 6e-15. The maintainers confirmed it and fixed it the same day
([xiaoyeli/superlu_dist#261](https://github.com/xiaoyeli/superlu_dist/issues/261)).

In Hazy Research's [Llama-1B megakernel](https://github.com/HazyResearch/Megakernels), activations move between SMs
through a TMA store, a relaxed `atomicAdd` on a counter and a `volatile` spin, with no gpu-scope fence. syncsan
reports 25 racing pairs, and in adversarial mode the model stops making sense (H100, 8 tokens):

| Build | Mode | Output |
|---|---|---|
| original | native | "Why did the cookie go to the" |
| original | adversarial | "iteriteriter abstractجرineronde" |
| original | control | "Why did the cookie go to the" |
| release/acquire counters | adversarial | "Why did the cookie go to the" |

The control run redirects the same loads but hands them their current values, so the redirection itself is not what
breaks the output. This race also fails on real hardware. When `silu_out` is filled with NaN before each step, the
original megakernel reads the NaN in all 1,000 generations and the fixed one in none. Making the counters
release/acquire fixes it at a 9% latency cost
([HazyResearch/Megakernels#16](https://github.com/HazyResearch/Megakernels/issues/16)).

The other finds:

- [AutoMegaKernel](https://github.com/RightNow-AI/AutoMegaKernel) waits with `atomicAdd(c, 0)`, which a comment calls
  an acquire but PTX treats as relaxed. In adversarial mode its own `--verify` check fails
  ([RightNow-AI/AutoMegaKernel#6](https://github.com/RightNow-AI/AutoMegaKernel/issues/6)).
- [megakernel-zoo](https://github.com/dangxingyu/megakernel-zoo) and [Lucebox](https://github.com/Luce-Org/lucebox)
  release correctly but spin on a `volatile` load with no fence after it. In both, the adversarial output is garbage
  and a fence after the spin fixes it ([dangxingyu/megakernel-zoo#3](https://github.com/dangxingyu/megakernel-zoo/issues/3),
  [Luce-Org/lucebox#1075](https://github.com/Luce-Org/lucebox/issues/1075)).
- [Mirage MPK](https://github.com/mirage-project/mirage)'s argmax reduction has no barrier between warp 0 reading the
  per-warp maxima and the other warps writing the next row's
  ([mirage-project/mirage#843](https://github.com/mirage-project/mirage/issues/843)).
- [Magni VM](https://github.com/Bobliuuu/mvm) had an admission race that 3,200 passing test runs never hit
  ([Bobliuuu/mvm#97](https://github.com/Bobliuuu/mvm/pull/97)).
- On CUTLASS 3.6.0, syncsan finds the known Stream-K race from issue #2027, and it finds nothing in the fixed 3.7.0.

Each one has a write-up in [docs/findings](docs/findings/) and a script in [reproduce](reproduce/) that reruns it on a
rented GPU. The summary of every run is in [results](results/).

## What it checks

syncsan follows the PTX memory model for global memory between thread blocks and for shared memory between warps,
including mbarriers and the Hopper and Blackwell copy engine (TMA, `cp.async.bulk`, `cp.async`). It does not check
races between lanes of one warp, distributed shared memory, or ordering across kernels and streams. It is slow: a
4-token decode of the Hazy megakernel takes 54 s under syncsan and 14 s without it.
[docs/how-it-works.md](docs/how-it-works.md) describes the model and its limits, and
[docs/prior-work.md](docs/prior-work.md) compares syncsan with Barracuda, iGUARD, SuperCollider and compute-sanitizer.

## Tests

`tests/run_tests.sh sm_90` runs 51 cases with known answers on a local GPU. All 51 pass on an H100; on a T4, the 33 that
don't need newer instructions pass and the other 18 are skipped. [tests/cuhadron](tests/cuhadron/) adds six
cross-block synchronization tests to NVIDIA's [cuHadron](https://github.com/NVlabs/cuHadron) race benchmark.

## License

Apache 2.0. To cite syncsan, see [CITATION.cff](CITATION.cff).
