Avoid warp divergence

From tensormux/kernel-skills · View interactive version

Avoid warp divergence is an AI agent skill from the GitHub repository tensormux/kernel-skills. Guide the agent through identifying, classifying, and restructuring warp divergence in CUDA kernels — distinguishing avoidable from unavoidable divergence, applying correct restructuring strategies, a

How to use this skill

Skill file

Skill: Avoid Warp Divergence

Purpose

Guide the agent through identifying, classifying, and restructuring warp divergence in CUDA kernels — distinguishing avoidable from unavoidable divergence, applying correct restructuring strategies, and assessing the real performance impact before spending engineering effort.

Use this when

Do not use this when

Inputs the agent should gather first

Required reasoning process

  1. Determine the warp-level uniformity of the branch condition. A condition that evaluates identically for all 32 threads in a warp does not cause divergence, even if different warps take different paths. Determine: is the condition a function of threadIdx.x alone (potentially divergent within a warp), a function of blockIdx.x alone (uniform within a warp), or a function of input data (potentially divergent)?
  1. Classify the branch as avoidable, unavoidable, or warp-uniform.
  1. Estimate the performance cost of the divergence. For a branch where fraction f of threads take path A (cost C_A) and (1-f) take path B (cost C_B): the divergent warp cost is approximately C_A + C_B (both paths execute, threads on the inactive path are predicated off). If f ≈ 1 (almost all threads take the same path), the cost of path B is low. If f ≈ 0.5, both paths execute at full cost. Compare C_A + C_B against max(C_A, C_B) (ideal non-divergent cost) to decide if restructuring is worthwhile.
  1. Apply branch hoisting for warp-uniform conditions. If a branch condition is warp-uniform (e.g., checking threadIdx.x / 32 == 0 which is a function of warpIdx), the hardware already optimizes this — the entire warp takes one path. No restructuring needed. For conditions that could be made warp-uniform by restructuring (e.g., checking a value that could be broadcast from lane 0 via __shfl_sync), consider broadcasting.
  1. Apply loop peeling for boundary divergence. For a loop where the first or last iteration has a different code path (e.g., loading a partial tile at the boundary): peel that iteration out of the main loop. Execute the main loop (no divergence) for all full tiles, then handle the partial tile separately. This concentrates divergence in the peeled code and eliminates it from the main loop.
  1. Apply data reorganization for data-dependent divergence. If threads in a warp diverge based on data values (e.g., processing only non-zero elements of a sparse tensor), reorganize the data so that elements requiring the same code path are grouped together. Techniques:
  1. Use predication awareness. The GPU hardware converts simple if/else branches to predicated instruction execution automatically when the branch body is short enough (typically < 5–10 instructions). In this case, both paths execute but predicated-off instructions are nullified. This is cheaper than full branch divergence for very short branch bodies. For longer bodies, the hardware takes the divergent branch with serialization. Do not manually force predication (e.g., via branchless code with masked adds) when the compiler already handles it, unless profiling shows the compiler is not doing so.
  1. Evaluate branchless alternatives. For simple conditional assignments (val = cond ? a : b): write as val = a * (float)cond + b * (float)(1 - cond) (branchless arithmetic). This is beneficial only if the branch body is very short and the arithmetic cost is lower than the serialization cost. For longer branch bodies, branchless conversion introduces dead arithmetic work on the inactive path, which may be worse.
  1. Consider separate kernels for divergent populations. If the input can be partitioned into two groups that take different code paths, and the partition is known before the kernel launch: launch two separate specialized kernels, one per group. This eliminates divergence entirely at the cost of two kernel launches and data partitioning logic. Worthwhile when the divergence is severe and the two populations are roughly equal in size.

Kernel design rules

Correctness requirements

Performance requirements

Output format

The final response must include:

  1. Divergence audit: for each branch in the kernel, classify it (warp-uniform, geometry-based, data-dependent, unavoidable boundary) and estimate the warp-level divergence rate.
  2. Cost estimate: for each divergent branch, compute the overhead ratio (cost_path_A + cost_path_B) / max(cost_path_A, cost_path_B).
  3. Prioritized restructuring plan: address the highest-impact divergence first. Skip low-impact boundary divergence.
  4. Restructured code: show the before and after for each applied restructuring strategy.
  5. Expected improvement: estimate (not guarantee) the instruction-count or throughput improvement. State that profiling is required to confirm.
  6. Tradeoffs: note any introduced complexity, extra memory traffic, or kernel launch overhead from the restructuring.

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.