Skip to content

fix(kernels): remove global --use_fast_math; selective per-kernel intrinsics only (T3.1) - #142

Closed
dndungu wants to merge 4 commits into
mainfrom
fix/remove-global-fast-math
Closed

fix(kernels): remove global --use_fast_math; selective per-kernel intrinsics only (T3.1)#142
dndungu wants to merge 4 commits into
mainfrom
fix/remove-global-fast-math

Conversation

@dndungu

@dndungu dndungu commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Implements zerfoo plan-gpu-training-hardening T3.1 + S3.1.1 (verifies UC-GH-5).

What

  • Drop --use_fast_math from NVCC_FLAGS (internal/cuda/kernels/Makefile). The flag swapped every expf/tanhf/logf/division for unsafe approximations library-wide; the non-saturating fast tanhf caused the GELU blowup behind the GPU f32 CrossAsset cliff (fix(cuda): saturate kernel_tanh argument (--use_fast_math tanhf overflow) #125).
  • Selective intrinsic, oracle-gated: kernel_softmax keeps __expf after max-subtraction (argument <= 0, result in (0,1], ~2^-21 rel error — inside the Softmax tolerance atol 1e-6 / rtol 1e-4). Everything else uses accurate libdevice functions.
  • Tanh saturation clamp (fix(cuda): saturate kernel_tanh argument (--use_fast_math tanhf overflow) #125) RETAINED as defense-in-depth.
  • oracle-gen -engine gpu: GenerateAllWith records compute.GPUEngine bundles through the same format — the GPU-side oracle gate the scripts/oracle README reserved for T3.x.
  • compute/gpu_kernel_bench_test.go: per-kernel GPU micro-benchmarks (Exp/Log/Sqrt/Rsqrt/Sin/Cos/Tanh/Div/Softmax) for the before/after perf delta.

Gates (GB10 via Spark, serialized — results to be appended to this PR + devlog)

  • sm_121 libkernels.so rebuilt from this branch
  • GPU-engine oracle bundles vs torch (run_oracle.py, NGC 26.02): all ops green within tolerance
  • Parity suite (scripts/parity/run.sh, test(parity): Wolf-pattern training loop under poison -- GB10 + CI variants (S2.3.1) #140 pattern): both schedules + red-proof green against the rebuilt kernels
  • Per-kernel + end-to-end perf delta recorded in devlog (a regression is expected and acceptable — correctness is the point)

dndungu added 2 commits June 11, 2026 22:27
…max only (T3.1)

Drop --use_fast_math from NVCC_FLAGS: the flag swapped every
expf/tanhf/logf/division for unsafe approximations library-wide; the
non-saturating fast tanhf caused the GELU blowup behind the GPU f32
CrossAsset cliff (ztensor#125). Per the torch numerics convention
(zerfoo plan-gpu-training-hardening T3.1) fast intrinsics are now
re-enabled selectively in kernel source, only where the PyTorch-oracle
gate proves equivalence within tolerance:

- kernel_softmax keeps __expf AFTER max-subtraction (argument <= 0,
  result in (0,1], ~2^-21 relative error -- inside the Softmax
  tolerance atol 1e-6 / rtol 1e-4).
- Everything else gets the accurate libdevice functions.
- The tanh saturation clamp (ztensor#125) is RETAINED as
  defense-in-depth.

Gate plumbing: oracle-gen grows -engine gpu (GenerateAllWith records
compute.GPUEngine bundles through the same format -- the GPU-side
oracle gate the scripts/oracle README reserved for T3.x), and
compute/gpu_kernel_bench_test.go adds per-kernel GPU micro-benchmarks
for the ops whose codegen changed, for the before/after perf delta.

Verifies zerfoo UC-GH-5.
@dndungu

dndungu commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

GB10 gate results (all green)

Build: sm_121 libkernels.so rebuilt from 393d8eb (Spark pod ztensor-t31-build-393d8eb, NGC nvcc).

Oracle (S3.1.1) — first live GPU-engine oracle run: 25 GPU-engine bundles (oracle-gen -engine gpu) vs torch 2.11.0a0+nv26.02 on the GB10: 25 passed / 0 failed / 0 errored. Softmax with the selective __expf: fwd max_abs 6.0e-8 / max_rel 1.4e-7 (tolerance 1e-6 / 1e-4). Tanh exact (0.0/0.0). Report archived at /home/ndungu/t31/393d8eb/report.json.

Parity sanity (#140 pattern, rebuilt kernels): TestParity_GPUvsCPU_ArenaStressSchedules_GPU PASS (both schedules), TestParity_GPURedProof_GPU PASS, TestTrainingLoop_WolfPattern_GPU PASS. Baseline run with the OLD fast-math kernels also green (constant gate).

Perf delta (engine-level, 4M f32, mean of 3x100):

kernel fast-math µs no-fast-math µs delta
Exp 425.6 417.1 -2.0%
Log 419.7 423.9 +1.0%
Sqrt 417.9 420.3 +0.6%
Rsqrt 417.9 426.1 +2.0%
Sin 419.3 423.3 +0.9%
Cos 421.7 427.9 +1.5%
Tanh 417.7 421.1 +0.8%
Div 778.4 778.9 +0.1%
Softmax 428.2 430.6 +0.6%

End-to-end: parity suite 0.720s (old) → 0.691s (new). All deltas within run-to-run noise (~±2%): the engine path is bandwidth-bound at these sizes, so no measurable regression. Full numbers + ops notes in the devlog entry in this PR.

@dndungu

dndungu commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

Post-merge-of-main re-gate (pod ztensor-t31-gate-new2-dbcb0da, merged head dbcb0da, rebuilt no-fast-math kernels): the FULL current parity gate including the #141 attention stress test is green on the GB10 — TestAttentionTraining_WolfPattern_GPU PASS, TestParity_GPUvsCPU_ArenaStressSchedules_GPU PASS, TestParity_GPURedProof_GPU PASS, TestTrainingLoop_WolfPattern_GPU PASS (VALIDATION_OK_new2).

@dndungu

dndungu commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #143 (same change + GB10 evidence), which is merged — main has 1fd2e89 + b596563.

@dndungu dndungu closed this Jun 12, 2026
@dndungu
dndungu deleted the fix/remove-global-fast-math branch June 12, 2026 22:00
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