Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-openvino/openvino/op/unary.cpp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 #include 23 #include 24 #include 25 26 namespace ov { 27 namespace frontend { 28 namespace ggml { 29 namespace op { 30 31 OutputVector 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(input, ov::op::GeluApproximationMode::TANH); 35 return rename_outputs_with_suffix({res}, context.get_name()); 36 } 37 38 OutputVector 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(input, ov::op::GeluApproximationMode::ERF); 42 return rename_outputs_with_suffix({res}, context.get_name()); 43 } 44 45 OutputVector 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(input, scale); 50 auto sig = std::make_shared(mul); 51 auto res = std::make_shared(input, sig); 52 return rename_outputs_with_suffix({res}, context.get_name()); 53 } 54 55 OutputVector 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(input, 1.0); 59 return rename_outputs_with_suffix({res}, context.get_name()); 60 } 61 62 OutputVector 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 x = input; 68 if (type != input.get_element_type()) { 69 x = std::make_shared(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 res = std::make_shared(x, alpha, beta); 74 if (type != input.get_element_type()) { 75 res = std::make_shared(res, input.get_element_type()); 76 } 77 return rename_outputs_with_suffix({res}, context.get_name()); 78 } 79 80 OutputVector 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(input, zero); 85 auto res = std::make_shared(cond, input.get_element_type()); 86 return rename_outputs_with_suffix({res}, context.get_name()); 87 } 88 89 OutputVector 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(input, ov::op::v5::Round::RoundMode::HALF_AWAY_FROM_ZERO); 93 return rename_outputs_with_suffix({res}, context.get_name()); 94 } 95 96 OutputVector 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 x = input; 102 if (type != input.get_element_type()) { 103 x = std::make_shared(input, type); 104 } 105 auto exp = std::make_shared(x); 106 auto one = ov::op::v0::Constant::create(type, ov::Shape{}, {1.0f}); 107 ov::Output res = std::make_shared(exp, one); 108 if (type != input.get_element_type()) { 109 res = std::make_shared(res, input.get_element_type()); 110 } 111 return rename_outputs_with_suffix({res}, context.get_name()); 112 } 113 114 OutputVector 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(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(input); 126 auto abs = std::make_shared(input); 127 auto neg_abs = std::make_shared(abs); 128 auto exp_neg_abs = std::make_shared(neg_abs); 129 auto log_term = std::make_shared(std::make_shared(one, exp_neg_abs)); 130 auto res = std::make_shared(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