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

239 lines · 10824 bytes Raw Blame History
1#include "../node_context.h"
2#include "../op_table.h"
3#include "../utils.h"
4
5#include <cstddef>
6#include <memory>
7#include <openvino/core/shape.hpp>
8#include <openvino/core/strides.hpp>
9#include <openvino/op/concat.hpp>
10#include <openvino/op/constant.hpp>
11#include <openvino/op/convert.hpp>
12#include <openvino/op/extractimagepatches.hpp>
13#include <openvino/op/pad.hpp>
14#include <openvino/op/reshape.hpp>
15#include <openvino/op/slice.hpp>
16#include <openvino/op/transpose.hpp>
17#include <openvino/op/util/attr_types.hpp>
18
19namespace ov {
20namespace frontend {
21namespace ggml {
22namespace op {
23
24OutputVector translate_im2col(const NodeContext & context) {
25 num_inputs_check(context, 2, 2);
26 const int32_t * params = context.get_output_op_params();
27 int32_t s0 = params[0];
28 int32_t s1 = params[1];
29 int32_t p0 = params[2];
30 int32_t p1 = params[3];
31 int32_t d0 = params[4];
32 int32_t d1 = params[5];
33 bool is_2D = params[6] == 1;
34 ov::Output<Node> res;
35
36 ov::Output<Node> image = context.get_input(1);
37 const ov::Shape kernel_shape = context.get_input(0).get_shape();
38
39 const size_t IC = is_2D ? kernel_shape[1] : kernel_shape[2];
40 const size_t KH = is_2D ? kernel_shape[2] : 1;
41 const size_t KW = kernel_shape[3];
42
43 int32_t stride_w = s0;
44 int32_t stride_h = is_2D ? s1 : 1;
45 int32_t pad_w = p0;
46 int32_t pad_h = is_2D ? p1 : 0;
47 int32_t dil_w = d0;
48 int32_t dil_h = is_2D ? d1 : 1;
49
50 if (!is_2D) {
51 // GGML input shape: [IW, IC, N, 1]
52 // OpenVINO input shape: [1, N, IC, IW]
53 // Reshape image to: [N, IC, 1, IW]
54 const ov::Shape image_shape = image.get_shape();
55 const size_t N = image_shape[1];
56 const size_t IW = image_shape[3];
57 auto image_reshape_shape = ov::op::v0::Constant::create(
58 ov::element::i64, ov::Shape{4},
59 std::vector<int64_t>{static_cast<int64_t>(N), static_cast<int64_t>(IC), 1, static_cast<int64_t>(IW)});
60 image = std::make_shared<ov::op::v1::Reshape>(image, image_reshape_shape, false);
61 }
62
63 const ov::Shape patch_sizes = {KH, KW};
64 const ov::Strides strides = {static_cast<size_t>(stride_h), static_cast<size_t>(stride_w)};
65 const ov::Shape rates = {static_cast<size_t>(dil_h), static_cast<size_t>(dil_w)};
66
67 auto pads_begin =
68 ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, std::vector<int64_t>{0, 0, pad_h, pad_w});
69 auto pads_end =
70 ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, std::vector<int64_t>{0, 0, pad_h, pad_w});
71
72 auto pad = std::make_shared<ov::op::v1::Pad>(image, pads_begin, pads_end, ov::op::PadMode::CONSTANT);
73 auto patches =
74 std::make_shared<ov::op::v3::ExtractImagePatches>(pad, patch_sizes, strides, rates, ov::op::PadType::VALID);
75
76 // [N, KH*KW*IC, OH, OW] → [N, OH, OW, KH*KW*IC]
77 auto perm1 = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, std::vector<int64_t>{0, 2, 3, 1});
78 auto t1 = std::make_shared<ov::op::v1::Transpose>(patches, perm1);
79
80 // [N, OH, OW, KH*KW*IC] → [N, OH, OW, KH*KW, IC]
81 const ov::Shape out_shape = t1->get_output_shape(0);
82 const size_t N = out_shape[0];
83 const size_t OH = out_shape[1];
84 const size_t OW = out_shape[2];
85 auto reshape1_shape = ov::op::v0::Constant::create(
86 ov::element::i64, ov::Shape{5},
87 std::vector<int64_t>{static_cast<int64_t>(N), static_cast<int64_t>(OH), static_cast<int64_t>(OW),
88 static_cast<int64_t>(KH * KW), static_cast<int64_t>(IC)});
89 auto r1 = std::make_shared<ov::op::v1::Reshape>(t1, reshape1_shape, false);
90
91 // [N, OH, OW, KH*KW, IC] → [N, OH, OW, IC, KH*KW]
92 auto perm2 = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{5}, std::vector<int64_t>{0, 1, 2, 4, 3});
93 auto t2 = std::make_shared<ov::op::v1::Transpose>(r1, perm2);
94
95 // flatten back to [N, OH, OW, IC*KH*KW]
96 auto r2_shape = ov::op::v0::Constant::create(
97 ov::element::i64, ov::Shape{4},
98 std::vector<int64_t>{static_cast<int64_t>(N), static_cast<int64_t>(OH), static_cast<int64_t>(OW),
99 static_cast<int64_t>(IC * KH * KW)});
100 res = std::make_shared<ov::op::v1::Reshape>(t2, r2_shape, false);
101
102 if (!is_2D) {
103 // [N, 1, OW, IC * KW] -> [1, N, OW, IC * KW]
104 auto final_reshape_shape = ov::op::v0::Constant::create(
105 ov::element::i64, ov::Shape{4},
106 std::vector<int64_t>{1, static_cast<int64_t>(N), static_cast<int64_t>(OW), static_cast<int64_t>(IC * KW)});
107 res = std::make_shared<ov::op::v1::Reshape>(res, final_reshape_shape, false);
108 }
109
110 auto output_type = context.get_output_type();
111 if (res.get_element_type() != output_type) {
112 res = std::make_shared<ov::op::v0::Convert>(res, output_type);
113 }
114
115 return rename_outputs_with_suffix({res}, context.get_name());
116}
117
118OutputVector translate_im2col_3d(const NodeContext & context) {
119 num_inputs_check(context, 2, 2);
120 const int32_t * params = context.get_output_op_params();
121 int32_t s0 = params[0];
122 int32_t s1 = params[1];
123 int32_t s2 = params[2];
124 int32_t p0 = params[3];
125 int32_t p1 = params[4];
126 int32_t p2 = params[5];
127 int32_t d0 = params[6];
128 int32_t d1 = params[7];
129 int32_t d2 = params[8];
130 int32_t IC = params[9];
131
132 ov::Output<Node> image = process_view_input_new(context, 1);
133 const ov::Shape kernel_shape = context.get_input(0).get_shape();
134 const ov::Shape image_shape = image.get_shape();
135 const ov::Shape out_shape = context.get_output_shape().to_shape();
136
137 const size_t KD = kernel_shape[1];
138 const size_t KH = kernel_shape[2];
139 const size_t KW = kernel_shape[3];
140
141 const size_t N = image_shape[0] / static_cast<size_t>(IC);
142 const size_t ID = image_shape[1];
143 const size_t IH = image_shape[2];
144 const size_t IW = image_shape[3];
145
146 const size_t OD = (ID + 2 * p2 - d2 * (KD - 1) - 1) / s2 + 1;
147 const size_t OH = (IH + 2 * p1 - d1 * (KH - 1) - 1) / s1 + 1;
148 const size_t OW = (IW + 2 * p0 - d0 * (KW - 1) - 1) / s0 + 1;
149
150 if (N == 0 || OD == 0 || OH == 0 || OW == 0) {
151 auto output_type = context.get_output_type();
152 ov::Output<Node> res = ov::op::v0::Constant::create(
153 output_type, ov::Shape{N * OD, OH, OW, static_cast<size_t>(IC * KD * KH * KW)}, {});
154 return rename_outputs_with_suffix({res}, context.get_name());
155 }
156
157 const size_t IH_pad = IH + 2 * p1;
158 const size_t IW_pad = IW + 2 * p0;
159
160 auto image_5d_shape = ov::op::v0::Constant::create(
161 ov::element::i64, ov::Shape{5},
162 std::vector<int64_t>{static_cast<int64_t>(N), static_cast<int64_t>(IC), static_cast<int64_t>(ID),
163 static_cast<int64_t>(IH), static_cast<int64_t>(IW)});
164 auto image_5d = std::make_shared<ov::op::v1::Reshape>(image, image_5d_shape, false);
165
166 auto pads_begin = ov::op::v0::Constant::create(
167 ov::element::i64, ov::Shape{5}, std::vector<int64_t>{0, 0, p2, p1, p0});
168 auto pads_end = ov::op::v0::Constant::create(
169 ov::element::i64, ov::Shape{5}, std::vector<int64_t>{0, 0, p2, p1, p0});
170 auto pad_3d = std::make_shared<ov::op::v1::Pad>(image_5d, pads_begin, pads_end, ov::op::PadMode::CONSTANT);
171
172 const ov::Shape patch_sizes = {KH, KW};
173 const ov::Strides strides = {static_cast<size_t>(s1), static_cast<size_t>(s0)};
174 const ov::Shape rates = {static_cast<size_t>(d1), static_cast<size_t>(d0)};
175
176 ov::OutputVector kd_slices;
177 kd_slices.reserve(KD);
178
179 auto perm_nod = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{5}, {0, 2, 1, 3, 4});
180 auto reshape_4d_shape = ov::op::v0::Constant::create(
181 ov::element::i64, ov::Shape{4},
182 std::vector<int64_t>{static_cast<int64_t>(N * OD), static_cast<int64_t>(IC),
183 static_cast<int64_t>(IH_pad), static_cast<int64_t>(IW_pad)});
184 auto perm1 = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 3, 1});
185 auto r1_shape = ov::op::v0::Constant::create(
186 ov::element::i64, ov::Shape{5},
187 std::vector<int64_t>{static_cast<int64_t>(N * OD), static_cast<int64_t>(OH), static_cast<int64_t>(OW),
188 static_cast<int64_t>(KH * KW), static_cast<int64_t>(IC)});
189 auto perm2 = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{5}, {0, 1, 2, 4, 3});
190 auto r2_shape = ov::op::v0::Constant::create(
191 ov::element::i64, ov::Shape{6},
192 std::vector<int64_t>{static_cast<int64_t>(N * OD), static_cast<int64_t>(OH), static_cast<int64_t>(OW),
193 static_cast<int64_t>(IC), 1, static_cast<int64_t>(KH * KW)});
194 auto step_c = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {static_cast<int64_t>(s2)});
195 auto axes_c = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});
196
197 for (size_t ikd = 0; ikd < KD; ++ikd) {
198 auto start_c = ov::op::v0::Constant::create(
199 ov::element::i64, ov::Shape{1}, {static_cast<int64_t>(ikd * d2)});
200 auto stop_c = ov::op::v0::Constant::create(
201 ov::element::i64, ov::Shape{1}, {static_cast<int64_t>(ikd * d2 + OD * s2)});
202 auto depth_slice = std::make_shared<ov::op::v8::Slice>(pad_3d, start_c, stop_c, step_c, axes_c);
203 auto depth_slice_trans = std::make_shared<ov::op::v1::Transpose>(depth_slice, perm_nod);
204 auto depth_slice_4d = std::make_shared<ov::op::v1::Reshape>(depth_slice_trans, reshape_4d_shape, false);
205
206 auto patches = std::make_shared<ov::op::v3::ExtractImagePatches>(
207 depth_slice_4d, patch_sizes, strides, rates, ov::op::PadType::VALID);
208 auto t1 = std::make_shared<ov::op::v1::Transpose>(patches, perm1);
209 auto r1 = std::make_shared<ov::op::v1::Reshape>(t1, r1_shape, false);
210 auto t2 = std::make_shared<ov::op::v1::Transpose>(r1, perm2);
211 auto r2 = std::make_shared<ov::op::v1::Reshape>(t2, r2_shape, false);
212 kd_slices.push_back(r2);
213 }
214
215 ov::Output<Node> res;
216 if (KD == 1) {
217 res = kd_slices[0];
218 } else {
219 res = std::make_shared<ov::op::v0::Concat>(kd_slices, 4);
220 }
221
222 auto final_shape = ov::op::v0::Constant::create(
223 ov::element::i64, ov::Shape{4},
224 std::vector<int64_t>{static_cast<int64_t>(N * OD), static_cast<int64_t>(OH), static_cast<int64_t>(OW),
225 static_cast<int64_t>(IC * KD * KH * KW)});
226 res = std::make_shared<ov::op::v1::Reshape>(res, final_shape, false);
227
228 auto output_type = context.get_output_type();
229 if (res.get_element_type() != output_type) {
230 res = std::make_shared<ov::op::v0::Convert>(res, output_type);
231 }
232
233 return rename_outputs_with_suffix({res}, context.get_name());
234}
235
236} // namespace op
237} // namespace ggml
238} // namespace frontend
239} // namespace ov