Hip kernel optimization

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

Hip kernel optimization is an AI agent skill from the GitHub repository wenyi-li/awesome-agent-kernel-skills. This skill should be used when writing or tuning HIP kernels on AMD/NVIDIA GPUs, covering memory coalescing, shared-memory tiling, bank conflict avoidance, warp primitives, occupancy, vectorization, async ops, loop unrolling, and profiling.

How to use this skill

Skill file

HIP Kernel Optimization

Purpose

Provide ready patterns for efficient HIP kernels and guide diagnosis of memory throughput, occupancy, and synchronization bottlenecks.

When to Use

Optimization Priority

Phase 1: Low-hanging fruit (try first, low risk)

  1. #pragma unroll on hot loops with small, fixed trip counts
  2. Enable -ffast-math compiler flag for floating-point kernels
  3. Use 32B vectorized loads/stores instead of 16B
  4. Add __launch_bounds__(maxThreads, minBlocks) to guarantee occupancy
  5. Add const qualifiers on read-only pointers
  6. Verify memory coalescing (consecutive threads → consecutive addresses)

Phase 2: Targeted improvements (profile first)

  1. Profile with rocprof to confirm bottleneck
  2. If memory-bound: CK-Tile buffer views with vectorization
  3. If compute-bound: Shared memory tiling
  4. Dynamically calculate block size based on problem dimensions
  5. Replace large 2D shared arrays with atomicAdd for sparse patterns
  6. Provide multiple block size configurations to avoid register spill
  7. Add explicit rounding mode control for numerical correctness
  8. Pre-compute workspace size to avoid dynamic allocation
  9. Implement CSV-based tuning cache for repeated GEMM shapes

Phase 3: Complex transformations (high effort)

  1. Algorithm changes (e.g., Top-K-only softmax)
  2. gfx950: Use 16x16x32 MFMA instead of 2x 16x16x16
  3. Kernel fusion (multi-op in single kernel)
  4. Persistent kernels for repeatedly executed operations
  5. Shape-based heuristic dispatching

Anti-patterns:

Core Optimization Patterns

1. Memory Access

```cpp

if(idx + VEC_SIZE <= d) {

vec_o out_vec;

#pragma unroll

for(size_t j = 0; j < VEC_SIZE; j++) {

out_vec[j] = compute(x[j], y[j]);

}

buffer_out.template set(idx, 0, true, out_vec); // Fast path

} else {

for(size_t j = 0; j < VEC_SIZE; j++) { // Boundary path

if(idx + j < d) ptr_out[idx + j] = compute(...);

}

}

```

2. Shared Memory

```cpp

// Three-pass pattern for sparse bucketing

// Pass 1: Count items per category

for(int i = start_idx; i < end_idx; ++i) {

int32_t category_id = input_ids[i];

atomicAdd(&category_counts[category_id], 1);

}

__syncthreads();

// Pass 2: Compute prefix sum for offsets

if(threadIdx.x == 0) {

for(int i = 0; i < num_categories; ++i)

cumsum[i+1] = cumsum[i] + category_counts[i];

}

__syncthreads();

// Pass 3: Assign items using atomic write positions

for(int i = start_idx; i < end_idx; ++i) {

int32_t position = atomicAdd(&write_positions[input_ids[i]], 1);

sorted_output[position] = i;

}

```

3. Warp/Wavefront Primitives

```cpp

auto arg_max = [](const kvp& a, const kvp& b) {

return (a.value > b.value || (a.value == b.value && a.key < b.key)) ? a : b;

};

kvp thread_kvp = {item_id, max_val};

thread_kvp = warp_reduce(thread_kvp, arg_max, WARP_SIZE);

```

4. Occupancy Tuning

```cpp

int vec_size = nextPow2(d / 64);

vec_size = min(vec_size, max_vec_size);

int num_wave = min(nextPow2(d / 64 / vec_size), max_wave_num);

dim3 block(max(num_wave, 1) * 64);

```

```cpp

launch_bounds(256, 8) global // 256 threads, min 8 blocks per CU

void kernel(scalar_t* restrict output, ...) { }

```

```cpp

if (MPerBlock == 64)

gemm_kernel<..., 64, ...>(...);

else if (MPerBlock == 128)

gemm_kernel<..., 128, ...>(...);

else if (MPerBlock == 256)

gemm_kernel<..., 256, ...>(...);

```

5. Loop Unrolling

6. Async Memory Operations

AMD-Specific Optimizations

7. MFMA Instructions (gfx940/942/950)

```cpp

#if defined(gfx950)

dout = gcn_mfma16x16x32_instr<scalar_t, 0, 0, 0>(K, Q, dout);

#else

for(int i = 0; i < 2; i++) {

dout = gcn_mfma16x16x16_instr<scalar_t, 0, 0, 0>(K.xy[i], Q.xy[i], dout);

}

#endif

```

8. Inline Assembly for Packed Operations

```cpp

float2 result;

asm volatile("v_pk_mul_f32 %0, %1, %2\n\t"

"v_pk_mul_f32 %0, %0, %3"

: "=v"(result) : "v"(act_vals), "v"(y_vals), "v"(scale_vals));

```

9. Compiler Flags

Advanced Optimization Strategies

10. Algorithm-Level Optimizations

```cpp

float thread_max = find_max_in_row();

for(int k_idx = 0; k_idx < k; ++k_idx) {

kvp top = find_argmax_in_remaining();

output[k_idx] = expf(top.value - thread_max);

renorm_value += output[k_idx];

row_chunk[top.index] = -INFINITY;

}

float row_sum_rest = compute_sum_of_remaining_exp(thread_max);

normalize_top_k(renorm_value + row_sum_rest);

```

11. Numerical Precision

12. Kernel Selection and Dispatching

```cpp

int get_algoIdx_from_csv(const std::string filename, ...) {

// Parse CSV and match (trans_a, trans_b, m, n, k, dtypes)

for each line:

if (all_params_match) return algo_index;

return -1; // Not found

}

```

```cpp

Kernel select_kernel(int M, int N, int K) {

if (M < 128) return gemm_small_m<...>;

else return gemm_large_m<...>;

}

```

```cpp

int64_t ws_size = topkValue * (sizeof(T) + sizeof(IdxT)) * numRows;

auto workspace = allocate_device_memory(ws_size);

```

Quick Reference

Profiling

Validation Checklist

Performance Impact (Production-Validated)

| Optimization | Use Case | Typical Impact |

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

| #pragma unroll | Memory kernels | +3-5% |

| AtomicAdd sparse | MOE, sorting | +15-20%, O(N²)→O(N) |

| 32B vectors | Memory-bound | Better throughput |

| -ffast-math | Math-heavy | +5-10% FP |

| Top-K softmax | Gating | Reduce exp by 50-90% |

| 16x16x32 MFMA | Attention | 2x→1x calls |

| __launch_bounds__ | Position encoding | Guaranteed occupancy |

| Multiple MPerBlock | GEMM stages | Fix register spill |

| Persistent kernels | Paged attention | -50-80% launch overhead |

| CSV cache | GEMM tuning | Eliminate repeat tuning |

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.