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 / rms_norm.cpp

89 lines · 3665 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/divide.hpp>
9#include <openvino/op/multiply.hpp>
10#include <openvino/op/negative.hpp>
11#include <openvino/op/power.hpp>
12#include <openvino/op/reduce_mean.hpp>
13#include <openvino/op/reshape.hpp>
14#include <openvino/op/slice.hpp>
15#include <openvino/op/sqrt.hpp>
16
17namespace ov {
18namespace frontend {
19namespace ggml {
20namespace op {
21
22OutputVector translate_rms_norm(const NodeContext & context) {
23 num_inputs_check(context, 1, 1);
24
25 auto op_case = context.get_op_case();
26
27 ov::Output<ov::Node> input_node;
28 if (op_case == 3) {
29 // Flatten sequence and token dimensions to match the gate layout.
30 auto input_shape = context.get_input_shape(0).to_shape();
31 input_node = std::make_shared<ov::op::v1::Reshape>(
32 context.get_input(0),
33 ov::op::v0::Constant::create(
34 ov::element::i64, {4}, std::vector<int64_t>{1, -1, (int64_t) input_shape[2], (int64_t) input_shape[3]}),
35 false);
36 } else if (op_case == 1) {
37 input_node = process_view_input_new(context, 0);
38 } else if (op_case == 2) {
39 auto ssm_state_size = context.get_ssm_state_size();
40 // The GDN op packs [attn | new_state] along the row axis; the state occupies the last
41 // ssm_state_size * n_seqs rows. Slice it off (scaling by the active sequence count) to keep
42 // just the attention output.
43 ov::Output<ov::Node> state_end;
44 if (context.has_input("s_copy_active_slot_len")) {
45 auto len = context.get_input("s_copy_active_slot_len");
46 auto state_rows = std::make_shared<ov::op::v1::Multiply>(
47 ov::op::v0::Constant::create(ov::element::i64, {1}, {ssm_state_size}), len);
48 state_end = std::make_shared<ov::op::v0::Negative>(state_rows);
49 } else {
50 state_end = ov::op::v0::Constant::create(ov::element::i64, {1}, {-ssm_state_size});
51 }
52 auto gdn_attn_output = std::make_shared<ov::op::v8::Slice>(
53 context.get_input(0), ov::op::v0::Constant::create(ov::element::i64, {1}, {0}), state_end,
54 ov::op::v0::Constant::create(ov::element::i64, {1}, {1}),
55 ov::op::v0::Constant::create(ov::element::i64, {1}, {2}));
56
57 auto input_shape = context.get_input_shape(0).to_shape();
58 input_node = std::make_shared<ov::op::v1::Reshape>(
59 gdn_attn_output,
60 ov::op::v0::Constant::create(
61 ov::element::i64, {4}, std::vector<int64_t>{1, -1, (int64_t) input_shape[2], (int64_t) input_shape[3]}),
62 false);
63
64 } else {
65 input_node = process_view_input_new(context, 0);
66 }
67 auto square = std::make_shared<ov::op::v1::Multiply>(input_node, input_node);
68
69 auto mean = std::make_shared<ov::op::v1::ReduceMean>(
70 square, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1}), true);
71
72 float eps;
73 memcpy(&eps, context.get_output_op_params(), sizeof(float));
74
75 auto rms = std::make_shared<ov::op::v0::Sqrt>(
76 std::make_shared<ov::op::v1::Add>(mean, ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1}, {eps})));
77
78 auto reciprocal =
79 std::make_shared<ov::op::v1::Divide>(ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1}, {1.0f}), rms);
80
81 auto res = std::make_shared<ov::op::v1::Multiply>(input_node, reciprocal);
82
83 return rename_outputs_with_suffix({res}, context.get_name());
84}
85
86} // namespace op
87} // namespace ggml
88} // namespace frontend
89} // namespace ov