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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include "getrows.cuh"
|
| 2 |
+
#include "dequantize.cuh"
|
| 3 |
+
|
| 4 |
+
template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
|
| 5 |
+
static __global__ void k_get_rows(
|
| 6 |
+
const void * src0, const int32_t * src1, dst_t * dst,
|
| 7 |
+
int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
|
| 8 |
+
/*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
|
| 9 |
+
/*size_t s0,*/ size_t s1, size_t s2, size_t s3,
|
| 10 |
+
/*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
|
| 11 |
+
size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
|
| 12 |
+
|
| 13 |
+
const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
|
| 14 |
+
const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
|
| 15 |
+
const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
|
| 16 |
+
const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
|
| 17 |
+
|
| 18 |
+
if (i00 >= ne00) {
|
| 19 |
+
return;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
|
| 23 |
+
|
| 24 |
+
dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
|
| 25 |
+
const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
|
| 26 |
+
|
| 27 |
+
const int ib = i00/qk; // block index
|
| 28 |
+
const int iqs = (i00%qk)/qr; // quant index
|
| 29 |
+
const int iybs = i00 - i00%qk; // dst block start index
|
| 30 |
+
const int y_offset = qr == 1 ? 1 : qk/2;
|
| 31 |
+
|
| 32 |
+
// dequantize
|
| 33 |
+
dfloat2 v;
|
| 34 |
+
dequantize_kernel(src0_row, ib, iqs, v);
|
| 35 |
+
|
| 36 |
+
dst_row[iybs + iqs + 0] = v.x;
|
| 37 |
+
dst_row[iybs + iqs + y_offset] = v.y;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
template<typename src0_t, typename dst_t>
|
| 41 |
+
static __global__ void k_get_rows_float(
|
| 42 |
+
const src0_t * src0, const int32_t * src1, dst_t * dst,
|
| 43 |
+
int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
|
| 44 |
+
/*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
|
| 45 |
+
/*size_t s0,*/ size_t s1, size_t s2, size_t s3,
|
| 46 |
+
/*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
|
| 47 |
+
size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
|
| 48 |
+
|
| 49 |
+
const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
|
| 50 |
+
const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
|
| 51 |
+
const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
|
| 52 |
+
const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
|
| 53 |
+
|
| 54 |
+
if (i00 >= ne00) {
|
| 55 |
+
return;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
|
| 59 |
+
|
| 60 |
+
dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
|
| 61 |
+
const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
|
| 62 |
+
|
| 63 |
+
dst_row[i00] = src0_row[i00];
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
template<int qk, int qr, dequantize_kernel_t dq>
|
| 67 |
+
static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
|
| 68 |
+
const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
|
| 69 |
+
|
| 70 |
+
GGML_TENSOR_BINARY_OP_LOCALS
|
| 71 |
+
|
| 72 |
+
const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
|
| 73 |
+
const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
|
| 74 |
+
const dim3 block_nums(block_num_x, ne10, ne11*ne12);
|
| 75 |
+
|
| 76 |
+
// strides in elements
|
| 77 |
+
//const size_t s0 = nb0 / ggml_element_size(dst);
|
| 78 |
+
const size_t s1 = nb1 / ggml_element_size(dst);
|
| 79 |
+
const size_t s2 = nb2 / ggml_element_size(dst);
|
| 80 |
+
const size_t s3 = nb3 / ggml_element_size(dst);
|
| 81 |
+
|
| 82 |
+
const size_t s10 = nb10 / ggml_element_size(src1);
|
| 83 |
+
const size_t s11 = nb11 / ggml_element_size(src1);
|
| 84 |
+
const size_t s12 = nb12 / ggml_element_size(src1);
|
| 85 |
+
//const size_t s13 = nb13 / ggml_element_size(src1);
|
| 86 |
+
|
| 87 |
+
GGML_ASSERT(ne00 % 2 == 0);
|
| 88 |
+
|
| 89 |
+
k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
|
| 90 |
+
src0_dd, src1_dd, dst_dd,
|
| 91 |
+
ne00, /*ne01, ne02, ne03,*/
|
| 92 |
+
/*ne10, ne11,*/ ne12, /*ne13,*/
|
| 93 |
+
/* s0,*/ s1, s2, s3,
|
| 94 |
+
/* nb00,*/ nb01, nb02, nb03,
|
| 95 |
+
s10, s11, s12/*, s13*/);
|
| 96 |
+
|
| 97 |
+
GGML_UNUSED(dst);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
template<typename src0_t>
|
| 101 |
+
static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
|
| 102 |
+
const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
|
| 103 |
+
|
| 104 |
+
GGML_TENSOR_BINARY_OP_LOCALS
|
| 105 |
+
|
| 106 |
+
const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
|
| 107 |
+
const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
|
| 108 |
+
const dim3 block_nums(block_num_x, ne10, ne11*ne12);
|
| 109 |
+
|
| 110 |
+
// strides in elements
|
| 111 |
+
//const size_t s0 = nb0 / ggml_element_size(dst);
|
| 112 |
+
const size_t s1 = nb1 / ggml_element_size(dst);
|
| 113 |
+
const size_t s2 = nb2 / ggml_element_size(dst);
|
| 114 |
+
const size_t s3 = nb3 / ggml_element_size(dst);
|
| 115 |
+
|
| 116 |
+
const size_t s10 = nb10 / ggml_element_size(src1);
|
| 117 |
+
const size_t s11 = nb11 / ggml_element_size(src1);
|
| 118 |
+
const size_t s12 = nb12 / ggml_element_size(src1);
|
| 119 |
+
//const size_t s13 = nb13 / ggml_element_size(src1);
|
| 120 |
+
|
| 121 |
+
k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
|
| 122 |
+
src0_dd, src1_dd, dst_dd,
|
| 123 |
+
ne00, /*ne01, ne02, ne03,*/
|
| 124 |
+
/*ne10, ne11,*/ ne12, /*ne13,*/
|
| 125 |
+
/* s0,*/ s1, s2, s3,
|
| 126 |
+
/* nb00,*/ nb01, nb02, nb03,
|
| 127 |
+
s10, s11, s12/*, s13*/);
|
| 128 |
+
|
| 129 |
+
GGML_UNUSED(dst);
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
void ggml_cuda_op_get_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
| 133 |
+
const ggml_tensor * src0 = dst->src[0];
|
| 134 |
+
const ggml_tensor * src1 = dst->src[1];
|
| 135 |
+
const float * src0_d = (const float *)src0->data;
|
| 136 |
+
const float * src1_d = (const float *)src1->data;
|
| 137 |
+
float * dst_d = (float *)dst->data;
|
| 138 |
+
cudaStream_t stream = ctx.stream();
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
GGML_ASSERT(src1->type == GGML_TYPE_I32);
|
| 142 |
+
GGML_ASSERT(dst->type == GGML_TYPE_F32);
|
| 143 |
+
|
| 144 |
+
GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
|
| 145 |
+
GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
|
| 146 |
+
GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
|
| 147 |
+
|
| 148 |
+
const int32_t * src1_i32 = (const int32_t *) src1_d;
|
| 149 |
+
|
| 150 |
+
switch (src0->type) {
|
| 151 |
+
case GGML_TYPE_F16:
|
| 152 |
+
get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
|
| 153 |
+
break;
|
| 154 |
+
case GGML_TYPE_F32:
|
| 155 |
+
get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
| 156 |
+
break;
|
| 157 |
+
case GGML_TYPE_Q4_0:
|
| 158 |
+
get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
| 159 |
+
break;
|
| 160 |
+
case GGML_TYPE_Q4_1:
|
| 161 |
+
get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
| 162 |
+
break;
|
| 163 |
+
case GGML_TYPE_Q5_0:
|
| 164 |
+
get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
| 165 |
+
break;
|
| 166 |
+
case GGML_TYPE_Q5_1:
|
| 167 |
+
get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
| 168 |
+
break;
|
| 169 |
+
case GGML_TYPE_Q8_0:
|
| 170 |
+
get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
| 171 |
+
break;
|
| 172 |
+
default:
|
| 173 |
+
// TODO: k-quants
|
| 174 |
+
GGML_ABORT("%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
|
| 175 |
+
break;
|
| 176 |
+
}
|
| 177 |
+
}
|