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/op/add_id.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 / op / add_id.cpp` 88 lines ยท 3740 bytes Raw Blame History 1 #include "../node_context.h" 2 #include "../op_table.h" 3 #include "../utils.h" 4 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 ov { 20 namespace frontend { 21 namespace ggml { 22 namespace op { 23 24 static ov::Output reshape_add_id_input_to_2d(const ov::Output & input, 25 const ov::PartialShape & input_shape, 26 const std::vector & 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(input, ov::element::i64); 37 return std::make_shared(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(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 keep = std::make_shared(ids, zero); 62 keep = std::make_shared(keep, bias.get_element_type()); 63 keep = std::make_shared(keep, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1})); 64 ids = std::make_shared(ids, zero); 65 66 auto gather_axis = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{}, {0}); 67 ov::Output selected_bias = std::make_shared(bias, ids, gather_axis); 68 selected_bias = std::make_shared(selected_bias, keep); 69 selected_bias = std::make_shared( 70 selected_bias, std::make_shared(input, ov::element::i64), false); 71 72 if (selected_bias.get_element_type() != input.get_element_type()) { 73 selected_bias = std::make_shared(selected_bias, input.get_element_type()); 74 } 75 76 ov::Output res = std::make_shared(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(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