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

138 lines · 5998 bytes Raw Blame History
1#include "../node_context.h"
2#include "../op_table.h"
3#include "../utils.h"
4#include "ggml-openvino/ggml-openvino-extra.h"
5
6#include <memory>
7#include <openvino/op/abs.hpp>
8#include <openvino/op/add.hpp>
9#include <openvino/op/clamp.hpp>
10#include <openvino/op/constant.hpp>
11#include <openvino/op/convert.hpp>
12#include <openvino/op/elu.hpp>
13#include <openvino/op/exp.hpp>
14#include <openvino/op/gelu.hpp>
15#include <openvino/op/greater.hpp>
16#include <openvino/op/hard_sigmoid.hpp>
17#include <openvino/op/log.hpp>
18#include <openvino/op/multiply.hpp>
19#include <openvino/op/negative.hpp>
20#include <openvino/op/relu.hpp>
21#include <openvino/op/round.hpp>
22#include <openvino/op/sigmoid.hpp>
23#include <openvino/op/softplus.hpp>
24#include <openvino/op/subtract.hpp>
25
26namespace ov {
27namespace frontend {
28namespace ggml {
29namespace op {
30
31OutputVector translate_unary_gelu(const NodeContext & context) {
32 num_inputs_check(context, 1, 1);
33 auto input = process_view_input_new(context, 0);
34 auto res = std::make_shared<ov::op::v7::Gelu>(input, ov::op::GeluApproximationMode::TANH);
35 return rename_outputs_with_suffix({res}, context.get_name());
36}
37
38OutputVector translate_unary_gelu_erf(const NodeContext & context) {
39 num_inputs_check(context, 1, 1);
40 auto input = process_view_input_new(context, 0);
41 auto res = std::make_shared<ov::op::v7::Gelu>(input, ov::op::GeluApproximationMode::ERF);
42 return rename_outputs_with_suffix({res}, context.get_name());
43}
44
45OutputVector translate_unary_gelu_quick(const NodeContext & context) {
46 num_inputs_check(context, 1, 1);
47 auto input = process_view_input_new(context, 0);
48 auto scale = ov::op::v0::Constant::create(input.get_element_type(), ov::Shape{}, {1.702f});
49 auto mul = std::make_shared<ov::op::v1::Multiply>(input, scale);
50 auto sig = std::make_shared<ov::op::v0::Sigmoid>(mul);
51 auto res = std::make_shared<ov::op::v1::Multiply>(input, sig);
52 return rename_outputs_with_suffix({res}, context.get_name());
53}
54
55OutputVector translate_unary_elu(const NodeContext & context) {
56 num_inputs_check(context, 1, 1);
57 auto input = process_view_input_new(context, 0);
58 auto res = std::make_shared<ov::op::v0::Elu>(input, 1.0);
59 return rename_outputs_with_suffix({res}, context.get_name());
60}
61
62OutputVector translate_unary_hardsigmoid(const NodeContext & context) {
63 num_inputs_check(context, 1, 1);
64 // compute in f32 like the ggml reference: 1/6 is not exact in f16/bf16 (NPU cannot take the f32 path)
65 auto input = process_view_input_new(context, 0);
66 const auto type = ggml_openvino_is_npu() ? input.get_element_type() : ov::element::f32;
67 ov::Output<ov::Node> x = input;
68 if (type != input.get_element_type()) {
69 x = std::make_shared<ov::op::v0::Convert>(input, type);
70 }
71 auto alpha = ov::op::v0::Constant::create(type, ov::Shape{}, {1.0f / 6.0f});
72 auto beta = ov::op::v0::Constant::create(type, ov::Shape{}, {0.5f});
73 ov::Output<ov::Node> res = std::make_shared<ov::op::v0::HardSigmoid>(x, alpha, beta);
74 if (type != input.get_element_type()) {
75 res = std::make_shared<ov::op::v0::Convert>(res, input.get_element_type());
76 }
77 return rename_outputs_with_suffix({res}, context.get_name());
78}
79
80OutputVector translate_unary_step(const NodeContext & context) {
81 num_inputs_check(context, 1, 1);
82 auto input = process_view_input_new(context, 0);
83 auto zero = ov::op::v0::Constant::create(input.get_element_type(), ov::Shape{}, {0.0f});
84 auto cond = std::make_shared<ov::op::v1::Greater>(input, zero);
85 auto res = std::make_shared<ov::op::v0::Convert>(cond, input.get_element_type());
86 return rename_outputs_with_suffix({res}, context.get_name());
87}
88
89OutputVector translate_unary_round(const NodeContext & context) {
90 num_inputs_check(context, 1, 1);
91 auto input = process_view_input_new(context, 0);
92 auto res = std::make_shared<ov::op::v5::Round>(input, ov::op::v5::Round::RoundMode::HALF_AWAY_FROM_ZERO);
93 return rename_outputs_with_suffix({res}, context.get_name());
94}
95
96OutputVector translate_unary_expm1(const NodeContext & context) {
97 num_inputs_check(context, 1, 1);
98 // compute in f32 like the ggml reference: exp(x) - 1 in f16 loses the small-x digits (NPU cannot take the f32 path)
99 auto input = process_view_input_new(context, 0);
100 const auto type = ggml_openvino_is_npu() ? input.get_element_type() : ov::element::f32;
101 ov::Output<ov::Node> x = input;
102 if (type != input.get_element_type()) {
103 x = std::make_shared<ov::op::v0::Convert>(input, type);
104 }
105 auto exp = std::make_shared<ov::op::v0::Exp>(x);
106 auto one = ov::op::v0::Constant::create(type, ov::Shape{}, {1.0f});
107 ov::Output<ov::Node> res = std::make_shared<ov::op::v1::Subtract>(exp, one);
108 if (type != input.get_element_type()) {
109 res = std::make_shared<ov::op::v0::Convert>(res, input.get_element_type());
110 }
111 return rename_outputs_with_suffix({res}, context.get_name());
112}
113
114OutputVector translate_unary_softplus(const NodeContext & context) {
115 num_inputs_check(context, 1, 1);
116
117 if (ggml_openvino_getenv_int("GGML_OPENVINO_NATIVE_SOFTPLUS") != 0) {
118 return translate_1to1_match_1_input<ov::op::v4::SoftPlus>(context);
119 }
120
121 auto input = process_view_input_new(context, 0);
122 const auto element_type = input.get_element_type();
123 auto one = ov::op::v0::Constant::create(element_type, ov::Shape{}, {1.0f});
124
125 auto positive = std::make_shared<ov::op::v0::Relu>(input);
126 auto abs = std::make_shared<ov::op::v0::Abs>(input);
127 auto neg_abs = std::make_shared<ov::op::v0::Negative>(abs);
128 auto exp_neg_abs = std::make_shared<ov::op::v0::Exp>(neg_abs);
129 auto log_term = std::make_shared<ov::op::v0::Log>(std::make_shared<ov::op::v1::Add>(one, exp_neg_abs));
130 auto res = std::make_shared<ov::op::v1::Add>(positive, log_term);
131
132 return rename_outputs_with_suffix({res}, context.get_name());
133}
134
135} // namespace op
136} // namespace ggml
137} // namespace frontend
138} // namespace ov