char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Ravi Panchumarthyopenvino: Update to OV 2026.2.1, self-contained release packages, operator improvements (#24974)5397c3619

char/aliceai-t5-moe
2.9 KiB76 linesraw
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/reshape.hpp>
13#include <openvino/op/shape_of.hpp>
14
15namespace ov {
16namespace frontend {
17namespace ggml {
18namespace op {
19
20static ov::Output<ov::Node> reshape_add_id_input_to_2d(const ov::Output<ov::Node> & input,
21                                                       const ov::PartialShape & input_shape,
22                                                       const std::vector<int> & dims) {
23    const auto actual_shape = input.get_partial_shape();
24    if (actual_shape.rank().is_static() && actual_shape.rank().get_length() == 2) {
25        return input;
26    }
27
28    if (input_shape.rank().is_static() && input_shape.rank().get_length() == 2) {
29        return input;
30    }
31
32    auto shape = std::make_shared<ov::op::v3::ShapeOf>(input, ov::element::i64);
33    return std::make_shared<ov::op::v1::Reshape>(input, get_dimensions(shape, dims), false);
34}
35
36OutputVector translate_add_id(const NodeContext & context) {
37    num_inputs_check(context, 3, 3);
38
39    auto input = process_view_input_new(context, 0);
40    auto bias = process_view_input_new(context, 1);
41    auto ids = process_view_input_new(context, 2);
42
43    // OpenVINO uses reversed GGML dimensions:
44    //   input: [1, n_token, n_used, n_embd]
45    //   bias:  [1, 1, n_expert, n_embd]
46    //   ids:   [1, 1, n_token, n_used]
47    // Model bias constants may already be stored as [n_expert, n_embd].
48    bias = reshape_add_id_input_to_2d(bias, context.get_input_shape(1), {2, 3});
49    ids = reshape_add_id_input_to_2d(ids, context.get_input_shape(2), {2, 3});
50
51    if (ids.get_element_type() != ov::element::i32 && ids.get_element_type() != ov::element::i64) {
52        ids = std::make_shared<ov::op::v0::Convert>(ids, ov::element::i32);
53    }
54
55    auto gather_axis = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{}, {0});
56    ov::Output<ov::Node> selected_bias = std::make_shared<ov::op::v8::Gather>(bias, ids, gather_axis);
57    selected_bias = std::make_shared<ov::op::v1::Reshape>(
58        selected_bias, std::make_shared<ov::op::v3::ShapeOf>(input, ov::element::i64), false);
59
60    if (selected_bias.get_element_type() != input.get_element_type()) {
61        selected_bias = std::make_shared<ov::op::v0::Convert>(selected_bias, input.get_element_type());
62    }
63
64    ov::Output<ov::Node> res = std::make_shared<ov::op::v1::Add>(input, selected_bias);
65    const auto output_type = context.get_output_type();
66    if (res.get_element_type() != output_type) {
67        res = std::make_shared<ov::op::v0::Convert>(res, output_type);
68    }
69
70    return rename_outputs_with_suffix({res}, context.get_name());
71}
72
73}  // namespace op
74}  // namespace ggml
75}  // namespace frontend
76}  // namespace ov