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

86 lines · 2817 bytes Raw Blame History
1#include "../node_context.h"
2#include "../op_table.h"
3#include "../utils.h"
4
5#include <cstdint>
6#include <memory>
7#include <openvino/core/node.hpp>
8#include <openvino/core/node_output.hpp>
9#include <openvino/frontend/exception.hpp>
10#include <openvino/op/concat.hpp>
11#include <openvino/op/constant.hpp>
12#include <openvino/op/reshape.hpp>
13#include <vector>
14
15namespace ov {
16namespace frontend {
17namespace ggml {
18namespace op {
19
20OutputVector translate_reshape(const NodeContext & context) {
21 num_inputs_check(context, 1, 1);
22 if (context.get_input(0).get_partial_shape().is_static() &&
23 context.get_input_shape(0) == context.get_output_shape()) {
24 return {context.get_input(0)};
25 }
26
27 const int op_case = context.get_op_case();
28 const auto output_shape = context.get_output_shape().to_shape();
29 std::vector<int64_t> shape(output_shape.begin(), output_shape.end());
30 std::shared_ptr<ov::Node> new_shape_node;
31 switch (op_case) {
32 case 0:
33 break;
34 case 1:
35 case 9:
36 shape[1] = -1;
37 if (context.is_stateful() && op_case == 1) {
38 shape.erase(shape.begin());
39 }
40 break;
41 case 2:
42 case 3:
43 shape[2] = -1;
44 if (op_case == 3) {
45 shape[3] = 1;
46 }
47 break;
48 case 4:
49 return {context.get_input(0).get_node_shared_ptr()->input_value(0)};
50 case 5:
51 case 7:
52 shape = {1, 1, -1, shape[3]};
53 if (context.is_stateful() && op_case == 5) {
54 shape.erase(shape.begin());
55 }
56 break;
57 case 6:
58 // Recurrent inputs keep the active sequence count separate from the token count.
59 if (context.has_input("s_copy_active_slot_len")) {
60 auto n_slot_active_len = context.get_input("s_copy_active_slot_len");
61 auto emb_size = ov::op::v0::Constant::create(ov::element::i64, {1}, {shape[3]});
62 auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
63 auto neg_one = ov::op::v0::Constant::create(ov::element::i64, {1}, {-1});
64 new_shape_node =
65 std::make_shared<ov::op::v0::Concat>(ov::OutputVector{one, n_slot_active_len, neg_one, emb_size}, 0);
66 } else {
67 shape = {1, 1, -1, shape[3]};
68 }
69 break;
70 case 8:
71 shape[0] = -1;
72 break;
73 default:
74 FRONT_END_OP_CONVERSION_CHECK(false, "Unsupported RESHAPE case: ", op_case);
75 }
76 if (!new_shape_node) {
77 new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {shape.size()}, shape);
78 }
79 auto res = std::make_shared<ov::op::v1::Reshape>(context.get_input(0), new_shape_node, false);
80 return rename_outputs_with_suffix({res}, context.get_name());
81}
82
83} // namespace op
84} // namespace ggml
85} // namespace frontend
86} // namespace ov