interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-openvino / openvino / op / add.cpp

67 lines · 3040 bytes Raw Blame History
1#include "../node_context.h"
2#include "../op_table.h"
3#include "../utils.h"
4
5#include <memory>
6#include <openvino/op/add.hpp>
7#include <openvino/op/constant.hpp>
8#include <openvino/op/convert.hpp>
9#include <openvino/op/reduce_sum.hpp>
10#include <openvino/op/unsqueeze.hpp>
11
12namespace ov {
13namespace frontend {
14namespace ggml {
15namespace op {
16
17OutputVector translate_add(const NodeContext & context) {
18 num_inputs_check(context, 2, 2);
19
20 if (context.get_op_case() == 1) {
21 // MoE expert-plane sum (see is_moe_expert_sum_add): input 1 is a VIEW plane of the
22 // shared base tensor `experts` = [n_embd, n_expert_used, n_tokens, 1] (ggml order) ->
23 // [1, n_tokens, n_expert_used, n_embd] (OV order). The whole ADD chain is equivalent to
24 // reducing the expert axis (OV axis 2) of that base, so bypass the chain and the
25 // per-plane Slices entirely.
26 size_t view_size = context.get_view_input_size(1);
27 auto base_name = context.get_view_input_src_name(1, view_size - 1);
28 auto base = context.get_input(base_name);
29
30 // Stateful models drop the leading batch dim, so the base is rank 3 and both axes
31 // below shift down by one. Take them from the actual rank: the expert axis is always
32 // second from last, and the token axis is re-added just before it.
33 const auto base_rank = base.get_partial_shape().rank();
34 FRONT_END_OP_CONVERSION_CHECK(base_rank.is_static() && base_rank.get_length() >= 3,
35 "MoE expert sum needs a static rank of at least 3");
36 const int64_t rank = base_rank.get_length();
37
38 auto reduced = std::make_shared<ov::op::v1::ReduceSum>(
39 base, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {rank - 2}), false);
40 auto res = std::make_shared<ov::op::v0::Unsqueeze>(
41 reduced, ov::op::v0::Constant::create(ov::element::i64, {1}, {rank - 3}));
42 return rename_outputs_with_suffix({res}, context.get_name());
43 }
44
45 auto input_0 = process_view_input_new(context, 0);
46 auto input_1 = process_view_input_new(context, 1);
47 // opset1::Add needs matching types (e.g. fused ADD_ADD mixes f16/f32); add in f32, cast once.
48 auto output_type = context.get_output_type();
49 if (input_0.get_element_type() != input_1.get_element_type()) {
50 if (input_0.get_element_type() != ov::element::f32) {
51 input_0 = std::make_shared<ov::op::v0::Convert>(input_0, ov::element::f32);
52 }
53 if (input_1.get_element_type() != ov::element::f32) {
54 input_1 = std::make_shared<ov::op::v0::Convert>(input_1, ov::element::f32);
55 }
56 }
57 ov::Output<ov::Node> res = std::make_shared<ov::op::v1::Add>(input_0, input_1);
58 if (res.get_element_type() != output_type) {
59 res = std::make_shared<ov::op::v0::Convert>(res, output_type);
60 }
61 return rename_outputs_with_suffix({res}, context.get_name());
62}
63
64} // namespace op
65} // namespace ggml
66} // namespace frontend
67} // namespace ov