Write a Numerically Stable Kernel

Identify and mitigate numerical instability in GPU compute kernels with dtype-annotated stabilizations.

54|7|Updated Apr 10, 2026
One-click install
npx skills add https://github.com/KrxGu/kernel-skills --skill write-a-numerically-stable-kernel
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Write a Numerically Stable Kernel
Source: https://github.com/KrxGu/kernel-skills/tree/main/skills/patterns/write-numerically-stable-kernel
Command: npx skills add https://github.com/KrxGu/kernel-skills --skill write-a-numerically-stable-kernel

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Many high-performance compute kernels produce incorrect results, NaNs/Infs, or large relative errors when run in low-precision dtypes (fp16, bf16) or on long accumulations. This Skill guides an agent to locate every accumulation and reduction path, classify numerical risks (overflow, catastrophic cancellation, accumulation error, subnormal underflow, inf propagation), and apply targeted, minimal-cost stabilizations so kernels remain correct and performant.

Core Features & Use Cases

  • Risk enumeration & classification: Systematically list every place values are combined and label the numeric failure mode.
  • Targeted mitigation patterns: Provide exact stabilized mathematical formulations (Welford, log-sum-exp, shift-by-max, fp32 accumulation) with dtypes annotated.
  • Design tradeoffs & performance accounting: State whether an algorithm requires extra passes, increased registers, or different tensor core APIs, and quantify expected costs.
  • Verification guidance: Include fp64 reference tests and adversarial input cases (large magnitudes, tiny values, all-equal inputs) to validate correctness.
  • Use Cases: Stabilizing softmax/log-softmax/attention kernels for fp16, computing variance robustly in streaming pipelines, correcting long dot-product accumulation in mixed-precision kernels.

Quick Start

Stabilize this softmax kernel for fp16 by subtracting the per-row maximum, accumulating sums in fp32, emitting explicit dtype annotations at each step, and providing an fp64 reference test.

Frequently Asked Questions about Write a Numerically Stable Kernel

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent NaNs and precision loss in fp16 softmax kernels?

Prevent NaNs in fp16 softmax kernels by subtracting the per-row maximum before exponentiation, accumulating intermediate sums in fp32, and annotating dtypes at each step to avoid overflow and reduction errors.

What causes numerical instability in mixed-precision GPU kernels?

Numerical instability in mixed-precision GPU kernels is caused by overflow, catastrophic cancellation, accumulation error, subnormal underflow, and inf propagation along reduction paths, which require targeted stabilization to mitigate.

What is the best way to compute variance robustly in streaming pipelines?

Compute variance robustly in streaming pipelines by applying the Welford algorithm, a targeted mitigation pattern that minimizes accumulation error and catastrophic cancellation without requiring extra data passes.

How do I verify numerical stability against an fp64 reference?

Verify numerical stability by testing adversarial input cases like large magnitudes, tiny values, and all-equal inputs against an fp64 reference to validate correctness and quantify relative errors in low-precision dtypes.

Does stabilizing a kernel with log-sum-exp impact performance?

Stabilizing a kernel with log-sum-exp impacts performance by potentially requiring extra passes, increased registers, or different tensor core APIs, requiring design tradeoffs and quantified cost statements to balance correctness and speed.