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-openvino/openvino/pass/fuse_to_conv.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-openvino / openvino / pass / fuse_to_conv.cpp` 283 lines ยท 12952 bytes Raw Blame History 1 #include "fuse_to_conv.h" 2 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 19 namespace opp = ov::pass::pattern; 20 21 namespace ov { 22 namespace frontend { 23 namespace ggml { 24 namespace pass { 25 26 FuseToConv::FuseToConv() { 27 const auto m_in0 = opp::any_input(); 28 const auto m_in1 = opp::any_input(); 29 const auto m_matmul = opp::wrap_type({m_in0, m_in1}); 30 31 const auto callback = [=](opp::Matcher & m) { 32 auto matmul_node = ov::as_type_ptr(m.get_match_root()); 33 if (!matmul_node) { 34 return false; 35 } 36 37 auto unwrap = [](ov::Output n) { 38 while (ov::is_type(n.get_node_shared_ptr()) || 39 ov::is_type(n.get_node_shared_ptr())) { 40 n = n.get_node_shared_ptr()->input_value(0); 41 } 42 return n; 43 }; 44 45 auto get_im2col = [&](ov::Output trace) 46 -> std::pair, std::shared_ptr> { 47 auto t2 = ov::as_type_ptr(unwrap(std::move(trace)).get_node_shared_ptr()); 48 auto r1 = t2 ? ov::as_type_ptr(t2->get_input_node_shared_ptr(0)) : nullptr; 49 auto t1 = r1 ? ov::as_type_ptr(r1->get_input_node_shared_ptr(0)) : nullptr; 50 auto eip = 51 t1 ? ov::as_type_ptr(t1->get_input_node_shared_ptr(0)) : nullptr; 52 auto pad = eip ? ov::as_type_ptr(eip->get_input_node_shared_ptr(0)) : nullptr; 53 return {eip, pad}; 54 }; 55 56 bool weight_is_in0 = true; 57 auto [eip, pad] = get_im2col(matmul_node->input_value(1)); 58 ov::Output w_trace = matmul_node->input_value(0); 59 if (!pad) { 60 std::tie(eip, pad) = get_im2col(matmul_node->input_value(0)); 61 w_trace = matmul_node->input_value(1); 62 weight_is_in0 = false; 63 if (!pad) { 64 return false; 65 } 66 } 67 68 auto pb_const = ov::as_type_ptr(pad->get_input_node_shared_ptr(1)); 69 auto pe_const = ov::as_type_ptr(pad->get_input_node_shared_ptr(2)); 70 if (!pb_const || !pe_const) { 71 return false; 72 } 73 74 const auto pb = pb_const->cast_vector(); 75 const auto pe = pe_const->cast_vector(); 76 if (pb.size() < 4 || pe.size() < 4) { 77 return false; 78 } 79 80 auto image_input = pad->input_value(0); 81 auto image_shape = image_input.get_partial_shape(); 82 if (image_shape.rank() != 4 || image_shape[1].is_dynamic()) { 83 return false; 84 } 85 const size_t IC = static_cast(image_shape[1].get_length()); 86 87 w_trace = unwrap(w_trace); 88 auto weight_pshape = w_trace.get_partial_shape(); 89 if (!weight_pshape.is_static()) { 90 return false; 91 } 92 93 const auto & ws = weight_pshape.to_shape(); 94 const size_t KH = eip->get_sizes()[0]; 95 const size_t KW = eip->get_sizes()[1]; 96 const size_t kernel_spatial_ic = IC * KH * KW; 97 98 size_t groups = 0; 99 if (IC == 1 && image_shape[0].is_static() && image_shape[2].is_static() && image_shape[3].is_static()) { 100 if ((ws.size() == 4 && ws[1] == 1 && ws[2] == KH && ws[3] == KW && ws[0] > 1) || 101 (ws.size() == 2 && ws[1] == KH * KW && ws[0] > 1) || 102 (ws.size() == 3 && ws[1] == 1 && ws[2] == KH * KW && ws[0] > 1)) { 103 groups = ws[0]; 104 } else if (ws.size() == 4 && ws[0] == 1 && ws[2] == 1 && ws[3] == KH * KW && ws[1] > 1) { 105 groups = ws[1]; 106 } 107 } 108 109 const bool is_depthwise = groups > 1 && (image_shape[0].get_length() % groups == 0); 110 111 ov::Output conv_out; 112 size_t OC = 0; 113 114 if (is_depthwise) { 115 const size_t N = static_cast(image_shape[0].get_length()) / groups; 116 const size_t IH = static_cast(image_shape[2].get_length()); 117 const size_t IW = static_cast(image_shape[3].get_length()); 118 OC = groups; 119 120 auto img_shape_const = register_new_node( 121 ov::element::i64, ov::Shape{4}, 122 std::vector{static_cast(N), static_cast(groups), static_cast(IH), 123 static_cast(IW)}); 124 ov::Output image_reshaped = 125 register_new_node(image_input, img_shape_const, false); 126 127 const ov::Shape conv_w_shape = {groups, 1, 1, KH, KW}; 128 ov::Output weight_input; 129 if (auto weight_const = ov::as_type_ptr(w_trace.get_node_shared_ptr())) { 130 weight_input = register_new_node(weight_const->get_element_type(), conv_w_shape, 131 weight_const->get_data_ptr()); 132 } else { 133 auto shape_const = register_new_node( 134 ov::element::i64, ov::Shape{5}, 135 std::vector{static_cast(groups), 1, 1, static_cast(KH), 136 static_cast(KW)}); 137 weight_input = register_new_node(w_trace, shape_const, false); 138 } 139 140 if (weight_input.get_element_type() != image_reshaped.get_element_type()) { 141 weight_input = register_new_node(weight_input, image_reshaped.get_element_type()); 142 } 143 144 conv_out = register_new_node( 145 image_reshaped, weight_input, eip->get_strides(), ov::CoordinateDiff{pb[2], pb[3]}, 146 ov::CoordinateDiff{pe[2], pe[3]}, ov::Strides{eip->get_rates()[0], eip->get_rates()[1]}, 147 ov::op::PadType::EXPLICIT); 148 } else { 149 if ((ws.size() == 4 && ws[1] == IC && ws[2] == KH && ws[3] == KW) || 150 (ws.size() == 3 && ws[1] == IC && ws[2] == KW) || (ws.size() == 2 && ws[1] == kernel_spatial_ic)) { 151 OC = ws[0]; 152 } else if ((ws.size() == 4 && ws[0] == 1 && ws[2] == IC && ws[3] == KW) || 153 (ws.size() == 2 && ws[0] == kernel_spatial_ic)) { 154 OC = ws[1]; 155 } else if (ws.size() == 3 && ws[0] == 1 && ws[1] == IC && ws[2] == KW) { 156 OC = 1; 157 } else if (ws.size() == 4 && ws[3] == kernel_spatial_ic) { 158 OC = ws[2]; 159 } else if (ws.size() == 4 && ws[2] == kernel_spatial_ic) { 160 OC = ws[3]; 161 } else if (kernel_spatial_ic && ov::shape_size(ws) % kernel_spatial_ic == 0) { 162 OC = ov::shape_size(ws) / kernel_spatial_ic; 163 } else { 164 return false; 165 } 166 167 const ov::Shape conv_w_shape = {OC, IC, KH, KW}; 168 169 ov::Output weight_input; 170 if (auto weight_const = ov::as_type_ptr(w_trace.get_node_shared_ptr())) { 171 weight_input = register_new_node(weight_const->get_element_type(), conv_w_shape, 172 weight_const->get_data_ptr()); 173 } else { 174 auto shape_const = register_new_node( 175 ov::element::i64, ov::Shape{4}, 176 std::vector{static_cast(OC), static_cast(IC), static_cast(KH), 177 static_cast(KW)}); 178 weight_input = register_new_node(w_trace, shape_const, false); 179 } 180 181 if (weight_input.get_element_type() != image_input.get_element_type()) { 182 weight_input = register_new_node(weight_input, image_input.get_element_type()); 183 } 184 185 conv_out = register_new_node( 186 image_input, weight_input, eip->get_strides(), ov::CoordinateDiff{pb[2], pb[3]}, 187 ov::CoordinateDiff{pe[2], pe[3]}, ov::Strides{eip->get_rates()[0], eip->get_rates()[1]}, 188 ov::op::PadType::EXPLICIT); 189 } 190 191 constexpr auto target_type = ov::element::f32; 192 if (conv_out.get_element_type() != target_type) { 193 conv_out = register_new_node(conv_out, target_type); 194 } 195 196 std::shared_ptr add_node; 197 ov::Output bias_input; 198 199 auto try_fuse_bias = [&](const std::shared_ptr & n) { 200 auto add = ov::as_type_ptr(n); 201 if (!add) { 202 return false; 203 } 204 for (size_t i = 0; i < 2; ++i) { 205 if (ov::is_type(add->get_input_node_shared_ptr(i))) { 206 bias_input = add->input_value(i); 207 add_node = add; 208 return true; 209 } 210 } 211 return false; 212 }; 213 214 for (const auto & consumer : matmul_node->output(0).get_target_inputs()) { 215 auto n = consumer.get_node()->shared_from_this(); 216 if (try_fuse_bias(n)) { 217 break; 218 } 219 if (ov::is_type(n) || ov::is_type(n)) { 220 for (const auto & next : n->output(0).get_target_inputs()) { 221 if (try_fuse_bias(next.get_node()->shared_from_this())) { 222 break; 223 } 224 } 225 } 226 if (add_node) { 227 break; 228 } 229 } 230 231 ov::Output final_out = conv_out; 232 std::shared_ptr target_node = matmul_node; 233 234 if (add_node) { 235 ov::Output bias = bias_input; 236 if (bias.get_element_type() != target_type) { 237 bias = register_new_node(bias, target_type); 238 } 239 auto bias_shape = register_new_node( 240 ov::element::i64, ov::Shape{4}, std::vector{1, static_cast(OC), 1, 1}); 241 bias = register_new_node(bias, bias_shape, false); 242 final_out = register_new_node(conv_out, bias); 243 target_node = add_node; 244 } 245 246 if (!is_depthwise) { 247 auto perm = register_new_node( 248 ov::element::i64, ov::Shape{4}, 249 weight_is_in0 ? std::vector{1, 0, 2, 3} : std::vector{0, 2, 3, 1}); 250 final_out = register_new_node(final_out, perm); 251 } 252 253 auto orig_shape = target_node->get_output_partial_shape(0); 254 if (orig_shape.is_static() && final_out.get_partial_shape().is_static()) { 255 if (ov::shape_size(orig_shape.to_shape()) != ov::shape_size(final_out.get_shape())) { 256 return false; 257 } 258 } 259 if (orig_shape.is_static() && final_out.get_partial_shape() != orig_shape) { 260 auto shape_const = register_new_node(ov::element::i64, ov::Shape{orig_shape.size()}, 261 orig_shape.to_shape()); 262 final_out = register_new_node(final_out, shape_const, false); 263 } 264 265 auto orig_type = target_node->get_output_element_type(0); 266 if (final_out.get_element_type() != orig_type) { 267 final_out = register_new_node(final_out, orig_type); 268 } 269 270 final_out.get_node_shared_ptr()->set_friendly_name(target_node->get_friendly_name()); 271 ov::copy_runtime_info(m.get_matched_nodes(), final_out.get_node_shared_ptr()); 272 ov::replace_node(target_node, final_out.get_node_shared_ptr()); 273 274 return true; 275 }; 276 277 register_matcher(std::make_shared(m_matmul, "ov::frontend::ggml::pass::FuseToConv"), callback); 278 } 279 280 } // namespace pass 281 } // namespace ggml 282 } // namespace frontend 283 } // namespace ov