Skip to content

[DSv4][P1-5] Implement CUDA and Triton rmsnorm_residual - #414

Draft
why1te wants to merge 3 commits into
RL-Align:dsv4-p1-devfrom
why1te:dsv4-p1-dev
Draft

why1te wants to merge 3 commits into
RL-Align:dsv4-p1-devfrom
why1te:dsv4-p1-dev

Conversation

@why1te

@why1te why1te commented Sep 16, 2026

Copy link
Copy Markdown

[DSv4][P1-5] rmsnorm_residual

Summary

Implements P1-5 rmsnorm_residual from #383, targeting dsv4-p1-dev. Adds CUDA/Triton forward and backward, autograd, registry fallback, and P1 providers.

The operator returns RMS-normalized output plus a copy of the original input—not RMSNorm(x + residual). Both backends pass bitwise oracle alignment and P1 acceptance on H100.

Implementation

  • BF16 activations/outputs; FP32 gamma, saved inverse RMS, and explicit dx/dgamma. Autograd returns BF16 x.grad and FP32 gamma.grad.
  • D=4096 in production; D=128 for fixtures; eps=1e-6.
  • Preserves rsqrt(mean(x²) + eps), (x * r) * gamma, ascending FP32 reductions, and separate rounding. No atomic reductions, Split-K, or Stream-K. Residual copying does not change the reduction layout.
  • FP32 gamma is maintainer-confirmed. The CPU-oracle manifest updates 59 affected hashes; residual, Sinkhorn-edge, and gamma-gradient hashes remain unchanged.
  • Registry order: CUDA → Triton → PyTorch reference. Providers replace P1-5 only; distributed P1-8 semantics are out of scope.

Operator documentation: docs/operators/rmsnorm-residual.md.

Validation environment

Item Value
GPU NVIDIA H100 PCIe, compute capability 9.0, 81,559 MiB, MIG disabled
Driver / CUDA toolkit 580.159.04 / 12.8 (nvcc V12.8.93)
PyTorch / Triton 2.8.0+cu128 / 3.4.0
Python / Host compiler 3.11.16 / GCC 13.3.0

Correctness / Tests

Coverage: T={1,7,16}, D={128,4096}; byte-equal forward/backward and saved state, edge values, strided inputs, repeatability, batch/padding invariance, both autograd branches, invalid inputs, and FP32 gamma rounding regressions.

uv run --no-sync python -m pytest \
  tests/test_rmsnorm_residual.py tests/test_rmsnorm_residual_triton.py -q -rs
# 28 passed in 7.89s

uv run --no-sync python -m pytest tests/test_rmsnorm_residual_gamma_precision.py -q
# 4 passed in 2.63s

uv run --no-sync python -m pytest \
  tests/test_p1_oracle.py tests/test_p1_provider.py \
  tests/test_operator_inputs.py tests/test_kernel_registry.py -q
# 77 passed in 2.16s, including the golden-manifest anchor

gtest

uv run --no-sync python scripts/check_operator.py \
  --op rmsnorm_residual --candidate cuda --device cuda \
  --dtype bf16 --batch 1 --seq 16 --normalized-dim 4096 --check-grad

uv run --no-sync python scripts/check_operator.py \
  --op rmsnorm_residual --candidate triton --device cuda \
  --dtype bf16 --batch 1 --seq 16 --normalized-dim 4096 --check-grad

Both report passed=True pass_rate=1.0000. Maximum absolute and relative errors are 0 for both outputs, BF16 x.grad, and FP32 gamma.grad.

P1 acceptance

uv run --no-sync python scripts/check_p1.py \
  --provider rl_engine.mhc.cuda_provider:CudaMHCProvider --device cuda

uv run --no-sync python scripts/check_p1.py \
  --provider rl_engine.mhc.triton_provider:TritonMHCProvider --device cuda

Both report RESULT: PASS (all boundaries byte-equal) for all five cases: one_row, packed_t16, packed_t7_odd, fused_pre_norm, and mixer_frozen.

Benchmarks

uv run --no-sync python benchmarks/benchmark_rmsnorm_residual.py \
  --tokens 32768 131072 262144 --warmup 50 --iterations 1000 \
  --output benchmark.json

H100 PCIe, D=4096, BF16 activations, FP32 gamma/explicit gradients; CUDA-event mean, eager execution, no CUDA Graphs. The script runs all three backends; both candidates pass pre-timing bitwise checks. CUDA versus torch-native forward + backward results:

