Skip to content

Commit 7a64374

Browse files
kmbandyclaude
andcommitted
fix(ggml-cuda): restore fork build portability across CUDA + HIP backends
The fork's ggml-cuda failed to build on the CUDA host (nvcc, GGML_CUDA=ON) and on gfx803/HIP — AMD-only vocabulary and a missing warp-sync shim had leaked into shared/core files. Backend-portability only; no quant math changed. - set-rows.cu: fp8 e4m3 device decode used __builtin_amdgcn_ldexp unconditionally (undefined under nvcc). Guard #if GGML_USE_HIP (amdgcn) / #else portable ldexpf — identical numerics, AMD codegen unchanged. - turbo_fp8_hadamard.cuh: mt_turbo_fp8_fwht{,_half} declared with raw hipError_t/hipStream_t, tripping CUDA_CHECK (cudaError_t) at ml8.cu:1533. Switch to CUDA vocabulary; vendors/hip.h remaps it back to hip* under HIP. - vendors/hip.h: * add cudaErrorInvalidValue -> hipErrorInvalidValue remap (needed above). * add __syncwarp(...) -> __builtin_amdgcn_wave_barrier() shim, beside the existing __shfl_*_sync shims. __syncwarp is undeclared in the HIP toolchain for TUs that don't pull it in (e.g. mt_pagedattn_decode.cu); the shim makes that warp barrier portable WITHOUT editing that file, and PRESERVES the call on every arch incl. RDNA4 (gfx1201). Verified: - CUDA sm_61 (GTX 1070): full llama-server links + runs on-GPU (token output). - HIP gfx803 / ROCm 6.4 (RX 480): full llama-server links + runs on-GPU (turbo4 KV, 6.18 GB VRAM, token output). - mt_pagedattn_decode.cu + fattn-vec compile clean for gfx803 AND gfx1201 (-fsyntax-only); RDNA4 WMMA decode path unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent a90b4dd commit 7a64374

3 files changed

Lines changed: 20 additions & 10 deletions

File tree

ggml/src/ggml-cuda/set-rows.cu

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,11 @@ static __device__ __forceinline__ float fp8_e4m3_byte_to_fp32_dev(uint8_t b) {
11671167
int e = (b >> 3) & 0xF;
11681168
int m = b & 0x7;
11691169
float v = (e == 0) ? (1.0f / 64.0f) * (m / 8.0f)
1170+
#if defined(GGML_USE_HIP)
11701171
: __builtin_amdgcn_ldexp(1.0f + m / 8.0f, e - 7);
1172+
#else
1173+
: ldexpf(1.0f + m / 8.0f, e - 7);
1174+
#endif
11711175
return sign ? -v : v;
11721176
}
11731177

ggml/src/ggml-cuda/turbo_fp8_hadamard.cuh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ __global__ void mt_turbo_fp8_fwht_kernel(float * __restrict__ data, int n_rows,
9595
//
9696
// row_stride is in fp32 elements (typically == head_dim for tightly packed rows).
9797
// ─────────────────────────────────────────────────────────────────────────
98-
static inline hipError_t mt_turbo_fp8_fwht(
99-
hipStream_t stream,
98+
static inline cudaError_t mt_turbo_fp8_fwht(
99+
cudaStream_t stream,
100100
float * data_device,
101101
int n_rows,
102102
int head_dim,
103103
int row_stride
104104
) {
105-
if (n_rows <= 0) return hipSuccess;
105+
if (n_rows <= 0) return cudaSuccess;
106106
dim3 grid(n_rows, 1, 1);
107107

108108
switch (head_dim) {
@@ -128,9 +128,9 @@ static inline hipError_t mt_turbo_fp8_fwht(
128128
mt_turbo_fp8_fwht_kernel<1024><<<grid, dim3(1024),0, stream>>>(data_device, n_rows, row_stride);
129129
break;
130130
default:
131-
return hipErrorInvalidValue;
131+
return cudaErrorInvalidValue;
132132
}
133-
return hipGetLastError();
133+
return cudaGetLastError();
134134
}
135135

136136

@@ -176,14 +176,14 @@ __global__ void mt_turbo_fp8_fwht_half_kernel(__half * __restrict__ data, int n_
176176
row_ptr[tid] = __float2half(smem[tid] * scale);
177177
}
178178

179-
static inline hipError_t mt_turbo_fp8_fwht_half(
180-
hipStream_t stream,
179+
static inline cudaError_t mt_turbo_fp8_fwht_half(
180+
cudaStream_t stream,
181181
__half * data_device,
182182
int n_rows,
183183
int head_dim,
184184
int row_stride
185185
) {
186-
if (n_rows <= 0) return hipSuccess;
186+
if (n_rows <= 0) return cudaSuccess;
187187
dim3 grid(n_rows, 1, 1);
188188

189189
switch (head_dim) {
@@ -194,9 +194,9 @@ static inline hipError_t mt_turbo_fp8_fwht_half(
194194
case 256: mt_turbo_fp8_fwht_half_kernel<256> <<<grid, dim3(256), 0, stream>>>(data_device, n_rows, row_stride); break;
195195
case 512: mt_turbo_fp8_fwht_half_kernel<512> <<<grid, dim3(512), 0, stream>>>(data_device, n_rows, row_stride); break;
196196
case 1024: mt_turbo_fp8_fwht_half_kernel<1024><<<grid, dim3(1024),0, stream>>>(data_device, n_rows, row_stride); break;
197-
default: return hipErrorInvalidValue;
197+
default: return cudaErrorInvalidValue;
198198
}
199-
return hipGetLastError();
199+
return cudaGetLastError();
200200
}
201201

202202

ggml/src/ggml-cuda/vendors/hip.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
#define __SHFL_XOR_GET(_1, _2, _3, _4, NAME, ...) NAME
5353
#define __shfl_xor_sync(...) __SHFL_XOR_GET(__VA_ARGS__, __SHFL_XOR_SYNC_4, __SHFL_XOR_SYNC_3)(__VA_ARGS__)
5454

55+
// __syncwarp: HIP/ROCm has no warp-mask sync intrinsic (undeclared on gfx803 and
56+
// in this toolchain on RDNA arches). A wavefront execution barrier is the
57+
// equivalent; HIP ignores the lane mask, like the __shfl_*_sync shims above.
58+
#define __syncwarp(...) __builtin_amdgcn_wave_barrier()
59+
5560
// __shfl_down_sync: support 3-arg and 4-arg calls (HIP ignores mask)
5661
#define __SHFL_DOWN_SYNC_3(mask, var, delta) __shfl_down(var, delta, warpSize)
5762
#define __SHFL_DOWN_SYNC_4(mask, var, delta, width) __shfl_down(var, delta, width)
@@ -82,6 +87,7 @@
8287
#define cudaDeviceProp hipDeviceProp_t
8388
#define cudaDeviceSynchronize hipDeviceSynchronize
8489
#define cudaError_t hipError_t
90+
#define cudaErrorInvalidValue hipErrorInvalidValue
8591
#define cudaErrorMemoryAllocation hipErrorOutOfMemory
8692
#define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled
8793
#define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled

0 commit comments

Comments
 (0)