Write backend agnostic kernel plan

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

Write backend agnostic kernel plan is an AI agent skill from the GitHub repository wenyi-li/awesome-agent-kernel-skills. Guide the agent through planning a compute kernel that must run correctly and performantly on multiple hardware backends (NVIDIA, AMD, CPU fallback, or future backends) before any backend-specific imp

How to use this skill

Skill file

Skill: Write a Backend-Agnostic Kernel Plan

Purpose

Guide the agent through planning a compute kernel that must run correctly and performantly on multiple hardware backends (NVIDIA, AMD, CPU fallback, or future backends) before any backend-specific implementation is written — covering abstraction strategy, feature compatibility mapping, and the tradeoffs between portability and performance.

Use this when

Do not use this when

Inputs the agent should gather first

Required reasoning process

  1. Enumerate the operation's characteristics. Write down:

This analysis determines which abstraction levels are viable.

  1. Map operations to cross-backend options. Choose the highest abstraction that meets performance requirements:

| Strategy | Backends | Performance | Complexity |

|---|---|---|---|

| Framework operator (PyTorch, JAX) | All | Framework-dependent | Low |

| cuBLAS + rocBLAS (GEMM) | NVIDIA + AMD | Near-peak | Low to medium |

| Triton kernel | NVIDIA + AMD (ROCm backend) | 80–95% of peak | Medium |

| CUTLASS (NVIDIA) + rocWMMA (AMD) | NVIDIA + AMD | Near-peak | High |

| HIP single-source (CUDA + HIP) | NVIDIA + AMD | Near-peak | Medium to high |

| Custom per-backend kernel | All | Peak | Very high |

Default recommendation for new multi-backend kernels: try Triton first. The ROCm Triton backend has matured significantly and covers most attention, GEMM, and elementwise patterns. Write a custom per-backend kernel only when Triton cannot reach the required performance or does not support the required operation.

  1. Identify backend-specific feature gaps. For each required hardware feature, verify availability:

| Feature | NVIDIA | AMD ROCm |

|---|---|---|

| FP8 tensor cores | H100+ (sm_90a) | MI300X (hipBLASLt) |

| BF16 | A100+ (sm_80) | MI200+ |

| Warp shuffles | All | All (wavefront size differs) |

| WMMA / matrix ops | Turing+ | rocWMMA (ROCm ≥ 5.4) |

| Cooperative launch | Pascal+ | ROCm 5.x+ |

| Dynamic parallelism | All | ROCm 5.x+ |

| Warp is 32 threads | Always | CDNA: 64; RDNA3: 32 |

Any feature in the left column that is "NVIDIA-only" or "not mature on AMD" becomes a portability risk item.

  1. Design the abstraction boundary. The plan must define where hardware-specific code lives:
  1. Write the portability header primitives. For the thin portability layer approach, define:

```cpp

// warp_primitives.h

#if defined(CUDA_ARCH)

#define WARP_SIZE 32

#define BALLOT(pred) __ballot_sync(0xffffffff, pred)

#define SHFL_DOWN(val, offset) __shfl_down_sync(0xffffffff, val, offset)

#elif defined(HIP_DEVICE_COMPILE)

#define WARP_SIZE __AMDGCN_WAVEFRONT_SIZE

#define BALLOT(pred) __ballot(pred) // returns uint64_t on CDNA

#define SHFL_DOWN(val, offset) __shfl_down(val, offset)

#endif

```

Document every macro in this header explicitly. Do not use implicit platform detection inside kernel body code — all platform differences must flow through named primitives in this header.

  1. Plan tile sizes per backend. Optimal tile shapes differ across backends:
  1. Write a portability risk register. Before implementation, list each risk:

This register becomes the implementation checklist.

  1. Define the CPU fallback. If a CPU fallback is required:
  1. Define the CI matrix. For a backend-agnostic kernel to be maintainable, every backend must be tested in CI:

A plan that has no CI for one of the stated backends will regress that backend within weeks.

Kernel design rules

Correctness requirements

Performance requirements

Output format

The final response must include:

  1. Backend matrix: which backends are in scope, with hardware targets and ROCm/CUDA versions.
  2. Operation analysis: compute intensity, data type requirements, special hardware features needed.
  3. Abstraction strategy: chosen portability approach with justification (Triton, HIP single-source, backend dispatch, or other).
  4. Portability risk register: each identified risk with its mitigation.
  5. Portability header: if using the thin abstraction approach, the complete warp_primitives.h or equivalent.
  6. Tile strategy per backend: tile size and block size recommendations for each target.
  7. CI matrix: which hardware configurations are required for testing.
  8. Performance expectations: documented expected throughput per backend relative to backend-specific peak.

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.