Port cuda kernel to hip

From tensormux/kernel-skills · View interactive version

Port cuda kernel to hip is an AI agent skill from the GitHub repository tensormux/kernel-skills. Guide the agent through translating a CUDA kernel to AMD HIP for ROCm-compatible hardware (MI250, MI300, RDNA), preserving correctness and performance intent while adapting to the HIP execution model,

How to use this skill

Skill file

Skill: Port a CUDA Kernel to HIP

Purpose

Guide the agent through translating a CUDA kernel to AMD HIP for ROCm-compatible hardware (MI250, MI300, RDNA), preserving correctness and performance intent while adapting to the HIP execution model, memory model, and AMD-specific toolchain.

Use this when

Do not use this when

Inputs the agent should gather first

Required reasoning process

  1. Run hipify as a first pass, then review every change. Use hipify-perl or hipify-clang on the source file to perform mechanical name substitutions (cudaMalloc → hipMalloc, cudaDeviceSynchronize → hipDeviceSynchronize, etc.). Do not trust the automated output blindly — review every changed line. The tool handles API names but not semantic differences.
  1. Audit wavefront width differences. This is the most common source of correctness bugs in CUDA-to-HIP ports:
  1. Map CUDA warp primitives to HIP equivalents.

| CUDA | HIP |

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

| __shfl_sync(mask, val, lane) | __shfl(val, lane) (HIP does not take a mask on older ROCm; __shfl_sync exists on newer ROCm ≥ 5.0) |

| __ballot_sync(mask, pred) | __ballot(pred) (returns uint64_t on 64-wide wavefront) |

| __any_sync(mask, pred) | __any(pred) |

| __all_sync(mask, pred) | __all(pred) |

| __activemask() | __activemask() (available ROCm ≥ 5.2, returns uint64_t) |

  1. Handle WMMA / Tensor Core differences. CUDA WMMA (nvcuda::wmma) has no direct HIP equivalent for NVIDIA-specific tensor core shapes. AMD equivalent:
  1. Adapt memory model and atomic operations. HIP's memory model is based on the HSA memory model, which differs from CUDA's weakly-ordered GPU memory model in subtle ways:
  1. Check launch configuration limits. AMD GPU limits:
  1. Update device queries and capability checks. Replace:
  1. Replace CUDA-specific libraries. Common replacements:

| CUDA library | HIP/ROCm equivalent |

|---|---|

| cuBLAS | rocBLAS / hipBLAS |

| cuDNN | MIOpen |

| NCCL | RCCL |

| cuRAND | rocRAND |

| cuFFT | rocFFT |

| Thrust | rocThrust (hipThrust) |

| NVTX | rocTX / roctx |

| cuda-memcheck | ROCm debugger (rocgdb) or AddressSanitizer |

  1. Compile and test iteratively. Build with hipcc targeting the AMD device. For single-source CUDA/HIP code: build with nvcc on NVIDIA and hipcc on AMD. Key flags:
  1. Evaluate performance and close gaps. After correctness is confirmed:

Kernel design rules

Correctness requirements

Performance requirements

Output format

The final response must include:

  1. Assessment of CUDA features used: list each CUDA feature and its HIP equivalent or the required design change.
  2. Wavefront width impact analysis: identify any code paths that assume a 32-thread warp and describe the required changes.
  3. Ported HIP kernel: the complete translated kernel with all changes annotated.
  4. Build instructions: hipcc command and offload-arch flag for the target.
  5. Correctness validation plan: how to compare HIP and CUDA outputs for the same inputs.
  6. Known performance differences: any architectural reasons the HIP kernel may behave differently than the CUDA original.

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.