ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-openvino / openvino / op / l2_norm.cpp
| 1 | #include "../node_context.h" |
| 2 | #include "../op_table.h" |
| 3 | #include "../utils.h" |
| 4 | |
| 5 | #include <memory> |
| 6 | #include <openvino/op/constant.hpp> |
| 7 | #include <openvino/op/divide.hpp> |
| 8 | #include <openvino/op/maximum.hpp> |
| 9 | #include <openvino/op/multiply.hpp> |
| 10 | #include <openvino/op/reduce_sum.hpp> |
| 11 | #include <openvino/op/reshape.hpp> |
| 12 | #include <openvino/op/sqrt.hpp> |
| 13 | #include <openvino/op/squeeze.hpp> |
| 14 | |
| 15 | namespace ov { |
| 16 | namespace frontend { |
| 17 | namespace ggml { |
| 18 | namespace op { |
| 19 | |
| 20 | OutputVector translate_l2_norm(const NodeContext & context) { |
| 21 | num_inputs_check(context, 1, 1); |
| 22 | |
| 23 | auto input_node = process_view_input_new(context, 0); |
| 24 | |
| 25 | if (context.get_op_case() == 1) { |
| 26 | // 92: [ 128, 16, 1, 2] VIEW q_conv-1 |
| 27 | // [ 6144, 1, 2, 1] 0: UNARY conv_output_silu-1 |
| 28 | // 93: [ 128, 16, 1, 2] L2_NORM q_conv_predelta-1 |
| 29 | // [ 128, 16, 1, 2] 0: VIEW q_conv-1 |
| 30 | auto output_shape = context.get_output_shape().to_shape(); |
| 31 | input_node = process_view_input(context, 0, output_shape[2] * output_shape[3], 3); |
| 32 | input_node = |
| 33 | std::make_shared<ov::op::v0::Squeeze>(input_node, ov::op::v0::Constant::create(ov::element::i64, {1}, {0})); |
| 34 | |
| 35 | std::vector<int64_t> reshape_pattern = {0, 0, (int64_t) output_shape[2], (int64_t) output_shape[3]}; |
| 36 | input_node = std::make_shared<ov::op::v1::Reshape>( |
| 37 | input_node, ov::op::v0::Constant::create(ov::element::i64, {4}, reshape_pattern), true); |
| 38 | } |
| 39 | |
| 40 | auto squared = std::make_shared<ov::op::v1::Multiply>(input_node, input_node); |
| 41 | |
| 42 | auto sum_squared = std::make_shared<ov::op::v1::ReduceSum>( |
| 43 | squared, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1}), true); |
| 44 | |
| 45 | auto l2_norm = std::make_shared<ov::op::v0::Sqrt>(sum_squared); |
| 46 | |
| 47 | float eps; |
| 48 | memcpy(&eps, context.get_output_op_params(), sizeof(float)); |
| 49 | |
| 50 | auto eps_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1}, {eps}); |
| 51 | auto clamped_norm = std::make_shared<ov::op::v1::Maximum>(l2_norm, eps_const); |
| 52 | |
| 53 | auto res = std::make_shared<ov::op::v1::Divide>(input_node, clamped_norm); |
| 54 | |
| 55 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 56 | } |
| 57 | |
| 58 | } // namespace op |
| 59 | } // namespace ggml |
| 60 | } // namespace frontend |
| 61 | } // namespace ov |