Amd kernel optimization

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

Amd kernel optimization is an AI agent skill from the GitHub repository wenyi-li/awesome-agent-kernel-skills. Optimize inference latency and throughput of PyTorch models on AMD GPUs (MI250/MI300/MI350) with ROCm. Use when profiling and optimizing GEMM, attention, elementwise ops, torch.compile, CUDAGraphs, or Triton kernels on AMD hardware. Covers the full optimize cycle: benchmark → profile → analyze → imp

How to use this skill

Skill file

AMD Kernel Optimization (ROCm)

3 Rules (read first)

  1. torch.compile FIRST. torch.compile(mode="default") with correct inductor config gives 2-5x speedup. Get this working before any manual optimization. Any code change that breaks compile is a net regression.
  1. Profile before optimizing. Never guess where time is spent. Run torch.profiler, classify GPU time into GEMM / attention / elementwise / launch overhead, then optimize the largest category.
  1. Measure after every change. Benchmark with proper warmup and iterations (see below). Revert if performance regresses.

Benchmarking (do this correctly or waste hours)

NEVER reduce warmup/iterations to "save time" — you get garbage numbers.

```python

start = torch.cuda.Event(enable_timing=True)

end = torch.cuda.Event(enable_timing=True)

start.record(); result = model(input); end.record()

torch.cuda.synchronize()

ms = start.elapsed_time(end)

```

Optimization Ladder

Each level builds on the previous. Do NOT skip to Level 3+ without Level 2.

Level 1: Environment (no code changes)

Level 2: torch.compile (MANDATORY — do this first)

Level 3: Model surgery (must preserve compile compatibility)

Level 4: Kernel & inductor tuning

Level 5: Architecture-specific kernels

AMD-Specific Alternatives Quick Reference

GEMM / Linear

| Option | Notes |

|---|---|

| rocBLAS (default) | Vendor BLAS; generally well-tuned |

| hipBLASLt | Fused epilogues; may beat rocBLAS for some shapes |

| aiter tuned GEMM | Auto-dispatches best kernel per (M,N,K) from tuned configs |

| FP8 GEMM (MI300+) | gemm_a8w8 via aiter; gfx942=e4m3fnuz, gfx950=e4m3fn |

Attention

| Option | Notes |

|---|---|

| aiter flash attention | torch.ops.aiter.mha_fwd.default(...) — compile-friendly, GQA native |

| SDPA | F.scaled_dot_product_attention(...) — good for KV-cache decode |

| Manual bmm+softmax+bmm | Slowest; replace with SDPA |

Compilation & Graphs

| Option | Notes |

|---|---|

| torch.compile(mode="default") | Start here. Stable on ROCm with correct inductor config |

| Manual CUDAGraph capture | Wrap full inference in one graph; needs Dynamo RNG patch |

| reduce-overhead / max-autotune | Avoid on ROCm unless you have verified stability |

Common Pitfalls

Reference Files

Read as needed for implementation details:

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.