ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-openvino / openvino / pass / fuse_to_conv.cpp
| 1 | #include "fuse_to_conv.h" |
| 2 | |
| 3 | #include <openvino/core/graph_util.hpp> |
| 4 | #include <openvino/core/rt_info.hpp> |
| 5 | #include <openvino/op/add.hpp> |
| 6 | #include <openvino/op/constant.hpp> |
| 7 | #include <openvino/op/convert.hpp> |
| 8 | #include <openvino/op/convolution.hpp> |
| 9 | #include <openvino/op/extractimagepatches.hpp> |
| 10 | #include <openvino/op/group_conv.hpp> |
| 11 | #include <openvino/op/matmul.hpp> |
| 12 | #include <openvino/op/pad.hpp> |
| 13 | #include <openvino/op/reshape.hpp> |
| 14 | #include <openvino/op/transpose.hpp> |
| 15 | #include <openvino/pass/pattern/op/pattern.hpp> |
| 16 | #include <openvino/pass/pattern/op/wrap_type.hpp> |
| 17 | #include <utility> |
| 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<ov::op::v0::MatMul>({m_in0, m_in1}); |
| 30 | |
| 31 | const auto callback = [=](opp::Matcher & m) { |
| 32 | auto matmul_node = ov::as_type_ptr<ov::op::v0::MatMul>(m.get_match_root()); |
| 33 | if (!matmul_node) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | auto unwrap = [](ov::Output<Node> n) { |
| 38 | while (ov::is_type<ov::op::v0::Convert>(n.get_node_shared_ptr()) || |
| 39 | ov::is_type<ov::op::v1::Reshape>(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<Node> trace) |
| 46 | -> std::pair<std::shared_ptr<ov::op::v3::ExtractImagePatches>, std::shared_ptr<ov::op::v1::Pad>> { |
| 47 | auto t2 = ov::as_type_ptr<ov::op::v1::Transpose>(unwrap(std::move(trace)).get_node_shared_ptr()); |
| 48 | auto r1 = t2 ? ov::as_type_ptr<ov::op::v1::Reshape>(t2->get_input_node_shared_ptr(0)) : nullptr; |
| 49 | auto t1 = r1 ? ov::as_type_ptr<ov::op::v1::Transpose>(r1->get_input_node_shared_ptr(0)) : nullptr; |
| 50 | auto eip = |
| 51 | t1 ? ov::as_type_ptr<ov::op::v3::ExtractImagePatches>(t1->get_input_node_shared_ptr(0)) : nullptr; |
| 52 | auto pad = eip ? ov::as_type_ptr<ov::op::v1::Pad>(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<Node> 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<ov::op::v0::Constant>(pad->get_input_node_shared_ptr(1)); |
| 69 | auto pe_const = ov::as_type_ptr<ov::op::v0::Constant>(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<std::ptrdiff_t>(); |
| 75 | const auto pe = pe_const->cast_vector<std::ptrdiff_t>(); |
| 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<size_t>(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<Node> conv_out; |
| 112 | size_t OC = 0; |
| 113 | |
| 114 | if (is_depthwise) { |
| 115 | const size_t N = static_cast<size_t>(image_shape[0].get_length()) / groups; |
| 116 | const size_t IH = static_cast<size_t>(image_shape[2].get_length()); |
| 117 | const size_t IW = static_cast<size_t>(image_shape[3].get_length()); |
| 118 | OC = groups; |
| 119 | |
| 120 | auto img_shape_const = register_new_node<ov::op::v0::Constant>( |
| 121 | ov::element::i64, ov::Shape{4}, |
| 122 | std::vector<int64_t>{static_cast<int64_t>(N), static_cast<int64_t>(groups), static_cast<int64_t>(IH), |
| 123 | static_cast<int64_t>(IW)}); |
| 124 | ov::Output<Node> image_reshaped = |
| 125 | register_new_node<ov::op::v1::Reshape>(image_input, img_shape_const, false); |
| 126 | |
| 127 | const ov::Shape conv_w_shape = {groups, 1, 1, KH, KW}; |
| 128 | ov::Output<Node> weight_input; |
| 129 | if (auto weight_const = ov::as_type_ptr<ov::op::v0::Constant>(w_trace.get_node_shared_ptr())) { |
| 130 | weight_input = register_new_node<ov::op::v0::Constant>(weight_const->get_element_type(), conv_w_shape, |
| 131 | weight_const->get_data_ptr()); |
| 132 | } else { |
| 133 | auto shape_const = register_new_node<ov::op::v0::Constant>( |
| 134 | ov::element::i64, ov::Shape{5}, |
| 135 | std::vector<int64_t>{static_cast<int64_t>(groups), 1, 1, static_cast<int64_t>(KH), |
| 136 | static_cast<int64_t>(KW)}); |
| 137 | weight_input = register_new_node<ov::op::v1::Reshape>(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<ov::op::v0::Convert>(weight_input, image_reshaped.get_element_type()); |
| 142 | } |
| 143 | |
| 144 | conv_out = register_new_node<ov::op::v1::GroupConvolution>( |
| 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<Node> weight_input; |
| 170 | if (auto weight_const = ov::as_type_ptr<ov::op::v0::Constant>(w_trace.get_node_shared_ptr())) { |
| 171 | weight_input = register_new_node<ov::op::v0::Constant>(weight_const->get_element_type(), conv_w_shape, |
| 172 | weight_const->get_data_ptr()); |
| 173 | } else { |
| 174 | auto shape_const = register_new_node<ov::op::v0::Constant>( |
| 175 | ov::element::i64, ov::Shape{4}, |
| 176 | std::vector<int64_t>{static_cast<int64_t>(OC), static_cast<int64_t>(IC), static_cast<int64_t>(KH), |
| 177 | static_cast<int64_t>(KW)}); |
| 178 | weight_input = register_new_node<ov::op::v1::Reshape>(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<ov::op::v0::Convert>(weight_input, image_input.get_element_type()); |
| 183 | } |
| 184 | |
| 185 | conv_out = register_new_node<ov::op::v1::Convolution>( |
| 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<ov::op::v0::Convert>(conv_out, target_type); |
| 194 | } |
| 195 | |
| 196 | std::shared_ptr<ov::op::v1::Add> add_node; |
| 197 | ov::Output<Node> bias_input; |
| 198 | |
| 199 | auto try_fuse_bias = [&](const std::shared_ptr<Node> & n) { |
| 200 | auto add = ov::as_type_ptr<ov::op::v1::Add>(n); |
| 201 | if (!add) { |
| 202 | return false; |
| 203 | } |
| 204 | for (size_t i = 0; i < 2; ++i) { |
| 205 | if (ov::is_type<ov::op::v0::Constant>(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<ov::op::v0::Convert>(n) || ov::is_type<ov::op::v1::Reshape>(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<Node> final_out = conv_out; |
| 232 | std::shared_ptr<Node> target_node = matmul_node; |
| 233 | |
| 234 | if (add_node) { |
| 235 | ov::Output<Node> bias = bias_input; |
| 236 | if (bias.get_element_type() != target_type) { |
| 237 | bias = register_new_node<ov::op::v0::Convert>(bias, target_type); |
| 238 | } |
| 239 | auto bias_shape = register_new_node<ov::op::v0::Constant>( |
| 240 | ov::element::i64, ov::Shape{4}, std::vector<int64_t>{1, static_cast<int64_t>(OC), 1, 1}); |
| 241 | bias = register_new_node<ov::op::v1::Reshape>(bias, bias_shape, false); |
| 242 | final_out = register_new_node<ov::op::v1::Add>(conv_out, bias); |
| 243 | target_node = add_node; |
| 244 | } |
| 245 | |
| 246 | if (!is_depthwise) { |
| 247 | auto perm = register_new_node<ov::op::v0::Constant>( |
| 248 | ov::element::i64, ov::Shape{4}, |
| 249 | weight_is_in0 ? std::vector<int64_t>{1, 0, 2, 3} : std::vector<int64_t>{0, 2, 3, 1}); |
| 250 | final_out = register_new_node<ov::op::v1::Transpose>(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::op::v0::Constant>(ov::element::i64, ov::Shape{orig_shape.size()}, |
| 261 | orig_shape.to_shape()); |
| 262 | final_out = register_new_node<ov::op::v1::Reshape>(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<ov::op::v0::Convert>(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<opp::Matcher>(m_matmul, "ov::frontend::ggml::pass::FuseToConv"), callback); |
| 278 | } |
| 279 | |
| 280 | } // namespace pass |
| 281 | } // namespace ggml |
| 282 | } // namespace frontend |
| 283 | } // namespace ov |