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

131 lines · 5537 bytes Raw Blame History
1#include "../node_context.h"
2#include "../op_table.h"
3#include "../utils.h"
4
5#include <cstdint>
6#include <limits>
7#include <memory>
8#include <openvino/core/node_output.hpp>
9#include <openvino/op/add.hpp>
10#include <openvino/op/clamp.hpp>
11#include <openvino/op/constant.hpp>
12#include <openvino/op/convert.hpp>
13#include <openvino/op/multiply.hpp>
14#include <openvino/op/slice.hpp>
15#include <openvino/op/swish.hpp>
16
17namespace ov {
18namespace frontend {
19namespace ggml {
20namespace op {
21
22static std::pair<ov::Output<ov::Node>, ov::Output<ov::Node>> get_glu_inputs(const NodeContext & context) {
23 num_inputs_check(context, 1, 2);
24
25 ov::Output<ov::Node> src0;
26 ov::Output<ov::Node> src1;
27 if (context.get_input_size() == 2) {
28 // Inputs may be VIEW slices of a combined gate_up tensor (MoE experts):
29 // resolve them so each half has its real sliced shape, not the base tensor.
30 src0 = process_view_input_new(context, 0);
31 src1 = process_view_input_new(context, 1);
32 } else {
33 // GGML splits along ne[0] (OV last axis) using floor division: nc = ne[0] / 2.
34 // Both halves are nc elements; if the dimension is odd, the last element is dropped.
35 // Use Slice instead of Split to handle odd dimensions correctly.
36 // Resolve a VIEW input (e.g. non-contiguous slice) to its real shape first.
37 auto combined = process_view_input_new(context, 0);
38 auto combined_shape = combined.get_partial_shape();
39 int64_t last_dim_val = combined_shape[combined_shape.rank().get_length() - 1].get_length();
40 int64_t nc = last_dim_val / 2;
41
42 auto axis = ov::op::v0::Constant::create(ov::element::i64, {1}, {-1});
43 auto step = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
44 auto start0 = ov::op::v0::Constant::create(ov::element::i64, {1}, {0});
45 auto stop0 = ov::op::v0::Constant::create(ov::element::i64, {1}, {nc});
46 auto start1 = ov::op::v0::Constant::create(ov::element::i64, {1}, {nc});
47 auto stop1 = ov::op::v0::Constant::create(ov::element::i64, {1}, {2 * nc});
48
49 src0 = std::make_shared<ov::op::v8::Slice>(combined, start0, stop0, step, axis);
50 src1 = std::make_shared<ov::op::v8::Slice>(combined, start1, stop1, step, axis);
51 }
52
53 int32_t * params = context.get_output_op_params();
54 const int32_t swapped = params[1];
55 if (swapped) {
56 std::swap(src0, src1);
57 }
58
59 return {src0, src1};
60}
61
62OutputVector translate_glu_swiglu(const NodeContext & context) {
63 auto [src0, src1] = get_glu_inputs(context);
64
65 auto silu = std::make_shared<ov::op::v4::Swish>(src0);
66 auto res = std::make_shared<ov::op::v1::Multiply>(silu, src1);
67
68 return rename_outputs_with_suffix({res}, context.get_name());
69}
70
71OutputVector translate_glu_swiglu_oai(const NodeContext & context) {
72 auto [src0, src1] = get_glu_inputs(context);
73
74 const int32_t * params = context.get_output_op_params();
75 const float alpha = reinterpret_cast<const float *>(params)[2];
76 const float limit = reinterpret_cast<const float *>(params)[3];
77
78 // Compute in f32: alpha_const/one are f32, and Swish/Add need matching input types.
79 auto output_type = context.get_output_type();
80 if (src0.get_element_type() != ov::element::f32) {
81 src0 = std::make_shared<ov::op::v0::Convert>(src0, ov::element::f32);
82 }
83 if (src1.get_element_type() != ov::element::f32) {
84 src1 = std::make_shared<ov::op::v0::Convert>(src1, ov::element::f32);
85 }
86
87 auto gate = std::make_shared<ov::op::v0::Clamp>(src0, -std::numeric_limits<float>::infinity(), limit);
88 auto alpha_const = ov::op::v0::Constant::create(ov::element::f32, {}, {alpha});
89 auto out_glu = std::make_shared<ov::op::v4::Swish>(gate, alpha_const);
90
91 auto up = std::make_shared<ov::op::v0::Clamp>(src1, -limit, limit);
92 auto one = ov::op::v0::Constant::create(ov::element::f32, {}, {1.0f});
93 auto up_plus_one = std::make_shared<ov::op::v1::Add>(up, one);
94 ov::Output<ov::Node> res = std::make_shared<ov::op::v1::Multiply>(out_glu, up_plus_one);
95 if (res.get_element_type() != output_type) {
96 res = std::make_shared<ov::op::v0::Convert>(res, output_type);
97 }
98
99 return rename_outputs_with_suffix({res}, context.get_name());
100}
101
102OutputVector translate_glu_swiglu_clamp(const NodeContext & context) {
103 auto [src0, src1] = get_glu_inputs(context);
104
105 const int32_t * params = context.get_output_op_params();
106 const float limit = reinterpret_cast<const float *>(params)[3];
107
108 // Compute in f32: f16 Swish/Clamp rounding drifts past the 1e-7 test tolerance.
109 auto output_type = context.get_output_type();
110 if (src0.get_element_type() != ov::element::f32) {
111 src0 = std::make_shared<ov::op::v0::Convert>(src0, ov::element::f32);
112 }
113 if (src1.get_element_type() != ov::element::f32) {
114 src1 = std::make_shared<ov::op::v0::Convert>(src1, ov::element::f32);
115 }
116
117 auto gate = std::make_shared<ov::op::v0::Clamp>(src0, -std::numeric_limits<float>::infinity(), limit);
118 auto silu = std::make_shared<ov::op::v4::Swish>(gate);
119 auto up = std::make_shared<ov::op::v0::Clamp>(src1, -limit, limit);
120 ov::Output<ov::Node> res = std::make_shared<ov::op::v1::Multiply>(silu, up);
121 if (res.get_element_type() != output_type) {
122 res = std::make_shared<ov::op::v0::Convert>(res, output_type);
123 }
124
125 return rename_outputs_with_suffix({res}, context.get_name());
126}
127
128} // namespace op
129} // namespace ggml
130} // namespace frontend
131} // namespace ov