Port cuda kernel to triton

From wenyi-li/awesome-agent-kernel-skills · View interactive version

Port cuda kernel to triton is an AI agent skill from the GitHub repository wenyi-li/awesome-agent-kernel-skills. Guide the agent through systematically porting an existing CUDA kernel to Triton, mapping the CUDA execution model to Triton's tile-based program model, preserving numerical correctness, and identifyi

How to use this skill

Skill file

Skill: Port a CUDA Kernel to Triton

Purpose

Guide the agent through systematically porting an existing CUDA kernel to Triton, mapping the CUDA execution model to Triton's tile-based program model, preserving numerical correctness, and identifying the patterns that do not translate directly.

Use this when

Do not use this when

Inputs the agent should gather first

Required reasoning process

  1. Understand the CUDA kernel's tile structure. Before writing any Triton code, identify:
  1. Map CUDA thread/block coordinates to Triton program_id and tile offsets. This is the central translation:
  1. Replace explicit shared memory loading with tl.load. CUDA loads a tile into smem explicitly:

```c

shared float smem[BLOCK_M][BLOCK_K];

smem[threadIdx.y][threadIdx.x] = A[row * K + col];

__syncthreads();

```

In Triton, this becomes:

```python

a_tile = tl.load(A_ptr + row_offs[:, None] * K + k_offs[None, :], mask=mask)

```

The compiler manages the smem staging. There is no explicit smem declaration and no __syncthreads. The synchronization is implicit at the tile boundary.

  1. Replace thread-level reductions with tl.sum / tl.max. CUDA warp-level or block-level reductions (including warp shuffles) map to Triton reduction operations:
  1. Replace CUDA boundary guards with Triton masks. CUDA:

```c

if (row < M && col < N) { C[row * N + col] = result; }

```

Triton:

```python

mask = (row_offs[:, None] < M) & (col_offs[None, :] < N)

tl.store(C_ptr + row_offs[:, None] * N + col_offs[None, :], result, mask=mask)

```

Apply masks to both tl.load and tl.store. Use other=0.0 (or the appropriate identity value) in masked loads.

  1. Handle the K (reduction) dimension loop. In CUDA, this is often a loop over K tiles with smem double buffering. In Triton:

```python

acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.float32)

for k in range(0, tl.cdiv(K, BLOCK_K)):

k_offs = k * BLOCK_K + tl.arange(0, BLOCK_K)

a = tl.load(...)

b = tl.load(...)

acc += tl.dot(a, b)

```

The software pipeline (double buffering) is controlled by the num_stages parameter in the @triton.jit decorator or triton.autotune config, not by explicit smem management.

  1. Translate the CUDA epilogue. After accumulation, CUDA often applies scaling, bias, or activation in the epilogue within the same kernel. In Triton, this is natural:

```python

result = acc * scale

result = tl.maximum(result, 0.0) # ReLU

tl.store(...)

```

Triton's tile-based model makes epilogue fusion straightforward.

  1. Validate numerically. After the port, run both the CUDA kernel and the Triton kernel on the same inputs and compare outputs:
  1. Tune the Triton kernel. After correctness is confirmed:

Kernel design rules

Correctness requirements

Performance requirements

Output format

The agent should produce:

  1. Tile structure analysis: a description of what each CUDA block computes (tile of the output), what it loads (smem regions), and what its inner loop processes (K or reduction dimension). This is the conceptual translation map.
  2. Coordinate mapping table: a table mapping CUDA execution model constructs (blockIdx, threadIdx, smem, __syncthreads, warp shuffle) to their Triton equivalents (program_id, tl.arange, implicit smem via tl.load, no explicit sync, tl.sum/tl.max).
  3. Triton kernel implementation: complete, runnable Triton kernel code with @triton.jit decorator, all required parameters as tl.constexpr where appropriate, and boundary masks on all tl.load/tl.store calls.
  4. Autotune configuration: a @triton.autotune config list covering tile size and num_warps/num_stages combinations.
  5. Validation test: code that runs both kernels on the same inputs and compares outputs with torch.allclose.
  6. Non-portable constructs list: explicit documentation of any CUDA constructs that were not directly ported and how they were handled (restructured, dropped, or approximated).

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.