diff --git a/ggml/include/ggml-backend.h b/ggml/include/ggml-backend.h index 778927f68217a..64be565a78378 100644 --- a/ggml/include/ggml-backend.h +++ b/ggml/include/ggml-backend.h @@ -340,6 +340,8 @@ extern "C" { // Compare the output of two backends GGML_API bool ggml_backend_compare_graph_backend(ggml_backend_t backend1, ggml_backend_t backend2, struct ggml_cgraph * graph, ggml_backend_eval_callback callback, void * user_data); + // Compare the output of two backends, graphs can be different and only the selected nodes will be compared + GGML_API bool ggml_backend_compare_graph_backend_node(ggml_backend_t backend1, ggml_backend_t backend2, struct ggml_cgraph * graph1, struct ggml_cgraph * graph2, ggml_backend_eval_callback callback, void * user_data, char* op_name_out_1, char* op_name_out_2); // Tensor initialization GGML_API enum ggml_status ggml_backend_tensor_alloc(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, void * addr); diff --git a/ggml/include/ggml.h b/ggml/include/ggml.h index 9c4e24023b5ad..bba7f0bbac8ec 100644 --- a/ggml/include/ggml.h +++ b/ggml/include/ggml.h @@ -481,6 +481,7 @@ extern "C" { GGML_OP_CONV_TRANSPOSE_1D, GGML_OP_IM2COL, GGML_OP_IM2COL_BACK, + GGML_OP_CONV_2D, GGML_OP_CONV_2D_DW, GGML_OP_CONV_TRANSPOSE_2D, GGML_OP_POOL_1D, @@ -1663,6 +1664,17 @@ extern "C" { int d0, // dilation dimension 0 int d1); // dilation dimension 1 + GGML_API struct ggml_tensor * ggml_conv_2d_direct( + struct ggml_context * ctx, + struct ggml_tensor * a, // convolution kernel + struct ggml_tensor * b, // data + int stride0, // stride dimension 0 + int stride1, // stride dimension 1 + int padding0, // padding dimension 0 + int padding1, // padding dimension 1 + int dilation0, // dilation dimension 0 + int dilation1); // dilation dimension 1 + // kernel size is a->ne[0] x a->ne[1] // stride is equal to kernel size // padding is zero diff --git a/ggml/src/ggml-backend.cpp b/ggml/src/ggml-backend.cpp index b1050ad59c26a..819506c87005b 100644 --- a/ggml/src/ggml-backend.cpp +++ b/ggml/src/ggml-backend.cpp @@ -1864,6 +1864,55 @@ bool ggml_backend_compare_graph_backend(ggml_backend_t backend1, ggml_backend_t return true; } +bool ggml_backend_compare_graph_backend_node( + ggml_backend_t backend1, + ggml_backend_t backend2, + struct ggml_cgraph * graph1, + struct ggml_cgraph * graph2, + ggml_backend_eval_callback callback, void * user_data, char* op_name_out_1, char* op_name_out_2) { + + ggml_tensor * out1 = NULL; + ggml_tensor * out2 = NULL; + + struct ggml_cgraph * g1 = graph1; + struct ggml_cgraph * g2 = graph2; + + for (int i = 0; i < g1->n_nodes; i++) { + struct ggml_tensor * t1 = g1->nodes[i]; + struct ggml_cgraph g1v = ggml_graph_view(g1, i, i + 1); + ggml_backend_graph_compute(backend1, &g1v); + if (ggml_is_view_op(t1->op)) { + continue; + } + if(strcmp(t1 -> name, op_name_out_1) == 0){ + out1 = t1; + } + } + + for (int i = 0; i < g2->n_nodes; i++) { + struct ggml_tensor * t2 = g2->nodes[i]; + struct ggml_cgraph g2v = ggml_graph_view(g2, i, i + 1); + ggml_backend_graph_compute(backend2, &g2v); + if (ggml_is_view_op(t2->op)) { + continue; + } + if(strcmp(t2 -> name, op_name_out_2) == 0){ + out2 = t2; + } + } + + assert(out1 != NULL); + assert(out2 != NULL); + assert(ggml_are_same_layout(out1, out2)); + + // compare results, calculate rms etc + if (!callback(0, out1, out2, user_data)) { + return false; + } + + return true; +} + // CPU backend - buffer static void * ggml_backend_cpu_buffer_get_base(ggml_backend_buffer_t buffer) { diff --git a/ggml/src/ggml-cpu/ggml-cpu.c b/ggml/src/ggml-cpu/ggml-cpu.c index 1d3cd009affc6..b601277599e37 100644 --- a/ggml/src/ggml-cpu/ggml-cpu.c +++ b/ggml/src/ggml-cpu/ggml-cpu.c @@ -1858,6 +1858,10 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm { ggml_compute_forward_im2col_back_f32(params, tensor); } break; + case GGML_OP_CONV_2D: + { + GGML_ABORT("Op not supported on CPU yet."); + } break; case GGML_OP_CONV_2D_DW: { ggml_compute_forward_conv_2d_dw(params, tensor); diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp index 99be5e45b2af7..e23826c31c70d 100644 --- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp +++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp @@ -457,6 +457,7 @@ struct vk_device_struct { vk_pipeline pipeline_rwkv_wkv6_f32; vk_pipeline pipeline_rwkv_wkv7_f32; vk_pipeline pipeline_opt_step_adamw_f32; + vk_pipeline pipeline_conv2d_f32; vk_pipeline pipeline_conv2d_dw_whcn_f32; vk_pipeline pipeline_conv2d_dw_cwhn_f32; @@ -816,6 +817,38 @@ struct vk_op_rwkv_wkv7_push_constants { uint32_t H; }; +struct vk_op_conv2d_push_constants { + uint32_t Cout; + uint32_t Cin; + uint32_t N; + + uint32_t KW; + uint32_t KH; + uint32_t W; + uint32_t H; + uint32_t OW; + uint32_t OH; + + uint32_t s0; + uint32_t s1; + uint32_t p0; + uint32_t p1; + uint32_t d0; + uint32_t d1; + + uint32_t nb01; + uint32_t nb02; + uint32_t nb03; + + uint32_t nb11; + uint32_t nb12; + uint32_t nb13; + + uint32_t nb1; + uint32_t nb2; + uint32_t nb3; +}; + struct vk_op_conv2d_dw_push_constants { uint32_t ne; uint32_t batches; @@ -916,16 +949,33 @@ class vk_memory_logger { class vk_perf_logger { public: void print_timings() { + if(timings.empty()){ + return; + } std::cerr << "----------------\nVulkan Timings:" << std::endl; for (const auto& t : timings) { uint64_t total = 0; for (const auto& time : t.second) { total += time; } - std::cerr << t.first << ": " << t.second.size() << " x " << (total / t.second.size() / 1000.0) << " us" << std::endl; + std::cerr << t.first << ": " << t.second.size() << " x " << (total / t.second.size() / 1000.0) << " us"; + + // If we have as many flops entries as timing entries for the op, then compute and log the flops/S. + auto it = flops.find(t.first); + if(it != flops.end() && (it->second).size() == t.second.size()){ + uint64_t total_nflops = 0; + for(const auto& elem : it->second){ + total_nflops += elem; + } + std::cout << " (" << (double(total_nflops)/(1000.0*1000.0*1000.0)) / (double(total)/(1000.0*1000.0*1000.0)) << " GFLOPS/s)"; + } + + + std::cerr << std::endl; } timings.clear(); + flops.clear(); } void log_timing(const ggml_tensor * node, uint64_t time) { @@ -944,12 +994,33 @@ class vk_perf_logger { name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k); } timings[name].push_back(time); + flops[name].push_back( m*n*(k+(k-1)) ); return; } + if(node->op == GGML_OP_CONV_2D){ + std::string name = ggml_op_name(node->op); + ggml_tensor * knl = node->src[0]; + uint64_t OW = node->ne[0]; + uint64_t OH = node->ne[1]; + uint64_t N = node->ne[3]; + uint64_t Cout = node->ne[2]; + uint64_t KW = knl->ne[0]; + uint64_t KH = knl->ne[1]; + uint64_t Cin = knl->ne[2]; + // KxCRS @ CRSxNPQ = KxNPQ -> M=K, K=CRS, N=NPQ + uint64_t size_M = Cout; + uint64_t size_K = Cin*KW*KH; + uint64_t size_N = N*OW*OH; + uint64_t n_flops = size_M*size_N*(size_K+(size_K-1)); + flops[name].push_back(n_flops); + timings[name].push_back(time); + return; + } timings[ggml_op_name(node->op)].push_back(time); } private: std::map> timings; + std::map> flops; }; struct ggml_backend_vk_context { @@ -2806,6 +2877,8 @@ static void ggml_vk_load_shaders(vk_device& device) { ggml_vk_create_pipeline(device, device->pipeline_opt_step_adamw_f32, "opt_step_adamw_f32", opt_step_adamw_f32_len, opt_step_adamw_f32_data, "main", 5, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1); + ggml_vk_create_pipeline(device, device->pipeline_conv2d_f32, "conv2d_f32", conv2d_f32_len, conv2d_f32_data, "main", 3, sizeof(vk_op_conv2d_push_constants), {128 /* equal to BS_K in the shader */, 128 /* equal to BS_NPQ in the shader */, 1}, {}, 1); + ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_whcn_f32, "conv2d_dw_whcn_f32", conv2d_dw_whcn_f32_len, conv2d_dw_whcn_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1); ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_cwhn_f32, "conv2d_dw_cwhn_f32", conv2d_dw_cwhn_f32_len, conv2d_dw_cwhn_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1); @@ -6578,6 +6651,16 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const return ctx->device->pipeline_leaky_relu_f32; } return nullptr; + case GGML_OP_CONV_2D: + if (src0->type == GGML_TYPE_F32 && + src1->type == GGML_TYPE_F32 && + dst->type == GGML_TYPE_F32 && + ggml_is_contiguous(src0) && + ggml_is_contiguous(src1) && + ggml_is_contiguous(dst)) { + return ctx->device->pipeline_conv2d_f32; + } + return nullptr; case GGML_OP_CONV_2D_DW: if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) { if (ggml_is_contiguous(src1)) { @@ -6899,6 +6982,30 @@ static void ggml_vk_op_f32(ggml_backend_vk_context * ctx, vk_context& subctx, co const uint32_t OW = dst->ne[0]; elements = { N * OC * OH * OW, 1, 1}; } break; + case GGML_OP_CONV_2D: + { + // src0 - kernel: [KW, KH, Cin, Cout] + // src1 - input: [W, H, Cin, N] + // dst - result: [OW, OH, Cout, N] + + // Copied from ggml.c: int64_t ggml_calc_conv_output_size(int64_t ins, int64_t ks, int s, int p, int d) + auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t { + return (ins + 2 * p - d * (ks - 1) - 1) / s + 1; + }; + // parallelize in {OW/BS_K, OH/BS_NPQ, 1} + int64_t W = src1->ne[0]; + int64_t H = src1->ne[1]; + int64_t KW = src0->ne[0]; + int64_t KH = src0->ne[1]; + int64_t Cout = src0->ne[3]; + int64_t N = src1->ne[3]; + int64_t OH = calc_conv_output_size(H, KH, dst->op_params[1], dst->op_params[3], dst->op_params[5]); + int64_t OW = calc_conv_output_size(W, KW, dst->op_params[0], dst->op_params[2], dst->op_params[4]); + int64_t NPQ = N*OW*OH; + + // Tile output matrix to (K/NB_K, NPQ/NB_NPQ, 1) workgroups + elements = {static_cast(Cout), static_cast(NPQ), 1}; + } break; case GGML_OP_ADD: case GGML_OP_SUB: case GGML_OP_DIV: @@ -7753,6 +7860,55 @@ static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, c }, dryrun); } +static void ggml_vk_conv_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) { + GGML_ASSERT(src0->type == GGML_TYPE_F32); + GGML_ASSERT(src1->type == GGML_TYPE_F32); + GGML_ASSERT( dst->type == GGML_TYPE_F32); + + GGML_TENSOR_BINARY_OP_LOCALS + + GGML_ASSERT(nb00 == sizeof(float)); + GGML_ASSERT(nb10 == sizeof(float)); + GGML_ASSERT(nb0 == sizeof(float)); + + vk_op_conv2d_push_constants p{}; + p.Cout = static_cast(ne03); + p.Cin = static_cast(ne02); + p.N = static_cast(ne13); + + p.KW = static_cast(ne00); + p.KH = static_cast(ne01); + p.W = static_cast(ne10); + p.H = static_cast(ne11); + p.OW = static_cast(ne0); + p.OH = static_cast(ne1); + + p.s0 = static_cast(dst->op_params[0]); + p.s1 = static_cast(dst->op_params[1]); + p.p0 = static_cast(dst->op_params[2]); + p.p1 = static_cast(dst->op_params[3]); + p.d0 = static_cast(dst->op_params[4]); + p.d1 = static_cast(dst->op_params[5]); + + p.nb01 = static_cast(nb01/nb00); + p.nb02 = static_cast(nb02/nb00); + p.nb03 = static_cast(nb03/nb00); + + p.nb11 = static_cast(nb11/nb10); + p.nb12 = static_cast(nb12/nb10); + p.nb13 = static_cast(nb13/nb10); + + p.nb1 = static_cast(nb1 / nb0); + p.nb2 = static_cast(nb2 / nb0); + p.nb3 = static_cast(nb3 / nb0); + + GGML_ASSERT(ne03 == ne2); + GGML_ASSERT(ne02 == ne12); + + ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D, std::move(p), dryrun); + +} + static void ggml_vk_conv_2d_dw(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) { vk_op_conv2d_dw_push_constants p{}; p.ne = ggml_nelements(dst); @@ -8799,6 +8955,7 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * nod case GGML_OP_TIMESTEP_EMBEDDING: case GGML_OP_CONV_TRANSPOSE_1D: case GGML_OP_POOL_2D: + case GGML_OP_CONV_2D: case GGML_OP_CONV_2D_DW: case GGML_OP_RWKV_WKV6: case GGML_OP_RWKV_WKV7: @@ -8864,6 +9021,7 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * nod case GGML_OP_TIMESTEP_EMBEDDING: case GGML_OP_CONV_TRANSPOSE_1D: case GGML_OP_POOL_2D: + case GGML_OP_CONV_2D: case GGML_OP_CONV_2D_DW: case GGML_OP_LEAKY_RELU: { @@ -9042,6 +9200,10 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * nod case GGML_OP_POOL_2D: ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun); + break; + case GGML_OP_CONV_2D: + ggml_vk_conv_2d(ctx, compute_ctx, src0, src1, node, dryrun); + break; case GGML_OP_CONV_2D_DW: ggml_vk_conv_2d_dw(ctx, compute_ctx, src0, src1, node, dryrun); @@ -9168,6 +9330,7 @@ static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_tensor * case GGML_OP_TIMESTEP_EMBEDDING: case GGML_OP_CONV_TRANSPOSE_1D: case GGML_OP_POOL_2D: + case GGML_OP_CONV_2D: case GGML_OP_CONV_2D_DW: case GGML_OP_RWKV_WKV6: case GGML_OP_RWKV_WKV7: @@ -10242,6 +10405,14 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm return true; case GGML_OP_CONV_TRANSPOSE_1D: return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32; + case GGML_OP_CONV_2D: + // Channel-contiguous format is not supported yet. + return (op->src[0]->type == GGML_TYPE_F32 && + op->src[1]->type == GGML_TYPE_F32 && + op->type == GGML_TYPE_F32 && + ggml_is_contiguous(op->src[0]) && + ggml_is_contiguous(op->src[1]) && + ggml_is_contiguous(op)); default: return false; } @@ -10765,6 +10936,14 @@ static void ggml_vk_check_results_0(ggml_tensor * tensor) { const int32_t p1 = tensor->op_params[6]; tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1); + } else if (tensor->op == GGML_OP_CONV_2D) { + const int32_t s0 = tensor->op_params[0]; + const int32_t s1 = tensor->op_params[1]; + const int32_t p0 = tensor->op_params[2]; + const int32_t p1 = tensor->op_params[3]; + const int32_t d0 = tensor->op_params[4]; + const int32_t d1 = tensor->op_params[5]; + tensor_clone = ggml_conv_2d(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1); } else if (tensor->op == GGML_OP_LEAKY_RELU) { const float * op_params = (const float *)tensor->op_params; tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false); diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_mm.comp b/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_mm.comp new file mode 100644 index 0000000000000..0ff942d7e2993 --- /dev/null +++ b/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_mm.comp @@ -0,0 +1,244 @@ +#version 450 + +#extension GL_EXT_control_flow_attributes : enable + +#include "types.comp" + +// shape notation: [dim(N), ..., dim(0)] -- stride(dim(j)) >= stride(dim(i)) if i > j +layout (binding = 0) readonly buffer A {A_TYPE knl_data[];}; // src0 - kernel: [KW, KH, Cin, Cout] +layout (binding = 1) readonly buffer B {B_TYPE src_data[];}; // src1 - input: [W, H, Cin, N] -- channel_first format +layout (binding = 2) writeonly buffer D {D_TYPE dst_data[];}; // dst - result: [OW, OH, Cout, N] + +layout (push_constant) uniform parameter { + // I/O channels, batch size + uint32_t Cout; + uint32_t Cin; + uint32_t N; + + // Tensor spatial sizes: kernel, input, output + uint32_t KW; + uint32_t KH; + uint32_t W; + uint32_t H; + uint32_t OW; + uint32_t OH; + + // Parameters: stride, padding, dilation - 0=y, 1=x + uint32_t s0; + uint32_t s1; + uint32_t p0; + uint32_t p1; + uint32_t d0; + uint32_t d1; + + // Strides in elements + uint32_t nb01; + uint32_t nb02; + uint32_t nb03; + + uint32_t nb11; + uint32_t nb12; + uint32_t nb13; + + uint32_t nb1; + uint32_t nb2; + uint32_t nb3; +} p; + +#define WG_SIZE 256 + +layout(local_size_x = WG_SIZE, local_size_y = 1, local_size_z = 1) in; + +uint32_t tid = gl_LocalInvocationID.x; +const uint32_t bs = gl_WorkGroupSize.x; + +uint splitWork(uint work_size, uint block_size){ + return (block_size + work_size -1) / block_size; +} + +uint32_t K = p.Cout; +uint32_t CRS = p.Cin*p.KH*p.KW; +uint32_t NPQ = p.N*p.OH*p.OW; + +uint32_t n_elems_out = K*NPQ; + +// Blocktile sizes +const uint32_t BS_K = 128; +const uint32_t BS_CRS = 16; +const uint32_t BS_NPQ = 128; + +// Number of blocktiles per input +uint32_t NB_CRS = splitWork(CRS, BS_CRS); + +const uint32_t Ash_stride = BS_CRS+1; +const uint32_t Bsh_stride = BS_NPQ+1; + +const uint32_t Ash_numel = BS_K*BS_CRS; +const uint32_t Bsh_numel = BS_CRS*BS_NPQ; + +const uint32_t Ash_len = BS_K*Ash_stride; +const uint32_t Bsh_len = BS_CRS*Bsh_stride; + +shared float Ash[Ash_len]; // K x CRS +shared float Bsh[Bsh_len]; // CRS x NPQ + +// Threadtile sizes +const uint32_t TS_K = 16; +const uint32_t TS_NPQ = BS_K*BS_NPQ / WG_SIZE / TS_K; + +// Number of threadtiles per blocktile +const uint32_t NT_K = BS_K / TS_K; +const uint32_t NT_NPQ = BS_NPQ / TS_NPQ; + +float regA[TS_K]; +float regB[TS_NPQ]; +float regC[TS_K][TS_NPQ]; + +/* +Compute +KxCRS @ CRSxNPQ = K x NPQ +K=Cout +C=Cin +R,S=KH,KW +P,Q=OH,OW +*/ + +uint32_t B_idx_K = gl_WorkGroupID.x; +uint32_t B_idx_NPQ = gl_WorkGroupID.y; + +uint32_t T_y = tid / NT_NPQ; +uint32_t T_x = tid % NT_NPQ; + +uint32_t Ar = tid / BS_CRS; +uint32_t Ac = tid % BS_CRS; +uint32_t ArpWg = WG_SIZE / BS_CRS; + +uint32_t Br = tid / BS_NPQ; +uint32_t Bc = tid % BS_NPQ; +uint32_t BrpWg = WG_SIZE / BS_NPQ; + +void initReg(){ + for(uint32_t T_ly = 0; T_ly < TS_K; T_ly++){ + for(uint32_t T_lx = 0; T_lx < TS_NPQ; T_lx++){ + regC[T_ly][T_lx] = 0.0; + } + } +} + +void outProdReg(){ + for(uint32_t CRS_lidx = 0; CRS_lidx < BS_CRS; CRS_lidx++){ + for(uint32_t T_ly = 0; T_ly < TS_K; T_ly++){ + regA[T_ly] = Ash[(T_y*TS_K + T_ly)*Ash_stride + CRS_lidx]; + } + for(uint32_t T_lx = 0; T_lx < TS_NPQ; T_lx++){ + regB[T_lx] = Bsh[CRS_lidx*Bsh_stride + T_x*TS_NPQ+T_lx]; + } + for(uint32_t T_ly = 0; T_ly < TS_K; T_ly++){ + for(uint32_t T_lx = 0; T_lx < TS_NPQ; T_lx++){ + regC[T_ly][T_lx] += regA[T_ly] * regB[T_lx]; + } + } + } +} + +// Generate different functions for computing the sides. + +#define NOOP() + +#define DEF_BOUNDARY_CONDITION_A_IF()\ +if(K_idx < K && CRS_idx < CRS){ + +#define DEF_BOUNDARY_CONDITION_A_ELSE()\ +}else{\ + Ash[B_ly * Ash_stride + B_lx] = 0.0;\ +} + +#define DEF_BOUNDARY_CONDITION_B_IF()\ +if(CRS_idx < CRS && NPQ_idx < NPQ){ + +#define DEF_BOUNDARY_CONDITION_B_ELSE()\ +}else{\ + Bsh[B_ly * Bsh_stride + B_lx] = 0.0;\ +} + +#define MAIN_LOOP(FUNC_NAME_SUFFIX, BOUNDARY_CONDITION_A_IF, BOUNDARY_CONDITION_A_ELSE, BOUNDARY_CONDITION_B_IF, BOUNDARY_CONDITION_B_ELSE)\ +void mainLoop ## FUNC_NAME_SUFFIX(){\ + initReg();\ + /* Advance block in CRS dim */\ + for(uint32_t B_idx_CRS = 0; B_idx_CRS < NB_CRS; B_idx_CRS++){\ + /* Load kernel to A_block: (BS_K x BS_CRS)*/\ + for(uint32_t r_offset = 0; r_offset < BS_K; r_offset += ArpWg){\ + uint32_t B_ly = r_offset + Ar;\ + uint32_t B_lx = Ac;\ + uint32_t K_idx = B_idx_K*BS_K + B_ly; /* Global K_idx (row index of A)*/\ + uint32_t CRS_idx = B_idx_CRS*BS_CRS + B_lx; /* Global CRS_idx (column index of A)*/\ + BOUNDARY_CONDITION_A_IF()\ + uint32_t Cin_idx = CRS_idx / (p.KW*p.KH);\ + uint32_t KH_idx = (CRS_idx - Cin_idx*p.KW*p.KH) / p.KW;\ + uint32_t KW_idx = CRS_idx - Cin_idx*p.KW*p.KH - KH_idx*p.KW;\ + uint32_t knl_idx = KW_idx + KH_idx*p.nb01 + Cin_idx*p.nb02 + K_idx*p.nb03;\ + Ash[B_ly * Ash_stride + B_lx] = knl_data[knl_idx];\ + BOUNDARY_CONDITION_A_ELSE()\ + }\ + barrier();\ + /* Load input to B_block: (BS_CRS x BS_NPQ) */\ + for(uint32_t r_offset = 0; r_offset < BS_CRS; r_offset += BrpWg){\ + uint32_t B_ly = r_offset + Br; /* Row index of B block */\ + uint32_t B_lx = Bc; /* Column index of B block */\ + uint32_t CRS_idx = B_idx_CRS*BS_CRS + B_ly; /* Global CRS index (row index of B) */\ + uint32_t NPQ_idx = B_idx_NPQ*BS_NPQ + B_lx; /* Global NPQ index (column index of B) */\ + BOUNDARY_CONDITION_B_IF()\ + uint32_t Cin_idx = CRS_idx / (p.KW*p.KH);\ + uint32_t KH_idx = (CRS_idx - Cin_idx*p.KW*p.KH) / p.KW;\ + uint32_t KW_idx = CRS_idx - Cin_idx*p.KW*p.KH - KH_idx*p.KW;\ + uint32_t N_idx = NPQ_idx / (p.OH*p.OW);\ + uint32_t OH_idx = (NPQ_idx - N_idx*p.OH*p.OW) / p.OW;\ + uint32_t OW_idx = NPQ_idx - N_idx*p.OH*p.OW - OH_idx*p.OW;\ + uint32_t H_idx = OH_idx*p.s1 + KH_idx*p.d1 - p.p1;\ + uint32_t W_idx = OW_idx*p.s0 + KW_idx*p.d0 - p.p0;\ + if(H_idx >= 0 && H_idx < p.H && W_idx >= 0 && W_idx < p.W){\ + uint32_t src_idx = W_idx + H_idx*p.nb11 + Cin_idx*p.nb12 + N_idx*p.nb13;\ + Bsh[B_ly * Bsh_stride + B_lx] = src_data[src_idx];\ + }else{\ + Bsh[B_ly * Bsh_stride + B_lx] = 0.0;\ + }\ + BOUNDARY_CONDITION_B_ELSE()\ + }\ + barrier();\ + outProdReg();\ + barrier();\ + }\ + /* Save C* */\ + for(uint32_t T_ly = 0; T_ly < TS_K; T_ly++){\ + for(uint32_t T_lx = 0; T_lx < TS_NPQ; T_lx++){\ + uint32_t K_idx = B_idx_K * BS_K + T_y * TS_K + T_ly;\ + uint32_t NPQ_idx = B_idx_NPQ * BS_NPQ + T_x * TS_NPQ + T_lx;\ + if(K_idx < K && NPQ_idx < NPQ){\ + uint32_t N_idx = NPQ_idx / (p.OH*p.OW);\ + uint32_t OH_idx = (NPQ_idx - N_idx*p.OH*p.OW) / p.OW;\ + uint32_t OW_idx = NPQ_idx - N_idx*p.OH*p.OW - OH_idx*p.OW;\ + uint32_t dst_idx = OW_idx + OH_idx*p.nb1 + K_idx*p.nb2 + N_idx*p.nb3;\ + dst_data[dst_idx] = regC[T_ly][T_lx];\ + }\ + }\ + }\ +} + +// Generates mainLoopBoundaryCheck +MAIN_LOOP(BoundaryCheck, + DEF_BOUNDARY_CONDITION_A_IF, + DEF_BOUNDARY_CONDITION_A_ELSE, + DEF_BOUNDARY_CONDITION_B_IF, + DEF_BOUNDARY_CONDITION_B_ELSE) + +// Generates mainLoopNoBoundaryCheck +MAIN_LOOP(NoBoundaryCheck, + NOOP, NOOP, NOOP, NOOP) + +void main(){ + if(gl_WorkGroupID.x == gl_NumWorkGroups.x-1 || gl_WorkGroupID.y == gl_NumWorkGroups.y-1){ + mainLoopBoundaryCheck(); + }else{ + mainLoopNoBoundaryCheck(); + } +} \ No newline at end of file diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp b/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp index c63345ec8b4b6..7cb998b0e5b24 100644 --- a/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +++ b/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp @@ -632,6 +632,8 @@ void process_shaders() { string_to_spv("opt_step_adamw_f32", "opt_step_adamw.comp", merge_maps(base_dict, {{"A_TYPE", "float"}})); + string_to_spv("conv2d_f32", "conv2d_mm.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}); + string_to_spv("conv2d_dw_whcn_f32", "conv2d_dw.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}, {"WHCN", "1"}})); string_to_spv("conv2d_dw_cwhn_f32", "conv2d_dw.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}, {"CWHN", "1"}})); diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c index f8e7c595bce15..4f31b0b4c5660 100644 --- a/ggml/src/ggml.c +++ b/ggml/src/ggml.c @@ -947,6 +947,7 @@ static const char * GGML_OP_NAME[GGML_OP_COUNT] = { "CONV_TRANSPOSE_1D", "IM2COL", "IM2COL_BACK", + "CONV_2D", "CONV_2D_DW", "CONV_TRANSPOSE_2D", "POOL_1D", @@ -986,7 +987,7 @@ static const char * GGML_OP_NAME[GGML_OP_COUNT] = { "OPT_STEP_ADAMW", }; -static_assert(GGML_OP_COUNT == 83, "GGML_OP_COUNT != 83"); +static_assert(GGML_OP_COUNT == 84, "GGML_OP_COUNT != 84"); static const char * GGML_OP_SYMBOL[GGML_OP_COUNT] = { "none", @@ -1043,6 +1044,7 @@ static const char * GGML_OP_SYMBOL[GGML_OP_COUNT] = { "conv_transpose_1d(x)", "im2col(x)", "im2col_back(x)", + "conv_2d(x)", "conv_2d_dw(x)", "conv_transpose_2d(x)", "pool_1d(x)", @@ -1082,7 +1084,7 @@ static const char * GGML_OP_SYMBOL[GGML_OP_COUNT] = { "adamw(x)", }; -static_assert(GGML_OP_COUNT == 83, "GGML_OP_COUNT != 83"); +static_assert(GGML_OP_COUNT == 84, "GGML_OP_COUNT != 84"); static_assert(GGML_OP_POOL_COUNT == 2, "GGML_OP_POOL_COUNT != 2"); @@ -4091,6 +4093,42 @@ struct ggml_tensor * ggml_conv_2d_dw( return result; } +// ggml_conv_2d_direct + +struct ggml_tensor * ggml_conv_2d_direct( + struct ggml_context * ctx, + struct ggml_tensor * a, + struct ggml_tensor * b, + int stride0, + int stride1, + int pad0, + int pad1, + int dilation0, + int dilation1) { + int64_t ne[4]; + int64_t OW = ggml_calc_conv_output_size(b->ne[0], a->ne[0], stride0, pad0, dilation0); + int64_t OH = ggml_calc_conv_output_size(b->ne[1], a->ne[1], stride1, pad1, dilation1); + GGML_ASSERT((OH > 0) && "b (kernel) too small compared to a (input)"); + GGML_ASSERT((OW > 0) && "b (kernel) too small compared to a (input)"); + + ne[0] = OW; + ne[1] = OH; + ne[2] = a->ne[3]; + ne[3] = b->ne[3]; + + struct ggml_tensor * result = ggml_new_tensor(ctx, b->type, 4, ne); + + // TODO: handle channel-contiguous layout + + int32_t params[] = { stride0, stride1, pad0, pad1, dilation0, dilation1 }; + ggml_set_op_params(result, params, sizeof(params)); + + result->op = GGML_OP_CONV_2D; + result->src[0] = a; + result->src[1] = b; + return result; +} + // ggml_conv_2d_dw_direct struct ggml_tensor * ggml_conv_2d_dw_direct( diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 772bee346f000..a33495a7e7a71 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -432,7 +432,7 @@ struct test_case { return t; } - bool eval(ggml_backend_t backend1, ggml_backend_t backend2, const char * op_name) { + virtual bool eval(ggml_backend_t backend1, ggml_backend_t backend2, const char * op_name) { mode = MODE_TEST; ggml_init_params params = { @@ -980,6 +980,250 @@ struct test_case { } }; +// This can be useful to compare the output/performance of +// different graphs implementing the same op. +// Possible use cases: +// * no CPU implementation exists for the op, but the op +// can be built by combining elementary ops already having implementation +// and the user wants to compare the results. +// * comparing the performance of different implementations +// of the op: graph revwriting/operation fusion. E.g. basic attention +// compared to flash attention or conv compared with im2col->matmul. +struct test_case_ref : public test_case { +public: + ggml_cgraph * gf_ref = nullptr; + + // Output tensor names to compare + const char* output_node_name_ref; + const char* output_node_name; + + // Input tensor names in (actual graph, reference graph) + std::vector> input_names = { + {"input", "input"}, + {"kernel", "kernel"} + }; + + // Copies the inputs of the graph built using build_graph() to the reference graph + virtual void copy_data_to_ref(ggml_context * ctx, ggml_context * ctx_ref){ + std::map inputs; + std::map inputs_ref; + + for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { + for(auto e : input_names){ + if(e.first == t->name){ + inputs[e.first] = t; + } + } + } + + for (ggml_tensor * t = ggml_get_first_tensor(ctx_ref); t != nullptr; t = ggml_get_next_tensor(ctx_ref, t)) { + for(auto e : input_names){ + if(e.second == t->name){ + inputs_ref[e.second] = t; + } + } + } + + for(auto e : input_names){ + GGML_ASSERT(inputs.count(e.first) == 1); + GGML_ASSERT(inputs_ref.count(e.second) == 1); + std::vector buf(ggml_nbytes(inputs[e.first])); + ggml_backend_tensor_get(inputs[e.first], buf.data(), 0, ggml_nbytes(inputs[e.first])); + ggml_backend_tensor_set(inputs_ref[e.second], buf.data(), 0, buf.size()); + } + } + + // Graph of the reference op implementation + virtual ggml_tensor * build_graph_ref(ggml_context * ctx) = 0; + + // Compares the output of the actual graph to the output of the reference + bool eval(ggml_backend_t backend1, ggml_backend_t backend2, const char * op_name) override { + mode = MODE_TEST; + + ggml_init_params params = { + /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead(), + /* .mem_base = */ NULL, + /* .no_alloc = */ true, + }; + ggml_context * ctx = ggml_init(params); + ggml_context * ctx_ref = ggml_init(params); + GGML_ASSERT(ctx); + GGML_ASSERT(ctx_ref); + + gf = ggml_new_graph(ctx); + gf_ref = ggml_new_graph(ctx_ref); + + // pre-graph sentinel + add_sentinel(ctx); + add_sentinel(ctx_ref); + + ggml_tensor * out = build_graph(ctx); + ggml_tensor * out_ref = build_graph_ref(ctx_ref); + + if (op_name != nullptr && op_desc(out) != op_name) { + //printf(" %s: skipping\n", op_desc(out).c_str()); + ggml_free(ctx); + return true; + } + + printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str()); + fflush(stdout); + + // check if the backends support the ops + bool supported = true; + for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { + if (!ggml_backend_supports_op(backend1, t)) { + printf("op %s not supported on [%s] ", ggml_op_name(t->op), ggml_backend_name(backend1)); + supported = false; + break; + } + } + + for (ggml_tensor * t = ggml_get_first_tensor(ctx_ref); t != NULL; t = ggml_get_next_tensor(ctx_ref, t)) { + if (!ggml_backend_supports_op(backend2, t)) { + printf("op %s not supported on [%s] ", ggml_op_name(t->op), ggml_backend_name(backend2)); + supported = false; + break; + } + } + + if (!supported) { + printf("\n"); + ggml_free(ctx); + return true; + } + + // post-graph sentinel + add_sentinel(ctx); + add_sentinel(ctx_ref); + + // allocate + ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend1); + + if (buf == NULL) { + printf("failed to allocate tensors [%s] ", ggml_backend_name(backend1)); + ggml_free(ctx); + return false; + } + + ggml_backend_buffer_t buf_ref = ggml_backend_alloc_ctx_tensors(ctx_ref, backend2); + if (buf_ref == NULL) { + printf("failed to allocate tensors [%s] ", ggml_backend_name(backend2)); + ggml_free(ctx_ref); + return false; + } + + // build graph + ggml_build_forward_expand(gf, out); + ggml_build_forward_expand(gf_ref, out_ref); + + // add sentinels as graph nodes so that they are checked in the callback + for (ggml_tensor * sentinel : sentinels) { + ggml_graph_add_node(gf, sentinel); + ggml_graph_add_node(gf_ref, sentinel); + } + + // randomize tensors + initialize_tensors(ctx); + copy_data_to_ref(ctx, ctx_ref); + + // compare + struct callback_userdata { + bool ok; + double max_err; + ggml_backend_t backend1; + ggml_backend_t backend2; + }; + + callback_userdata ud { + true, + max_nmse_err(), + backend1, + backend2 + }; + + auto callback = [](int index, ggml_tensor * t1, ggml_tensor * t2, void * user_data) -> bool { + callback_userdata * ud = (callback_userdata *) user_data; + const char * bn1 = ggml_backend_name(ud->backend1); + const char * bn2 = ggml_backend_name(ud->backend2); + + if (t1->op == GGML_OP_NONE) { + // sentinels must be unchanged + std::vector t1_data(ggml_nbytes(t1)); + std::vector t2_data(ggml_nbytes(t2)); + ggml_backend_tensor_get(t1, t1_data.data(), 0, ggml_nbytes(t1)); + ggml_backend_tensor_get(t2, t2_data.data(), 0, ggml_nbytes(t2)); + + if (memcmp(t1_data.data(), t2_data.data(), ggml_nbytes(t1)) != 0) { + printf("sentinel mismatch: %s ", t1->name); + ud->ok = false; + return true; + } + } + + std::vector f1 = tensor_to_float(t1); + std::vector f2 = tensor_to_float(t2); + + for (size_t i = 0; i < f1.size(); i++) { + // check for nans + if (std::isnan(f1[i]) || std::isnan(f2[i])) { + printf("[%s] NaN at index %zu (%s=%f %s=%f) ", ggml_op_desc(t1), i, bn1, f1[i], bn2, f2[i]); + ud->ok = false; + return true; + } + // check for infs: both must be inf of the same sign, or both must be finite + if (isinf_or_max(f1[i]) || isinf_or_max(f2[i])) { + if (isinf_or_max(f1[i]) && isinf_or_max(f2[i])) { + if (std::signbit(f1[i]) != std::signbit(f2[i])) { + printf("[%s] inf sign mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]); + ud->ok = false; + return true; + } + } else { + printf("[%s] inf mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]); + ud->ok = false; + return true; + } + } + } + + double err = nmse(f1.data(), f2.data(), f1.size()); + if (err > ud->max_err) { + printf("[%s] NMSE = %.9f > %.9f ", ggml_op_desc(t1), err, ud->max_err); + //for (int i = 0; i < (int) f1.size(); i++) { + // printf("%5d %9.6f %9.6f, diff = %9.6f\n", i, f1[i], f2[i], f1[i] - f2[i]); + //} + //printf("\n"); + //exit(1); + ud->ok = false; + } + return true; + + GGML_UNUSED(index); + }; + + + const bool cmp_ok = ggml_backend_compare_graph_backend_node(backend1, backend2, gf, gf_ref, callback, &ud, const_cast(output_node_name), const_cast(output_node_name_ref)); + + if (!cmp_ok) { + printf("compare failed "); + } + + ggml_backend_buffer_free(buf); + ggml_backend_buffer_free(buf_ref); + + ggml_free(ctx); + ggml_free(ctx_ref); + + if (ud.ok && cmp_ok) { + printf("\033[1;32mOK\033[0m\n"); + return true; + } + + printf("\033[1;31mFAIL\033[0m\n"); + return false; + } +}; // ################################### // ## Section 2: GGML Op Defintions ## @@ -2801,6 +3045,141 @@ struct test_im2col : public test_case { } }; +// Tests CONV_2D by comparing it to the IM2COL -> MUL_MM +// reference implementation. +struct test_conv_2d : public test_case_ref { + const std::array ne_input; + const std::array ne_kernel; + const int stride0; + const int stride1; + const int padding0; + const int padding1; + const int dilation0; + const int dilation1; + // Whether the inputs are contiguous in the channel dim or the width dim + const bool cwhn; + // If true, the direct CONV_2D will be used in the graph, otherwise it + // uses ggml_conv_2d: + // * if the program is called with -o CONV_2D_DIRECT_IMPL, the + // CONV_2D graph will be built, while + // * if the program is called with -o CONV_2D_INDIRECT_IMPL, the + // IM2COL -> MUL_MM graph will be built. + const bool direct_impl; + + virtual std::string op_desc(ggml_tensor * t) { + (void) t; + if(direct_impl){ + return std::string("CONV_2D_DIRECT_IMPL"); + }else{ + return std::string("CONV_2D_INDIRECT_IMPL"); + } + } + + std::string vars() override { + return VARS_TO_STR9(ne_input, ne_kernel, stride0, stride1, padding0, padding1, dilation0, dilation1, cwhn); + } + + uint64_t op_flops(ggml_tensor * t) override { + GGML_UNUSED(t); + // Just counting matmul costs: + // KxCRS @ CRSxNPQ = KxNPQ --> KxNPQx(CRS+CRS-1) flops + + // Copied from ggml.c: int64_t ggml_calc_conv_output_size(int64_t ins, int64_t ks, int s, int p, int d) + auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t { + return (ins + 2 * p - d * (ks - 1) - 1) / s + 1; + }; + + int64_t W = ne_input[0]; + int64_t H = ne_input[1]; + int64_t KW = ne_kernel[0]; + int64_t KH = ne_kernel[1]; + int64_t Cin = ne_kernel[2]; + int64_t Cout = ne_kernel[3]; + int64_t N = ne_input[3]; + int64_t OH = calc_conv_output_size(H, KH, stride0, padding0, dilation0); + int64_t OW = calc_conv_output_size(W, KW, stride0, padding0, dilation0); + + int64_t K = Cout; + int64_t CRS = Cin*KH*KW; + int64_t NPQ = N*OH*OW; + + return K*NPQ*(2*CRS-1); + } + + + test_conv_2d(std::array ne_input = {64, 64, 16, 1}, + std::array ne_kernel = {3, 3, 1, 16}, + int stride0 = 1, int stride1 = 1, int padding0 = 0, int padding1 = 0, int dilation0 = 1, int dilation1 = 1, bool cwhn = false, bool direct_impl = true) + : ne_input(ne_input), ne_kernel(ne_kernel), stride0(stride0), stride1(stride1), padding0(padding0), padding1(padding1), dilation0(dilation0), dilation1(dilation1), cwhn(cwhn), direct_impl(direct_impl) { + output_node_name_ref = "out"; + output_node_name = "out"; + } + + ggml_tensor * build_graph_indirect(ggml_context * ctx) { + ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data()); + ggml_set_name(input, "input"); + + ggml_tensor * kernel = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_kernel.data()); + ggml_set_name(kernel, "kernel"); + + GGML_ASSERT(cwhn==false); + + if (cwhn) { + // change memory layout to channel-most-contiguous (CWHN), + // then permute it back so NE matches the original input + input = ggml_cont(ctx, ggml_permute(ctx, input, 1, 2, 0, 3)); + input = ggml_permute(ctx, input, 2, 0, 1, 3); + kernel = ggml_cont(ctx, ggml_permute(ctx, kernel, 2, 3, 1, 0)); + kernel = ggml_permute(ctx, kernel, 3, 2, 0, 1); + } + + ggml_tensor * conv2d_out = ggml_conv_2d( + ctx, kernel, input, + stride0, stride1, padding0, padding1, dilation0, dilation1); + + ggml_tensor * out = ggml_cont(ctx, conv2d_out); + + ggml_set_name(out, "out"); + return out; + } + + ggml_tensor * build_graph_direct(ggml_context * ctx) { + ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data()); + ggml_set_name(input, "input"); + + ggml_tensor * kernel = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_kernel.data()); + ggml_set_name(kernel, "kernel"); + + if (cwhn) { + // change memory layout to channel-most-contiguous (CWHN), + // then permute it back so NE matches the original input + input = ggml_cont(ctx, ggml_permute(ctx, input, 1, 2, 0, 3)); + input = ggml_permute(ctx, input, 2, 0, 1, 3); + kernel = ggml_cont(ctx, ggml_permute(ctx, kernel, 2, 3, 1, 0)); + kernel = ggml_permute(ctx, kernel, 3, 2, 0, 1); + } + + ggml_tensor * out = ggml_conv_2d_direct( + ctx, kernel, input, + stride0, stride1, padding0, padding1, dilation0, dilation1); + ggml_set_name(out, "out"); + return out; + } + + ggml_tensor * build_graph(ggml_context * ctx) override { + if(direct_impl){ + return build_graph_direct(ctx); + }else{ + return build_graph_indirect(ctx); + } + } + + // Reference always uses the indirect impl. + ggml_tensor * build_graph_ref(ggml_context * ctx) override { + return build_graph_indirect(ctx); + } +}; + // GGML_OP_CONV_2D_DW struct test_conv_2d_dw : public test_case { const std::array ne_input; @@ -4046,6 +4425,34 @@ static std::vector> make_test_cases_eval() { test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {12, 12, 1, 2560}, {3, 3, 1, 2560}, 1, 1, 1, 1, 1, 1, true)); test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {12, 12, 2, 2560}, {3, 3, 2, 2560}, 1, 1, 1, 1, 1, 1, true)); + // CONV_2D: + auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t { + return (ins + 2 * p - d * (ks - 1) - 1) / s + 1; + }; + + uint32_t s0 = 3; + uint32_t s1 = 5; + uint32_t p0 = 5; + uint32_t p1 = 2; + uint32_t d0 = 2; + uint32_t d1 = 4; + + for(uint32_t Cin : {1, 25}){ + for(uint32_t Cout : {1, 12}){ + for(uint32_t KH : {1, 2, 3, 11}){ + for(uint32_t KW : {1, 2, 3, 11}){ + for(uint32_t H : {1, 100}){ + for(uint32_t W : {1, 100}){ + if(calc_conv_output_size(W, KW, s0, p0, d0) > 0 && calc_conv_output_size(H, KH, s1, p1, d1) > 0){ + test_cases.emplace_back(new test_conv_2d({W, H, Cin, 2}, {KW, KH, Cin, Cout}, s0, s1, p0, p1, d0, d1, false, true)); + } + } + } + } + } + } + } + // sycl backend will limit task global_range < MAX_INT // test cases for 2D im2col with large input W and H (occurs in stable-diffusion) // however these cases need to alloc more memory which may fail in some devices (Intel Arc770, etc.) @@ -4597,6 +5004,12 @@ static std::vector> make_test_cases_eval() { static std::vector> make_test_cases_perf() { std::vector> test_cases; + // Conv2d: K=CRS=NPQ=4096 matmul performance + // Direct CONV_2D + test_cases.emplace_back(new test_conv_2d({19, 19, 256, 16}, {4, 4, 256, 4096}, 1, 1, 0, 0, 1, 1, false, true)); + // Indirect CONV_2D (uses im2col + sgemm) + test_cases.emplace_back(new test_conv_2d({19, 19, 256, 16}, {4, 4, 256, 4096}, 1, 1, 0, 0, 1, 1, false, false)); + test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {4096, 1, 1, 1}, {1, 1, 1, 1})); test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {4096, 1, 1, 1}, {1, 512, 1, 1}));