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

96 lines · 4594 bytes Raw Blame History
1#include "../node_context.h"
2#include "../op_table.h"
3#include "../utils.h"
4
5#include <cassert>
6#include <cstdint>
7#include <memory>
8#include <openvino/core/node.hpp>
9#include <openvino/core/node_output.hpp>
10#include <openvino/core/type.hpp>
11#include <openvino/frontend/exception.hpp>
12#include <openvino/op/broadcast.hpp>
13#include <openvino/op/concat.hpp>
14#include <openvino/op/constant.hpp>
15#include <openvino/op/convert.hpp>
16#include <openvino/op/gather.hpp>
17#include <openvino/op/reshape.hpp>
18#include <openvino/op/scatter_elements_update.hpp>
19#include <openvino/op/scatter_update.hpp>
20#include <openvino/op/shape_of.hpp>
21#include <openvino/op/slice.hpp>
22#include <openvino/op/squeeze.hpp>
23#include <openvino/op/transpose.hpp>
24#include <vector>
25
26namespace ov {
27namespace frontend {
28namespace ggml {
29namespace op {
30
31OutputVector translate_set_rows(const NodeContext & context) {
32 num_inputs_check(context, 3, 3);
33
34 auto data = process_view_input_new(context, 0);
35 auto indices = process_view_input_new(context, 1);
36 auto dst = process_view_input_new(context, 2);
37
38 data = std::make_shared<ov::op::v0::Convert>(data, context.get_output_type());
39
40 const auto indices_shape = context.get_input_shape(1);
41 const bool multidim_indices = indices_shape.rank().is_static() &&
42 indices_shape.rank().get_length() == 4 &&
43 ((indices_shape[1].is_static() && indices_shape[1].get_length() > 1) ||
44 (indices_shape[2].is_static() && indices_shape[2].get_length() > 1));
45
46 auto axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {2});
47
48 Output<Node> res;
49 if (context.is_stateful()) {
50 int concat_axis = 1;
51 int64_t dim2 = dst.get_partial_shape()[2].get_length();
52 int64_t dim3 = dst.get_partial_shape()[3].get_length();
53 data = std::make_shared<ov::op::v1::Reshape>(
54 data, ov::op::v0::Constant::create(ov::element::i64, {4}, {(int64_t) 1, (int64_t) -1, dim2, dim3}), false);
55 res = std::make_shared<ov::op::v0::Concat>(OutputVector{dst, data}, concat_axis);
56 } else if (multidim_indices) {
57 auto updates_shape = std::make_shared<ov::op::v3::ShapeOf>(data, ov::element::i64);
58
59 auto indices_rank3 = std::make_shared<ov::op::v0::Squeeze>(
60 indices, ov::op::v0::Constant::create(ov::element::i64, {1}, {0}));
61 auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
62 auto indices_rank4_shape = std::make_shared<ov::op::v0::Concat>(OutputVector{get_dimensions(updates_shape, {0, 1, 2}), one}, 0);
63 auto indices_rank4 = std::make_shared<ov::op::v1::Reshape>(indices_rank3, indices_rank4_shape, false);
64 auto broadcasted_indices = std::make_shared<ov::op::v3::Broadcast>(indices_rank4, updates_shape);
65
66 res = std::make_shared<ov::op::v3::ScatterElementsUpdate>(dst, broadcasted_indices, data, axes);
67 } else {
68 auto row_size = context.get_input_shape(2)[3].get_length();
69 auto ind_squeezed = std::make_shared<ov::op::v0::Squeeze>(
70 indices, ov::op::v0::Constant::create(ov::element::i64, {3}, {0, 1, 2}));
71 auto data_reshaped = std::make_shared<ov::op::v1::Reshape>(
72 data,
73 ov::op::v0::Constant::create(ov::element::i64, {4},
74 {(int64_t) 1, (int64_t) 1, (int64_t) -1, (int64_t) row_size}),
75 false);
76 res = std::make_shared<ov::op::v3::ScatterUpdate>(dst, ind_squeezed, data_reshaped, axes);
77 }
78
79 auto dst_reshape = ov::as_type_ptr<ov::op::v1::Reshape>(dst.get_node_shared_ptr());
80 if (!multidim_indices && dst_reshape) {
81 // Fix the case of multiple sequences, reshape back to original shape [1, n_seq, ctx_per_seq, emb]
82 // ctx_per_seq is not fixed due to llama-bench compatibility
83 auto dst_shape_partial = dst_reshape->get_input_partial_shape(0);
84 std::vector<int64_t> dst_shape = {dst_shape_partial[0].get_length(), dst_shape_partial[1].get_length(),
85 dst_shape_partial[2].is_static() ? dst_shape_partial[2].get_length() : -1,
86 dst_shape_partial[3].get_length()};
87 res = std::make_shared<ov::op::v1::Reshape>(res, ov::op::v0::Constant::create(ov::element::i64, {4}, dst_shape),
88 false);
89 }
90 return rename_outputs_with_suffix({res}, context.get_name());
91}
92
93} // namespace op
94} // namespace ggml
95} // namespace frontend
96} // namespace ov