T torch-native ms CUDA ms Speedup torch-native extra MiB CUDA extra MiB
32768 12.908420 10.043916 1.29× 3584.3906 1024.1406
131072 51.176590 39.021008 1.31× 14337.5156 4096.5156
262144 102.286531 80.839070 1.27× 28675.0156 8193.0156

CUDA forward speedup is 1.62–1.63×; combined peak extra allocated memory is approximately 71.4% lower. Memory is the allocator peak above the live baseline, not total GPU usage. Torch-native uses vectorized reductions and is a performance baseline, not the fixed-order oracle. Results apply to these measured workloads.

Kernel PR checklist

Follow PR #204.

  • CUDA and/or Triton kernel
  • Binding + build integration
  • Python/autograd wrapper + MHCProvider
  • Hardware-gated registry entry with clean fallback
  • check_p1.py passing result
  • Forward/backward test sweep
  • Bitwise batch-invariance test
  • Benchmark: latency + peak extra memory
  • Validation environment table
  • Operator documentation

@coderabbitai

coderabbitai Bot commented Sep 16, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

Comment @coderabbitai help to get the list of available commands.

@why1te
why1te force-pushed the dsv4-p1-dev branch 3 times, most recently from bbaa792 to ccc7b1a Compare September 16, 2026 08:20
Signed-off-by: why1te <youngtingwang@gmail.com>
Keep the RMSNorm gain in FP32 as confirmed with the maintainer. Previously
the BF16 storage path discarded gain bits before the operator's FP32 math.

Implementation:
- Change NormParams validation and seeded fixtures to FP32 gamma.
- Read gamma through float pointers in CUDA forward and input-gradient
  kernels; update Triton dtype validation without changing reduction order.
- Preserve BF16 activations, upstream gradients, normalized/residual outputs,
  and FP32 explicit dx/dgamma. Autograd returns BF16 x.grad and FP32 gamma.grad.
- Update gtest/benchmark input generation and record benchmark gamma dtype.
- Adapt direct callers to the oracle backward signature without x; keep
  provider/backend interfaces with explicit x through the reference adapter.
- Add tests using gamma=1+2**-10 to detect premature BF16 rounding in both
  forward and backward, plus dtype validation and gradient-precision checks.

Golden migration:
- Regenerate the CPU-oracle manifest for the approved FP32 input contract.
- Update 59 hashes: normalized outputs and gamma-dependent upstream
  gradients in five block fixtures, plus y/grad.dx in rms_edges.
- Leave Sinkhorn-edge, residual, and dgamma hashes unchanged and retain
  strict manifest equality; no candidate output is used as a golden value.
- The migration validation recorded in the original refresh commit rounded
  gamma through BF16 in a diagnostic process and reproduced the entire old
  manifest. No diagnostic rounding was added to production code or tests.

H100 PCIe validation (PyTorch 2.8.0+cu128, Triton 3.4.0, CUDA 12.8):
- uv run --no-sync python -m pytest tests/test_rmsnorm_residual.py
  tests/test_rmsnorm_residual_triton.py -q -rs: 28 passed in 7.89s.
- uv run --no-sync python -m pytest
  tests/test_rmsnorm_residual_gamma_precision.py -q: 4 passed in 2.63s.
- uv run --no-sync python -m pytest tests/test_p1_oracle.py
  tests/test_p1_provider.py tests/test_operator_inputs.py
  tests/test_kernel_registry.py -q: 77 passed in 2.16s, including the anchor.
- check_operator.py for cuda and triton at T=16/D=4096 with --check-grad:
  both passed, zero reported errors, gamma gradient FP32.
- check_p1.py for both providers: all boundaries byte-equal in all five cases.
- Benchmark at T=32768/131072/262144, D=4096, 50 warmup/1000 iterations:
  both candidates byte-equal to the oracle before timing; CUDA combined
  speedup 1.27-1.31x versus torch-native on these measured workloads.

Squashes the FP32 implementation and reviewed golden refresh into one
atomic contract migration. The source tree is unchanged from 6b24500.

Signed-off-by: why1te <youngtingwang@gmail.com>
Document public and explicit APIs, FP32 gamma and gradient boundaries, fixed-order arithmetic, backend dispatch, validation commands, H100 results, and rank-local limitations. Add the operator page to the documentation index and navigation.

Validation: Python and shell example syntax, YAML navigation, and git diff checks passed. Full MkDocs build not run because MkDocs is not installed in the local environment.
Signed-off-by: why1te <youngtingwang@gmail.com>

This branch has not been deployed

No deployments
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