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 / scale.cpp
| 1 | #include "../node_context.h" |
| 2 | #include "../op_table.h" |
| 3 | #include "../utils.h" |
| 4 | |
| 5 | #include <openvino/core/except.hpp> |
| 6 | #include <openvino/op/add.hpp> |
| 7 | #include <openvino/op/concat.hpp> |
| 8 | #include <openvino/op/constant.hpp> |
| 9 | #include <openvino/op/convert.hpp> |
| 10 | #include <openvino/op/equal.hpp> |
| 11 | #include <openvino/op/gather.hpp> |
| 12 | #include <openvino/op/greater_eq.hpp> |
| 13 | #include <openvino/op/if.hpp> |
| 14 | #include <openvino/op/less.hpp> |
| 15 | #include <openvino/op/logical_or.hpp> |
| 16 | #include <openvino/op/multiply.hpp> |
| 17 | #include <openvino/op/range.hpp> |
| 18 | #include <openvino/op/reshape.hpp> |
| 19 | #include <openvino/op/shape_of.hpp> |
| 20 | #include <openvino/op/slice.hpp> |
| 21 | #include <openvino/op/squeeze.hpp> |
| 22 | #include <openvino/op/unsqueeze.hpp> |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace ov { |
| 26 | namespace frontend { |
| 27 | namespace ggml { |
| 28 | namespace op { |
| 29 | |
| 30 | OutputVector translate_scale(const NodeContext & context) { |
| 31 | num_inputs_check(context, 1, 1); |
| 32 | |
| 33 | float scale; |
| 34 | float bias; |
| 35 | memcpy(&scale, (float *) context.get_output_op_params() + 0, sizeof(float)); |
| 36 | memcpy(&bias, (float *) context.get_output_op_params() + 1, sizeof(float)); |
| 37 | |
| 38 | auto scale_node = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{}, std::vector<float>{scale}); |
| 39 | |
| 40 | if (context.get_op_case() == 2) { |
| 41 | return {context.get_input(0)}; |
| 42 | } |
| 43 | |
| 44 | if (context.get_op_case() == 1 && context.has_input("cache_rs_reset_len")) { |
| 45 | auto cache_rs_reset_idx = context.get_input("cache_rs_reset_idx"); |
| 46 | auto cache_rs_reset_len = context.get_input("cache_rs_reset_len"); |
| 47 | |
| 48 | auto cache_rs = context.get_input(0); |
| 49 | |
| 50 | auto cache_shape = std::make_shared<ov::op::v3::ShapeOf>(cache_rs, ov::element::i64); |
| 51 | auto n_slots_1d = std::make_shared<ov::op::v8::Gather>( |
| 52 | cache_shape, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2}), |
| 53 | ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0})); |
| 54 | auto n_slots = std::make_shared<ov::op::v0::Squeeze>(n_slots_1d); |
| 55 | |
| 56 | auto iota = std::make_shared<ov::op::v4::Range>( |
| 57 | ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0}), n_slots, |
| 58 | ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {1}), ov::element::i64); |
| 59 | |
| 60 | auto idx_plus_len = std::make_shared<ov::op::v1::Add>(cache_rs_reset_idx, cache_rs_reset_len); |
| 61 | auto less_than_idx = std::make_shared<ov::op::v1::Less>(iota, cache_rs_reset_idx); |
| 62 | auto greater_equal_idx_plus_len = std::make_shared<ov::op::v1::GreaterEqual>(iota, idx_plus_len); |
| 63 | auto keep_mask = std::make_shared<ov::op::v1::LogicalOr>(less_than_idx, greater_equal_idx_plus_len); |
| 64 | |
| 65 | auto keep_mask_f32 = std::make_shared<ov::op::v0::Convert>(keep_mask, ov::element::f32); |
| 66 | auto keep_mask_reshape = std::make_shared<ov::op::v0::Unsqueeze>( |
| 67 | keep_mask_f32, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {1})); |
| 68 | |
| 69 | auto cleared_cache_rs = std::make_shared<ov::op::v1::Multiply>(cache_rs, keep_mask_reshape); |
| 70 | return rename_outputs_with_suffix({cleared_cache_rs}, context.get_name()); |
| 71 | } |
| 72 | |
| 73 | auto scaled = std::make_shared<ov::op::v1::Multiply>(process_view_input_new(context, 0), scale_node); |
| 74 | |
| 75 | std::shared_ptr<ov::Node> res; |
| 76 | if (bias != 0.0f) { |
| 77 | auto bias_node = |
| 78 | std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{}, std::vector<float>{bias}); |
| 79 | res = std::make_shared<ov::op::v1::Add>(scaled, bias_node); |
| 80 | } else { |
| 81 | res = scaled; |
| 82 | } |
| 83 | |
| 84 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 85 | } |
| 86 | |
| 87 | } // namespace op |
| 88 | } // namespace ggml |
| 89 | } // namespace frontend |
| 90 | } // namespace ov |