Fuse elementwise ops

From tensormux/kernel-skills · View interactive version

Fuse elementwise ops is an AI agent skill from the GitHub repository tensormux/kernel-skills. Guide the agent through deciding whether to fuse multiple elementwise operations into a single kernel pass, and if so, how to implement the fusion correctly and efficiently in CUDA or Triton.

How to use this skill

Skill file

Skill: Fuse Elementwise Operations

Purpose

Guide the agent through deciding whether to fuse multiple elementwise operations into a single kernel pass, and if so, how to implement the fusion correctly and efficiently in CUDA or Triton.

Use this when

Do not use this when

Inputs the agent should gather first

Required reasoning process

  1. Classify each operation. Confirm every operation is strictly elementwise: output at index i depends only on inputs at index i. Operations like exp, relu, mul, add, cast, clamp qualify. Softmax, cumsum, and reductions do not.
  1. Count memory traffic before and after fusion. For an unfused chain of N operations over a tensor of size S bytes:
  1. Check arithmetic intensity of the fused kernel. If the fused kernel has very low arithmetic intensity (few FLOPs per byte), it will remain bandwidth-bound. Fusion still helps by reducing total bytes transferred, but do not expect compute-bound behavior.
  1. Assess register pressure. Each live intermediate value in the fused operation chain consumes registers. Enumerate the intermediates that must be live simultaneously at the peak of the computation. If this count is high, check occupancy impact before proceeding.
  1. Check dtype consistency. If operations transition from one dtype to another, insert explicit casts. Never silently widen or narrow at the wrong stage — state where each cast occurs and why.
  1. Decide on CUDA vs Triton vs torch.compile.
  1. Design the kernel structure.
  1. Handle epilogue fusion into GEMM/conv separately. This requires integrating with CUTLASS epilogue visitors or cuBLAS workspace epilogue APIs, not writing a standalone elementwise kernel. Treat this as a distinct design path.

Kernel design rules

Correctness requirements

Performance requirements

Output format

The agent should produce:

  1. Fusion decision summary: one paragraph stating which ops are fused, why fusion is valid, estimated bandwidth reduction, and whether torch.compile was considered.
  2. Dtype and cast map: a table or annotated expression showing the dtype at each stage of the computation chain.
  3. Kernel implementation: complete, compilable kernel code with launch parameters. Include the scalar fallback for non-aligned inputs if using vectorized loads.
  4. Test harness: a short test that runs both the fused kernel and a reference sequential implementation, compares outputs with torch.allclose, and reports timing.
  5. Known limitations: explicit statements about what shapes, dtypes, or use cases are not covered by this implementation.

Common failure modes

Review checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.