|
3 | 3 | #include "ggml-cpu.h"
|
4 | 4 | #include "ggml-impl.h"
|
5 | 5 | #include "binary-ops.h"
|
| 6 | +#include "ggml.h" |
6 | 7 | #include "unary-ops.h"
|
7 | 8 | #include "vec.h"
|
8 | 9 |
|
@@ -6116,70 +6117,173 @@ void ggml_compute_forward_im2col_back_f32(
|
6116 | 6117 | }
|
6117 | 6118 | }
|
6118 | 6119 |
|
| 6120 | +static void ggml_call_mul_mat( |
| 6121 | + const ggml_compute_params * params, |
| 6122 | + int64_t m, int64_t n, int64_t k, |
| 6123 | + void * a, void * b, void * c) { |
| 6124 | + |
| 6125 | + struct ggml_tensor src1 = {}; |
| 6126 | + src1.ne[0] = k; |
| 6127 | + src1.ne[1] = m; |
| 6128 | + src1.ne[2] = 1; |
| 6129 | + src1.ne[3] = 1; |
| 6130 | + src1.nb[0] = sizeof(float); |
| 6131 | + src1.nb[1] = k * sizeof(float); |
| 6132 | + src1.nb[2] = src1.nb[1]; |
| 6133 | + src1.nb[3] = src1.nb[2]; |
| 6134 | + src1.data = a; |
| 6135 | + |
| 6136 | + struct ggml_tensor src0 = {}; |
| 6137 | + src0.ne[0] = k; |
| 6138 | + src0.ne[1] = n; |
| 6139 | + src0.ne[2] = 1; |
| 6140 | + src0.ne[3] = 1; |
| 6141 | + src0.nb[0] = sizeof(float); |
| 6142 | + src0.nb[1] = k * sizeof(float); |
| 6143 | + src0.nb[2] = src0.nb[1]; |
| 6144 | + src0.nb[3] = src0.nb[2]; |
| 6145 | + src0.data = b; |
| 6146 | + |
| 6147 | + struct ggml_tensor dst = {}; |
| 6148 | + dst.ne[0] = n; |
| 6149 | + dst.ne[1] = m; |
| 6150 | + dst.ne[2] = 1; |
| 6151 | + dst.ne[3] = 1; |
| 6152 | + dst.nb[0] = sizeof(float); |
| 6153 | + dst.nb[1] = n * sizeof(float); |
| 6154 | + dst.nb[2] = dst.nb[1]; |
| 6155 | + dst.nb[3] = dst.nb[2]; |
| 6156 | + dst.data = c; |
| 6157 | + dst.src[0] = &src0; |
| 6158 | + dst.src[1] = &src1; |
| 6159 | + |
| 6160 | + ggml_compute_forward_mul_mat(params, &dst); |
| 6161 | +} |
| 6162 | + |
| 6163 | + |
6119 | 6164 | // ggml_compute_forward_conv_2d
|
6120 | 6165 |
|
6121 |
| -static void ggml_compute_forward_conv_2d_f32( |
6122 |
| - const ggml_compute_params * params, |
6123 |
| - const ggml_tensor * kernel, // [KW, KH, IC, OC] |
6124 |
| - const ggml_tensor * src, // [W, H, C, N] |
6125 |
| - ggml_tensor * dst) { // [OW, OH, OC, N] |
| 6166 | +static void ggml_compute_forward_conv_2d_f32(const ggml_compute_params * params, |
| 6167 | + ggml_tensor * dst) { |
6126 | 6168 |
|
6127 |
| - const int32_t s0 = ggml_get_op_params_i32(dst, 0); |
6128 |
| - const int32_t s1 = ggml_get_op_params_i32(dst, 1); |
6129 |
| - const int32_t p0 = ggml_get_op_params_i32(dst, 2); |
6130 |
| - const int32_t p1 = ggml_get_op_params_i32(dst, 3); |
6131 |
| - const int32_t d0 = ggml_get_op_params_i32(dst, 4); |
6132 |
| - const int32_t d1 = ggml_get_op_params_i32(dst, 5); |
| 6169 | + const ggml_tensor * src = dst->src[1]; // [W H C_in N] |
| 6170 | + const ggml_tensor * kernel = dst->src[0]; // [W H C_in C_out] |
6133 | 6171 |
|
6134 |
| - const int64_t OW = dst->ne[0]; |
6135 |
| - const int64_t OH = dst->ne[1]; |
6136 |
| - const int64_t OC = dst->ne[2]; |
6137 |
| - const int64_t N = dst->ne[3]; |
| 6172 | + GGML_ASSERT(ggml_is_contiguous(kernel)); |
6138 | 6173 |
|
6139 |
| - const int64_t IW = src->ne[0]; |
6140 |
| - const int64_t IH = src->ne[1]; |
6141 |
| - const int64_t IC = src->ne[2]; |
| 6174 | + const int32_t stride_x = dst->op_params[0]; |
| 6175 | + const int32_t stride_y = dst->op_params[1]; |
| 6176 | + const int32_t pad_x = dst->op_params[2]; |
| 6177 | + const int32_t pad_y = dst->op_params[3]; |
6142 | 6178 |
|
6143 |
| - const int64_t KW = kernel->ne[0]; |
6144 |
| - const int64_t KH = kernel->ne[1]; |
| 6179 | + const int64_t c_in = src->ne[2]; |
| 6180 | + const int64_t c_out = kernel->ne[3]; |
| 6181 | + GGML_ASSERT(c_in == kernel->ne[2]); |
6145 | 6182 |
|
6146 |
| - const float * kernel_data = (const float *)kernel->data; |
6147 |
| - const float * src_data = (const float *)src->data; |
6148 |
| - float * dst_data = (float *)dst->data; |
| 6183 | + const int64_t src_w = src->ne[0]; |
| 6184 | + const int64_t src_h = src->ne[1]; |
| 6185 | + const int64_t knl_w = kernel->ne[0]; |
| 6186 | + const int64_t knl_h = kernel->ne[1]; |
| 6187 | + const int64_t dst_w = dst->ne[0]; |
| 6188 | + const int64_t dst_h = dst->ne[1]; |
6149 | 6189 |
|
6150 |
| - const int64_t rows_total = OH * N; |
6151 |
| - const int64_t rows_per_thread = (rows_total + params->nth - 1) / params->nth; |
6152 |
| - const int64_t row_start = params->ith * rows_per_thread; |
6153 |
| - const int64_t row_end = MIN(row_start + rows_per_thread, rows_total); |
6154 | 6190 |
|
6155 |
| - for (int64_t row = row_start; row < row_end; ++row) { |
6156 |
| - const int64_t oh = row % OH; |
6157 |
| - const int64_t n = row / OH; |
6158 |
| - const float * src_batch = src_data + n * IW * IH * IC; |
| 6191 | + float * src_data = (float *) src->data; |
| 6192 | + float * knl_data = (float *) kernel->data; |
| 6193 | + float * dst_data = ( float *) dst->data; |
6159 | 6194 |
|
6160 |
| - for (int64_t ow = 0; ow < OW; ++ow) { |
6161 |
| - for (int64_t oc = 0; oc < OC; ++oc) { |
6162 |
| - float sum = 0.0f; |
6163 |
| - const float * kernel_channel = kernel_data + oc * KW * KH * IC; |
6164 | 6195 |
|
6165 |
| - for (int64_t kh = 0; kh < KH; ++kh) { |
6166 |
| - const int64_t ih = oh * s1 - p1 + kh * d1; |
6167 |
| - if (ih < 0 || ih >= IH) continue; |
| 6196 | + const int64_t knl_n = knl_w * knl_h * c_in; |
| 6197 | + const int64_t patch_total = dst->ne[3] * dst_w * dst_h; |
| 6198 | + |
6168 | 6199 |
|
6169 |
| - for (int64_t kw = 0; kw < KW; ++kw) { |
6170 |
| - const int64_t iw = ow * s0 - p0 + kw * d0; |
6171 |
| - if (iw < 0 || iw >= IW) continue; |
| 6200 | + |
| 6201 | + const int64_t space_per_patch = knl_n * sizeof(float) + patch_total * c_out * sizeof(float); |
6172 | 6202 |
|
6173 |
| - #pragma omp simd |
6174 |
| - for (int64_t ic = 0; ic < IC; ++ic) { |
6175 |
| - const float * kernel_ptr = kernel_channel + (kh * KW + kw) + ic * KW * KH; |
6176 |
| - const float * src_ptr = src_batch + (ih * IW + iw) + ic * IW * IH; |
6177 |
| - sum += (*kernel_ptr) * (*src_ptr); |
| 6203 | + const int64_t batch_size = params->wsize / space_per_patch; |
| 6204 | + const int64_t patches_per_batch = batch_size > 8 ? (batch_size / 8) * 8 : batch_size; |
| 6205 | + const int64_t batch_n = (patch_total + patches_per_batch - 1) / patches_per_batch; |
| 6206 | + |
| 6207 | + |
| 6208 | + GGML_ASSERT(patches_per_batch > 0 && batch_size >= 1); |
| 6209 | + |
| 6210 | + float * tmp = (float *) params->wdata; // per-thread scratch |
| 6211 | + |
| 6212 | + for (int64_t batch_i = 0; batch_i < batch_n; ++batch_i) { |
| 6213 | + |
| 6214 | + const int64_t patch_start_batch = batch_i * patches_per_batch; |
| 6215 | + const int64_t patch_end_batch = std::min(patch_start_batch + patches_per_batch, |
| 6216 | + patch_total); |
| 6217 | + const int64_t patch_n = patch_end_batch - patch_start_batch; |
| 6218 | + |
| 6219 | + const int64_t patch_per_thread = |
| 6220 | + (patch_n + params->nth - 1) / params->nth; |
| 6221 | + const int64_t patch_start = patch_start_batch + |
| 6222 | + params->ith * patch_per_thread; |
| 6223 | + const int64_t patch_end = std::min(patch_start + patch_per_thread, |
| 6224 | + patch_end_batch); |
| 6225 | + |
| 6226 | + //im2col for a patch |
| 6227 | + for (int64_t p = patch_start; p < patch_end; ++p) { |
| 6228 | + const int64_t b = p / (dst_w * dst_h); |
| 6229 | + const int64_t dy = (p / dst_w) % dst_h; |
| 6230 | + const int64_t dx = p % dst_w; |
| 6231 | + |
| 6232 | + const float * src_base = (const float *)((char *)src_data + b * src->nb[3]); |
| 6233 | + float * out_row = tmp + (p % patches_per_batch) * knl_n; |
| 6234 | + |
| 6235 | + // Extract patch in IC,KH,KW order (same as im2col) |
| 6236 | + for (int64_t ic = 0; ic < c_in; ++ic) { |
| 6237 | + for (int64_t ky = 0; ky < knl_h; ++ky) { |
| 6238 | + for (int64_t kx = 0; kx < knl_w; ++kx) { |
| 6239 | + const int64_t sy = dy * stride_y + ky - pad_y; |
| 6240 | + const int64_t sx = dx * stride_x + kx - pad_x; |
| 6241 | + |
| 6242 | + int64_t dst_idx = ic * (knl_h * knl_w) + ky * knl_w + kx; |
| 6243 | + |
| 6244 | + if (sy < 0 || sy >= src_h || sx < 0 || sx >= src_w) { |
| 6245 | + out_row[dst_idx] = 0.0f; |
| 6246 | + } else { |
| 6247 | + float * src_ptr = (float *)((char *)src_base + |
| 6248 | + sx * src->nb[0] + sy * src->nb[1] + ic * src->nb[2]); |
| 6249 | + out_row[dst_idx] = *src_ptr; |
6178 | 6250 | }
|
6179 | 6251 | }
|
6180 | 6252 | }
|
| 6253 | + } |
| 6254 | + } // patches handled by this thread |
| 6255 | + |
| 6256 | + ggml_barrier(params->threadpool); // wait for all threads |
6181 | 6257 |
|
6182 |
| - dst_data[((n * OC + oc) * OH + oh) * OW + ow] = sum; |
| 6258 | + //GEMM output is patch_n * cout |
| 6259 | + float * gemm_output = tmp + patches_per_batch * knl_n; |
| 6260 | + |
| 6261 | + // GEMM: patches[patch_n, knl_n] × kernel[knl_n, c_out] = output[patch_n, c_out] |
| 6262 | + ggml_call_mul_mat(params, patch_n, c_out, knl_n, |
| 6263 | + tmp, knl_data, gemm_output); |
| 6264 | + |
| 6265 | + // Barrier to ensure GEMM completes before permutation |
| 6266 | + ggml_barrier(params->threadpool); |
| 6267 | + |
| 6268 | + // Distribute permutation work across threads |
| 6269 | + const int64_t permute_per_thread = (patch_n + params->nth - 1) / params->nth; |
| 6270 | + const int64_t permute_start = params->ith * permute_per_thread; |
| 6271 | + const int64_t permute_end = std::min(permute_start + permute_per_thread, patch_n); |
| 6272 | + |
| 6273 | + // Each thread handles part of the permutation from [patch_n, c_out] to WHCN layout |
| 6274 | + for (int64_t i = permute_start; i < permute_end; ++i) { |
| 6275 | + const int64_t p = patch_start_batch + i; |
| 6276 | + const int64_t b = p / (dst_w * dst_h); // batch index |
| 6277 | + const int64_t dy = (p / dst_w) % dst_h; // height index |
| 6278 | + const int64_t dx = p % dst_w; // width index |
| 6279 | + |
| 6280 | + // Copy all channels for this spatial position |
| 6281 | + for (int64_t oc = 0; oc < c_out; ++oc) { |
| 6282 | + const float value = gemm_output[i * c_out + oc]; |
| 6283 | + // Write to WHCN layout: dst[w, h, c, n] |
| 6284 | + float * dst_ptr = (float *)((char *)dst_data + |
| 6285 | + dx * dst->nb[0] + dy * dst->nb[1] + oc * dst->nb[2] + b * dst->nb[3]); |
| 6286 | + *dst_ptr = value; |
6183 | 6287 | }
|
6184 | 6288 | }
|
6185 | 6289 | }
|
@@ -6264,7 +6368,7 @@ void ggml_compute_forward_conv_2d(
|
6264 | 6368 | } break;
|
6265 | 6369 | case GGML_TYPE_F32:
|
6266 | 6370 | {
|
6267 |
| - ggml_compute_forward_conv_2d_f32(params, src0, src1, dst); |
| 6371 | + ggml_compute_forward_conv_2d_f32(params, dst); |
6268 | 6372 | } break;
|
6269 | 6373 | default:
|
6270 | 6374 | {
|
|
0 commit comments