Handle boundary conditions

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

Handle boundary conditions is an AI agent skill from the GitHub repository wenyi-li/awesome-agent-kernel-skills. Guide the agent through correctly handling partial tiles — cases where a problem dimension does not evenly divide the tile size — in CUDA and Triton kernels, without introducing out-of-bounds accesses

How to use this skill

Skill file

Skill: Handle Boundary Conditions in Tiled Kernels

Purpose

Guide the agent through correctly handling partial tiles — cases where a problem dimension does not evenly divide the tile size — in CUDA and Triton kernels, without introducing out-of-bounds accesses, incorrect output values, or silent data corruption.

Use this when

Do not use this when

Inputs the agent should gather first

Required reasoning process

  1. Identify all tiled dimensions. For each dimension tiled by the kernel, compute the number of tiles as ceil(N / TILE_SIZE). The last tile covers indices [(num_tiles-1)*TILE_SIZE, N) and has N % TILE_SIZE valid elements (when N % TILE_SIZE != 0).
  1. Classify each tensor access in the last tile as read or write. Reads and writes have different safety implications:
  1. Choose a boundary strategy for each dimension. The three options are:
  1. For CUDA kernels: write the boundary guard explicitly. For a 1D kernel:

```

int idx = blockIdx.x * blockDim.x + threadIdx.x;

if (idx >= N) return; // or mask out store only, keeping load safe

```

For a 2D tiled kernel loading a tile into shared memory, use a per-element guard:

```

int row = tile_row * TILE_M + threadIdx.y;

int col = tile_col * TILE_N + threadIdx.x;

if (row < M && col < N) {

smem[threadIdx.y][threadIdx.x] = input[row * N + col];

} else {

smem[threadIdx.y][threadIdx.x] = 0.0f; // safe padding value

}

```

  1. For Triton kernels: use masks on every tl.load and tl.store. Triton's tile-based model makes this natural:

```python

offs = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)

mask = offs < N

x = tl.load(ptr + offs, mask=mask, other=0.0)

# ... computation ...

tl.store(out_ptr + offs, result, mask=mask)

```

For 2D tiles:

```python

row_offs = pid_m * BLOCK_M + tl.arange(0, BLOCK_M)

col_offs = pid_n * BLOCK_N + tl.arange(0, BLOCK_N)

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

```

  1. Assess reduction over partial tiles separately. When the last tile is partial, the reduction must only include the valid elements:
  1. Handle variable-length batched inputs. When batch elements have different sizes:
  1. Verify that padding value semantics are consistent with the computation. Zero padding is appropriate for sums and dot products. For softmax, padding should be -inf so masked positions contribute zero to the softmax output. For max reductions, padding should be -inf. Using zero padding in a softmax kernel is a correctness bug.

Kernel design rules

Correctness requirements

Performance requirements

Output format

The agent should produce:

  1. Boundary analysis: for each tiled dimension, state the tile count, the size of the last partial tile, and which tensors are read or written in that tile.
  2. Masking strategy: explicit statement of which strategy (in-kernel mask, external padding, loop guard) is used for each dimension and why.
  3. Kernel implementation: complete, compilable kernel code with boundary guards clearly annotated with comments.
  4. Correctness test: a test suite that includes shapes that trigger the partial tile path — specifically: N = TILE_SIZE - 1, N = TILE_SIZE + 1, and one prime-sized N. Each test compares output to a CPU reference.
  5. Edge case documentation: explicit documentation of any shapes or configurations the kernel does not support (e.g., "requires N >= 1", "undefined for empty tensors").

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.