ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cuda / convert.cu
| 1 | #include "convert.cuh" |
| 2 | #include "dequantize.cuh" |
| 3 | |
| 4 | #include <cstdint> |
| 5 | |
| 6 | #define CUDA_Q8_0_NE_ALIGN 2048 |
| 7 | |
| 8 | template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t> |
| 9 | static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, |
| 10 | const int64_t ne00, const int64_t ne01, |
| 11 | const int64_t ne0203, const uint3 ne02, |
| 12 | const int64_t s01, const int64_t s02, const int64_t s03) { |
| 13 | const int64_t i00 = 2 * (int64_t(blockDim.x)*blockIdx.x + threadIdx.x); |
| 14 | |
| 15 | if (i00 >= ne00) { |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | for (int64_t i01 = blockIdx.y; i01 < ne01; i01 += gridDim.y) { |
| 20 | for (int64_t i0203 = blockIdx.z; i0203 < ne0203; i0203 += gridDim.z) { |
| 21 | const uint2 dm = fast_div_modulo((uint32_t)i0203, ne02); |
| 22 | const int64_t i02 = dm.y; |
| 23 | const int64_t i03 = dm.x; |
| 24 | |
| 25 | const int64_t ibx0 = i03*s03 + i02*s02 + i01*s01; |
| 26 | |
| 27 | const int64_t ib = ibx0 + i00/qk; // block index |
| 28 | const int64_t iqs = (i00%qk)/qr; // quant index |
| 29 | const int64_t iybs = i00 - i00%qk; // y block start index |
| 30 | const int64_t y_offset = qr == 1 ? 1 : qk/2; |
| 31 | |
| 32 | // dequantize |
| 33 | float2 v; |
| 34 | dequantize_kernel(vx, ib, iqs, v); |
| 35 | |
| 36 | const int64_t iy0 = (i0203*ne01 + i01)*ne00 + iybs + iqs; |
| 37 | y[iy0 + 0] = ggml_cuda_cast<dst_t>(v.x); |
| 38 | y[iy0 + y_offset] = ggml_cuda_cast<dst_t>(v.y); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | template <bool need_check> |
| 44 | static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int64_t k) { |
| 45 | #if __CUDA_ARCH__ >= GGML_CUDA_CC_PASCAL |
| 46 | constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE; |
| 47 | |
| 48 | const int64_t i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x; |
| 49 | const int * x0 = ((int *) vx) + blockIdx.x * nint; |
| 50 | half2 * y2 = (half2 *) (y + i0); |
| 51 | |
| 52 | __shared__ int vals[nint]; |
| 53 | |
| 54 | #pragma unroll |
| 55 | for (int ix0 = 0; ix0 < nint; ix0 += WARP_SIZE) { |
| 56 | if (need_check && i0*sizeof(block_q8_0)/QK8_0 + sizeof(int)*(ix0 + threadIdx.x) >= k*sizeof(block_q8_0)/QK8_0) { |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | const int ix = ix0 + threadIdx.x; |
| 61 | vals[ix] = x0[ix]; |
| 62 | } |
| 63 | |
| 64 | __syncthreads(); |
| 65 | |
| 66 | #pragma unroll |
| 67 | for (int iy = 0; iy < CUDA_Q8_0_NE_ALIGN; iy += 2*WARP_SIZE) { |
| 68 | if (need_check && i0 + iy + 2*threadIdx.x >= k) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | const half * b0 = ((const half *) vals) + (sizeof(block_q8_0)/sizeof(half)) * ((iy + 2*threadIdx.x)/QK8_0); |
| 73 | const half d = *b0; |
| 74 | const char2 qs = ((const char2 *) (b0 + 1))[threadIdx.x % (QK8_0/2)]; |
| 75 | |
| 76 | y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d)); |
| 77 | } |
| 78 | #else |
| 79 | GGML_UNUSED_VARS(vx, y, k); |
| 80 | NO_DEVICE_CODE; |
| 81 | #endif // __CUDA_ARCH__ >= GGML_CUDA_CC_PASCAL |
| 82 | } |
| 83 | |
| 84 | template<typename dst_t> |
| 85 | static __global__ void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) { |
| 86 | |
| 87 | const int64_t i = blockIdx.x; |
| 88 | |
| 89 | // assume 32 threads |
| 90 | const int64_t tid = threadIdx.x; |
| 91 | const int64_t il = tid/8; |
| 92 | const int64_t ir = tid%8; |
| 93 | const int64_t ib = 8*i + ir; |
| 94 | if (ib >= nb32) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | dst_t * y = yy + 256*i + 32*ir + 4*il; |
| 99 | |
| 100 | const block_q4_0 * x = (const block_q4_0 *)vx + ib; |
| 101 | const float d = __half2float(x->d); |
| 102 | const float dm = -8*d; |
| 103 | |
| 104 | const uint8_t * q = x->qs + 4*il; |
| 105 | |
| 106 | for (int l = 0; l < 4; ++l) { |
| 107 | y[l+ 0] = ggml_cuda_cast<dst_t>(d * (q[l] & 0xF) + dm); |
| 108 | y[l+16] = ggml_cuda_cast<dst_t>(d * (q[l] >> 4) + dm); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | template<typename dst_t> |
| 113 | static __global__ void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) { |
| 114 | |
| 115 | const int64_t i = blockIdx.x; |
| 116 | |
| 117 | // assume 32 threads |
| 118 | const int64_t tid = threadIdx.x; |
| 119 | const int64_t il = tid/8; |
| 120 | const int64_t ir = tid%8; |
| 121 | const int64_t ib = 8*i + ir; |
| 122 | if (ib >= nb32) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | dst_t * y = yy + 256*i + 32*ir + 4*il; |
| 127 | |
| 128 | const block_q4_1 * x = (const block_q4_1 *)vx + ib; |
| 129 | const float2 d = __half22float2(x->dm); |
| 130 | |
| 131 | const uint8_t * q = x->qs + 4*il; |
| 132 | |
| 133 | for (int l = 0; l < 4; ++l) { |
| 134 | y[l+ 0] = ggml_cuda_cast<dst_t>(d.x * (q[l] & 0xF) + d.y); |
| 135 | y[l+16] = ggml_cuda_cast<dst_t>(d.x * (q[l] >> 4) + d.y); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | //================================== k-quants |
| 140 | |
| 141 | template<typename dst_t> |
| 142 | static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 143 | const int64_t i = blockIdx.x; |
| 144 | |
| 145 | dequantize_q2_K(vx, i, yy + i*QK_K, threadIdx.x); |
| 146 | } |
| 147 | |
| 148 | template<typename dst_t> |
| 149 | static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 150 | const int64_t i = blockIdx.x; |
| 151 | |
| 152 | dequantize_q3_K(vx, i, yy + i*QK_K, threadIdx.x); |
| 153 | } |
| 154 | |
| 155 | template<typename dst_t> |
| 156 | static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 157 | const int64_t i = blockIdx.x; |
| 158 | |
| 159 | dequantize_q4_K(vx, i, yy + i*QK_K, threadIdx.x); |
| 160 | } |
| 161 | |
| 162 | template<typename dst_t> |
| 163 | static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 164 | const int64_t i = blockIdx.x; |
| 165 | |
| 166 | dequantize_q5_K(vx, i, yy + i*QK_K, threadIdx.x); |
| 167 | } |
| 168 | |
| 169 | template<typename dst_t> |
| 170 | static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 171 | const int64_t i = blockIdx.x; |
| 172 | |
| 173 | dequantize_q6_K(vx, i, yy + i*QK_K, threadIdx.x); |
| 174 | } |
| 175 | |
| 176 | template<typename dst_t> |
| 177 | static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 178 | const int64_t i = blockIdx.x; |
| 179 | |
| 180 | dequantize_iq2_xxs(vx, i, yy + i*QK_K, threadIdx.x); |
| 181 | } |
| 182 | |
| 183 | template<typename dst_t> |
| 184 | static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 185 | const int64_t i = blockIdx.x; |
| 186 | |
| 187 | dequantize_iq2_xs(vx, i, yy + i*QK_K, threadIdx.x); |
| 188 | } |
| 189 | |
| 190 | template<typename dst_t> |
| 191 | static __global__ void dequantize_block_iq2_s(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 192 | const int64_t i = blockIdx.x; |
| 193 | |
| 194 | dequantize_iq2_s(vx, i, yy + i*QK_K, threadIdx.x); |
| 195 | } |
| 196 | |
| 197 | template<typename dst_t> |
| 198 | static __global__ void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 199 | const int64_t i = blockIdx.x; |
| 200 | |
| 201 | dequantize_iq3_xxs(vx, i, yy + i*QK_K, threadIdx.x); |
| 202 | } |
| 203 | |
| 204 | template<typename dst_t> |
| 205 | static __global__ void dequantize_block_iq3_s(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 206 | const int64_t i = blockIdx.x; |
| 207 | |
| 208 | dequantize_iq3_s(vx, i, yy + i*QK_K, threadIdx.x); |
| 209 | } |
| 210 | |
| 211 | template<typename dst_t> |
| 212 | static __global__ void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 213 | const int64_t i = blockIdx.x; |
| 214 | |
| 215 | dequantize_iq1_s(vx, i, yy + i*QK_K, threadIdx.x); |
| 216 | } |
| 217 | |
| 218 | template<typename dst_t> |
| 219 | static __global__ void dequantize_block_iq1_m(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 220 | const int64_t i = blockIdx.x; |
| 221 | |
| 222 | dequantize_iq1_m(vx, i, yy + i*QK_K, threadIdx.x); |
| 223 | } |
| 224 | |
| 225 | template<typename dst_t> |
| 226 | static __global__ void dequantize_block_iq4_nl(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) { |
| 227 | const int64_t i = blockIdx.x; |
| 228 | |
| 229 | const int64_t ib = 8*i + threadIdx.x%8; |
| 230 | if (ib >= nb32) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | dequantize_iq4_nl(vx, i, yy + i*QK_K, threadIdx.x); |
| 235 | } |
| 236 | |
| 237 | template<typename dst_t> |
| 238 | static __global__ void dequantize_block_iq4_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 239 | const int64_t i = blockIdx.x; |
| 240 | |
| 241 | dequantize_iq4_xs(vx, i, yy + i*QK_K, threadIdx.x); |
| 242 | } |
| 243 | |
| 244 | template<typename dst_t> |
| 245 | static __global__ void dequantize_block_mxfp4(const void * __restrict__ vx, dst_t * __restrict__ yy) { |
| 246 | const int64_t i = blockIdx.x; |
| 247 | |
| 248 | dequantize_mxfp4(vx, i, yy + i*QK_K, threadIdx.x); |
| 249 | } |
| 250 | |
| 251 | template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t> |
| 252 | static void dequantize_block_cuda(const void * vx, dst_t * y, |
| 253 | const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t ne03, |
| 254 | const int64_t s01, const int64_t s02, const int64_t s03, cudaStream_t stream) { |
| 255 | const int64_t ne0203 = ne02*ne03; |
| 256 | const uint3 ne02_fdv = init_fastdiv_values(ne02); |
| 257 | const dim3 num_blocks((ne00 + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE), (int)std::min(ne01, (int64_t)65535), (int)std::min(ne0203, (int64_t)65535)); |
| 258 | dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>> |
| 259 | (vx, y, ne00, ne01, ne0203, ne02_fdv, s01, s02, s03); |
| 260 | } |
| 261 | |
| 262 | template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t> |
| 263 | static void dequantize_block_cont_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t k, cudaStream_t stream) { |
| 264 | dequantize_block_cuda<qk, qr, dequantize_kernel, dst_t>(vx, y, k, 1, 1, 1, k/qk, k/qk, k/qk, stream); |
| 265 | } |
| 266 | |
| 267 | static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half * __restrict__ y, const int64_t k, cudaStream_t stream) { |
| 268 | const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN; |
| 269 | if (k % CUDA_Q8_0_NE_ALIGN == 0) { |
| 270 | const bool need_check = false; |
| 271 | dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k); |
| 272 | } else { |
| 273 | const bool need_check = true; |
| 274 | dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | template<typename dst_t> |
| 279 | static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 280 | const int nb = k / QK_K; |
| 281 | dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y); |
| 282 | } |
| 283 | |
| 284 | template<typename dst_t> |
| 285 | static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 286 | const int nb = k / QK_K; |
| 287 | dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y); |
| 288 | } |
| 289 | |
| 290 | template<typename dst_t> |
| 291 | static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 292 | const int nb32 = k / 32; |
| 293 | const int nb = (k + 255) / 256; |
| 294 | dequantize_block_q4_0<<<nb, 32, 0, stream>>>(vx, y, nb32); |
| 295 | } |
| 296 | |
| 297 | template<typename dst_t> |
| 298 | static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 299 | const int nb32 = k / 32; |
| 300 | const int nb = (k + 255) / 256; |
| 301 | dequantize_block_q4_1<<<nb, 32, 0, stream>>>(vx, y, nb32); |
| 302 | } |
| 303 | |
| 304 | template<typename dst_t> |
| 305 | static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 306 | const int nb = k / QK_K; |
| 307 | dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y); |
| 308 | } |
| 309 | |
| 310 | template<typename dst_t> |
| 311 | static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 312 | const int nb = k / QK_K; |
| 313 | dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y); |
| 314 | } |
| 315 | |
| 316 | template<typename dst_t> |
| 317 | static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 318 | const int nb = k / QK_K; |
| 319 | dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y); |
| 320 | } |
| 321 | |
| 322 | template<typename dst_t> |
| 323 | static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 324 | const int nb = k / QK_K; |
| 325 | dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y); |
| 326 | } |
| 327 | |
| 328 | template<typename dst_t> |
| 329 | static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 330 | const int nb = k / QK_K; |
| 331 | dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y); |
| 332 | } |
| 333 | |
| 334 | template<typename dst_t> |
| 335 | static void dequantize_row_iq2_s_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 336 | const int nb = k / QK_K; |
| 337 | dequantize_block_iq2_s<<<nb, 32, 0, stream>>>(vx, y); |
| 338 | } |
| 339 | |
| 340 | template<typename dst_t> |
| 341 | static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 342 | const int nb = k / QK_K; |
| 343 | dequantize_block_iq3_xxs<<<nb, 32, 0, stream>>>(vx, y); |
| 344 | } |
| 345 | |
| 346 | template<typename dst_t> |
| 347 | static void dequantize_row_iq3_s_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 348 | const int nb = k / QK_K; |
| 349 | dequantize_block_iq3_s<<<nb, 32, 0, stream>>>(vx, y); |
| 350 | } |
| 351 | |
| 352 | template<typename dst_t> |
| 353 | static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 354 | const int nb = k / QK_K; |
| 355 | dequantize_block_iq1_s<<<nb, 32, 0, stream>>>(vx, y); |
| 356 | } |
| 357 | |
| 358 | template<typename dst_t> |
| 359 | static void dequantize_row_iq4_nl_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 360 | const int nb32 = k / QK4_NL; |
| 361 | const int nb = (k + QK_K - 1) / QK_K; |
| 362 | dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y, nb32); |
| 363 | } |
| 364 | |
| 365 | template<typename dst_t> |
| 366 | static void dequantize_row_iq1_m_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 367 | const int nb = k / QK_K; |
| 368 | dequantize_block_iq1_m<<<nb, 32, 0, stream>>>(vx, y); |
| 369 | } |
| 370 | |
| 371 | template<typename dst_t> |
| 372 | static void dequantize_row_iq4_xs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 373 | const int nb = (k + QK_K - 1) / QK_K; |
| 374 | dequantize_block_iq4_xs<<<nb, 32, 0, stream>>>(vx, y); |
| 375 | } |
| 376 | |
| 377 | template<typename dst_t> |
| 378 | static void dequantize_row_mxfp4_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 379 | const int nb = (k + QK_K - 1) / QK_K; |
| 380 | dequantize_block_mxfp4<<<nb, 32, 0, stream>>>(vx, y); |
| 381 | } |
| 382 | |
| 383 | template <typename dst_t> |
| 384 | static __global__ void dequantize_block_nvfp4( |
| 385 | const void * __restrict__ vx, |
| 386 | dst_t * __restrict__ yy, |
| 387 | const int64_t ne) { |
| 388 | const int64_t i = blockIdx.x; |
| 389 | const int tid = threadIdx.x; |
| 390 | |
| 391 | const int64_t base = i * QK_NVFP4; |
| 392 | if (base >= ne) { |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | const block_nvfp4 * x = (const block_nvfp4 *) vx; |
| 397 | const block_nvfp4 & xb = x[i]; |
| 398 | |
| 399 | const int sub = tid / (QK_NVFP4_SUB / 2); |
| 400 | const int j = tid % (QK_NVFP4_SUB / 2); |
| 401 | |
| 402 | const float d = ggml_cuda_ue4m3_to_fp32(xb.d[sub]); |
| 403 | const uint8_t q = xb.qs[sub * (QK_NVFP4_SUB / 2) + j]; |
| 404 | |
| 405 | const int64_t y0 = base + sub * QK_NVFP4_SUB + j; |
| 406 | const int64_t y1 = y0 + QK_NVFP4_SUB / 2; |
| 407 | |
| 408 | yy[y0] = ggml_cuda_cast<dst_t>(d * kvalues_mxfp4[q & 0x0F]); |
| 409 | yy[y1] = ggml_cuda_cast<dst_t>(d * kvalues_mxfp4[q >> 4]); |
| 410 | } |
| 411 | |
| 412 | template <typename dst_t> |
| 413 | static void dequantize_row_nvfp4_cuda( |
| 414 | const void * vx, |
| 415 | dst_t * y, |
| 416 | const int64_t k, |
| 417 | cudaStream_t stream) { |
| 418 | GGML_ASSERT(k % QK_NVFP4 == 0); |
| 419 | const int nb = k / QK_NVFP4; |
| 420 | dequantize_block_nvfp4<<<nb, 32, 0, stream>>>(vx, y, k); |
| 421 | } |
| 422 | template <typename src_t, typename dst_t> |
| 423 | static __global__ void convert_unary( |
| 424 | const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t ne00, const int64_t ne01, |
| 425 | const int64_t ne0203, const uint3 ne02, |
| 426 | const int64_t s01, const int64_t s02, const int64_t s03) { |
| 427 | const int64_t i00 = (int64_t)blockDim.x*blockIdx.x + threadIdx.x; |
| 428 | |
| 429 | if (i00 >= ne00) { |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | const src_t * x = (const src_t *) vx; |
| 434 | |
| 435 | for (int64_t i01 = blockIdx.y; i01 < ne01; i01 += gridDim.y) { |
| 436 | for (int64_t i0203 = blockIdx.z; i0203 < ne0203; i0203 += gridDim.z) { |
| 437 | const uint2 dm = fast_div_modulo((uint32_t)i0203, ne02); |
| 438 | const int64_t i02 = dm.y; |
| 439 | const int64_t i03 = dm.x; |
| 440 | |
| 441 | const int64_t ix = i03*s03 + i02*s02 + i01*s01 + i00; |
| 442 | const int64_t iy = (i0203*ne01 + i01)*ne00 + i00; |
| 443 | y[iy] = ggml_cuda_cast<dst_t>(x[ix]); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | template <typename T> struct alignas(sizeof(T)*4) cvt_vec4 { T v[4]; }; |
| 449 | |
| 450 | // four elements per thread, so a warp moves 512B (RDNA) / 1k (CDNA) per load |
| 451 | template <typename src_t, typename dst_t> |
| 452 | static __global__ void convert_unary_cont_vec4( |
| 453 | const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t k4) { |
| 454 | const int64_t i = (int64_t)blockDim.x*blockIdx.x + threadIdx.x; |
| 455 | |
| 456 | if (i >= k4) { |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | const cvt_vec4<src_t> xv = ((const cvt_vec4<src_t> *) vx)[i]; |
| 461 | |
| 462 | cvt_vec4<dst_t> yv; |
| 463 | #pragma unroll |
| 464 | for (int j = 0; j < 4; ++j) { |
| 465 | yv.v[j] = ggml_cuda_cast<dst_t>(xv.v[j]); |
| 466 | } |
| 467 | |
| 468 | ((cvt_vec4<dst_t> *) y)[i] = yv; |
| 469 | } |
| 470 | |
| 471 | template <typename src_t, typename dst_t> |
| 472 | static void convert_unary_cuda(const void * vx, dst_t * y, |
| 473 | const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t ne03, |
| 474 | const int64_t s01, const int64_t s02, const int64_t s03, cudaStream_t stream) { |
| 475 | const int64_t ne0203 = ne02*ne03; |
| 476 | const uint3 ne02_fdv = init_fastdiv_values(ne02); |
| 477 | const dim3 num_blocks((ne00 + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE, (int)std::min(ne01, (int64_t)65535), (int)std::min(ne0203, (int64_t)65535)); |
| 478 | convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>> |
| 479 | (vx, y, ne00, ne01, ne0203, ne02_fdv, s01, s02, s03); |
| 480 | } |
| 481 | |
| 482 | template <typename src_t, typename dst_t> |
| 483 | static void convert_unary_cont_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { |
| 484 | if (k % 4 == 0 && |
| 485 | (uintptr_t) vx % alignof(cvt_vec4<src_t>) == 0 && |
| 486 | (uintptr_t) y % alignof(cvt_vec4<dst_t>) == 0) { |
| 487 | const int64_t k4 = k/4; |
| 488 | const int64_t num_blocks = (k4 + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE; |
| 489 | convert_unary_cont_vec4<src_t, dst_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k4); |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | convert_unary_cuda<src_t>(vx, y, k, 1, 1, 1, k, k, k, stream); |
| 494 | } |
| 495 | |
| 496 | to_bf16_cuda_t ggml_get_to_bf16_cuda(ggml_type type) { |
| 497 | switch (type) { |
| 498 | case GGML_TYPE_Q1_0: |
| 499 | return dequantize_block_cont_cuda<QK1_0, QR1_0, dequantize_q1_0>; |
| 500 | case GGML_TYPE_Q2_0: |
| 501 | return dequantize_block_cont_cuda<QK2_0, QR2_0, dequantize_q2_0>; |
| 502 | case GGML_TYPE_Q4_0: |
| 503 | return dequantize_row_q4_0_cuda; |
| 504 | case GGML_TYPE_Q4_1: |
| 505 | return dequantize_row_q4_1_cuda; |
| 506 | case GGML_TYPE_Q5_0: |
| 507 | return dequantize_block_cont_cuda<QK5_0, QR5_0, dequantize_q5_0>; |
| 508 | case GGML_TYPE_Q5_1: |
| 509 | return dequantize_block_cont_cuda<QK5_1, QR5_1, dequantize_q5_1>; |
| 510 | case GGML_TYPE_Q8_0: |
| 511 | return dequantize_block_cont_cuda<QK8_0, QR8_0, dequantize_q8_0>; |
| 512 | case GGML_TYPE_Q2_K: |
| 513 | return dequantize_row_q2_K_cuda; |
| 514 | case GGML_TYPE_Q3_K: |
| 515 | return dequantize_row_q3_K_cuda; |
| 516 | case GGML_TYPE_Q4_K: |
| 517 | return dequantize_row_q4_K_cuda; |
| 518 | case GGML_TYPE_Q5_K: |
| 519 | return dequantize_row_q5_K_cuda; |
| 520 | case GGML_TYPE_Q6_K: |
| 521 | return dequantize_row_q6_K_cuda; |
| 522 | case GGML_TYPE_IQ2_XXS: |
| 523 | return dequantize_row_iq2_xxs_cuda; |
| 524 | case GGML_TYPE_IQ2_XS: |
| 525 | return dequantize_row_iq2_xs_cuda; |
| 526 | case GGML_TYPE_IQ2_S: |
| 527 | return dequantize_row_iq2_s_cuda; |
| 528 | case GGML_TYPE_IQ3_XXS: |
| 529 | return dequantize_row_iq3_xxs_cuda; |
| 530 | case GGML_TYPE_IQ1_S: |
| 531 | return dequantize_row_iq1_s_cuda; |
| 532 | case GGML_TYPE_IQ1_M: |
| 533 | return dequantize_row_iq1_m_cuda; |
| 534 | case GGML_TYPE_IQ4_NL: |
| 535 | return dequantize_row_iq4_nl_cuda; |
| 536 | case GGML_TYPE_IQ4_XS: |
| 537 | return dequantize_row_iq4_xs_cuda; |
| 538 | case GGML_TYPE_IQ3_S: |
| 539 | return dequantize_row_iq3_s_cuda; |
| 540 | case GGML_TYPE_MXFP4: |
| 541 | return dequantize_row_mxfp4_cuda; |
| 542 | case GGML_TYPE_NVFP4: |
| 543 | return dequantize_row_nvfp4_cuda; |
| 544 | case GGML_TYPE_F32: |
| 545 | return convert_unary_cont_cuda<float>; |
| 546 | case GGML_TYPE_F16: |
| 547 | return convert_unary_cont_cuda<half>; |
| 548 | default: |
| 549 | return nullptr; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) { |
| 554 | switch (type) { |
| 555 | case GGML_TYPE_Q1_0: |
| 556 | return dequantize_block_cont_cuda<QK1_0, QR1_0, dequantize_q1_0>; |
| 557 | case GGML_TYPE_Q2_0: |
| 558 | return dequantize_block_cont_cuda<QK2_0, QR2_0, dequantize_q2_0>; |
| 559 | case GGML_TYPE_Q4_0: |
| 560 | return dequantize_row_q4_0_cuda; |
| 561 | case GGML_TYPE_Q4_1: |
| 562 | return dequantize_row_q4_1_cuda; |
| 563 | case GGML_TYPE_Q5_0: |
| 564 | return dequantize_block_cont_cuda<QK5_0, QR5_0, dequantize_q5_0>; |
| 565 | case GGML_TYPE_Q5_1: |
| 566 | return dequantize_block_cont_cuda<QK5_1, QR5_1, dequantize_q5_1>; |
| 567 | case GGML_TYPE_Q8_0: |
| 568 | if (fp16_available(ggml_cuda_info().devices[ggml_cuda_get_device()].cc)) { |
| 569 | return dequantize_block_q8_0_f16_cuda; |
| 570 | } |
| 571 | return dequantize_block_cont_cuda<QK8_0, QR8_0, dequantize_q8_0>; |
| 572 | case GGML_TYPE_Q2_K: |
| 573 | return dequantize_row_q2_K_cuda; |
| 574 | case GGML_TYPE_Q3_K: |
| 575 | return dequantize_row_q3_K_cuda; |
| 576 | case GGML_TYPE_Q4_K: |
| 577 | return dequantize_row_q4_K_cuda; |
| 578 | case GGML_TYPE_Q5_K: |
| 579 | return dequantize_row_q5_K_cuda; |
| 580 | case GGML_TYPE_Q6_K: |
| 581 | return dequantize_row_q6_K_cuda; |
| 582 | case GGML_TYPE_IQ2_XXS: |
| 583 | return dequantize_row_iq2_xxs_cuda; |
| 584 | case GGML_TYPE_IQ2_XS: |
| 585 | return dequantize_row_iq2_xs_cuda; |
| 586 | case GGML_TYPE_IQ2_S: |
| 587 | return dequantize_row_iq2_s_cuda; |
| 588 | case GGML_TYPE_IQ3_XXS: |
| 589 | return dequantize_row_iq3_xxs_cuda; |
| 590 | case GGML_TYPE_IQ1_S: |
| 591 | return dequantize_row_iq1_s_cuda; |
| 592 | case GGML_TYPE_IQ1_M: |
| 593 | return dequantize_row_iq1_m_cuda; |
| 594 | case GGML_TYPE_IQ4_NL: |
| 595 | return dequantize_row_iq4_nl_cuda; |
| 596 | case GGML_TYPE_IQ4_XS: |
| 597 | return dequantize_row_iq4_xs_cuda; |
| 598 | case GGML_TYPE_IQ3_S: |
| 599 | return dequantize_row_iq3_s_cuda; |
| 600 | case GGML_TYPE_MXFP4: |
| 601 | return dequantize_row_mxfp4_cuda; |
| 602 | case GGML_TYPE_NVFP4: |
| 603 | return dequantize_row_nvfp4_cuda; |
| 604 | case GGML_TYPE_F32: |
| 605 | return convert_unary_cont_cuda<float>; |
| 606 | case GGML_TYPE_BF16: |
| 607 | return convert_unary_cont_cuda<nv_bfloat16>; |
| 608 | default: |
| 609 | return nullptr; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) { |
| 614 | switch (type) { |
| 615 | case GGML_TYPE_Q1_0: |
| 616 | return dequantize_block_cont_cuda<QK1_0, QR1_0, dequantize_q1_0>; |
| 617 | case GGML_TYPE_Q2_0: |
| 618 | return dequantize_block_cont_cuda<QK2_0, QR2_0, dequantize_q2_0>; |
| 619 | case GGML_TYPE_Q4_0: |
| 620 | return dequantize_row_q4_0_cuda; |
| 621 | case GGML_TYPE_Q4_1: |
| 622 | return dequantize_row_q4_1_cuda; |
| 623 | case GGML_TYPE_Q5_0: |
| 624 | return dequantize_block_cont_cuda<QK5_0, QR5_0, dequantize_q5_0>; |
| 625 | case GGML_TYPE_Q5_1: |
| 626 | return dequantize_block_cont_cuda<QK5_1, QR5_1, dequantize_q5_1>; |
| 627 | case GGML_TYPE_Q8_0: |
| 628 | return dequantize_block_cont_cuda<QK8_0, QR8_0, dequantize_q8_0>; |
| 629 | case GGML_TYPE_Q2_K: |
| 630 | return dequantize_row_q2_K_cuda; |
| 631 | case GGML_TYPE_Q3_K: |
| 632 | return dequantize_row_q3_K_cuda; |
| 633 | case GGML_TYPE_Q4_K: |
| 634 | return dequantize_row_q4_K_cuda; |
| 635 | case GGML_TYPE_Q5_K: |
| 636 | return dequantize_row_q5_K_cuda; |
| 637 | case GGML_TYPE_Q6_K: |
| 638 | return dequantize_row_q6_K_cuda; |
| 639 | case GGML_TYPE_IQ2_XXS: |
| 640 | return dequantize_row_iq2_xxs_cuda; |
| 641 | case GGML_TYPE_IQ2_XS: |
| 642 | return dequantize_row_iq2_xs_cuda; |
| 643 | case GGML_TYPE_IQ2_S: |
| 644 | return dequantize_row_iq2_s_cuda; |
| 645 | case GGML_TYPE_IQ3_XXS: |
| 646 | return dequantize_row_iq3_xxs_cuda; |
| 647 | case GGML_TYPE_IQ1_S: |
| 648 | return dequantize_row_iq1_s_cuda; |
| 649 | case GGML_TYPE_IQ1_M: |
| 650 | return dequantize_row_iq1_m_cuda; |
| 651 | case GGML_TYPE_IQ4_NL: |
| 652 | return dequantize_row_iq4_nl_cuda; |
| 653 | case GGML_TYPE_IQ4_XS: |
| 654 | return dequantize_row_iq4_xs_cuda; |
| 655 | case GGML_TYPE_IQ3_S: |
| 656 | return dequantize_row_iq3_s_cuda; |
| 657 | case GGML_TYPE_MXFP4: |
| 658 | return dequantize_row_mxfp4_cuda; |
| 659 | case GGML_TYPE_NVFP4: |
| 660 | return dequantize_row_nvfp4_cuda; |
| 661 | case GGML_TYPE_F16: |
| 662 | return convert_unary_cont_cuda<half>; |
| 663 | case GGML_TYPE_BF16: |
| 664 | return convert_unary_cont_cuda<nv_bfloat16>; |
| 665 | default: |
| 666 | return nullptr; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | to_fp16_nc_cuda_t ggml_get_to_fp16_nc_cuda(ggml_type type) { |
| 671 | switch (type) { |
| 672 | case GGML_TYPE_F32: |
| 673 | return convert_unary_cuda<float>; |
| 674 | case GGML_TYPE_Q1_0: |
| 675 | return dequantize_block_cuda<QK1_0, QR1_0, dequantize_q1_0>; |
| 676 | case GGML_TYPE_Q2_0: |
| 677 | return dequantize_block_cuda<QK2_0, QR2_0, dequantize_q2_0>; |
| 678 | case GGML_TYPE_Q4_0: |
| 679 | return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>; |
| 680 | case GGML_TYPE_Q4_1: |
| 681 | return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>; |
| 682 | case GGML_TYPE_Q5_0: |
| 683 | return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>; |
| 684 | case GGML_TYPE_Q5_1: |
| 685 | return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>; |
| 686 | case GGML_TYPE_Q8_0: |
| 687 | return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>; |
| 688 | case GGML_TYPE_BF16: |
| 689 | return convert_unary_cuda<nv_bfloat16>; |
| 690 | default: |
| 691 | return nullptr; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | to_bf16_nc_cuda_t ggml_get_to_bf16_nc_cuda(ggml_type type) { |
| 696 | switch (type) { |
| 697 | case GGML_TYPE_F32: |
| 698 | return convert_unary_cuda<float, nv_bfloat16>; |
| 699 | case GGML_TYPE_Q1_0: |
| 700 | return dequantize_block_cuda<QK1_0, QR1_0, dequantize_q1_0>; |
| 701 | case GGML_TYPE_Q2_0: |
| 702 | return dequantize_block_cuda<QK2_0, QR2_0, dequantize_q2_0>; |
| 703 | case GGML_TYPE_Q4_0: |
| 704 | return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>; |
| 705 | case GGML_TYPE_Q4_1: |
| 706 | return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>; |
| 707 | case GGML_TYPE_Q5_0: |
| 708 | return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>; |
| 709 | case GGML_TYPE_Q5_1: |
| 710 | return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>; |
| 711 | case GGML_TYPE_Q8_0: |
| 712 | return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>; |
| 713 | case GGML_TYPE_F16: |
| 714 | return convert_unary_cuda<half, nv_bfloat16>; |
| 715 | default: |
| 716 | return nullptr; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | to_fp32_nc_cuda_t ggml_get_to_fp32_nc_cuda(ggml_type type) { |
| 721 | switch (type) { |
| 722 | case GGML_TYPE_F16: |
| 723 | return convert_unary_cuda<half, float>; |
| 724 | case GGML_TYPE_Q1_0: |
| 725 | return dequantize_block_cuda<QK1_0, QR1_0, dequantize_q1_0>; |
| 726 | case GGML_TYPE_Q2_0: |
| 727 | return dequantize_block_cuda<QK2_0, QR2_0, dequantize_q2_0>; |
| 728 | case GGML_TYPE_Q4_0: |
| 729 | return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>; |
| 730 | case GGML_TYPE_Q4_1: |
| 731 | return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>; |
| 732 | case GGML_TYPE_Q5_0: |
| 733 | return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>; |
| 734 | case GGML_TYPE_Q5_1: |
| 735 | return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>; |
| 736 | case GGML_TYPE_Q8_0: |
| 737 | return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>; |
| 738 | case GGML_TYPE_BF16: |
| 739 | return convert_unary_cuda<nv_bfloat16, float>; |
| 740 | default: |
| 741 | return nullptr; |
| 742 | } |
| 743 | } |