ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cuda / quantize.cu
| 1 | #include "quantize.cuh" |
| 2 | #include <cstdint> |
| 3 | |
| 4 | #if defined(BLACKWELL_MMA_AVAILABLE) |
| 5 | // this maps to 256-bit loads in PTX on supported devices, |
| 6 | // and otherwise falls back to 2 128-bit loads |
| 7 | struct __builtin_align__(32) float8 { |
| 8 | float x; float y; float z; float w; |
| 9 | float p; float q; float r; float s; |
| 10 | }; |
| 11 | |
| 12 | #if CUDART_VERSION >= 12080 |
| 13 | static __device__ __forceinline__ float nvfp4_native_scale_error( |
| 14 | const float vals[QK_NVFP4_SUB], const float inv_col_scale, const float inv_scale, const float scale) { |
| 15 | const float scale_dequant = 2.0f * scale; |
| 16 | float err = 0.0f; |
| 17 | |
| 18 | #pragma unroll |
| 19 | for (int k = 0; k < QK_NVFP4_SUB; k += 4) { |
| 20 | const float v0 = vals[k + 0] * inv_col_scale; |
| 21 | const float v1 = vals[k + 1] * inv_col_scale; |
| 22 | const float v2 = vals[k + 2] * inv_col_scale; |
| 23 | const float v3 = vals[k + 3] * inv_col_scale; |
| 24 | |
| 25 | const __nv_fp4x4_e2m1 q(make_float4(v0 * inv_scale, v1 * inv_scale, v2 * inv_scale, v3 * inv_scale)); |
| 26 | const __nv_fp4x4_storage_t q_storage = q.__x; |
| 27 | const __nv_fp4x2_storage_t q_lo = static_cast<__nv_fp4x2_storage_t>(q_storage); |
| 28 | const __nv_fp4x2_storage_t q_hi = static_cast<__nv_fp4x2_storage_t>(q_storage >> 8U); |
| 29 | |
| 30 | const __half2_raw hraw2_lo = __nv_cvt_fp4x2_to_halfraw2(q_lo, __NV_E2M1); |
| 31 | const __half2_raw hraw2_hi = __nv_cvt_fp4x2_to_halfraw2(q_hi, __NV_E2M1); |
| 32 | const __half2 h2_lo = static_cast<__half2>(hraw2_lo); |
| 33 | const __half2 h2_hi = static_cast<__half2>(hraw2_hi); |
| 34 | const float2 dq_lo = __half22float2(h2_lo); |
| 35 | const float2 dq_hi = __half22float2(h2_hi); |
| 36 | |
| 37 | const float err0 = fabsf(v0) - fabsf(dq_lo.x) * scale_dequant; |
| 38 | const float err1 = fabsf(v1) - fabsf(dq_lo.y) * scale_dequant; |
| 39 | const float err2 = fabsf(v2) - fabsf(dq_hi.x) * scale_dequant; |
| 40 | const float err3 = fabsf(v3) - fabsf(dq_hi.y) * scale_dequant; |
| 41 | |
| 42 | err = fmaf(err0, err0, err); |
| 43 | err = fmaf(err1, err1, err); |
| 44 | err = fmaf(err2, err2, err); |
| 45 | err = fmaf(err3, err3, err); |
| 46 | } |
| 47 | |
| 48 | return err; |
| 49 | } |
| 50 | #endif // CUDART_VERSION >= 12080 |
| 51 | #endif // defined(BLACKWELL_MMA_AVAILABLE) |
| 52 | |
| 53 | __launch_bounds__(CUDA_QUANTIZE_BLOCK_SIZE, 1) |
| 54 | static __global__ void quantize_q8_1( |
| 55 | const float * x_ptr, void * vy_ptr, |
| 56 | const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03, |
| 57 | const int64_t ne0, const uint32_t ne1, const uint3 ne2) { |
| 58 | ggml_cuda_pdl_lc(); |
| 59 | const float * GGML_CUDA_RESTRICT x = x_ptr; |
| 60 | void * GGML_CUDA_RESTRICT vy = vy_ptr; |
| 61 | const int64_t i0 = (int64_t)blockDim.x*blockIdx.x + threadIdx.x; |
| 62 | |
| 63 | if (i0 >= ne0) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | const int64_t i3 = fastdiv(blockIdx.z, ne2); |
| 68 | const int64_t i2 = blockIdx.z - i3*ne2.z; |
| 69 | const int64_t i1 = blockIdx.y; |
| 70 | |
| 71 | const int64_t & i00 = i0; |
| 72 | const int64_t & i01 = i1; |
| 73 | const int64_t & i02 = i2; |
| 74 | const int64_t & i03 = i3; |
| 75 | |
| 76 | const int64_t i_cont = ((i3*ne2.z + i2) * ne1 + i1) * ne0 + i0; |
| 77 | |
| 78 | block_q8_1 * y = (block_q8_1 *) vy; |
| 79 | |
| 80 | const int64_t ib = i_cont / QK8_1; // block index |
| 81 | const int64_t iqs = i_cont % QK8_1; // quant index |
| 82 | |
| 83 | ggml_cuda_pdl_sync(); |
| 84 | const float xi = i0 < ne00 ? x[i03*s03 + i02*s02 + i01*s01 + i00] : 0.0f; |
| 85 | float amax = fabsf(xi); |
| 86 | |
| 87 | amax = warp_reduce_max<QK8_1>(amax); |
| 88 | |
| 89 | const float d = amax / 127.0f; |
| 90 | const int8_t q = amax == 0.0f ? 0 : roundf(xi / d); |
| 91 | |
| 92 | const int sum = warp_reduce_sum<QK8_1>((int) q); |
| 93 | |
| 94 | y[ib].qs[iqs] = q; |
| 95 | |
| 96 | if (iqs > 0) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | y[ib].ds = make_half2(d, d * sum); |
| 101 | } |
| 102 | |
| 103 | __device__ __forceinline__ uint8_t compute_e8m0_scale(float amax) { |
| 104 | if (!(amax > 0.0f)) { |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | // FP4 E2M1: max exponent (unbiased) is 2. |
| 109 | constexpr int FP4_E2M1_EMAX = 2; |
| 110 | |
| 111 | const float e = log2f(amax); |
| 112 | |
| 113 | // "even" -> round-to-nearest integer, ties-to-even |
| 114 | const int e_int = __float2int_rn(e); |
| 115 | |
| 116 | const int shared_exp = e_int - FP4_E2M1_EMAX; |
| 117 | |
| 118 | int biased = shared_exp + 127; |
| 119 | |
| 120 | biased = max(biased, 0); |
| 121 | biased = min(biased, 254); |
| 122 | |
| 123 | return static_cast<uint8_t>(biased); |
| 124 | } |
| 125 | |
| 126 | // scatter: grid over tokens, quantize once, write to all the token's compact rows |
| 127 | template <bool scatter, bool use_aligned_float8> |
| 128 | static __global__ void quantize_mmq_nvfp4( |
| 129 | const float * __restrict__ x, const int32_t * __restrict__ ids, void * __restrict__ vy, float * __restrict__ scale, |
| 130 | const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03, |
| 131 | const int64_t ne0, const int64_t ne1, const int64_t ne2, const int n_expert_used) { |
| 132 | #if defined(BLACKWELL_MMA_AVAILABLE) |
| 133 | |
| 134 | int64_t base_idx; |
| 135 | if constexpr (scatter) { |
| 136 | base_idx = (int64_t) blockIdx.x * s02; // one physical row per token |
| 137 | } else { |
| 138 | const int64_t i2 = blockIdx.y % ne2; |
| 139 | const int64_t i3 = blockIdx.y / ne2; |
| 140 | const int64_t i01 = ids ? ids[blockIdx.x] : blockIdx.x; |
| 141 | base_idx = i3 * s03 + i2 * s02 + i01 * s01; |
| 142 | } |
| 143 | const float * __restrict__ x_row = x + base_idx; |
| 144 | |
| 145 | float amax = 0.0f; |
| 146 | if constexpr (use_aligned_float8) { |
| 147 | for (int64_t i0 = 8 * threadIdx.x; i0 < ne00; i0 += 8 * blockDim.x) { |
| 148 | const float * x_base = x_row + i0; |
| 149 | const float8 v = reinterpret_cast<const float8 *>(x_base)[0]; |
| 150 | amax = fmaxf(amax, fabsf(v.x)); |
| 151 | amax = fmaxf(amax, fabsf(v.y)); |
| 152 | amax = fmaxf(amax, fabsf(v.z)); |
| 153 | amax = fmaxf(amax, fabsf(v.w)); |
| 154 | amax = fmaxf(amax, fabsf(v.p)); |
| 155 | amax = fmaxf(amax, fabsf(v.q)); |
| 156 | amax = fmaxf(amax, fabsf(v.r)); |
| 157 | amax = fmaxf(amax, fabsf(v.s)); |
| 158 | } |
| 159 | } else { |
| 160 | for (int64_t i0 = threadIdx.x; i0 < ne00; i0 += blockDim.x) { |
| 161 | amax = fmaxf(amax, fabsf(x_row[i0])); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | amax = warp_reduce_max<WARP_SIZE>(amax); |
| 166 | |
| 167 | __shared__ float warp_amax[CUDA_QUANTIZE_BLOCK_SIZE_MMQ / WARP_SIZE]; |
| 168 | const int lane = threadIdx.x % WARP_SIZE; |
| 169 | const int warp = threadIdx.x / WARP_SIZE; |
| 170 | |
| 171 | if (lane == 0) { |
| 172 | warp_amax[warp] = amax; |
| 173 | } |
| 174 | __syncthreads(); |
| 175 | |
| 176 | if (warp == 0) { |
| 177 | amax = threadIdx.x < int(CUDA_QUANTIZE_BLOCK_SIZE_MMQ / WARP_SIZE) ? warp_amax[lane] : 0.0f; |
| 178 | amax = warp_reduce_max<WARP_SIZE>(amax); |
| 179 | if (lane == 0) { |
| 180 | warp_amax[0] = amax / (6.0f * 448.0f); |
| 181 | if constexpr (scatter) { |
| 182 | #pragma unroll |
| 183 | for (int slot = 0; slot < n_expert_used; ++slot) { |
| 184 | const int64_t i = ids[(int64_t) blockIdx.x * n_expert_used + slot]; |
| 185 | scale[i] = warp_amax[0]; |
| 186 | } |
| 187 | } else { |
| 188 | scale[blockIdx.y * ne1 + blockIdx.x] = warp_amax[0]; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | __syncthreads(); |
| 193 | |
| 194 | block_fp4_mmq * y = (block_fp4_mmq *) vy; |
| 195 | const int64_t n_subblocks = (ne0 + QK_NVFP4_SUB - 1) / QK_NVFP4_SUB; |
| 196 | |
| 197 | for (int64_t isb = threadIdx.x; isb < n_subblocks; isb += blockDim.x) { |
| 198 | const int64_t i0_base = isb * QK_NVFP4_SUB; |
| 199 | const int64_t k_block = i0_base / QK_FP4_MMQ; |
| 200 | const int sub = (i0_base % QK_FP4_MMQ) / QK_NVFP4_SUB; |
| 201 | |
| 202 | const float row_scale = warp_amax[0]; |
| 203 | const float inv_col_scale = row_scale > 0.0f ? 1.0f / row_scale : 0.0f; |
| 204 | |
| 205 | float vals[QK_NVFP4_SUB]; |
| 206 | if constexpr (use_aligned_float8) { |
| 207 | const float * x_base = x_row + i0_base; |
| 208 | const float8 v0 = i0_base + 7 < ne00 ? reinterpret_cast<const float8 *>(x_base)[0] : float8{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; |
| 209 | const float8 v1 = i0_base + 15 < ne00 ? reinterpret_cast<const float8 *>(x_base + 8)[0] : float8{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; |
| 210 | vals[0] = v0.x; vals[1] = v0.y; vals[2] = v0.z; vals[3] = v0.w; |
| 211 | vals[4] = v0.p; vals[5] = v0.q; vals[6] = v0.r; vals[7] = v0.s; |
| 212 | vals[8] = v1.x; vals[9] = v1.y; vals[10] = v1.z; vals[11] = v1.w; |
| 213 | vals[12] = v1.p; vals[13] = v1.q; vals[14] = v1.r; vals[15] = v1.s; |
| 214 | } else { |
| 215 | #pragma unroll |
| 216 | for (int k = 0; k < QK_NVFP4_SUB; ++k) { |
| 217 | const int64_t i00 = i0_base + k; |
| 218 | vals[k] = i00 < ne00 ? x_row[i00] : 0.0f; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | uint32_t q0 = 0; |
| 223 | uint32_t q1 = 0; |
| 224 | |
| 225 | float amax_sub = 0.0f; |
| 226 | #pragma unroll |
| 227 | for (int k = 0; k < QK_NVFP4_SUB; ++k) { |
| 228 | amax_sub = fmaxf(amax_sub, fabsf(vals[k] * inv_col_scale)); |
| 229 | } |
| 230 | |
| 231 | static constexpr int test_offsets[5] = { 0, -1, 1, -2, 2 }; |
| 232 | const int first_fp8_code = (int) ggml_cuda_fp32_to_ue4m3(amax_sub / 6.0f); |
| 233 | |
| 234 | uint8_t fp8_code = (uint8_t) first_fp8_code; |
| 235 | float subblock_scale = ggml_cuda_ue4m3_to_fp32(fp8_code); |
| 236 | float inv_scale_err = subblock_scale > 0.0f ? 0.5f / subblock_scale : 0.0f; |
| 237 | #if CUDART_VERSION >= 12080 |
| 238 | float best_err = nvfp4_native_scale_error(vals, inv_col_scale, inv_scale_err, subblock_scale); |
| 239 | #else |
| 240 | float best_err = 0.0f; |
| 241 | #pragma unroll |
| 242 | for (int k = 0; k < QK_NVFP4_SUB; ++k) { |
| 243 | const float v = vals[k] * inv_col_scale; |
| 244 | const uint8_t q = ggml_cuda_float_to_fp4_e2m1(v, inv_scale_err); |
| 245 | const float err_diff = fabsf(v) - fabsf(kvalues_fp4[q & 0x7]) * subblock_scale; |
| 246 | best_err = fmaf(err_diff, err_diff, best_err); |
| 247 | } |
| 248 | #endif // CUDART_VERSION >= 12080 |
| 249 | |
| 250 | #pragma unroll |
| 251 | for (int i = 1; i < 5; ++i) { |
| 252 | const int test_code = first_fp8_code + test_offsets[i]; |
| 253 | if (test_code < 0 || test_code > 0x7e) { |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | const float test_scale = ggml_cuda_ue4m3_to_fp32((uint8_t) test_code); |
| 258 | const float test_inv_scale = test_scale > 0.0f ? 0.5f / test_scale : 0.0f; |
| 259 | #if CUDART_VERSION >= 12080 |
| 260 | const float cur_err = nvfp4_native_scale_error(vals, inv_col_scale, test_inv_scale, test_scale); |
| 261 | #else |
| 262 | float cur_err = 0.0f; |
| 263 | #pragma unroll |
| 264 | for (int k = 0; k < QK_NVFP4_SUB; ++k) { |
| 265 | const float v = vals[k] * inv_col_scale; |
| 266 | const uint8_t q = ggml_cuda_float_to_fp4_e2m1(v, test_inv_scale); |
| 267 | const float err_diff = fabsf(v) - fabsf(kvalues_fp4[q & 0x7]) * test_scale; |
| 268 | cur_err = fmaf(err_diff, err_diff, cur_err); |
| 269 | } |
| 270 | #endif // CUDART_VERSION >= 12080 |
| 271 | |
| 272 | if (cur_err < best_err) { |
| 273 | best_err = cur_err; |
| 274 | fp8_code = (uint8_t) test_code; |
| 275 | subblock_scale = test_scale; |
| 276 | } |
| 277 | } |
| 278 | #if CUDART_VERSION >= 12080 |
| 279 | const float inv_scale = subblock_scale > 0.0f ? 0.5f / subblock_scale : 0.0f; |
| 280 | const float s = inv_col_scale * inv_scale; |
| 281 | |
| 282 | __nv_fp4x4_e2m1 q0_lo(make_float4(vals[0] * s, vals[8] * s, vals[1] * s, vals[9] * s)); |
| 283 | __nv_fp4x4_e2m1 q0_hi(make_float4(vals[2] * s, vals[10] * s, vals[3] * s, vals[11] * s)); |
| 284 | __nv_fp4x4_e2m1 q1_lo(make_float4(vals[4] * s, vals[12] * s, vals[5] * s, vals[13] * s)); |
| 285 | __nv_fp4x4_e2m1 q1_hi(make_float4(vals[6] * s, vals[14] * s, vals[7] * s, vals[15] * s)); |
| 286 | |
| 287 | const char2 q0_lo_c = *reinterpret_cast<char2 *>(&q0_lo); |
| 288 | const char2 q0_hi_c = *reinterpret_cast<char2 *>(&q0_hi); |
| 289 | const char2 q1_lo_c = *reinterpret_cast<char2 *>(&q1_lo); |
| 290 | const char2 q1_hi_c = *reinterpret_cast<char2 *>(&q1_hi); |
| 291 | |
| 292 | q0 = uint32_t(uint8_t(q0_lo_c.x)) | (uint32_t(uint8_t(q0_lo_c.y)) << 8) | |
| 293 | (uint32_t(uint8_t(q0_hi_c.x)) << 16) | (uint32_t(uint8_t(q0_hi_c.y)) << 24); |
| 294 | q1 = uint32_t(uint8_t(q1_lo_c.x)) | (uint32_t(uint8_t(q1_lo_c.y)) << 8) | |
| 295 | (uint32_t(uint8_t(q1_hi_c.x)) << 16) | (uint32_t(uint8_t(q1_hi_c.y)) << 24); |
| 296 | #else |
| 297 | const float inv_scale = subblock_scale > 0.0f ? 0.5f / subblock_scale : 0.0f; |
| 298 | #pragma unroll |
| 299 | for (int k = 0; k < QK_NVFP4_SUB / 4; ++k) { |
| 300 | q0 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 0] * inv_col_scale, inv_scale)) << (8 * k); |
| 301 | q0 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 8] * inv_col_scale, inv_scale)) << (8 * k + 4); |
| 302 | q1 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 4] * inv_col_scale, inv_scale)) << (8 * k); |
| 303 | q1 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 12] * inv_col_scale, inv_scale)) << (8 * k + 4); |
| 304 | } |
| 305 | #endif // CUDART_VERSION >= 12080 |
| 306 | |
| 307 | if constexpr (scatter) { |
| 308 | #pragma unroll |
| 309 | for (int slot = 0; slot < n_expert_used; ++slot) { |
| 310 | const int64_t i = ids[(int64_t) blockIdx.x * n_expert_used + slot]; |
| 311 | block_fp4_mmq * yb = y + (k_block * ne1 + i); |
| 312 | uint32_t * yqs = reinterpret_cast<uint32_t *>(yb->qs); |
| 313 | yqs[2 * sub + 0] = q0; |
| 314 | yqs[2 * sub + 1] = q1; |
| 315 | reinterpret_cast<uint8_t *>(yb->d4)[sub] = fp8_code; |
| 316 | } |
| 317 | } else { |
| 318 | const int64_t blocks_per_col = (ne0 + QK_FP4_MMQ - 1) / QK_FP4_MMQ; |
| 319 | block_fp4_mmq * yb = y + (blockIdx.y * (blocks_per_col * ne1) + k_block * ne1 + blockIdx.x); |
| 320 | uint32_t * yqs = reinterpret_cast<uint32_t *>(yb->qs); |
| 321 | yqs[2 * sub + 0] = q0; |
| 322 | yqs[2 * sub + 1] = q1; |
| 323 | reinterpret_cast<uint8_t *>(yb->d4)[sub] = fp8_code; |
| 324 | } |
| 325 | } |
| 326 | #else |
| 327 | GGML_UNUSED_VARS(x, ids, vy, scale, ne00, s01, s02, s03, ne0, ne1, ne2, n_expert_used); |
| 328 | NO_DEVICE_CODE; // This is for Blackwell NVFP4 activations only. |
| 329 | #endif // defined(BLACKWELL_MMA_AVAILABLE) |
| 330 | |
| 331 | } |
| 332 | |
| 333 | // quantize values in the format mxfp4 is stored which is interleaved nibbles |
| 334 | // i.e. a block a0-a31 is represented as a0a16,a1a17 ...a15a31 |
| 335 | // scatter: grid over tokens, quantize once, write to all the token's compact rows |
| 336 | template <bool scatter> |
| 337 | static __global__ void quantize_mmq_mxfp4(const float * __restrict__ x, |
| 338 | const int32_t * __restrict__ ids, |
| 339 | void * __restrict__ vy, |
| 340 | const int64_t ne00, |
| 341 | const int64_t s01, |
| 342 | const int64_t s02, |
| 343 | const int64_t s03, |
| 344 | const int64_t ne0, |
| 345 | const int ne1, |
| 346 | const int ne2, |
| 347 | const int n_expert_used) { |
| 348 | constexpr int vals_per_scale = 32; |
| 349 | constexpr int vals_per_warp = 2 * vals_per_scale; // Each warp processes 2 blocks of 32 = 64 values |
| 350 | |
| 351 | const int warp_id = threadIdx.y; |
| 352 | const int lane_id_32 = threadIdx.x; |
| 353 | |
| 354 | const int nwarps = blockDim.y; |
| 355 | |
| 356 | const int64_t warp_start_offset = (blockIdx.y * nwarps + warp_id) * vals_per_warp; |
| 357 | |
| 358 | if (warp_start_offset >= ne0) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | const int64_t block_fp4_mmq_size = QK_FP4_MMQ; |
| 363 | const int64_t k_block = warp_start_offset / block_fp4_mmq_size; |
| 364 | const int64_t quad_idx_in_block = (warp_start_offset % block_fp4_mmq_size) / vals_per_warp; |
| 365 | |
| 366 | const int group_id = lane_id_32 / 4; |
| 367 | const int lane_in_group = lane_id_32 % 4; |
| 368 | const int base = group_id * 2; |
| 369 | |
| 370 | ggml_cuda_pdl_sync(); |
| 371 | int64_t base_pos; |
| 372 | if constexpr (scatter) { |
| 373 | base_pos = (int64_t) blockIdx.x * s02; // one physical row per token |
| 374 | } else { |
| 375 | const int64_t i2 = blockIdx.z % ne2; |
| 376 | const int64_t i3 = blockIdx.z / ne2; |
| 377 | const int64_t i01 = ids ? ids[blockIdx.x] : blockIdx.x; |
| 378 | base_pos = i3 * s03 + i2 * s02 + i01 * s01; |
| 379 | } |
| 380 | |
| 381 | uint8_t scales[2]; |
| 382 | char2 packed[2]; |
| 383 | |
| 384 | #pragma unroll |
| 385 | for (int b = 0; b < 2; ++b) { |
| 386 | const int64_t i0 = warp_start_offset + b * vals_per_scale + lane_id_32; |
| 387 | const float xi = (i0 < ne00) ? x[base_pos + i0] : 0.0f; |
| 388 | |
| 389 | float amax = fabsf(xi); |
| 390 | #pragma unroll |
| 391 | for (int mask = 16; mask > 0; mask >>= 1) { |
| 392 | amax = fmaxf(amax, __shfl_xor_sync(0xFFFFFFFF, amax, mask, WARP_SIZE)); |
| 393 | } |
| 394 | |
| 395 | const uint8_t e = compute_e8m0_scale(amax); |
| 396 | scales[b] = e; |
| 397 | const float inv_s = (amax == 0.0f) ? 0.0f : __frcp_rn(ggml_cuda_e8m0_to_fp32(e)); |
| 398 | |
| 399 | #if CUDART_VERSION >= 12080 |
| 400 | const float scaled_val = xi * inv_s; |
| 401 | |
| 402 | const float val0 = __shfl_sync(0xFFFFFFFF, scaled_val, base, WARP_SIZE); |
| 403 | const float val1 = __shfl_sync(0xFFFFFFFF, scaled_val, base + 16, WARP_SIZE); |
| 404 | const float val2 = __shfl_sync(0xFFFFFFFF, scaled_val, base + 1, WARP_SIZE); |
| 405 | const float val3 = __shfl_sync(0xFFFFFFFF, scaled_val, base + 17, WARP_SIZE); |
| 406 | |
| 407 | __nv_fp4x4_e2m1 fp4_packed(make_float4(val0, val1, val2, val3)); |
| 408 | packed[b] = *(char2 *) &fp4_packed; |
| 409 | #else |
| 410 | // Fallback: manual FP4 conversion using LUT |
| 411 | const uint8_t q_val = ggml_cuda_float_to_fp4_e2m1(xi, inv_s); |
| 412 | |
| 413 | const uint8_t q_lo_0 = __shfl_sync(0xFFFFFFFF, q_val, base, WARP_SIZE); |
| 414 | const uint8_t q_lo_1 = __shfl_sync(0xFFFFFFFF, q_val, base + 1, WARP_SIZE); |
| 415 | const uint8_t q_hi_0 = __shfl_sync(0xFFFFFFFF, q_val, base + 16, WARP_SIZE); |
| 416 | const uint8_t q_hi_1 = __shfl_sync(0xFFFFFFFF, q_val, base + 17, WARP_SIZE); |
| 417 | |
| 418 | char2 q; |
| 419 | q.x = (q_hi_0 << 4) | q_lo_0; |
| 420 | q.y = (q_hi_1 << 4) | q_lo_1; |
| 421 | packed[b] = q; |
| 422 | #endif // CUDART_VERSION >= 12080 |
| 423 | } |
| 424 | |
| 425 | block_fp4_mmq * y = (block_fp4_mmq *) vy; |
| 426 | if constexpr (scatter) { |
| 427 | #pragma unroll |
| 428 | for (int slot = 0; slot < n_expert_used; ++slot) { |
| 429 | const int64_t i = ids[(int64_t) blockIdx.x * n_expert_used + slot]; |
| 430 | block_fp4_mmq * yb = y + (k_block * ne1 + i); |
| 431 | char2 * yqs2 = (char2 *) yb->qs; |
| 432 | if (lane_in_group == 0) { |
| 433 | yqs2[quad_idx_in_block * 16 + 0 * 8 + group_id] = packed[0]; |
| 434 | yqs2[quad_idx_in_block * 16 + 1 * 8 + group_id] = packed[1]; |
| 435 | } |
| 436 | if (lane_id_32 == 0) { |
| 437 | yb->d4[quad_idx_in_block] = (scales[1] << 8) | scales[0]; |
| 438 | } |
| 439 | } |
| 440 | } else { |
| 441 | const int64_t ib0 = blockIdx.z * ((int64_t) ne1 * (ne0 / block_fp4_mmq_size)); |
| 442 | block_fp4_mmq * yb = y + (ib0 + k_block * ne1 + blockIdx.x); |
| 443 | char2 * yqs2 = (char2 *) yb->qs; |
| 444 | if (lane_in_group == 0) { |
| 445 | yqs2[quad_idx_in_block * 16 + 0 * 8 + group_id] = packed[0]; |
| 446 | yqs2[quad_idx_in_block * 16 + 1 * 8 + group_id] = packed[1]; |
| 447 | } |
| 448 | if (lane_id_32 == 0) { |
| 449 | yb->d4[quad_idx_in_block] = (scales[1] << 8) | scales[0]; |
| 450 | } |
| 451 | } |
| 452 | GGML_UNUSED(n_expert_used); |
| 453 | } |
| 454 | |
| 455 | // scatter: grid over tokens, quantize once, write to all the token's compact rows |
| 456 | template <mmq_q8_1_ds_layout ds_layout, bool scatter> |
| 457 | static __global__ void quantize_mmq_q8_1( |
| 458 | const float * __restrict__ x, const int32_t * __restrict__ ids, void * __restrict__ vy, |
| 459 | const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03, |
| 460 | const int64_t ne0, const int ne1, const int ne2, const int n_expert_used) { |
| 461 | |
| 462 | constexpr int vals_per_scale = ds_layout == MMQ_Q8_1_DS_LAYOUT_D2S6 ? 64 : 32; |
| 463 | constexpr int vals_per_sum = ds_layout == MMQ_Q8_1_DS_LAYOUT_D2S6 ? 16 : 32; |
| 464 | |
| 465 | const int64_t i0 = ((int64_t)blockDim.x*blockIdx.y + threadIdx.x)*4; |
| 466 | |
| 467 | if (i0 >= ne0) { |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | const int64_t i00 = i0; |
| 472 | ggml_cuda_pdl_sync(); |
| 473 | |
| 474 | int64_t base_idx; |
| 475 | if constexpr (scatter) { |
| 476 | base_idx = (int64_t) blockIdx.x * s02; // one physical row per token |
| 477 | } else { |
| 478 | const int64_t i2 = blockIdx.z % ne2; |
| 479 | const int64_t i3 = blockIdx.z / ne2; |
| 480 | const int64_t i01 = ids ? ids[blockIdx.x] : blockIdx.x; |
| 481 | base_idx = i3*s03 + i2*s02 + i01*s01; |
| 482 | } |
| 483 | |
| 484 | const float4 * x4 = (const float4 *) x; |
| 485 | block_q8_1_mmq * y = (block_q8_1_mmq *) vy; |
| 486 | |
| 487 | const int64_t k_block = i0 / QK8_1_MMQ; // column block in the channel |
| 488 | const int64_t iqs = i0 % QK8_1_MMQ; // quant index in block |
| 489 | |
| 490 | // Load 4 floats per thread and calculate max. abs. value between them: |
| 491 | const float4 xi = i0 < ne00 ? x4[(base_idx + i00)/4] : make_float4(0.0f, 0.0f, 0.0f, 0.0f); |
| 492 | float amax = fabsf(xi.x); |
| 493 | amax = fmaxf(amax, fabsf(xi.y)); |
| 494 | amax = fmaxf(amax, fabsf(xi.z)); |
| 495 | amax = fmaxf(amax, fabsf(xi.w)); |
| 496 | |
| 497 | // Exchange max. abs. value between vals_per_scale/4 threads. |
| 498 | #pragma unroll |
| 499 | for (int offset = vals_per_scale/8; offset > 0; offset >>= 1) { |
| 500 | amax = fmaxf(amax, __shfl_xor_sync(0xFFFFFFFF, amax, offset, WARP_SIZE)); |
| 501 | } |
| 502 | |
| 503 | const float d_inv = 127.0f / amax; |
| 504 | char4 q; |
| 505 | q.x = roundf(xi.x*d_inv); |
| 506 | q.y = roundf(xi.y*d_inv); |
| 507 | q.z = roundf(xi.z*d_inv); |
| 508 | q.w = roundf(xi.w*d_inv); |
| 509 | const float d = 1.0f / d_inv; |
| 510 | |
| 511 | float sum = 0.0f; |
| 512 | if (ds_layout != MMQ_Q8_1_DS_LAYOUT_D4) { |
| 513 | int sum_q = int(q.x) + int(q.y) + int(q.z) + int(q.w); |
| 514 | |
| 515 | // Calculate sums across vals_per_sum/4 threads. |
| 516 | #pragma unroll |
| 517 | for (int offset = vals_per_sum/8; offset > 0; offset >>= 1) { |
| 518 | sum_q += __shfl_xor_sync(0xFFFFFFFF, sum_q, offset, WARP_SIZE); |
| 519 | } |
| 520 | |
| 521 | sum = d * float(sum_q); |
| 522 | } |
| 523 | |
| 524 | // write the block once (normal) or to each of the token's compact rows (scatter) |
| 525 | const int nwrite = scatter ? n_expert_used : 1; |
| 526 | #pragma unroll |
| 527 | for (int slot = 0; slot < nwrite; ++slot) { |
| 528 | int64_t ib; |
| 529 | if constexpr (scatter) { |
| 530 | const int64_t i = ids[(int64_t) blockIdx.x * n_expert_used + slot]; |
| 531 | ib = k_block*ne1 + i; |
| 532 | } else { |
| 533 | const int64_t ib0 = blockIdx.z*((int64_t)gridDim.x*gridDim.y*blockDim.x/QK8_1); // first block of channel |
| 534 | ib = ib0 + k_block*ne1 + blockIdx.x; |
| 535 | } |
| 536 | |
| 537 | // Write back 4 int8 values as a single 32 bit value for better memory bandwidth: |
| 538 | char4 * yqs4 = (char4 *) y[ib].qs; |
| 539 | yqs4[iqs/4] = q; |
| 540 | |
| 541 | if (ds_layout == MMQ_Q8_1_DS_LAYOUT_D2S6) { |
| 542 | if (iqs % 16 == 0 && iqs < 96) { |
| 543 | y[ib].d2s6[2 + iqs/16] = sum; |
| 544 | if (iqs % 64 == 0) { |
| 545 | y[ib].d2s6[iqs/64] = d; |
| 546 | } |
| 547 | } |
| 548 | } else if (iqs % 32 == 0) { |
| 549 | if (ds_layout == MMQ_Q8_1_DS_LAYOUT_DS4) { |
| 550 | y[ib].ds4[iqs/32] = make_half2(d, sum); |
| 551 | } else { |
| 552 | y[ib].d4[iqs/32] = d; |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | GGML_UNUSED(n_expert_used); |
| 557 | } |
| 558 | |
| 559 | void quantize_row_q8_1_cuda( |
| 560 | const float * x, const int32_t * ids, void * vy, const ggml_type type_src0, |
| 561 | const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03, |
| 562 | const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) { |
| 563 | GGML_ASSERT(!ids); |
| 564 | GGML_ASSERT(ne0 % QK8_1 == 0); |
| 565 | |
| 566 | const uint3 ne2_fastdiv = init_fastdiv_values(ne2); |
| 567 | |
| 568 | const int64_t block_num_x = (ne0 + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE; |
| 569 | const dim3 num_blocks(block_num_x, ne1, ne2*ne3); |
| 570 | const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE, 1, 1); |
| 571 | const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(num_blocks, block_size, 0, stream); |
| 572 | ggml_cuda_kernel_launch(quantize_q8_1, launch_params, x, vy, ne00, s01, s02, s03, ne0, ne1, ne2_fastdiv); |
| 573 | GGML_UNUSED(type_src0); |
| 574 | } |
| 575 | |
| 576 | void quantize_mmq_q8_1_cuda( |
| 577 | const float * x, const int32_t * ids, void * vy, const ggml_type type_src0, |
| 578 | const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03, |
| 579 | const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) { |
| 580 | GGML_ASSERT(ne00 % 4 == 0); |
| 581 | GGML_ASSERT(ne0 % QK8_1_MMQ == 0); |
| 582 | |
| 583 | // ne1 tends to assume the highest values, therefore use it as the "x" dimension of the CUDA grid: |
| 584 | const int64_t block_num_y = (ne0 + 4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ - 1) / (4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ); |
| 585 | const dim3 num_blocks(ne1, block_num_y, ne2*ne3); |
| 586 | const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1); |
| 587 | switch (mmq_get_q8_1_ds_layout(type_src0)) { |
| 588 | case MMQ_Q8_1_DS_LAYOUT_D4: |
| 589 | quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D4, false> |
| 590 | <<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0); |
| 591 | break; |
| 592 | case MMQ_Q8_1_DS_LAYOUT_DS4: |
| 593 | quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_DS4, false> |
| 594 | <<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0); |
| 595 | break; |
| 596 | case MMQ_Q8_1_DS_LAYOUT_D2S6: |
| 597 | quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D2S6, false> |
| 598 | <<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0); |
| 599 | break; |
| 600 | default: |
| 601 | GGML_ABORT("fatal error"); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | // scatter=true reuses the quant kernel: grid over tokens, ids = inverse map (token slot -> compact row) |
| 606 | void quantize_scatter_mmq_q8_1_cuda( |
| 607 | const float * x, const int32_t * ids_src1_inv, void * vy, const ggml_type type_src0, |
| 608 | const int64_t ne00, const int64_t stride_token, const int64_t ne0, |
| 609 | const int64_t n_tokens, const int64_t nrows_dst, const int n_expert_used, cudaStream_t stream) { |
| 610 | GGML_ASSERT(ne00 % 4 == 0); |
| 611 | GGML_ASSERT(ne0 % QK8_1_MMQ == 0); |
| 612 | |
| 613 | const int64_t block_num_y = (ne0 + 4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ - 1) / (4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ); |
| 614 | const dim3 num_blocks(n_tokens, block_num_y, 1); |
| 615 | const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1); |
| 616 | switch (mmq_get_q8_1_ds_layout(type_src0)) { |
| 617 | case MMQ_Q8_1_DS_LAYOUT_D4: |
| 618 | quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D4, true><<<num_blocks, block_size, 0, stream>>>( |
| 619 | x, ids_src1_inv, vy, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/(int) nrows_dst, /*ne2=*/1, n_expert_used); |
| 620 | break; |
| 621 | case MMQ_Q8_1_DS_LAYOUT_DS4: |
| 622 | quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_DS4, true><<<num_blocks, block_size, 0, stream>>>( |
| 623 | x, ids_src1_inv, vy, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/(int) nrows_dst, /*ne2=*/1, n_expert_used); |
| 624 | break; |
| 625 | case MMQ_Q8_1_DS_LAYOUT_D2S6: |
| 626 | quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D2S6, true><<<num_blocks, block_size, 0, stream>>>( |
| 627 | x, ids_src1_inv, vy, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/(int) nrows_dst, /*ne2=*/1, n_expert_used); |
| 628 | break; |
| 629 | default: |
| 630 | GGML_ABORT("fatal error"); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | // scatter=true reuses the quant kernels: grid over tokens, ids = inverse map (token slot -> compact row) |
| 635 | void quantize_scatter_mmq_fp4_cuda( |
| 636 | const float * x, const int32_t * ids_src1_inv, void * vy, float * scale, const ggml_type type_src0, const bool use_aligned_float8, |
| 637 | const int64_t ne00, const int64_t stride_token, const int64_t ne0, |
| 638 | const int64_t n_tokens, const int64_t nrows_dst, const int n_expert_used, cudaStream_t stream) { |
| 639 | GGML_ASSERT(ne0 > 0); |
| 640 | if (type_src0 == GGML_TYPE_NVFP4) { |
| 641 | GGML_ASSERT(scale); |
| 642 | GGML_ASSERT(ne00 % QK_NVFP4 == 0); |
| 643 | const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1); |
| 644 | const dim3 num_blocks(n_tokens, 1, 1); |
| 645 | if (use_aligned_float8) { |
| 646 | quantize_mmq_nvfp4<true, true><<<num_blocks, block_size, 0, stream>>>( |
| 647 | x, ids_src1_inv, vy, scale, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/nrows_dst, /*ne2=*/1, n_expert_used); |
| 648 | } else { |
| 649 | quantize_mmq_nvfp4<true, false><<<num_blocks, block_size, 0, stream>>>( |
| 650 | x, ids_src1_inv, vy, scale, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/nrows_dst, /*ne2=*/1, n_expert_used); |
| 651 | } |
| 652 | } else { |
| 653 | GGML_ASSERT(type_src0 == GGML_TYPE_MXFP4); |
| 654 | constexpr int nwarps = 8; |
| 655 | constexpr int vals_per_block = nwarps * 2 * QK_MXFP4; |
| 656 | const int64_t block_num_y = (ne0 + vals_per_block - 1) / vals_per_block; |
| 657 | const dim3 block_size(WARP_SIZE, nwarps, 1); |
| 658 | const dim3 num_blocks(n_tokens, block_num_y, 1); |
| 659 | quantize_mmq_mxfp4<true><<<num_blocks, block_size, 0, stream>>>( |
| 660 | x, ids_src1_inv, vy, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/(int) nrows_dst, /*ne2=*/1, n_expert_used); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | void quantize_mmq_fp4_cuda( |
| 665 | const float * x, const int32_t * ids, void * vy, float * scale, const ggml_type type_src0, const bool use_aligned_float8, |
| 666 | const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03, |
| 667 | const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) { |
| 668 | GGML_ASSERT(type_src0 == GGML_TYPE_MXFP4 || type_src0 == GGML_TYPE_NVFP4); |
| 669 | GGML_ASSERT(ne0 > 0); |
| 670 | |
| 671 | if (type_src0 == GGML_TYPE_NVFP4) { |
| 672 | GGML_ASSERT(scale); |
| 673 | GGML_ASSERT(ne00 % QK_NVFP4 == 0); |
| 674 | const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1); |
| 675 | const dim3 num_blocks(ne1, ne2 * ne3, 1); |
| 676 | if (use_aligned_float8) { |
| 677 | quantize_mmq_nvfp4<false, true><<<num_blocks, block_size, 0, stream>>>( |
| 678 | x, ids, vy, scale, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0); |
| 679 | } else { |
| 680 | quantize_mmq_nvfp4<false, false><<<num_blocks, block_size, 0, stream>>>( |
| 681 | x, ids, vy, scale, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0); |
| 682 | } |
| 683 | } else { |
| 684 | GGML_ASSERT(ne0 % (2 * QK_MXFP4) == 0); |
| 685 | |
| 686 | constexpr int nwarps = 8; |
| 687 | constexpr int vals_per_warp = 2 * QK_MXFP4; |
| 688 | constexpr int vals_per_block = nwarps * vals_per_warp; |
| 689 | |
| 690 | const int64_t block_num_y = (ne0 + vals_per_block - 1) / vals_per_block; |
| 691 | const dim3 num_blocks(ne1, block_num_y, ne2 * ne3); |
| 692 | const dim3 block_size(WARP_SIZE, nwarps, 1); |
| 693 | |
| 694 | quantize_mmq_mxfp4<false><<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0); |
| 695 | } |
| 696 | } |