ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cuda / conv3d.cu
| 1 | #include "conv3d.cuh" |
| 2 | #include "convert.cuh" |
| 3 | #include "mma.cuh" |
| 4 | |
| 5 | struct conv3d_params { |
| 6 | int64_t IW, IH, ID; |
| 7 | int64_t OW, OH, OD; |
| 8 | int64_t KW, KH, KD; |
| 9 | int64_t ST_X, ST_Y, ST_Z; |
| 10 | int64_t PD_X, PD_Y, PD_Z; |
| 11 | int64_t DL_X, DL_Y, DL_Z; |
| 12 | int64_t IC, OC, B, TOTAL; |
| 13 | }; |
| 14 | |
| 15 | template <typename T> |
| 16 | static __global__ void conv3d_kernel(const float * input, const T * weight, float * output, const conv3d_params P) { |
| 17 | const int64_t spatial = P.OW * P.OH * P.OD; |
| 18 | for (int64_t i = int64_t(blockIdx.x) * blockDim.x + threadIdx.x; i < P.TOTAL; |
| 19 | i += int64_t(gridDim.x) * blockDim.x) { |
| 20 | const int64_t x = i % P.OW, y = i / P.OW % P.OH, z = i / (P.OW * P.OH) % P.OD; |
| 21 | const int64_t co = i / spatial % P.OC, n = i / (spatial * P.OC); |
| 22 | float sum = 0.0f; |
| 23 | for (int64_t ci = 0; ci < P.IC; ++ci) { |
| 24 | for (int64_t kz = 0; kz < P.KD; ++kz) { |
| 25 | const int64_t iz = z * P.ST_Z + kz * P.DL_Z - P.PD_Z; |
| 26 | if (iz < 0 || iz >= P.ID) { |
| 27 | continue; |
| 28 | } |
| 29 | for (int64_t ky = 0; ky < P.KH; ++ky) { |
| 30 | const int64_t iy = y * P.ST_Y + ky * P.DL_Y - P.PD_Y; |
| 31 | if (iy < 0 || iy >= P.IH) { |
| 32 | continue; |
| 33 | } |
| 34 | for (int64_t kx = 0; kx < P.KW; ++kx) { |
| 35 | const int64_t ix = x * P.ST_X + kx * P.DL_X - P.PD_X; |
| 36 | if (ix >= 0 && ix < P.IW) { |
| 37 | const int64_t xi = (((n * P.IC + ci) * P.ID + iz) * P.IH + iy) * P.IW + ix; |
| 38 | const int64_t wi = (((co * P.IC + ci) * P.KD + kz) * P.KH + ky) * P.KW + kx; |
| 39 | sum += input[xi] * ggml_cuda_cast<float>(weight[wi]); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | output[i] = sum; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static __global__ void conv3d_pad_f16(const float * input, |
| 50 | half * output, |
| 51 | int iw, |
| 52 | int ih, |
| 53 | int id, |
| 54 | int pw, |
| 55 | int ph, |
| 56 | int pd, |
| 57 | int px, |
| 58 | int py, |
| 59 | int pz, |
| 60 | int total) { |
| 61 | const int i = blockIdx.x * blockDim.x + threadIdx.x; |
| 62 | if (i >= total) { |
| 63 | return; |
| 64 | } |
| 65 | const int x = i % pw - px, y = i / pw % ph - py, z = i / (pw * ph) % pd - pz; |
| 66 | const int nc = i / (pw * ph * pd); |
| 67 | output[i] = |
| 68 | __float2half((unsigned) x < (unsigned) iw && (unsigned) y < (unsigned) ih && (unsigned) z < (unsigned) id ? |
| 69 | input[((nc * id + z) * ih + y) * iw + x] : |
| 70 | 0.0f); |
| 71 | } |
| 72 | |
| 73 | template <int KW, int KH, int KD, bool use_mma> |
| 74 | static __global__ void conv3d_implicit_gemm_f16(const half * __restrict__ input, |
| 75 | const half * __restrict__ weight, |
| 76 | float * __restrict__ output, |
| 77 | const conv3d_params P, |
| 78 | const int split_k, |
| 79 | const bool aligned_weights) { |
| 80 | using namespace ggml_cuda_mma; |
| 81 | constexpr int warp_size = ggml_cuda_get_physical_warp_size(); |
| 82 | constexpr int nthreads = 4 * warp_size; |
| 83 | constexpr int BM = 64, BN = 64, BK = 64; |
| 84 | constexpr int AS = BK / 2 + 4; |
| 85 | constexpr int BS = BN / 2 + 4; |
| 86 | static_assert(AS * sizeof(half2) % sizeof(int4) == 0, "shared weight rows must be 16-byte aligned"); |
| 87 | __shared__ __align__(16) half2 a_s[BM][AS]; |
| 88 | __shared__ __align__(16) half2 b_s[BK][BS]; |
| 89 | const int tid = threadIdx.y * warp_size + threadIdx.x; |
| 90 | const int iw = int(P.IW), ih = int(P.IH), id = int(P.ID); |
| 91 | const int ow = int(P.OW), oh = int(P.OH), od = int(P.OD); |
| 92 | const int kw = KW ? KW : int(P.KW), kh = KH ? KH : int(P.KH), kd = KD ? KD : int(P.KD); |
| 93 | const int ic = int(P.IC), oc = int(P.OC); |
| 94 | const int sx = int(P.ST_X), sy = int(P.ST_Y), sz = int(P.ST_Z); |
| 95 | const int dx = int(P.DL_X), dy = int(P.DL_Y), dz = int(P.DL_Z); |
| 96 | const int n = blockIdx.z / split_k, split = blockIdx.z % split_k; |
| 97 | const int m0 = blockIdx.y * BM, n0 = blockIdx.x * BN; |
| 98 | const int k_total = ic * kw * kh * kd; |
| 99 | const int load_lane = warp_size == 32 ? threadIdx.x : threadIdx.x % (BN / 2); |
| 100 | const int load_row = threadIdx.y * (warp_size / (BN / 2)) + (warp_size == 32 ? 0 : threadIdx.x / (BN / 2)); |
| 101 | const int spatial = n0 + 2 * load_lane; |
| 102 | const int spatial0 = min(spatial, ow * oh * od - 1), spatial1 = min(spatial + 1, ow * oh * od - 1); |
| 103 | const int z0 = spatial0 / (ow * oh), y0 = spatial0 / ow % oh, x0 = spatial0 % ow; |
| 104 | const int z1 = spatial1 / (ow * oh), y1 = spatial1 / ow % oh, x1 = spatial1 % ow; |
| 105 | const int pos0 = (z0 * sz * ih + y0 * sy) * iw + x0 * sx; |
| 106 | const int pos1 = (z1 * sz * ih + y1 * sy) * iw + x1 * sx; |
| 107 | [[maybe_unused]] const int wm = threadIdx.y / 2 * 32, wn = threadIdx.y % 2 * 32; |
| 108 | using tile_ab = tile<16, 8, half2, get_input_data_layout()>; |
| 109 | #if defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE) |
| 110 | // AMD accumulator fragments transpose the input fragment's row/column mapping. |
| 111 | using tile_c = tile<16, 16, float, DATA_LAYOUT_J_MAJOR>; |
| 112 | #else |
| 113 | using tile_c = tile<16, 16, float>; |
| 114 | #endif |
| 115 | [[maybe_unused]] tile_c c[2][2]; |
| 116 | constexpr int RM = 4, RN = BM * BN / (nthreads * RM); |
| 117 | [[maybe_unused]] const int simt_m = tid / (BN / RN) * RM, simt_n = tid % (BN / RN) * RN; |
| 118 | [[maybe_unused]] float c_simt[RM][RN] = {}; |
| 119 | const int tiles = (k_total + BK - 1) / BK; |
| 120 | const int begin = int(int64_t(tiles) * split / split_k) * BK; |
| 121 | const int end = int(int64_t(tiles) * (split + 1) / split_k) * BK; |
| 122 | for (int k0 = begin; k0 < end; k0 += BK) { |
| 123 | if (aligned_weights) { |
| 124 | #pragma unroll |
| 125 | for (int i0 = 0; i0 < BM * BK / 8; i0 += nthreads) { |
| 126 | const int i = i0 + tid; |
| 127 | const int row = i / (BK / 8), col = 8 * (i % (BK / 8)); |
| 128 | const int4 v = m0 + row < oc && k0 + col < k_total ? |
| 129 | ((const int4 *) weight)[((m0 + row) * k_total + k0 + col) / 8] : |
| 130 | make_int4(0, 0, 0, 0); |
| 131 | *(int4 *) &a_s[row][col / 2] = v; |
| 132 | } |
| 133 | } else { |
| 134 | #pragma unroll |
| 135 | for (int i0 = 0; i0 < BM * BK / 2; i0 += nthreads) { |
| 136 | const int i = i0 + tid; |
| 137 | const int row = i / (BK / 2), col = 2 * (i % (BK / 2)); |
| 138 | half lo = __float2half(0.0f), hi = lo; |
| 139 | if (m0 + row < oc && k0 + col < k_total) { |
| 140 | lo = weight[(m0 + row) * k_total + k0 + col]; |
| 141 | if (k0 + col + 1 < k_total) { |
| 142 | hi = weight[(m0 + row) * k_total + k0 + col + 1]; |
| 143 | } |
| 144 | } |
| 145 | a_s[row][col / 2] = __halves2half2(lo, hi); |
| 146 | } |
| 147 | } |
| 148 | #pragma unroll |
| 149 | for (int kb = 0; kb < BK; kb += nthreads / (BN / 2)) { |
| 150 | const int k = kb + load_row; |
| 151 | const int ki = k0 + k; |
| 152 | const int ci = ki / (kw * kh * kd), kz = ki / (kw * kh) % kd, ky = ki / kw % kh, kx = ki % kw; |
| 153 | const int offset = ki < k_total ? ((n * ic + ci) * id + kz * dz) * ih * iw + ky * dy * iw + kx * dx : 0; |
| 154 | half lo = __float2half(0.0f), hi = lo; |
| 155 | if (ki < k_total && spatial < ow * oh * od) { |
| 156 | lo = input[offset + pos0]; |
| 157 | } |
| 158 | if (ki < k_total && spatial + 1 < ow * oh * od) { |
| 159 | hi = input[offset + pos1]; |
| 160 | } |
| 161 | b_s[k][load_lane] = __halves2half2(lo, hi); |
| 162 | } |
| 163 | __syncthreads(); |
| 164 | if constexpr (use_mma) { |
| 165 | #pragma unroll |
| 166 | for (int k = 0; k < BK; k += 16) { |
| 167 | tile_ab a[2], b[2]; |
| 168 | #pragma unroll |
| 169 | for (int i = 0; i < 2; ++i) { |
| 170 | load_ldmatrix(a[i], &a_s[wm + 16 * i][k / 2], AS); |
| 171 | load_ldmatrix_trans(b[i], &b_s[k][(wn + 16 * i) / 2], BS); |
| 172 | } |
| 173 | #pragma unroll |
| 174 | for (int i = 0; i < 2; ++i) { |
| 175 | #pragma unroll |
| 176 | for (int j = 0; j < 2; ++j) { |
| 177 | mma(c[i][j], a[i], b[j]); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } else { |
| 182 | #pragma unroll 4 |
| 183 | for (int k = 0; k < BK; ++k) { |
| 184 | float a[RM], b[RN]; |
| 185 | #pragma unroll |
| 186 | for (int i = 0; i < RM; ++i) { |
| 187 | a[i] = __half2float(((const half *) a_s[simt_m + i])[k]); |
| 188 | } |
| 189 | #pragma unroll |
| 190 | for (int j = 0; j < RN; ++j) { |
| 191 | b[j] = __half2float(((const half *) b_s[k])[simt_n + j]); |
| 192 | } |
| 193 | #pragma unroll |
| 194 | for (int i = 0; i < RM; ++i) { |
| 195 | #pragma unroll |
| 196 | for (int j = 0; j < RN; ++j) { |
| 197 | c_simt[i][j] += a[i] * b[j]; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | __syncthreads(); |
| 203 | } |
| 204 | if constexpr (use_mma) { |
| 205 | #pragma unroll |
| 206 | for (int i = 0; i < 2; ++i) { |
| 207 | #pragma unroll |
| 208 | for (int j = 0; j < 2; ++j) { |
| 209 | #pragma unroll |
| 210 | for (int l = 0; l < c[i][j].ne; ++l) { |
| 211 | const int co = m0 + wm + 16 * i + c[i][j].get_i(l); |
| 212 | const int pos = n0 + wn + 16 * j + c[i][j].get_j(l); |
| 213 | if (co < oc && pos < ow * oh * od) { |
| 214 | output[(int64_t(blockIdx.z) * oc + co) * ow * oh * od + pos] = c[i][j].x[l]; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } else { |
| 220 | #pragma unroll |
| 221 | for (int i = 0; i < RM; ++i) { |
| 222 | #pragma unroll |
| 223 | for (int j = 0; j < RN; ++j) { |
| 224 | const int co = m0 + simt_m + i, pos = n0 + simt_n + j; |
| 225 | if (co < oc && pos < ow * oh * od) { |
| 226 | output[(int64_t(blockIdx.z) * oc + co) * ow * oh * od + pos] = c_simt[i][j]; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static __global__ void conv3d_reduce_split_k(const float * __restrict__ partial, |
| 234 | float * __restrict__ output, |
| 235 | const int total, |
| 236 | const int per_batch, |
| 237 | const int split_k) { |
| 238 | const int i = blockIdx.x * blockDim.x + threadIdx.x; |
| 239 | if (i >= total) { |
| 240 | return; |
| 241 | } |
| 242 | const int n = i / per_batch; |
| 243 | // Partial slices are ordered as [batch, split, output channel, spatial position]. |
| 244 | const float * src = partial + int64_t(n) * (split_k - 1) * per_batch + i; |
| 245 | float sum = 0.0f; |
| 246 | for (int k = 0; k < split_k; ++k) { |
| 247 | sum += src[int64_t(k) * per_batch]; |
| 248 | } |
| 249 | output[i] = sum; |
| 250 | } |
| 251 | |
| 252 | template <bool use_mma> |
| 253 | static void conv3d_launch_implicit_gemm(const half * input, |
| 254 | const half * weight, |
| 255 | float * output, |
| 256 | const conv3d_params & params, |
| 257 | int split_k, |
| 258 | dim3 grid, |
| 259 | dim3 block, |
| 260 | cudaStream_t stream) { |
| 261 | // Vector loads require both the base pointer and each weight row to be 16-byte aligned. |
| 262 | const bool aligned_weights = uintptr_t(weight) % sizeof(int4) == 0 && |
| 263 | (params.IC * params.KW * params.KH * params.KD) % (sizeof(int4) / sizeof(half)) == 0; |
| 264 | if (params.KW == 3 && params.KH == 3 && params.KD == 3) { |
| 265 | conv3d_implicit_gemm_f16<3, 3, 3, use_mma> |
| 266 | <<<grid, block, 0, stream>>>(input, weight, output, params, split_k, aligned_weights); |
| 267 | } else if (params.KW == 1 && params.KH == 1 && params.KD == 3) { |
| 268 | conv3d_implicit_gemm_f16<1, 1, 3, use_mma> |
| 269 | <<<grid, block, 0, stream>>>(input, weight, output, params, split_k, aligned_weights); |
| 270 | } else if (params.KW == 1 && params.KH == 1 && params.KD == 1) { |
| 271 | conv3d_implicit_gemm_f16<1, 1, 1, use_mma> |
| 272 | <<<grid, block, 0, stream>>>(input, weight, output, params, split_k, aligned_weights); |
| 273 | } else { |
| 274 | conv3d_implicit_gemm_f16<0, 0, 0, use_mma> |
| 275 | <<<grid, block, 0, stream>>>(input, weight, output, params, split_k, aligned_weights); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void ggml_cuda_op_conv3d(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { |
| 280 | const ggml_tensor * kernel = dst->src[0]; |
| 281 | const ggml_tensor * input = dst->src[1]; |
| 282 | GGML_ASSERT(input->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32); |
| 283 | GGML_ASSERT(kernel->type == GGML_TYPE_F16 || kernel->type == GGML_TYPE_F32); |
| 284 | GGML_ASSERT(ggml_is_contiguous(input) && ggml_is_contiguous(kernel) && ggml_is_contiguous(dst)); |
| 285 | |
| 286 | const int32_t * p = dst->op_params; |
| 287 | const int64_t IW = input->ne[0], IH = input->ne[1], ID = input->ne[2]; |
| 288 | const int64_t OW = dst->ne[0], OH = dst->ne[1], OD = dst->ne[2]; |
| 289 | const int64_t KW = kernel->ne[0], KH = kernel->ne[1], KD = kernel->ne[2]; |
| 290 | const int64_t IC = p[9], B = p[10], OC = p[11]; |
| 291 | GGML_ASSERT(IC > 0 && B > 0 && OC > 0 && input->ne[3] == IC * B && kernel->ne[3] == IC * OC); |
| 292 | GGML_ASSERT(dst->ne[3] == OC * B && p[0] > 0 && p[1] > 0 && p[2] > 0 && p[6] > 0 && p[7] > 0 && p[8] > 0); |
| 293 | const int64_t total = ggml_nelements(dst); |
| 294 | const conv3d_params params = { IW, IH, ID, OW, OH, OD, KW, KH, KD, p[0], p[1], |
| 295 | p[2], p[3], p[4], p[5], p[6], p[7], p[8], IC, OC, B, total }; |
| 296 | const float * x = (const float *) input->data; |
| 297 | const half * w = (const half *) kernel->data; |
| 298 | float * y = (float *) dst->data; |
| 299 | cudaStream_t stream = ctx.stream(); |
| 300 | const auto & device = ggml_cuda_info().devices[ctx.device]; |
| 301 | const bool use_mma = |
| 302 | turing_mma_available(device.cc) || amd_wmma_available(device.cc) || amd_mfma_available(device.cc); |
| 303 | const bool pointwise = |
| 304 | KW == 1 && KH == 1 && KD == 1 && p[0] == 1 && p[1] == 1 && p[2] == 1 && p[3] == 0 && p[4] == 0 && p[5] == 0; |
| 305 | const bool use_blas = pointwise && fast_fp16_hardware_available(device.cc); |
| 306 | const int64_t limit = INT_MAX - 256; |
| 307 | const int64_t pw = IW + 2 * int64_t(p[3]), ph = IH + 2 * int64_t(p[4]), pd = ID + 2 * int64_t(p[5]); |
| 308 | const bool padded_fits = pw > 0 && pw <= limit && ph > 0 && ph <= limit && pd > 0 && pd <= limit && |
| 309 | pw * ph <= limit / pd && IC * B <= limit / (pw * ph * pd); |
| 310 | if (kernel->type == GGML_TYPE_F16 && KW > 0 && KH > 0 && KD > 0 && ggml_nelements(input) <= limit && |
| 311 | ggml_nelements(kernel) <= limit && total <= limit && padded_fits && p[3] >= 0 && p[4] >= 0 && p[5] >= 0 && |
| 312 | (OW - 1) * p[0] + (KW - 1) * p[6] < pw && (OH - 1) * p[1] + (KH - 1) * p[7] < ph && |
| 313 | (OD - 1) * p[2] + (KD - 1) * p[8] < pd && (OC + 63) / 64 <= 65535 && B <= 65535) { |
| 314 | const int padded_total = int(pw * ph * pd * IC * B); |
| 315 | ggml_cuda_pool_alloc<half> x_half(ctx.pool(), padded_total); |
| 316 | // Match im2col's F16 input precision without materializing all patches in global memory. |
| 317 | if (p[3] == 0 && p[4] == 0 && p[5] == 0) { |
| 318 | ggml_get_to_fp16_cuda(input->type)(x, x_half.get(), padded_total, stream); |
| 319 | } else { |
| 320 | conv3d_pad_f16<<<(padded_total + 255) / 256, 256, 0, stream>>>( |
| 321 | x, x_half.get(), int(IW), int(IH), int(ID), int(pw), int(ph), int(pd), p[3], p[4], p[5], padded_total); |
| 322 | } |
| 323 | const conv3d_params padded_params = { pw, ph, pd, OW, OH, OD, KW, KH, KD, p[0], p[1], |
| 324 | p[2], 0, 0, 0, p[6], p[7], p[8], IC, OC, B, total }; |
| 325 | const int positions = int(OW * OH * OD); |
| 326 | if (use_blas) { |
| 327 | const float alpha = 1.0f, beta = 0.0f; |
| 328 | cublasHandle_t cublas_h = ctx.cublas_handle(); |
| 329 | for (int n = 0; n < B; ++n) { |
| 330 | CUBLAS_CHECK(cublasGemmEx(cublas_h, CUBLAS_OP_N, CUBLAS_OP_N, positions, int(OC), int(IC), &alpha, |
| 331 | x_half.get() + int64_t(n) * IC * positions, CUDA_R_16F, positions, w, |
| 332 | CUDA_R_16F, int(IC), &beta, y + int64_t(n) * OC * positions, CUDA_R_32F, |
| 333 | positions, CUBLAS_COMPUTE_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP)); |
| 334 | } |
| 335 | return; |
| 336 | } |
| 337 | const int64_t blocks = ((positions + 63) / 64) * ((OC + 63) / 64) * B; |
| 338 | const int target = 8 * device.nsm; |
| 339 | const int split_k = int(std::min({ int64_t(32), int64_t(65535) / B, (IC * KW * KH * KD + 63) / 64, |
| 340 | std::max(int64_t(1), (target + blocks - 1) / blocks) })); |
| 341 | ggml_cuda_pool_alloc<float> partial(ctx.pool()); |
| 342 | float * result = split_k == 1 ? y : partial.alloc(total * split_k); |
| 343 | const dim3 block(device.warp_size, 4); |
| 344 | const dim3 grid(unsigned((positions + 63) / 64), unsigned((OC + 63) / 64), unsigned(B * split_k)); |
| 345 | if (use_mma) { |
| 346 | conv3d_launch_implicit_gemm<true>(x_half.get(), w, result, padded_params, split_k, grid, block, stream); |
| 347 | } else { |
| 348 | conv3d_launch_implicit_gemm<false>(x_half.get(), w, result, padded_params, split_k, grid, block, stream); |
| 349 | } |
| 350 | if (split_k > 1) { |
| 351 | conv3d_reduce_split_k<<<unsigned((total + 255) / 256), 256, 0, stream>>>(result, y, int(total), |
| 352 | int(OC * positions), split_k); |
| 353 | } |
| 354 | return; |
| 355 | } |
| 356 | const int blocks = int(std::min(int64_t(65535), (total + 255) / 256)); |
| 357 | if (kernel->type == GGML_TYPE_F16) { |
| 358 | conv3d_kernel<<<blocks, 256, 0, stream>>>(x, w, y, params); |
| 359 | } else { |
| 360 | conv3d_kernel<<<blocks, 256, 0, stream>>>(x, (const float *) kernel->data, y, params); |
| 361 | } |
| 362 | } |