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

233 lines · 9817 bytes Raw Blame History
1#include "../node_context.h"
2#include "../op_table.h"
3#include "../utils.h"
4
5#include <openvino/op/constant.hpp>
6#include <openvino/op/convert.hpp>
7#include <openvino/op/convolution.hpp>
8#include <openvino/op/group_conv.hpp>
9#include <openvino/op/reshape.hpp>
10#include <openvino/op/unsqueeze.hpp>
11
12namespace ov {
13namespace frontend {
14namespace ggml {
15namespace op {
16
17OutputVector translate_conv_2d(const NodeContext & context) {
18 num_inputs_check(context, 2, 2);
19
20 ov::Output<Node> kernel = process_view_input_new(context, 0);
21 ov::Output<Node> input = process_view_input_new(context, 1);
22
23 if (kernel.get_element_type() != input.get_element_type()) {
24 kernel = std::make_shared<ov::op::v0::Convert>(kernel, input.get_element_type());
25 }
26
27 const int32_t * params = context.get_output_op_params();
28 const int s0 = params[0];
29 const int s1 = params[1];
30 const int p0 = params[2];
31 const int p1 = params[3];
32 const int d0 = params[4];
33 const int d1 = params[5];
34
35 ov::Strides strides{static_cast<size_t>(s1), static_cast<size_t>(s0)};
36 ov::CoordinateDiff pads_begin{static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
37 ov::CoordinateDiff pads_end{static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
38 ov::Strides dilations{static_cast<size_t>(d1), static_cast<size_t>(d0)};
39
40 ov::Output<Node> res = std::make_shared<ov::op::v1::Convolution>(
41 input, kernel, strides, pads_begin, pads_end, dilations, ov::op::PadType::EXPLICIT);
42
43 const auto output_type = context.get_output_type();
44 if (res.get_element_type() != output_type) {
45 res = std::make_shared<ov::op::v0::Convert>(res, output_type);
46 }
47
48 return rename_outputs_with_suffix({res}, context.get_name());
49}
50
51OutputVector translate_conv_2d_dw(const NodeContext & context) {
52 num_inputs_check(context, 2, 2);
53
54 ov::Output<Node> kernel = process_view_input_new(context, 0);
55 ov::Output<Node> input = process_view_input_new(context, 1);
56
57 if (kernel.get_element_type() != input.get_element_type()) {
58 kernel = std::make_shared<ov::op::v0::Convert>(kernel, input.get_element_type());
59 }
60
61 const int32_t * params = context.get_output_op_params();
62 const int s0 = params[0];
63 const int s1 = params[1];
64 const int p0 = params[2];
65 const int p1 = params[3];
66 const int d0 = params[4];
67 const int d1 = params[5];
68
69 // Reshape kernel from [C, 1, KH, KW] to [C, 1, 1, KH, KW] for 2D GroupConvolution
70 auto unsqueeze_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {1});
71 auto kernel_5d = std::make_shared<ov::op::v0::Unsqueeze>(kernel, unsqueeze_axis);
72
73 ov::Strides strides{static_cast<size_t>(s1), static_cast<size_t>(s0)};
74 ov::CoordinateDiff pads_begin{static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
75 ov::CoordinateDiff pads_end{static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
76 ov::Strides dilations{static_cast<size_t>(d1), static_cast<size_t>(d0)};
77
78 ov::Output<Node> res = std::make_shared<ov::op::v1::GroupConvolution>(
79 input, kernel_5d, strides, pads_begin, pads_end, dilations, ov::op::PadType::EXPLICIT);
80
81 const auto output_type = context.get_output_type();
82 if (res.get_element_type() != output_type) {
83 res = std::make_shared<ov::op::v0::Convert>(res, output_type);
84 }
85
86 return rename_outputs_with_suffix({res}, context.get_name());
87}
88
89OutputVector translate_conv_transpose_1d(const NodeContext & context) {
90 num_inputs_check(context, 2, 2);
91
92 ov::Output<Node> kernel = process_view_input_new(context, 0);
93 ov::Output<Node> input = process_view_input_new(context, 1);
94
95 if (kernel.get_element_type() != input.get_element_type()) {
96 kernel = std::make_shared<ov::op::v0::Convert>(kernel, input.get_element_type());
97 }
98
99 const int32_t * params = context.get_output_op_params();
100 const int s0 = params[0];
101 const int p0 = params[1];
102 const int d0 = params[2];
103
104 const auto kernel_shape = context.get_input_shape(0).to_shape(); // [1, Cin, Cout, K]
105 const int64_t Cin = kernel_shape[1];
106 const int64_t Cout = kernel_shape[2];
107 const int64_t K = kernel_shape[3];
108
109 const auto input_shape = context.get_input_shape(1).to_shape(); // [1, N, Cin, L]
110 const int64_t N = input_shape[0] * input_shape[1];
111 const int64_t L = input_shape[3];
112
113 auto kernel_3d = std::make_shared<ov::op::v1::Reshape>(
114 kernel, ov::op::v0::Constant::create(ov::element::i64, {3}, {Cin, Cout, K}), false);
115 auto input_3d = std::make_shared<ov::op::v1::Reshape>(
116 input, ov::op::v0::Constant::create(ov::element::i64, {3}, {N, Cin, L}), false);
117
118 ov::Strides strides{static_cast<size_t>(s0)};
119 ov::CoordinateDiff pads_begin{static_cast<ptrdiff_t>(p0)};
120 ov::CoordinateDiff pads_end{static_cast<ptrdiff_t>(p0)};
121 ov::Strides dilations{static_cast<size_t>(d0)};
122
123 auto conv_tr = std::make_shared<ov::op::v1::ConvolutionBackpropData>(
124 input_3d, kernel_3d, strides, pads_begin, pads_end, dilations);
125
126 const auto out_shape = context.get_output_shape().to_shape();
127 auto out_shape_const = ov::op::v0::Constant::create(
128 ov::element::i64, {4}, {static_cast<int64_t>(out_shape[0]), static_cast<int64_t>(out_shape[1]),
129 static_cast<int64_t>(out_shape[2]), static_cast<int64_t>(out_shape[3])});
130 ov::Output<Node> res = std::make_shared<ov::op::v1::Reshape>(conv_tr, out_shape_const, false);
131
132 const auto output_type = context.get_output_type();
133 if (res.get_element_type() != output_type) {
134 res = std::make_shared<ov::op::v0::Convert>(res, output_type);
135 }
136
137 return rename_outputs_with_suffix({res}, context.get_name());
138}
139
140OutputVector translate_conv_transpose_2d(const NodeContext & context) {
141 num_inputs_check(context, 2, 2);
142
143 ov::Output<Node> kernel = process_view_input_new(context, 0);
144 ov::Output<Node> input = process_view_input_new(context, 1);
145
146 if (kernel.get_element_type() != input.get_element_type()) {
147 kernel = std::make_shared<ov::op::v0::Convert>(kernel, input.get_element_type());
148 }
149
150 const int32_t * params = context.get_output_op_params();
151 const int stride = params[0];
152
153 ov::Strides strides{static_cast<size_t>(stride), static_cast<size_t>(stride)};
154 ov::CoordinateDiff pads_begin{0, 0};
155 ov::CoordinateDiff pads_end{0, 0};
156 ov::Strides dilations{1, 1};
157
158 ov::Output<Node> res = std::make_shared<ov::op::v1::ConvolutionBackpropData>(
159 input, kernel, strides, pads_begin, pads_end, dilations);
160
161 const auto output_type = context.get_output_type();
162 if (res.get_element_type() != output_type) {
163 res = std::make_shared<ov::op::v0::Convert>(res, output_type);
164 }
165
166 return rename_outputs_with_suffix({res}, context.get_name());
167}
168
169OutputVector translate_conv_3d(const NodeContext & context) {
170 num_inputs_check(context, 2, 2);
171
172 ov::Output<Node> kernel = process_view_input_new(context, 0);
173 ov::Output<Node> input = process_view_input_new(context, 1);
174
175 if (kernel.get_element_type() != input.get_element_type()) {
176 kernel = std::make_shared<ov::op::v0::Convert>(kernel, input.get_element_type());
177 }
178
179 const int32_t * params = context.get_output_op_params();
180 const int s0 = params[0];
181 const int s1 = params[1];
182 const int s2 = params[2];
183 const int p0 = params[3];
184 const int p1 = params[4];
185 const int p2 = params[5];
186 const int d0 = params[6];
187 const int d1 = params[7];
188 const int d2 = params[8];
189 const int c = params[9];
190 const int n = params[10];
191 const int oc = params[11];
192
193 const auto kshape = context.get_input_shape(0).to_shape(); // [c*oc, KD, KH, KW]
194 const int64_t KD = kshape[1];
195 const int64_t KH = kshape[2];
196 const int64_t KW = kshape[3];
197
198 const auto inshape = context.get_input_shape(1).to_shape(); // [c*n, ID, IH, IW]
199 const int64_t ID = inshape[1];
200 const int64_t IH = inshape[2];
201 const int64_t IW = inshape[3];
202
203 auto kernel_5d = std::make_shared<ov::op::v1::Reshape>(
204 kernel, ov::op::v0::Constant::create(ov::element::i64, {5}, {static_cast<int64_t>(oc), static_cast<int64_t>(c), KD, KH, KW}), false);
205 auto input_5d = std::make_shared<ov::op::v1::Reshape>(
206 input, ov::op::v0::Constant::create(ov::element::i64, {5}, {static_cast<int64_t>(n), static_cast<int64_t>(c), ID, IH, IW}), false);
207
208 ov::Strides strides{static_cast<size_t>(s2), static_cast<size_t>(s1), static_cast<size_t>(s0)};
209 ov::CoordinateDiff pads_begin{static_cast<ptrdiff_t>(p2), static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
210 ov::CoordinateDiff pads_end{static_cast<ptrdiff_t>(p2), static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
211 ov::Strides dilations{static_cast<size_t>(d2), static_cast<size_t>(d1), static_cast<size_t>(d0)};
212
213 auto conv = std::make_shared<ov::op::v1::Convolution>(
214 input_5d, kernel_5d, strides, pads_begin, pads_end, dilations, ov::op::PadType::EXPLICIT);
215
216 const auto out_shape = context.get_output_shape().to_shape(); // [oc*n, OD, OH, OW]
217 auto out_shape_const = ov::op::v0::Constant::create(
218 ov::element::i64, {4}, {static_cast<int64_t>(out_shape[0]), static_cast<int64_t>(out_shape[1]),
219 static_cast<int64_t>(out_shape[2]), static_cast<int64_t>(out_shape[3])});
220 ov::Output<Node> res = std::make_shared<ov::op::v1::Reshape>(conv, out_shape_const, false);
221
222 const auto output_type = context.get_output_type();
223 if (res.get_element_type() != output_type) {
224 res = std::make_shared<ov::op::v0::Convert>(res, output_type);
225 }
226
227 return rename_outputs_with_suffix({res}, context.get_name());
228}
229
230} // namespace op
231} // namespace ggml
232} // namespace frontend
233} // namespace ov