write-numerically-stable-kernel

write-numerically-stable-kernel is a skill for Claude Code, Codex from tensormux/kernel-skills. It costs 0 tokens per session (2,859 once invoked), scanned A, original, MIT.

A guide for finding and fixing numerical instability in GPU kernel calculations using floating-point numbers. It covers operations such as sums, exponentials, logarithms, divisions, variance, softmax, and layer normalization.

In plain words
What is it for?
Use it when writing or reviewing kernels with reductions, accumulations, or sensitive mathematical operations, especially when comparing results with a double-precision reference.
Why use it?
It helps explain why a calculation that works in fp32 can produce infinity, NaN, or large errors in fp16 or bf16. It also helps avoid adding unnecessary fixes when the inputs and precision are already safe.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/tensormux/kernel-skills/write-numerically-stable-kernel
Any agent
npx skills add tensormux/kernel-skills --skill write-numerically-stable-kernel
Clone the repo
git clone --depth 1 https://github.com/tensormux/kernel-skills

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for write-numerically-stable-kernel

README.md
[![agentmods](https://agentmods.dev/badge/skills/tensormux/kernel-skills/write-numerically-stable-kernel.svg)](https://agentmods.dev/skills/tensormux/kernel-skills/write-numerically-stable-kernel)
Your own site
<a href="https://agentmods.dev/skills/tensormux/kernel-skills/write-numerically-stable-kernel"><img src="https://agentmods.dev/badge/skills/tensormux/kernel-skills/write-numerically-stable-kernel.svg" alt="Measured on agentmods" height="20"></a>
Per session 0 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,859 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.02859
Opus 5 $0.00000 $0.01430
Sonnet 5 $0.00000 $0.00572
Haiku 4.5 $0.00000 $0.00286

Measured 4d ago against content hash 402ec2a63a6a, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

write-numerically-stable-kernel scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 4d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/patterns/write-numerically-stable-kernel/SKILL.md · 108 lines

How it starts

The opening of the file, as written. The whole thing — 108 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Skill: Write a Numerically Stable Kernel

Purpose

Guide the agent through identifying numerical instability risks in a kernel's computation path and applying the correct stabilization strategy for each risk class.

Use this when

  • Writing or reviewing a kernel that contains reductions, accumulations, exponentials, logarithms, or divisions over floating-point inputs.
  • A kernel produces correct results in fp32 but diverges when run in fp16 or bf16.
  • A kernel computes variance, softmax, log-softmax, cross-entropy, or layer normalization — all of which have standard stable formulations that differ from the naive algebraic form.
  • A kernel accumulates a large number of values (e.g., dot products over long sequences, large reduction trees).
  • Results show inf, NaN, or unexpectedly large relative error relative to a double-precision reference.

Do not use this when

  • The computation is already fp32 or fp64 throughout, operates on bounded inputs, and correctness has been validated against a reference. Do not add unnecessary stabilization steps that cost performance without improving correctness.
  • The instability is caused by a bug (wrong indexing, wrong reduction tree, missing synchronization) rather than a precision limitation. Fix the bug first.
  • The application explicitly accepts approximate computation (e.g., stochastic rounding for training with intentional noise). Understand the tolerance before adding stabilization overhead.

Inputs the agent should gather first

  • The mathematical definition of the computation, written out explicitly — not just "softmax" but the exact formula being implemented.
  • Input dtype (fp16, bf16, fp32, fp64) and whether that dtype is fixed or configurable.
  • Expected input value range: are inputs bounded, potentially large, or potentially near zero?
  • Accumulation length: how many values are summed or dot-producted? Longer accumulations accumulate more rounding error.
  • Whether the output is consumed by a loss function, an activation, or another reduction — downstream consumers may have their own precision requirements.
  • Hardware: which compute capability? On Hopper (sm_90), fp8 and bf16 tensor core paths have different precision characteristics than on Ampere.
  • Whether correctness is validated against a double-precision reference or only against another fp16 run.

Read the full file on GitHub · 108 lines

Files

What ships with it

1 file beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 4d ago First seen · 108 lines · 0 tokens per session scan A 402ec2a63a6a

Subscribe to this mod's changes

write-numerically-stable-kernel is a skill published in the GitHub repository tensormux/kernel-skills (73 stars, last pushed 2mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 2,859 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.

Related

Other skills, from other repositories

quantum-qiskit

Reference qiskit 2.x patterns for variational quantum machine learning. Covers data-encoding feature maps, variational quantum classifier (VQC) training, variational quantum eigensolver (VQE) for chemistry, matrix-product-state circuits, and noise model integration. Use when writing Python code that imports qiskit…

aiming-lab/AutoResearchClaw · 102 tokens

fba-simulator

Run Flux Balance Analysis (FBA) and related constraint-based simulations using COBRApy. Covers standard FBA, parsimonious FBA (pFBA), Flux Variability Analysis (FVA), loopless FBA, gene/reaction knockouts, and carbon source swapping. Outputs flux distributions and CSV files.

aiming-lab/AutoResearchClaw · 69 tokens

flux-analyzer

Analyse FBA flux distributions to extract biological insights. Covers gene essentiality, phenotypic phase planes, flux sampling, pathway-level aggregation, secretion product prediction, and production of publication- quality figures.

aiming-lab/AutoResearchClaw · 44 tokens

gsmm-validator

Validate a COBRApy genome-scale metabolic model for mass/charge balance, stoichiometric consistency, biomass producibility, dead-end metabolites, thermodynamic loops, and GPR rule formatting. Outputs a structured validation report with errors and warnings.

aiming-lab/AutoResearchClaw · 52 tokens

metabolic-study-planner

Plan publishable constraint-based metabolic modelling studies when the user has a broad biological or metabolic-engineering topic but no concrete dataset, organism, model, or hypothesis. Selects feasible BiGG/COBRA models, objectives, perturbations, analyses, metrics, figures, and risk controls before FBA code is…

aiming-lab/AutoResearchClaw · 69 tokens

gsmm-builder

Build or load a genome-scale metabolic model (GSMM) using COBRApy. Covers loading from BIGG, constructing minimal models from scratch, setting medium constraints, and exporting validated .json model files.

aiming-lab/AutoResearchClaw · 45 tokens