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-sycl/conv2d.cpp # 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-sycl / conv2d.cpp` 171 lines ยท 7051 bytes Raw Blame History 1 #include "conv2d.hpp" 2 #include "convert.hpp" 3 4 struct conv2d_params { 5 const int64_t IW, IH; 6 const int64_t OW, OH; 7 const int64_t KW, KH; 8 const int64_t ST_X, ST_Y; 9 const int64_t PD_X, PD_Y; 10 const int64_t DL_X, DL_Y; 11 const int64_t IC, OC; 12 const int64_t B; 13 const int64_t TOTAL; 14 }; 15 16 struct conv2d_kernel_bounds { 17 int64_t y_min, y_max; 18 int64_t x_min, x_max; 19 }; 20 21 static inline int64_t conv2d_max64(int64_t a, int64_t b) { 22 return (a > b) ? a : b; 23 } 24 25 static inline int64_t conv2d_min64(int64_t a, int64_t b) { 26 return (a < b) ? a : b; 27 } 28 29 static inline conv2d_kernel_bounds calculate_kernel_bounds(int64_t out_x, int64_t out_y, const conv2d_params & P) { 30 conv2d_kernel_bounds bounds; 31 bounds.y_min = conv2d_max64(0, (P.PD_Y - out_y * P.ST_Y + P.DL_Y - 1) / P.DL_Y); 32 bounds.y_max = conv2d_min64(P.KH, (P.IH + P.PD_Y - out_y * P.ST_Y + P.DL_Y - 1) / P.DL_Y); 33 bounds.x_min = conv2d_max64(0, (P.PD_X - out_x * P.ST_X + P.DL_X - 1) / P.DL_X); 34 bounds.x_max = conv2d_min64(P.KW, (P.IW + P.PD_X - out_x * P.ST_X + P.DL_X - 1) / P.DL_X); 35 return bounds; 36 } 37 38 static inline int calculate_input_coord(int64_t out_coord, int64_t kern_coord, int64_t stride, 39 int64_t dilation, int64_t padding) { 40 return out_coord * stride + kern_coord * dilation - padding; 41 } 42 43 // whcn layout helpers (matching ggml tensor memory order) 44 static inline int64_t whcn_input_index(int64_t n, int64_t c, int64_t y, int64_t x, const conv2d_params & P) { 45 return n * (P.IC * P.IW * P.IH) + c * P.IW * P.IH + y * P.IW + x; 46 } 47 48 static inline int64_t whcn_kernel_index(int64_t c_out, int64_t c_in, int64_t ky, int64_t kx, const conv2d_params & P) { 49 return c_out * (P.IC * P.KH * P.KW) + c_in * (P.KH * P.KW) + ky * P.KW + kx; 50 } 51 52 static inline int64_t whcn_output_index(int64_t n, int64_t c, int64_t y, int64_t x, const conv2d_params & P) { 53 return n * (P.OC * P.OW * P.OH) + c * P.OW * P.OH + y * P.OW + x; 54 } 55 56 // cwhn layout helpers: buffers are [N][H][W][C] input and [KH][KW][IC][OC] kernel 57 static inline int64_t cwhn_input_index(int64_t n, int64_t c, int64_t y, int64_t x, const conv2d_params & P) { 58 return ((n * P.IH + y) * P.IW + x) * P.IC + c; 59 } 60 61 static inline int64_t cwhn_kernel_index(int64_t c_out, int64_t c_in, int64_t ky, int64_t kx, const conv2d_params & P) { 62 return ((ky * P.KW + kx) * P.IC + c_in) * P.OC + c_out; 63 } 64 65 template 66 static void conv2d_kernel(const float * input, const T * kernel, float * output, 67 const conv2d_params P, const sycl::nd_item<3> & item_ct1) { 68 const int64_t global_idx = item_ct1.get_local_id(2) + 69 item_ct1.get_group(2) * item_ct1.get_local_range(2); 70 71 if (global_idx >= P.TOTAL) { 72 return; 73 } 74 75 const int64_t out_x = global_idx % P.OW; 76 const int64_t out_y = (global_idx / P.OW) % P.OH; 77 const int64_t c_out = (global_idx / (P.OW * P.OH)) % P.OC; 78 const int64_t n = global_idx / (P.OW * P.OH * P.OC); 79 80 float acc = 0.0f; 81 82 const conv2d_kernel_bounds bounds = calculate_kernel_bounds(out_x, out_y, P); 83 84 for (int64_t c_in = 0; c_in < P.IC; ++c_in) { 85 for (int64_t ky = bounds.y_min; ky < bounds.y_max; ++ky) { 86 const int64_t in_y = calculate_input_coord(out_y, ky, P.ST_Y, P.DL_Y, P.PD_Y); 87 for (int64_t kx = bounds.x_min; kx < bounds.x_max; ++kx) { 88 const int64_t in_x = calculate_input_coord(out_x, kx, P.ST_X, P.DL_X, P.PD_X); 89 const float input_val = input[CWHN ? cwhn_input_index(n, c_in, in_y, in_x, P) : whcn_input_index(n, c_in, in_y, in_x, P)]; 90 const T kernel_val = kernel[CWHN ? cwhn_kernel_index(c_out, c_in, ky, kx, P) : whcn_kernel_index(c_out, c_in, ky, kx, P)]; 91 acc += input_val * ggml_sycl_cast(kernel_val); 92 } 93 } 94 } 95 96 output[whcn_output_index(n, c_out, out_y, out_x, P)] = acc; 97 } 98 99 template 100 static void conv2d_sycl(const float * X_D, const T * K_D, float * Y_D, 101 const conv2d_params P, const queue_ptr & stream) { 102 const int num_blocks = (P.TOTAL + SYCL_CONV2D_BLOCK_SIZE - 1) / SYCL_CONV2D_BLOCK_SIZE; 103 const sycl::range<3> block_dims(1, 1, SYCL_CONV2D_BLOCK_SIZE); 104 const sycl::range<3> block_nums(1, 1, num_blocks); 105 stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims), 106 [=](sycl::nd_item<3> item_ct1) { 107 conv2d_kernel(X_D, K_D, Y_D, P, item_ct1); 108 }); 109 } 110 111 void ggml_sycl_op_conv2d(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { 112 scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/2); 113 114 const ggml_tensor * kernel = dst->src[0]; 115 const ggml_tensor * input = dst->src[1]; 116 const float * K_D = (const float *) kernel->data; 117 const float * X_D = (const float *) input->data; 118 float * Y_D = (float *) dst->data; 119 120 // WHCN: contiguous [KW,KH,IC,OC] kernel and [W,H,C,N] input 121 // CWHN: channel-fastest buffer [N][H][W][C] input and [KH][KW][IC][OC] kernel (OC fastest) 122 const bool whcn = ggml_is_contiguous(kernel) && ggml_is_contiguous(input); 123 const bool cwhn = input->nb[2] == ggml_type_size(input->type) && 124 kernel->nb[3] == ggml_type_size(kernel->type); 125 GGML_ASSERT(whcn || cwhn); 126 const bool cwhn_layout = !whcn && cwhn; // prefer whcn when both match (degenerate size-1 dims) 127 128 GGML_ASSERT(kernel->type == GGML_TYPE_F16 || kernel->type == GGML_TYPE_F32); 129 GGML_ASSERT(input->type == GGML_TYPE_F32); 130 GGML_ASSERT(dst->type == GGML_TYPE_F32); 131 132 // same number of input channels 133 GGML_ASSERT(input->ne[2] == kernel->ne[2]); 134 135 const queue_ptr stream = ctx.stream(); 136 137 const int32_t * p = (const int32_t *) dst->op_params; 138 const int ST_X = p[0]; 139 const int ST_Y = p[1]; 140 const int PD_X = p[2]; 141 const int PD_Y = p[3]; 142 const int DL_X = p[4]; 143 const int DL_Y = p[5]; 144 145 const int IW = input->ne[0]; 146 const int IH = input->ne[1]; 147 const int OW = dst->ne[0]; 148 const int OH = dst->ne[1]; 149 const int KW = kernel->ne[0]; 150 const int KH = kernel->ne[1]; 151 const int IC = input->ne[2]; 152 const int OC = kernel->ne[3]; 153 const int B = input->ne[3]; 154 155 const int64_t total = (int64_t) B * OC * OH * OW; 156 const conv2d_params params = { IW, IH, OW, OH, KW, KH, ST_X, ST_Y, PD_X, PD_Y, DL_X, DL_Y, IC, OC, B, total }; 157 158 if (cwhn_layout) { 159 if (kernel->type == GGML_TYPE_F16) { 160 conv2d_sycl(X_D, (const sycl::half *) K_D, Y_D, params, stream); 161 } else { 162 conv2d_sycl(X_D, K_D, Y_D, params, stream); 163 } 164 } else { 165 if (kernel->type == GGML_TYPE_F16) { 166 conv2d_sycl(X_D, (const sycl::half *) K_D, Y_D, params, stream); 167 } else { 168 conv2d_sycl(X_D, K_D, Y_D, params, stream); 169 } 170 } 171 }