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 / add_id.cpp
| 1 | #include "../node_context.h" |
| 2 | #include "../op_table.h" |
| 3 | #include "../utils.h" |
| 4 | |
| 5 | #include <memory> |
| 6 | #include <openvino/core/node.hpp> |
| 7 | #include <openvino/core/node_output.hpp> |
| 8 | #include <openvino/op/add.hpp> |
| 9 | #include <openvino/op/constant.hpp> |
| 10 | #include <openvino/op/convert.hpp> |
| 11 | #include <openvino/op/gather.hpp> |
| 12 | #include <openvino/op/greater_eq.hpp> |
| 13 | #include <openvino/op/maximum.hpp> |
| 14 | #include <openvino/op/multiply.hpp> |
| 15 | #include <openvino/op/reshape.hpp> |
| 16 | #include <openvino/op/shape_of.hpp> |
| 17 | #include <openvino/op/unsqueeze.hpp> |
| 18 | |
| 19 | namespace ov { |
| 20 | namespace frontend { |
| 21 | namespace ggml { |
| 22 | namespace op { |
| 23 | |
| 24 | static ov::Output<ov::Node> reshape_add_id_input_to_2d(const ov::Output<ov::Node> & input, |
| 25 | const ov::PartialShape & input_shape, |
| 26 | const std::vector<int> & dims) { |
| 27 | const auto & actual_shape = input.get_partial_shape(); |
| 28 | if (actual_shape.rank().is_static() && actual_shape.rank().get_length() == 2) { |
| 29 | return input; |
| 30 | } |
| 31 | |
| 32 | if (input_shape.rank().is_static() && input_shape.rank().get_length() == 2) { |
| 33 | return input; |
| 34 | } |
| 35 | |
| 36 | auto shape = std::make_shared<ov::op::v3::ShapeOf>(input, ov::element::i64); |
| 37 | return std::make_shared<ov::op::v1::Reshape>(input, get_dimensions(shape, dims), false); |
| 38 | } |
| 39 | |
| 40 | OutputVector translate_add_id(const NodeContext & context) { |
| 41 | num_inputs_check(context, 3, 3); |
| 42 | |
| 43 | auto input = process_view_input_new(context, 0); |
| 44 | auto bias = process_view_input_new(context, 1); |
| 45 | auto ids = process_view_input_new(context, 2); |
| 46 | |
| 47 | // OpenVINO uses reversed GGML dimensions: |
| 48 | // input: [1, n_token, n_used, n_embd] |
| 49 | // bias: [1, 1, n_expert, n_embd] |
| 50 | // ids: [1, 1, n_token, n_used] |
| 51 | // Model bias constants may already be stored as [n_expert, n_embd]. |
| 52 | bias = reshape_add_id_input_to_2d(bias, context.get_input_shape(1), {2, 3}); |
| 53 | ids = reshape_add_id_input_to_2d(ids, context.get_input_shape(2), {2, 3}); |
| 54 | |
| 55 | if (ids.get_element_type() != ov::element::i32 && ids.get_element_type() != ov::element::i64) { |
| 56 | ids = std::make_shared<ov::op::v0::Convert>(ids, ov::element::i32); |
| 57 | } |
| 58 | |
| 59 | // an id of -1 marks a skipped slot: gather row 0 for it and zero the bias with a mask |
| 60 | auto zero = ov::op::v0::Constant::create(ids.get_element_type(), ov::Shape{}, {0}); |
| 61 | ov::Output<ov::Node> keep = std::make_shared<ov::op::v1::GreaterEqual>(ids, zero); |
| 62 | keep = std::make_shared<ov::op::v0::Convert>(keep, bias.get_element_type()); |
| 63 | keep = std::make_shared<ov::op::v0::Unsqueeze>(keep, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1})); |
| 64 | ids = std::make_shared<ov::op::v1::Maximum>(ids, zero); |
| 65 | |
| 66 | auto gather_axis = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{}, {0}); |
| 67 | ov::Output<ov::Node> selected_bias = std::make_shared<ov::op::v8::Gather>(bias, ids, gather_axis); |
| 68 | selected_bias = std::make_shared<ov::op::v1::Multiply>(selected_bias, keep); |
| 69 | selected_bias = std::make_shared<ov::op::v1::Reshape>( |
| 70 | selected_bias, std::make_shared<ov::op::v3::ShapeOf>(input, ov::element::i64), false); |
| 71 | |
| 72 | if (selected_bias.get_element_type() != input.get_element_type()) { |
| 73 | selected_bias = std::make_shared<ov::op::v0::Convert>(selected_bias, input.get_element_type()); |
| 74 | } |
| 75 | |
| 76 | ov::Output<ov::Node> res = std::make_shared<ov::op::v1::Add>(input, selected_bias); |
| 77 | const auto output_type = context.get_output_type(); |
| 78 | if (res.get_element_type() != output_type) { |
| 79 | res = std::make_shared<ov::op::v0::Convert>(res, output_type); |
| 80 | } |
| 81 | |
| 82 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 83 | } |
| 84 | |
| 85 | } // namespace op |
| 86 | } // namespace ggml |
| 87 | } // namespace frontend |
| 88 | } // namespace ov |