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 / op / mulmat.cpp
| 1 | #include "../node_context.h" |
| 2 | #include "../op_table.h" |
| 3 | #include "../utils.h" |
| 4 | |
| 5 | #include <climits> |
| 6 | #include <cstdint> |
| 7 | #include <memory> |
| 8 | #include <openvino/core/node.hpp> |
| 9 | #include <openvino/core/node_output.hpp> |
| 10 | #include <openvino/op/broadcast.hpp> |
| 11 | #include <openvino/op/concat.hpp> |
| 12 | #include <openvino/op/constant.hpp> |
| 13 | #include <openvino/op/convert.hpp> |
| 14 | #include <openvino/op/matmul.hpp> |
| 15 | #include <openvino/op/reshape.hpp> |
| 16 | #include <openvino/op/slice.hpp> |
| 17 | #include <openvino/op/transpose.hpp> |
| 18 | #include <openvino/op/unsqueeze.hpp> |
| 19 | #include <openvino/op/util/op_types.hpp> |
| 20 | #include <vector> |
| 21 | |
| 22 | namespace ov { |
| 23 | namespace frontend { |
| 24 | namespace ggml { |
| 25 | namespace op { |
| 26 | |
| 27 | OutputVector translate_mulmat(const NodeContext & context) { |
| 28 | num_inputs_check(context, 2, 2); |
| 29 | |
| 30 | int op_case = context.get_op_case(); |
| 31 | |
| 32 | ov::Output<ov::Node> B = process_view_input_new(context, 0); |
| 33 | ov::Output<ov::Node> A = process_view_input_new(context, 1); |
| 34 | |
| 35 | if (A.get_element_type() != B.get_element_type()) { |
| 36 | B = std::make_shared<ov::op::v0::Convert>(B, context.get_input_type(1)); |
| 37 | } |
| 38 | |
| 39 | auto B_shape = context.get_input_shape(0).to_shape(); |
| 40 | auto A_shape = context.get_input_shape(1).to_shape(); |
| 41 | int64_t A_batch = A_shape[1]; |
| 42 | int64_t B_batch = B_shape[1]; |
| 43 | |
| 44 | auto A_batch_larger = A_batch > B_batch; |
| 45 | auto batch_large = A_batch_larger ? A_batch : B_batch; |
| 46 | auto batch_small = A_batch_larger ? B_batch : A_batch; |
| 47 | |
| 48 | Output<Node> Z = A_batch_larger ? B : A; |
| 49 | auto Z_shape = A_batch_larger ? B_shape : A_shape; |
| 50 | int64_t factor = batch_large / batch_small; |
| 51 | if (factor > 1 && batch_small > 1) { |
| 52 | auto batch_large_node = ov::op::v0::Constant::create(ov::element::i64, {1}, std::vector<int64_t>{batch_large}); |
| 53 | auto batch_small_node = ov::op::v0::Constant::create(ov::element::i64, {1}, std::vector<int64_t>{batch_small}); |
| 54 | auto factor_node = ov::op::v0::Constant::create(ov::element::i64, {1}, std::vector<int64_t>{factor}); |
| 55 | |
| 56 | auto unsqueeze_axes = ov::op::v0::Constant::create(ov::element::i64, Shape{}, {2}); |
| 57 | auto Z_unsqueezed = std::make_shared<ov::op::v0::Unsqueeze>(Z, unsqueeze_axes); |
| 58 | |
| 59 | auto broadcast_shape = ov::op::v0::Constant::create( |
| 60 | ov::element::i64, {5}, {(int64_t) 1, (int64_t) 1, factor, (int64_t) 1, (int64_t) 1}); |
| 61 | auto new_Z_shape = ov::op::v0::Constant::create(ov::element::i64, {4}, |
| 62 | {(int64_t) 0, batch_large, (int64_t) -1, (int64_t) Z_shape[3]}); |
| 63 | if (op_case == 2) { |
| 64 | new_Z_shape = ov::op::v0::Constant::create(ov::element::i64, {4}, |
| 65 | {(int64_t) 0, batch_large, (int64_t) Z_shape[2], (int64_t) -1}); |
| 66 | } |
| 67 | |
| 68 | auto Z_broadcasted = std::make_shared<ov::op::v3::Broadcast>(Z_unsqueezed, broadcast_shape, |
| 69 | ov::op::BroadcastType::BIDIRECTIONAL); |
| 70 | Z = std::make_shared<ov::op::v1::Reshape>(Z_broadcasted, new_Z_shape, true); |
| 71 | } |
| 72 | if (A_batch_larger) { |
| 73 | B = Z; |
| 74 | } else { |
| 75 | A = Z; |
| 76 | } |
| 77 | |
| 78 | bool transpose_b = true; |
| 79 | ov::Output<Node> res = std::make_shared<ov::op::v0::MatMul>(A, B, false, transpose_b); |
| 80 | |
| 81 | const auto output_type = context.get_output_type(); |
| 82 | if (res.get_element_type() != output_type) { |
| 83 | res = std::make_shared<ov::op::v0::Convert>(res, output_type); |
| 84 | } |
| 85 | |
| 86 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 87 | } |
| 88 | |
| 89 | } // namespace op |
| 90 | } // namespace ggml |
| 91 | } // namespace frontend |
| 92 | } // namespace ov |