Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cuda/cpy.cu # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cuda / cpy.cu` 626 lines ยท 32072 bytes Raw Blame History 1 #include "cpy.cuh" 2 #include "dequantize.cuh" 3 #include "cpy-utils.cuh" 4 #if defined(GGML_USE_MUSA) && defined(GGML_MUSA_MUDNN_COPY) 5 #include "ggml-musa/mudnn.cuh" 6 #endif // GGML_USE_MUSA && GGML_MUSA_MUDNN_COPY 7 8 typedef void (*cpy_kernel_t)(const char * cx, char * cdst); 9 10 const int CUDA_CPY_TILE_DIM_2D = 32; // 2D tile dimension for transposed blocks 11 const int CUDA_CPY_BLOCK_NM = 8; // block size of 3rd dimension if available 12 const int CUDA_CPY_BLOCK_ROWS = 8; // block dimension for marching through rows 13 14 template 15 static __global__ void cpy_scalar(const char * cx, char * cdst, const int64_t ne, 16 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 17 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, 18 const int64_t nb12, const int64_t nb13) { 19 ggml_cuda_pdl_lc(); 20 const int64_t i = (int64_t)blockDim.x*blockIdx.x + threadIdx.x; 21 22 if (i >= ne) { 23 return; 24 } 25 26 // determine indices i03/i13, i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor 27 // then combine those indices with the corresponding byte offsets to get the total offsets 28 const int64_t i03 = i/(ne00 * ne01 * ne02); 29 const int64_t i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01); 30 const int64_t i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00; 31 const int64_t i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00; 32 const int64_t x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03; 33 34 const int64_t i13 = i/(ne10 * ne11 * ne12); 35 const int64_t i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11); 36 const int64_t i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10; 37 const int64_t i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10; 38 const int64_t dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13; 39 40 ggml_cuda_pdl_sync(); 41 cpy_1(cx + x_offset, cdst + dst_offset); 42 } 43 44 template 45 static __global__ void cpy_scalar_transpose(const char * cx, char * cdst, const int64_t ne, 46 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 47 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, 48 const int64_t nb12, const int64_t nb13) { 49 50 const T* src = reinterpret_cast(cx); 51 T* dst = reinterpret_cast(cdst); 52 53 const int64_t nmat = ne / (ne00 * ne01); 54 const int64_t n = ne00 * ne01; 55 56 const int64_t x = (int64_t) blockIdx.x * CUDA_CPY_TILE_DIM_2D + threadIdx.x; 57 const int64_t y = (int64_t) blockIdx.y * CUDA_CPY_TILE_DIM_2D + threadIdx.y; 58 const int64_t tx = (int64_t) blockIdx.y * CUDA_CPY_TILE_DIM_2D + threadIdx.x; // transpose block offset 59 const int64_t ty = (int64_t) blockIdx.x * CUDA_CPY_TILE_DIM_2D + threadIdx.y; 60 61 __shared__ float tile[2][CUDA_CPY_TILE_DIM_2D][CUDA_CPY_TILE_DIM_2D+1]; 62 int cur_tile_buf = 0; 63 64 ggml_cuda_pdl_sync(); 65 #pragma unroll 66 for (int i = 0; i < CUDA_CPY_BLOCK_NM; ++i) { 67 68 const unsigned int imat = blockIdx.z * CUDA_CPY_BLOCK_NM + i; 69 if (imat >= nmat) 70 break; 71 72 #pragma unroll 73 for (int j = 0; j < CUDA_CPY_TILE_DIM_2D; j += CUDA_CPY_BLOCK_ROWS) { 74 if(x < ne01 && y + j < ne00){ 75 const int row = threadIdx.y+j; 76 const int col = threadIdx.x * sizeof(float)/sizeof(T); 77 T *tile2 = reinterpret_cast(tile[cur_tile_buf][row]); 78 tile2[col] = src[imat*n + (y+j)*ne01 + x]; 79 } 80 } 81 82 __syncthreads(); 83 84 #pragma unroll 85 for (int j = 0; j < CUDA_CPY_TILE_DIM_2D; j += CUDA_CPY_BLOCK_ROWS) { 86 if (ty + j < ne01 && tx < ne00) { 87 const int col = (threadIdx.y+j)*sizeof(float)/sizeof(T); 88 const T *tile2 = reinterpret_cast(tile[cur_tile_buf][threadIdx.x]); 89 dst[imat*n + (ty+j)*ne00 + tx] = tile2[col]; 90 } 91 } 92 93 cur_tile_buf = (cur_tile_buf + 1) % 2; 94 } 95 96 GGML_UNUSED_VARS(ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, 97 nb12, nb13); 98 } 99 100 static __device__ void cpy_blck_q8_0_f32(const char * cxi, char * cdsti) { 101 float * cdstf = (float *)(cdsti); 102 103 #pragma unroll 104 for (int j = 0; j < QK8_0; j += 2) { 105 float2 dq; 106 dequantize_q8_0(cxi, 0, j, dq); 107 *(cdstf + j) = dq.x; 108 *(cdstf + j + 1) = dq.y; 109 } 110 } 111 112 template 113 static __device__ void cpy_blck_q_f32(const char * cxi, char * cdsti) { 114 float * cdstf = (float *)(cdsti); 115 116 #pragma unroll 117 for (int j = 0; j < qk/2; j++) { 118 float2 dq; 119 dequant(cxi, 0, j, dq); 120 *(cdstf + j) = dq.x; 121 *(cdstf + j + qk/2) = dq.y; 122 } 123 } 124 125 template 126 static __global__ void cpy_f32_q(const char * cx, char * cdst, const int64_t ne, 127 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 128 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, 129 const int64_t nb12, const int64_t nb13) { 130 const int64_t i = ((int64_t)blockDim.x*blockIdx.x + threadIdx.x)*qk; 131 132 if (i >= ne) { 133 return; 134 } 135 136 const int64_t i03 = i/(ne00 * ne01 * ne02); 137 const int64_t i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01); 138 const int64_t i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00; 139 const int64_t i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00; 140 const int64_t x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03; 141 142 const int64_t i13 = i/(ne10 * ne11 * ne12); 143 const int64_t i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11); 144 const int64_t i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10; 145 const int64_t i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10; 146 const int64_t dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13; 147 148 ggml_cuda_pdl_sync(); 149 cpy_blck(cx + x_offset, cdst + dst_offset); 150 } 151 152 template 153 static __global__ void cpy_q_f32(const char * cx, char * cdst, const int64_t ne, 154 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 155 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, 156 const int64_t nb12, const int64_t nb13) { 157 const int64_t i = ((int64_t)blockDim.x*blockIdx.x + threadIdx.x)*qk; 158 159 if (i >= ne) { 160 return; 161 } 162 163 const int64_t i03 = i/(ne00 * ne01 * ne02); 164 const int64_t i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01); 165 const int64_t i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00; 166 const int64_t i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00; 167 const int64_t x_offset = (i00/qk)*nb00 + i01*nb01 + i02*nb02 + i03 * nb03; 168 169 const int64_t i13 = i/(ne10 * ne11 * ne12); 170 const int64_t i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11); 171 const int64_t i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10; 172 const int64_t i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10; 173 const int64_t dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13*nb13; 174 175 ggml_cuda_pdl_sync(); 176 cpy_blck(cx + x_offset, cdst + dst_offset); 177 } 178 179 template 180 static __global__ void cpy_scalar_contiguous(const char * cx, char * cdst, const int64_t ne) { 181 const int64_t i = (int64_t)blockDim.x*blockIdx.x + threadIdx.x; 182 183 if (i >= ne) { 184 return; 185 } 186 187 const src_t * x = (const src_t *) cx; 188 dst_t * dst = (dst_t *) cdst; 189 190 ggml_cuda_pdl_sync(); 191 dst[i] = ggml_cuda_cast(x[i]); 192 } 193 194 template 195 static void ggml_cpy_scalar_contiguous_cuda( 196 const char * cx, char * cdst, const int64_t ne, 197 cudaStream_t stream) { 198 199 const int64_t num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 200 GGML_ASSERT(num_blocks <= INT_MAX); 201 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params((dim3)num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream); 202 ggml_cuda_kernel_launch(cpy_scalar_contiguous, launch_params, cx, cdst, ne); 203 } 204 205 template 206 static void ggml_cpy_scalar_cuda( 207 const char * cx, char * cdst, const int64_t ne, 208 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 209 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, cudaStream_t stream) { 210 211 const auto launch_scalar_generic = [&]() { 212 const int64_t num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 213 GGML_ASSERT(num_blocks <= INT_MAX); 214 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params((dim3)num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream); 215 ggml_cuda_kernel_launch(cpy_scalar>, launch_params, 216 cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13); 217 }; 218 219 if (transposed) { 220 GGML_ASSERT(ne == ne00*ne01*ne02); // ne[3] is 1 assumed 221 int64_t ne00n, ne01n, ne02n; 222 if (nb00 <= nb02) { // most likely safe to handle nb00 = nb02 case here 223 ne00n = ne00; 224 ne01n = ne01; 225 ne02n = ne02; 226 } else { 227 ne00n = ne00; 228 ne01n = ne01*ne02; 229 ne02n = 1; 230 } 231 232 int64_t grid_x = (ne01n + CUDA_CPY_TILE_DIM_2D - 1) / CUDA_CPY_TILE_DIM_2D; 233 int64_t grid_y = (ne00n + CUDA_CPY_TILE_DIM_2D - 1) / CUDA_CPY_TILE_DIM_2D; 234 int64_t grid_z = (ne/(ne01n*ne00n) + CUDA_CPY_BLOCK_NM - 1) / CUDA_CPY_BLOCK_NM; 235 GGML_ASSERT(grid_x <= INT_MAX); 236 if (grid_y > USHRT_MAX || grid_z > USHRT_MAX) { 237 launch_scalar_generic(); 238 } else { 239 dim3 dimGrid(grid_x, grid_y, grid_z); 240 dim3 dimBlock(CUDA_CPY_TILE_DIM_2D, CUDA_CPY_BLOCK_ROWS, 1); 241 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(dimGrid, dimBlock, 0, stream); 242 ggml_cuda_kernel_launch(cpy_scalar_transpose, launch_params, 243 cx, cdst, ne, ne00n, ne01n, ne02n, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13); 244 } 245 } else { 246 launch_scalar_generic(); 247 } 248 } 249 250 static void ggml_cpy_f32_q8_0_cuda( 251 const char * cx, char * cdst, const int64_t ne, 252 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 253 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, cudaStream_t stream) { 254 255 GGML_ASSERT(ne % QK8_0 == 0); 256 const int64_t num_blocks = (ne/QK8_0 + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 257 GGML_ASSERT(num_blocks <= INT_MAX); 258 cpy_f32_q<<>> 259 (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13); 260 } 261 262 static void ggml_cpy_q8_0_f32_cuda( 263 const char * cx, char * cdst, const int64_t ne, 264 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 265 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, cudaStream_t stream) { 266 267 const int64_t num_blocks = (ne/QK8_0 + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 268 GGML_ASSERT(num_blocks <= INT_MAX); 269 cpy_q_f32<<>> 270 (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13); 271 } 272 273 static void ggml_cpy_f32_q4_0_cuda( 274 const char * cx, char * cdst, const int64_t ne, 275 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 276 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, cudaStream_t stream) { 277 278 GGML_ASSERT(ne % QK4_0 == 0); 279 const int64_t num_blocks = (ne/QK4_0 + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 280 GGML_ASSERT(num_blocks <= INT_MAX); 281 cpy_f32_q<<>> 282 (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13); 283 } 284 285 static void ggml_cpy_q4_0_f32_cuda( 286 const char * cx, char * cdst, const int64_t ne, 287 const int64_t ne00, const int64_t ne01, const int64_t ne02, 288 const int64_t nb00, const int64_t nb01, const int64_t nb02, 289 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, 290 const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, 291 cudaStream_t stream) { 292 const int64_t num_blocks = (ne/QK4_0 + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 293 GGML_ASSERT(num_blocks <= INT_MAX); 294 cpy_q_f32, QK4_0><<>>( 295 cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, 296 ne10, ne11, ne12, nb10, nb11, nb12, nb13); 297 } 298 299 static void ggml_cpy_f32_q4_1_cuda( 300 const char * cx, char * cdst, const int64_t ne, 301 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 302 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, cudaStream_t stream) { 303 304 GGML_ASSERT(ne % QK4_1 == 0); 305 const int64_t num_blocks = (ne/QK4_1 + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 306 GGML_ASSERT(num_blocks <= INT_MAX); 307 cpy_f32_q<<>> 308 (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13); 309 } 310 311 static void ggml_cpy_q4_1_f32_cuda( 312 const char * cx, char * cdst, const int64_t ne, 313 const int64_t ne00, const int64_t ne01, const int64_t ne02, 314 const int64_t nb00, const int64_t nb01, const int64_t nb02, 315 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, 316 const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, 317 cudaStream_t stream) { 318 const int64_t num_blocks = (ne/QK4_1 + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 319 GGML_ASSERT(num_blocks <= INT_MAX); 320 cpy_q_f32, QK4_1><<>>( 321 cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, 322 ne10, ne11, ne12, nb10, nb11, nb12, nb13); 323 } 324 325 static void ggml_cpy_f32_q5_0_cuda( 326 const char * cx, char * cdst, const int64_t ne, 327 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 328 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, cudaStream_t stream) { 329 330 GGML_ASSERT(ne % QK5_0 == 0); 331 const int64_t num_blocks = (ne/QK5_0 + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 332 GGML_ASSERT(num_blocks <= INT_MAX); 333 cpy_f32_q<<>> 334 (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13); 335 } 336 337 static void ggml_cpy_q5_0_f32_cuda( 338 const char * cx, char * cdst, const int64_t ne, 339 const int64_t ne00, const int64_t ne01, const int64_t ne02, 340 const int64_t nb00, const int64_t nb01, const int64_t nb02, 341 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, 342 const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, 343 cudaStream_t stream) { 344 const int64_t num_blocks = (ne/QK5_0 + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 345 GGML_ASSERT(num_blocks <= INT_MAX); 346 cpy_q_f32, QK5_0><<>>( 347 cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, 348 ne10, ne11, ne12, nb10, nb11, nb12, nb13); 349 } 350 351 static void ggml_cpy_f32_q5_1_cuda( 352 const char * cx, char * cdst, const int64_t ne, 353 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 354 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, cudaStream_t stream) { 355 356 GGML_ASSERT(ne % QK5_1 == 0); 357 const int64_t num_blocks = (ne/QK5_1 + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 358 GGML_ASSERT(num_blocks <= INT_MAX); 359 cpy_f32_q<<>> 360 (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13); 361 } 362 363 static void ggml_cpy_q5_1_f32_cuda( 364 const char * cx, char * cdst, const int64_t ne, 365 const int64_t ne00, const int64_t ne01, const int64_t ne02, 366 const int64_t nb00, const int64_t nb01, const int64_t nb02, 367 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, 368 const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, 369 cudaStream_t stream) { 370 const int64_t num_blocks = (ne/QK5_1 + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 371 GGML_ASSERT(num_blocks <= INT_MAX); 372 cpy_q_f32, QK5_1><<>>( 373 cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, 374 ne10, ne11, ne12, nb10, nb11, nb12, nb13); 375 } 376 377 static void ggml_cpy_f32_iq4_nl_cuda( 378 const char * cx, char * cdst, const int64_t ne, 379 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t nb00, const int64_t nb01, const int64_t nb02, 380 const int64_t nb03, const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t nb10, const int64_t nb11, const int64_t nb12, const int64_t nb13, cudaStream_t stream) { 381 382 GGML_ASSERT(ne % QK4_NL == 0); 383 const int64_t num_blocks = (ne/QK4_NL + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE; 384 GGML_ASSERT(num_blocks <= INT_MAX); 385 cpy_f32_q<<>> 386 (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13); 387 } 388 389 // check if a same-type copy reduces to a 2D strided copy (height rows of width 390 // contiguous bytes), so it can use cudaMemcpy2DAsync instead of the scalar kernel 391 static bool ggml_cuda_cpy_as_memcpy_2d(const ggml_tensor * src0, const ggml_tensor * src1, 392 size_t & width, size_t & height, size_t & spitch, size_t & dpitch) { 393 // require matching shape: a reshaped copy maps elements by flat order, which the 394 // prefix walk below does not handle 395 if (src0->type != src1->type || !ggml_are_same_shape(src0, src1)) { 396 return false; 397 } 398 399 // grow the contiguous prefix block shared by both tensors 400 size_t block_nb = ggml_element_size(src0); 401 int d = 0; 402 for (; d < GGML_MAX_DIMS; ++d) { 403 if (src0->nb[d] != block_nb || src1->nb[d] != block_nb) { 404 break; 405 } 406 block_nb *= src0->ne[d]; 407 } 408 409 // d == 0: nothing contiguous; d == GGML_MAX_DIMS: fully contiguous (handled by memcpy) 410 if (d == 0 || d == GGML_MAX_DIMS) { 411 return false; 412 } 413 414 // dim d carries the rows; everything above it must be a single element 415 for (int i = d + 1; i < GGML_MAX_DIMS; ++i) { 416 if (src0->ne[i] != 1) { 417 return false; 418 } 419 } 420 421 width = block_nb; 422 height = src0->ne[d]; 423 spitch = src0->nb[d]; 424 dpitch = src1->nb[d]; 425 426 return spitch >= width && dpitch >= width; 427 } 428 429 void ggml_cuda_cpy(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, ggml_tensor * src1) { 430 const int64_t ne = ggml_nelements(src0); 431 GGML_ASSERT(ne == ggml_nelements(src1)); 432 433 const int64_t ne00 = src0->ne[0]; 434 const int64_t ne01 = src0->ne[1]; 435 const int64_t ne02 = src0->ne[2]; 436 437 //GGML_ASSERT(src0->ne[3] == 1); 438 439 const int64_t nb00 = src0->nb[0]; 440 const int64_t nb01 = src0->nb[1]; 441 const int64_t nb02 = src0->nb[2]; 442 const int64_t nb03 = src0->nb[3]; 443 444 const int64_t ne10 = src1->ne[0]; 445 const int64_t ne11 = src1->ne[1]; 446 const int64_t ne12 = src1->ne[2]; 447 448 //GGML_ASSERT(src1->ne[3] == 1); 449 450 const int64_t nb10 = src1->nb[0]; 451 const int64_t nb11 = src1->nb[1]; 452 const int64_t nb12 = src1->nb[2]; 453 const int64_t nb13 = src1->nb[3]; 454 455 cudaStream_t main_stream = ctx.stream(); 456 457 char * src0_ddc = (char *) src0->data; 458 char * src1_ddc = (char *) src1->data; 459 460 const bool contiguous_srcs = ggml_is_contiguous(src0) && ggml_is_contiguous(src1); 461 const bool can_be_transposed = nb01 == (int64_t)ggml_element_size(src0) && 462 src0->ne[3] == 1 && nb02 == ne00 * ne01 * (int64_t)ggml_element_size(src0) && 463 ggml_is_contiguous(src1); 464 465 size_t mc_width = 0, mc_height = 0, mc_spitch = 0, mc_dpitch = 0; 466 467 if (src0->type == src1->type && contiguous_srcs) { 468 GGML_ASSERT(ggml_nbytes(src0) == ggml_nbytes(src1)); 469 #if defined(GGML_USE_MUSA) && defined(GGML_MUSA_MUDNN_COPY) 470 if (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16) { 471 CUDA_CHECK(mudnnMemcpyAsync(ctx, src1, src0)); 472 } else 473 #endif // GGML_USE_MUSA && GGML_MUSA_MUDNN_COPY 474 { 475 CUDA_CHECK(cudaMemcpyAsync(src1_ddc, src0_ddc, ggml_nbytes(src0), cudaMemcpyDeviceToDevice, main_stream)); 476 } 477 } else if (ggml_cuda_cpy_as_memcpy_2d(src0, src1, mc_width, mc_height, mc_spitch, mc_dpitch)) { 478 CUDA_CHECK(cudaMemcpy2DAsync(src1_ddc, mc_dpitch, src0_ddc, mc_spitch, 479 mc_width, mc_height, cudaMemcpyDeviceToDevice, main_stream)); 480 } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) { 481 if (can_be_transposed) { 482 ggml_cpy_scalar_cuda 483 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 484 } else { 485 ggml_cpy_scalar_cuda 486 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 487 } 488 } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_BF16) { 489 if (contiguous_srcs) { 490 ggml_cpy_scalar_contiguous_cuda 491 (src0_ddc, src1_ddc, ne, main_stream); 492 } else { 493 ggml_cpy_scalar_cuda 494 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 495 } 496 } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) { 497 if (contiguous_srcs) { 498 ggml_cpy_scalar_contiguous_cuda 499 (src0_ddc, src1_ddc, ne, main_stream); 500 } else { 501 ggml_cpy_scalar_cuda 502 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 503 } 504 } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) { 505 ggml_cpy_f32_q8_0_cuda 506 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 507 } else if (src0->type == GGML_TYPE_Q8_0 && src1->type == GGML_TYPE_F32) { 508 ggml_cpy_q8_0_f32_cuda 509 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 510 } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) { 511 ggml_cpy_f32_q4_0_cuda 512 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 513 } else if (src0->type == GGML_TYPE_Q4_0 && src1->type == GGML_TYPE_F32) { 514 ggml_cpy_q4_0_f32_cuda 515 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 516 } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) { 517 ggml_cpy_f32_q4_1_cuda 518 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 519 } else if (src0->type == GGML_TYPE_Q4_1 && src1->type == GGML_TYPE_F32) { 520 ggml_cpy_q4_1_f32_cuda 521 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 522 } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q5_0) { 523 ggml_cpy_f32_q5_0_cuda 524 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 525 } else if (src0->type == GGML_TYPE_Q5_0 && src1->type == GGML_TYPE_F32) { 526 ggml_cpy_q5_0_f32_cuda 527 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 528 } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_IQ4_NL) { 529 ggml_cpy_f32_iq4_nl_cuda 530 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 531 } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q5_1) { 532 ggml_cpy_f32_q5_1_cuda 533 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 534 } else if (src0->type == GGML_TYPE_Q5_1 && src1->type == GGML_TYPE_F32) { 535 ggml_cpy_q5_1_f32_cuda 536 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 537 } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) { 538 if (can_be_transposed) { 539 ggml_cpy_scalar_cuda 540 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 541 } else { 542 ggml_cpy_scalar_cuda 543 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 544 } 545 } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_BF16) { 546 if (contiguous_srcs) { 547 ggml_cpy_scalar_contiguous_cuda 548 (src0_ddc, src1_ddc, ne, main_stream); 549 } else { 550 ggml_cpy_scalar_cuda 551 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 552 } 553 } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) { 554 if (contiguous_srcs) { 555 ggml_cpy_scalar_contiguous_cuda 556 (src0_ddc, src1_ddc, ne, main_stream); 557 } else { 558 ggml_cpy_scalar_cuda 559 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 560 } 561 } else if (src0->type == GGML_TYPE_BF16 && src1->type == GGML_TYPE_BF16) { 562 if (can_be_transposed) { 563 ggml_cpy_scalar_cuda 564 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 565 } else { 566 ggml_cpy_scalar_cuda 567 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 568 } 569 } else if (src0->type == GGML_TYPE_BF16 && src1->type == GGML_TYPE_F16) { 570 if (contiguous_srcs) { 571 ggml_cpy_scalar_contiguous_cuda 572 (src0_ddc, src1_ddc, ne, main_stream); 573 } else { 574 ggml_cpy_scalar_cuda 575 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 576 } 577 } else if (src0->type == GGML_TYPE_BF16 && src1->type == GGML_TYPE_F32) { 578 if (contiguous_srcs) { 579 ggml_cpy_scalar_contiguous_cuda 580 (src0_ddc, src1_ddc, ne, main_stream); 581 } else { 582 ggml_cpy_scalar_cuda 583 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 584 } 585 } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32) { 586 if (can_be_transposed) { 587 ggml_cpy_scalar_cuda 588 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 589 } else { 590 ggml_cpy_scalar_cuda 591 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 592 } 593 } else if (src0->type == GGML_TYPE_I16 && src1->type == GGML_TYPE_I16) { 594 if (can_be_transposed) { 595 ggml_cpy_scalar_cuda 596 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 597 } else { 598 ggml_cpy_scalar_cuda 599 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 600 } 601 } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_I32) { 602 if (contiguous_srcs) { 603 ggml_cpy_scalar_contiguous_cuda 604 (src0_ddc, src1_ddc, ne, main_stream); 605 } else { 606 ggml_cpy_scalar_cuda 607 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 608 } 609 } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_F32) { 610 if (contiguous_srcs) { 611 ggml_cpy_scalar_contiguous_cuda 612 (src0_ddc, src1_ddc, ne, main_stream); 613 } else { 614 ggml_cpy_scalar_cuda 615 (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream); 616 } 617 } else { 618 GGML_ABORT("%s: unsupported type combination (%s to %s)\n", __func__, 619 ggml_type_name(src0->type), ggml_type_name(src1->type)); 620 } 621 } 622 623 void ggml_cuda_dup(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { 624 const ggml_tensor * src0 = dst->src[0]; 625 ggml_cuda_cpy(ctx, src0, dst); 626 }