Projects

CUDA residual RMSNorm

A fused residual-add and RMSNorm study on the DGX Spark’s GB10 GPU. Five kernel variants test when fewer launches and retained values help, and when they cost more than they save.

Independent study on DGX Spark · Kernel optimization and measurement

Target case
28.05 to 17.97 µs at FP16 256 × 8192
Correctness
1,920 cases per dtype in each native run
Repetition
Two native runs, 15 trials per measured case
Tools
CUDA C++, FP32 / FP16, NVIDIA GB10, PyTorch, Compute Sanitizer, Nsight Systems

Credit. An independent CPU FP64 reference checks correctness; eager and compiled PyTorch provide comparison points.

One row, one block

The register-retaining kernel keeps the residual sum live through the reduction.

  1. Addz = x + residual

    Adjacent lanes read adjacent columns. Arithmetic uses FP32.

  2. Reducemean(z²) + ε

    Warp shuffles and shared partial sums combine the row.

  3. Scaley = z × rsqrt(…) × γ

    Retained values feed the output without a full residual buffer.

Architecture of the measured fused register variants. The baseline uses three launches and a full FP32 intermediate. The reread alternative loads input values again after reduction.

The operation

For each row, add the input and residual in FP32, compute the mean of the squared sum, then normalize and multiply by gamma. FP16 inputs still use FP32 arithmetic before conversion to the output type. The contract returns the normalized output only; a caller that also needs the residual sum has a different workload.

Five ways to run it

The three-pass baseline launches separate add, reduction, and scale kernels. Its reduction is parallel. The alternatives combine the operation into one launch: reread the inputs after reduction, retain values with 128 threads per row, assign one row to each warp, or retain values with 256 threads per row.

Adjacent lanes access adjacent columns. Warp shuffles reduce partial sums, and the block-per-row variants use shared memory and barriers to combine warps. Bounds checks cover odd widths and partial rows. Above 8,192 columns, both register variants explicitly fall back to the reread kernel.

A measured bottleneck

At FP16 256 × 8192, the 128-thread variant took 28.05 µs. A 256-thread follow-up halved the retained values per thread and reached 17.97 µs. The compiler reported 116 versus 64 registers per thread, with zero spills. A separate full rerun measured 28.00 versus 18.44 µs.

The reread kernel still had a slightly lower first-run median at 17.31 µs, with overlapping distributions. Reduced per-thread state is verified; a causal claim about achieved occupancy is not, because performance counters were unavailable.

FP16 256 by 8192 measured graph latency. Register128: 28.05 and 28.00 microseconds. Register256: 17.97 and 18.44 microseconds. The reread kernel is also shown. Lines show the tenth to ninetieth percentile across 15 trials per run.
Physical GB10 measurements. Median and p10–p90 across 15 trials, using reused buffers and CUDA graph sequences. Both runs used the same shared workstation and session.

Where fusion loses

At FP32 1024 × 4096, the tuned kernel measured 236.00 µs versus 343.52 µs for three passes. At a single row of 4,096 values, it regressed to 4.92 µs versus 3.68 µs. One block gives the GPU much less parallel work.

Compiled PyTorch also won several comparisons. In the same Python harness at FP32 1 × 4096, torch.compile measured 2.77 µs, the tuned CUDA kernel 4.90 µs, and eager PyTorch 8.74 µs. Rankings changed with shape and timing distributions often overlapped. There is no overall speedup claim over compiled PyTorch.

Checks and limits

Each final native run passed 1,920 cases per dtype against the CPU reference, checking input preservation and output canaries. Four Compute Sanitizer runs, covering memcheck and synccheck for both dtypes, reported zero errors. The evidence audit accounted for 15,750 timing samples across the two native runs and one Python comparison.

Correctness checks covered ordinary calls and warmup graph outputs; every timed replay was not checked individually. Nsight Systems captured 21 kernel launches. Nsight Compute could not access performance counters, so actual DRAM traffic and achieved occupancy remain unmeasured.

The resident vLLM service remained running. These are shared-workstation observations with unfixed clocks and warm reused buffers, not service latency or a production dispatch policy.

Evidence

The plot is generated from the retained raw trials, with the complete native and PyTorch timing data available below.