write-cuda-reduction-kernel

A guide for designing and implementing CUDA reduction kernels, which combine many GPU values into one result such as a sum, maximum, or minimum.

In plain words
What is it for?
Building reductions for arrays or tensor axes, custom associative operations, irregular segments, or fused per-element transformations, and deciding when to use CUB.
Why use it?
It covers the choices needed for correct and efficient reductions across GPU threads and blocks.

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-cuda-reduction-kernel
Any agent
npx skills add tensormux/kernel-skills --skill write-cuda-reduction-kernel
Clone the repo
git clone --depth 1 https://github.com/tensormux/kernel-skills

Made for: Claude Code, Codex.

Per session 0 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 3,624 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.03624
Opus 5 $0.00000 $0.01812
Sonnet 5 $0.00000 $0.00725
Haiku 4.5 $0.00000 $0.00362

Measured yesterday against content hash 6f3c6df33a89, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

write-cuda-reduction-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 yesterday.

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/cuda/write-cuda-reduction-kernel/SKILL.md · 115 lines

How it starts

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

Skill: Write CUDA Reduction Kernel

Purpose

Guide the agent through designing and implementing a correct, efficient CUDA reduction kernel for a given operator (sum, max, min, or custom binary associative op), covering warp-level primitives, block-level reduction, multi-block strategies, and when to use CUB instead.

Use this when

  • You need a reduction over a 1D array, a specific axis of a multi-dimensional tensor, or a segmented reduction with irregular segment sizes
  • The reduction operator is non-standard (e.g., log-sum-exp, online variance update, argmax with index tracking) and is not directly supported by CUB or Thrust
  • You need to fuse the reduction with a preceding or following per-element transformation and cannot afford the extra memory round-trip
  • You are implementing a custom training loop component (e.g., gradient norm, loss reduction) where you need exact control over accumulation order or precision

Do not use this when

  • The reduction is a standard sum/min/max/count over a contiguous array: use cub::DeviceReduce — it handles multi-block staging, SM-specific tuning, and dtype variants correctly and will outperform a first-attempt custom kernel
  • The input is large (> 1M elements) and throughput is the only concern: CUB's DeviceReduce uses a highly tuned multi-block algorithm with kernel fusion
  • You need segmented reductions over fixed-size segments: use cub::DeviceSegmentedReduce
  • The reduction is over a batch of small vectors and you just need row-wise sums: a simple warp-per-row kernel may suffice; use that pattern instead of a full multi-block reduction

Inputs the agent should gather first

  • Reduction operator: sum, max, min, product, logical AND/OR, argmax (value + index pair), custom binary op — the op must be associative; commutativity affects atomics strategy but is not strictly required
  • Input dtype: fp32, fp16, bf16, int32, int64, uint8; whether mixed precision (e.g., fp16 input, fp32 accumulator) is needed
  • Input shape: total element count; whether it is a 1D flat reduction or a reduction along an axis of a multi-dimensional tensor (e.g., reduce axis=1 of a [B, L] tensor → output shape [B])
  • Memory layout: contiguous or strided input; stride values for the reduction axis and non-reduction axes
  • Numerical precision requirements: is fp32 accumulation required for fp16 inputs, or is fp16 accumulation acceptable? Is the result expected to be deterministic across runs?
  • Output: scalar output (single value), or one output per non-reduced dimension (batched reduction)
  • Hardware target: SM architecture, for warp size (always 32 on current NVIDIA hardware), and to choose between atomics vs two-pass strategies

Read the full file on GitHub · 115 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. yesterday First seen · 115 lines · 0 tokens per session scan A 6f3c6df33a89

Subscribe to this mod's changes

write-cuda-reduction-kernel is a skill published in the GitHub repository tensormux/kernel-skills (70 stars, last pushed 2mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 3,624 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

design-impl-audit

The Design-vs-Implementation Audit Copilot. A universal skill for any project, any language, any design document. Before work starts, it reminds engineers what must be delivered; after code lands, it checks what was actually delivered, what drifted, and what silently went beyond the design. Feed it a design doc plus a…

MagicKidd/Rokid-agentic-workflow · 0 tokens

skill-creator

Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Claude's capabilities with specialized knowledge, workflows, or tool integrations.

MagicKidd/Rokid-agentic-workflow · 45 tokens

brainstorming

Brainstorming design process. Must be used before any creative work - creating features, building components, adding functionality, or modifying behavior. Transforms ideas into complete designs and specifications through collaborative dialogue. Trigger words: brainstorm, design discussion, requirement analysis…

MagicKidd/Rokid-agentic-workflow · 0 tokens

diagnose

Disciplined diagnosis loop for hard bugs and performance regressions. Reproduce → minimise → hypothesise → instrument → fix → regression-test. Use when user says "diagnose this" / "debug this", reports a bug, says something is broken/throwing/failing, or describes a performance regression.

MagicKidd/Rokid-agentic-workflow · 66 tokens

mergeable-feature-dev

For new-module development or cross-module refactors, enforce separation of scaffolding from deliverables so that "locally standalone runnable" and "low-cost mergeable into the trunk" hold simultaneously. Trigger words: mergeable, low-cost handoff, standalone run, scaffolding, deliverable layer, refactor merge.

MagicKidd/Rokid-agentic-workflow · 0 tokens

prompt-engineering

Prompt engineering techniques and patterns. Use when writing agent commands, hooks, skills, subagent prompts, or any LLM interaction: optimizing prompts, improving output reliability, and designing production-grade prompt templates. Trigger words: prompt engineering, prompt, prompt optimization, LLM interaction.

MagicKidd/Rokid-agentic-workflow · 0 tokens