ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cuda / conv2d-dw.cu
| 1 | #include "conv2d-dw.cuh" |
| 2 | #include "convert.cuh" |
| 3 | |
| 4 | struct conv_params { |
| 5 | int in_w, in_h; |
| 6 | int out_w, out_h; |
| 7 | int kernel_w, kernel_h; |
| 8 | int stride_x, stride_y; |
| 9 | int padding_x, padding_y; |
| 10 | int dilation_x, dilation_y; |
| 11 | int channels, batches; |
| 12 | }; |
| 13 | |
| 14 | struct kernel_bounds { |
| 15 | int y_min, y_max; |
| 16 | int x_min, x_max; |
| 17 | }; |
| 18 | |
| 19 | __device__ __forceinline__ kernel_bounds calculate_kernel_bounds(int out_x, int out_y, const conv_params & params) { |
| 20 | kernel_bounds bounds; |
| 21 | bounds.y_min = max(0, (params.padding_y - out_y * params.stride_y + params.dilation_y - 1) / params.dilation_y); |
| 22 | bounds.y_max = |
| 23 | min(params.kernel_h, |
| 24 | (params.in_h + params.padding_y - out_y * params.stride_y + params.dilation_y - 1) / params.dilation_y); |
| 25 | bounds.x_min = max(0, (params.padding_x - out_x * params.stride_x + params.dilation_x - 1) / params.dilation_x); |
| 26 | bounds.x_max = |
| 27 | min(params.kernel_w, |
| 28 | (params.in_w + params.padding_x - out_x * params.stride_x + params.dilation_x - 1) / params.dilation_x); |
| 29 | return bounds; |
| 30 | } |
| 31 | |
| 32 | __device__ __forceinline__ int calculate_input_coord(int out_coord, int kern_coord, int stride, int dilation, int padding) { |
| 33 | return out_coord * stride + kern_coord * dilation - padding; |
| 34 | } |
| 35 | |
| 36 | struct whcn_layout { |
| 37 | __device__ static int input_index(int n, int c, int y, int x, const conv_params & params) { |
| 38 | return n * (params.channels * params.in_w * params.in_h) + c * params.in_w * params.in_h + y * params.in_w + x; |
| 39 | } |
| 40 | |
| 41 | __device__ static int kernel_index(int c, int ky, int kx, const conv_params & params) { |
| 42 | return c * params.kernel_h * params.kernel_w + ky * params.kernel_w + kx; |
| 43 | } |
| 44 | |
| 45 | __device__ static int output_index(int n, int c, int y, int x, const conv_params & params) { |
| 46 | return n * (params.channels * params.out_w * params.out_h) + c * params.out_w * params.out_h + |
| 47 | y * params.out_w + x; |
| 48 | } |
| 49 | |
| 50 | __device__ static void unpack_indices(int global_idx, const conv_params & params, int & n, int & c, int & out_y, |
| 51 | int & out_x) { |
| 52 | out_x = global_idx % params.out_w; |
| 53 | out_y = (global_idx / params.out_w) % params.out_h; |
| 54 | c = (global_idx / (params.out_w * params.out_h)) % params.channels; |
| 55 | n = global_idx / (params.out_w * params.out_h * params.channels); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | struct cwhn_layout { |
| 60 | __device__ static int input_index(int n, int c, int y, int x, const conv_params & params) { |
| 61 | return n * (params.channels * params.in_w * params.in_h) + (y * params.in_w + x) * params.channels + c; |
| 62 | } |
| 63 | |
| 64 | __device__ static int kernel_index(int c, int ky, int kx, const conv_params & params) { |
| 65 | return (ky * params.kernel_w + kx) * params.channels + c; |
| 66 | } |
| 67 | |
| 68 | __device__ static int output_index(int n, int c, int y, int x, const conv_params & params) { |
| 69 | return n * (params.channels * params.out_w * params.out_h) + y * (params.out_w * params.channels) + |
| 70 | x * params.channels + c; |
| 71 | } |
| 72 | |
| 73 | __device__ static void unpack_indices(int global_idx, const conv_params & params, int & n, int & c, int & out_y, |
| 74 | int & out_x) { |
| 75 | c = global_idx % params.channels; |
| 76 | out_x = (global_idx / params.channels) % params.out_w; |
| 77 | out_y = (global_idx / (params.channels * params.out_w)) % params.out_h; |
| 78 | n = global_idx / (params.channels * params.out_w * params.out_h); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | template <typename T, typename Layout> |
| 83 | __global__ void conv2d_dw_kernel(const float * __restrict__ input, const T * __restrict__ kernel, float * __restrict__ output, |
| 84 | const int in_w, const int in_h, const int out_w, const int out_h, |
| 85 | const int kernel_w, const int kernel_h, const int stride_x, const int stride_y, |
| 86 | const int padding_x, const int padding_y, const int dilation_x, const int dilation_y, |
| 87 | const int channels, const int batches) { |
| 88 | const int global_idx = blockIdx.x * blockDim.x + threadIdx.x; |
| 89 | const int total_elements = batches * channels * out_h * out_w; |
| 90 | |
| 91 | if (global_idx >= total_elements) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | conv_params params = { in_w, in_h, out_w, out_h, kernel_w, kernel_h, stride_x, |
| 96 | stride_y, padding_x, padding_y, dilation_x, dilation_y, channels, batches }; |
| 97 | |
| 98 | int batch_idx, channel_idx, out_y_idx, out_x_idx; |
| 99 | Layout::unpack_indices(global_idx, params, batch_idx, channel_idx, out_y_idx, out_x_idx); |
| 100 | |
| 101 | float accumulator = 0.0f; |
| 102 | kernel_bounds bounds = calculate_kernel_bounds(out_x_idx, out_y_idx, params); |
| 103 | |
| 104 | for (int kern_y = bounds.y_min; kern_y < bounds.y_max; ++kern_y) { |
| 105 | int in_y_idx = calculate_input_coord(out_y_idx, kern_y, params.stride_y, params.dilation_y, params.padding_y); |
| 106 | |
| 107 | for (int kern_x = bounds.x_min; kern_x < bounds.x_max; ++kern_x) { |
| 108 | int in_x_idx = calculate_input_coord(out_x_idx, kern_x, params.stride_x, params.dilation_x, params.padding_x); |
| 109 | |
| 110 | const float input_val = input[Layout::input_index(batch_idx, channel_idx, in_y_idx, in_x_idx, params)]; |
| 111 | const T kernel_val = kernel[Layout::kernel_index(channel_idx, kern_y, kern_x, params)]; |
| 112 | |
| 113 | accumulator += input_val * ggml_cuda_cast<float>(kernel_val); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | output[Layout::output_index(batch_idx, channel_idx, out_y_idx, out_x_idx, params)] = accumulator; |
| 118 | } |
| 119 | |
| 120 | void ggml_cuda_op_conv2d_dw(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { |
| 121 | const ggml_tensor * kernel = dst->src[0]; |
| 122 | const ggml_tensor * input = dst->src[1]; |
| 123 | |
| 124 | GGML_ASSERT(kernel->type == GGML_TYPE_F16 || kernel->type == GGML_TYPE_F32); |
| 125 | GGML_ASSERT(input->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32); |
| 126 | const void * w_d = kernel->data; |
| 127 | const float * x_d = (const float *) input->data; |
| 128 | float * y_d = (float *) dst->data; |
| 129 | |
| 130 | const int32_t * p = (const int32_t *) dst->op_params; |
| 131 | const int stride_x = p[0]; |
| 132 | const int stride_y = p[1]; |
| 133 | const int padding_x = p[2]; |
| 134 | const int padding_y = p[3]; |
| 135 | const int dilation_x = p[4]; |
| 136 | const int dilation_y = p[5]; |
| 137 | |
| 138 | const int in_w = input->ne[0]; |
| 139 | const int in_h = input->ne[1]; |
| 140 | const int kernel_w = kernel->ne[0]; |
| 141 | const int kernel_h = kernel->ne[1]; |
| 142 | const int out_w = dst->ne[0]; |
| 143 | const int out_h = dst->ne[1]; |
| 144 | const int channels = dst->ne[2]; |
| 145 | const int batches = dst->ne[3]; |
| 146 | |
| 147 | cudaStream_t st = ctx.stream(); |
| 148 | |
| 149 | const int total = batches * channels * out_h * out_w; |
| 150 | const int blocks = (total + CUDA_CONV2D_DW_BLOCK_SIZE - 1) / CUDA_CONV2D_DW_BLOCK_SIZE; |
| 151 | |
| 152 | if (ggml_is_contiguous(input)) { |
| 153 | if (kernel->type == GGML_TYPE_F16) { |
| 154 | conv2d_dw_kernel<half, whcn_layout><<<blocks, CUDA_CONV2D_DW_BLOCK_SIZE, 0, st>>>( |
| 155 | x_d, (const half *) w_d, y_d, in_w, in_h, out_w, out_h, kernel_w, kernel_h, stride_x, stride_y, |
| 156 | padding_x, padding_y, dilation_x, dilation_y, channels, batches); |
| 157 | } else { |
| 158 | conv2d_dw_kernel<float, whcn_layout><<<blocks, CUDA_CONV2D_DW_BLOCK_SIZE, 0, st>>>( |
| 159 | x_d, (const float *) w_d, y_d, in_w, in_h, out_w, out_h, kernel_w, kernel_h, stride_x, stride_y, |
| 160 | padding_x, padding_y, dilation_x, dilation_y, channels, batches); |
| 161 | } |
| 162 | } else if (ggml_is_contiguous_channels(input)) { |
| 163 | if (kernel->type == GGML_TYPE_F16) { |
| 164 | conv2d_dw_kernel<half, cwhn_layout><<<blocks, CUDA_CONV2D_DW_BLOCK_SIZE, 0, st>>>( |
| 165 | x_d, (const half *) w_d, y_d, in_w, in_h, out_w, out_h, kernel_w, kernel_h, stride_x, stride_y, |
| 166 | padding_x, padding_y, dilation_x, dilation_y, channels, batches); |
| 167 | } else { |
| 168 | conv2d_dw_kernel<float, cwhn_layout><<<blocks, CUDA_CONV2D_DW_BLOCK_SIZE, 0, st>>>( |
| 169 | x_d, (const float *) w_d, y_d, in_w, in_h, out_w, out_h, kernel_w, kernel_h, stride_x, stride_y, |
| 170 | padding_x, padding_y, dilation_x, dilation_y, channels, batches); |
| 171 | } |
| 172 | } else { |
| 173 | GGML_ABORT("Unsupported memory layout for conv_2d_dw"); |
| 174 | } |
| 175 | } |