2 // Copyright (c) 2017-2019 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include <gtest/gtest.h>
19 #include "api/memory.hpp"
20 #include <api/input_layout.hpp>
21 #include "api/crop.hpp"
22 #include <api/topology.hpp>
23 #include <api/network.hpp>
24 #include <api/engine.hpp>
25 #include "test_utils/test_utils.h"
27 using namespace cldnn;
28 using namespace tests;
31 std::vector<T> generate_random_input(size_t b, size_t f, size_t z, size_t y, size_t x, int min, int max) {
32 static std::default_random_engine generator(random_seed);
33 int k = 8; // 1/k is the resolution of the floating point numbers
34 std::uniform_int_distribution<int> distribution(k * min, k * max);
35 std::vector<T> v(b*f*x*y*z);
36 for (size_t i = 0; i < b*f*x*y*z; ++i) {
37 v[i] = (T)distribution(generator);
44 std::vector<T> generate_random_input(size_t b, size_t f, size_t y, size_t x, int min, int max) {
45 return generate_random_input<T>(b, f, 1, y, x, -min, max);
48 TEST(crop_gpu, basic_in2x3x2x2_crop_all) {
49 // Reference : 1x2x2x2
53 const auto& engine = get_test_engine();
60 auto crop_batch_num = batch_num - 1;
61 auto crop_feature_num = feature_num - 1;
62 auto crop_x_size = x_size - 2;
63 auto crop_y_size = y_size - 2;
65 auto input = memory::allocate(engine, { data_types::f32, format::yxfb,{ batch_num, feature_num, x_size, y_size } });
68 topology.add(input_layout("input", input.get_layout()));
69 topology.add(crop("crop", "input", { crop_batch_num, crop_feature_num, crop_x_size, crop_y_size }, { 0, 0, 0, 0 }));
71 std::vector<float> input_vec = generate_random_input<float>(batch_num, feature_num, y_size, x_size, -10, 10);
72 set_values(input, input_vec);
74 network network(engine, topology);
76 network.set_input_data("input", input);
78 auto outputs = network.execute();
80 auto output = outputs.at("crop").get_memory();
81 auto output_ptr = output.pointer<float>();
83 for (int b = 0; b < crop_batch_num; ++b) { //B
84 for (int f = 0; f < crop_feature_num; ++f) { //F
85 for (int y = 0; y < crop_y_size; ++y) { //Y
86 for (int x = 0; x < crop_x_size; ++x) { //X
87 int linear_id = b + batch_num * (f + feature_num * (x + x_size * y));
88 int output_linear_id = b + crop_batch_num * (f + crop_feature_num * (x + crop_x_size * y));
89 EXPECT_EQ(output_ptr[output_linear_id], input_vec[linear_id]);
96 TEST(crop_gpu, basic_in2x2x2x3_crop_all) {
97 const auto& engine = get_test_engine();
100 auto feature_num = 2;
104 auto crop_batch_num = batch_num - 1;
105 auto crop_feature_num = feature_num - 1;
106 auto crop_x_size = x_size - 1;
107 auto crop_y_size = y_size - 1;
109 auto input = memory::allocate(engine, { data_types::f32, format::yxfb,{ batch_num, feature_num, x_size, y_size } });
112 topology.add(input_layout("input", input.get_layout()));
113 topology.add(crop("crop", "input", { crop_batch_num, crop_feature_num, crop_x_size, crop_y_size }, { 0, 0, 0, 0 }));
115 std::vector<float> input_vec;
116 for (int i = 0; i < batch_num * feature_num * y_size * x_size; i++)
117 input_vec.push_back(i);
118 set_values(input, input_vec);
120 network network(engine, topology);
122 network.set_input_data("input", input);
124 auto outputs = network.execute();
126 auto output = outputs.at("crop").get_memory();
127 auto output_ptr = output.pointer<float>();
129 printf("Results:\n");
130 for (int b = 0; b < crop_batch_num; ++b) { //B
131 for (int f = 0; f < crop_feature_num; ++f) { //F
132 for (int y = 0; y < crop_y_size; ++y) { //Y
133 for (int x = 0; x < crop_x_size; ++x) { //X
134 int linear_id = b + batch_num * (f + feature_num * (x + x_size * y));
135 int output_linear_id = b + crop_batch_num * (f + crop_feature_num * (x + crop_x_size * y));
136 EXPECT_EQ(output_ptr[output_linear_id], input_vec[linear_id]);
143 TEST(crop_gpu, basic_int_in2x3x2x2_crop_all) {
144 // Reference : 1x2x2x2
148 const auto& engine = get_test_engine();
151 auto feature_num = 3;
155 auto crop_batch_num = batch_num - 1;
156 auto crop_feature_num = feature_num - 1;
157 auto crop_x_size = x_size - 2;
158 auto crop_y_size = y_size - 2;
160 auto input = memory::allocate(engine, { data_types::i32, format::yxfb,{ batch_num, feature_num, x_size, y_size } });
163 topology.add(input_layout("input", input.get_layout()));
164 topology.add(crop("crop", "input", { crop_batch_num, crop_feature_num, crop_x_size, crop_y_size }, { 0, 0, 0, 0 }));
166 std::vector<int32_t> input_vec = generate_random_input<int32_t>(batch_num, feature_num, y_size, x_size, -10, 10);
167 set_values(input, input_vec);
169 network network(engine, topology);
171 network.set_input_data("input", input);
173 auto outputs = network.execute();
175 auto output = outputs.at("crop").get_memory();
176 auto output_ptr = output.pointer<int32_t>();
178 for (int b = 0; b < crop_batch_num; ++b) { //B
179 for (int f = 0; f < crop_feature_num; ++f) { //F
180 for (int y = 0; y < crop_y_size; ++y) { //Y
181 for (int x = 0; x < crop_x_size; ++x) { //X
182 int linear_id = b + batch_num * (f + feature_num * (x + x_size * y));
183 int output_linear_id = b + crop_batch_num * (f + crop_feature_num * (x + crop_x_size * y));
184 EXPECT_EQ(output_ptr[output_linear_id], input_vec[linear_id]);
191 TEST(crop_gpu, basic_in2x3x2x2_crop_all_bfyx) {
192 // Reference : 3x1x2x2
196 const auto& engine = get_test_engine();
199 auto feature_num = 2;
203 auto crop_batch_num = batch_num - 3;
204 auto crop_feature_num = feature_num - 1;
205 auto crop_x_size = x_size - 2;
206 auto crop_y_size = y_size - 1;
208 auto input = memory::allocate(engine, { data_types::f32,format::bfyx,{ batch_num, feature_num, x_size, y_size } });
211 topology.add(input_layout("input", input.get_layout()));
212 topology.add(crop("crop", "input", { crop_batch_num, crop_feature_num, crop_x_size, crop_y_size }, {0, 0, 0, 0} ));
214 std::vector<float> input_vec = generate_random_input<float>(batch_num, feature_num, y_size, x_size, -10, 10);
215 set_values(input, input_vec);
217 network network(engine, topology);
219 network.set_input_data("input", input);
221 auto outputs = network.execute();
223 auto output = outputs.at("crop").get_memory();
224 auto output_ptr = output.pointer<float>();
225 std::vector<float> a;
226 for (int b = 0; b < crop_batch_num; ++b) { //B
227 for (int f = 0; f < crop_feature_num; ++f) { //F
228 for (int y = 0; y < crop_y_size; ++y) { //Y
229 for (int x = 0; x < crop_x_size; ++x) { //X
230 int linear_id = x + x_size * (y + y_size * (f + feature_num * b));
231 int output_linear_id = x + crop_x_size * (y + crop_y_size * (f + crop_feature_num * b));
232 a.push_back(output_ptr[output_linear_id]);
233 EXPECT_EQ(output_ptr[output_linear_id], input_vec[linear_id]);
240 TEST(crop_gpu, basic_int_in2x3x2x2_crop_all_bfyx) {
241 // Reference : 3x1x2x2
245 const auto& engine = get_test_engine();
248 auto feature_num = 2;
252 auto crop_batch_num = batch_num - 3;
253 auto crop_feature_num = feature_num - 1;
254 auto crop_x_size = x_size - 2;
255 auto crop_y_size = y_size - 1;
257 auto input = memory::allocate(engine, { data_types::i32,format::bfyx,{ batch_num, feature_num, x_size, y_size } });
260 topology.add(input_layout("input", input.get_layout()));
261 topology.add(crop("crop", "input", { crop_batch_num, crop_feature_num, crop_x_size, crop_y_size }, { 0, 0, 0, 0 }));
263 std::vector<int32_t> input_vec = generate_random_input<int32_t>(batch_num, feature_num, y_size, x_size, -10, 10);
264 set_values(input, input_vec);
266 network network(engine, topology);
268 network.set_input_data("input", input);
270 auto outputs = network.execute();
272 auto output = outputs.at("crop").get_memory();
273 auto output_ptr = output.pointer<int32_t>();
274 std::vector<int32_t> a;
275 for (int b = 0; b < crop_batch_num; ++b) { //B
276 for (int f = 0; f < crop_feature_num; ++f) { //F
277 for (int y = 0; y < crop_y_size; ++y) { //Y
278 for (int x = 0; x < crop_x_size; ++x) { //X
279 int linear_id = x + x_size * (y + y_size * (f + feature_num * b));
280 int output_linear_id = x + crop_x_size * (y + crop_y_size * (f + crop_feature_num * b));
281 a.push_back(output_ptr[output_linear_id]);
282 EXPECT_EQ(output_ptr[output_linear_id], input_vec[linear_id]);
289 TEST(crop_gpu, basic_in2x3x2x2_crop_all_fyxb) {
290 // Reference : 3x1x2x2
294 const auto& engine = get_test_engine();
297 auto feature_num = 2;
301 auto crop_batch_num = batch_num - 3;
302 auto crop_feature_num = feature_num - 1;
303 auto crop_x_size = x_size - 2;
304 auto crop_y_size = y_size - 1;
306 auto input = memory::allocate(engine, { data_types::f32,format::fyxb,{ batch_num, feature_num, x_size, y_size } });
309 topology.add(input_layout("input", input.get_layout()));
310 topology.add(crop("crop", "input", { crop_batch_num, crop_feature_num, crop_x_size, crop_y_size }, {0, 0, 0, 0} ));
312 std::vector<float> input_vec = generate_random_input<float>(batch_num, feature_num, y_size, x_size, -10, 10);
313 set_values(input, input_vec);
315 network network(engine, topology);
317 network.set_input_data("input", input);
319 auto outputs = network.execute();
321 auto output = outputs.at("crop").get_memory();
322 auto output_ptr = output.pointer<float>();
323 for (int b = 0; b < crop_batch_num; ++b) { //B
324 for (int f = 0; f < crop_feature_num; ++f) { //F
325 for (int y = 0; y < crop_y_size; ++y) { //Y
326 for (int x = 0; x < crop_x_size; ++x) { //X
327 int linear_id = b + batch_num * (x + x_size * (y + y_size * f));
328 int output_linear_id = b + crop_batch_num * (x + crop_x_size * (y + crop_y_size * f));
329 EXPECT_EQ(output_ptr[output_linear_id], input_vec[linear_id]);
336 TEST(crop_gpu, basic_int_in2x3x2x2_crop_all_fyxb) {
337 // Reference : 3x1x2x2
341 const auto& engine = get_test_engine();
344 auto feature_num = 2;
348 auto crop_batch_num = batch_num - 3;
349 auto crop_feature_num = feature_num - 1;
350 auto crop_x_size = x_size - 2;
351 auto crop_y_size = y_size - 1;
353 auto input = memory::allocate(engine, { data_types::i32,format::fyxb,{ batch_num, feature_num, x_size, y_size } });
356 topology.add(input_layout("input", input.get_layout()));
357 topology.add(crop("crop", "input", { crop_batch_num, crop_feature_num, crop_x_size, crop_y_size }, { 0, 0, 0, 0 }));
359 std::vector<int32_t> input_vec = generate_random_input<int32_t>(batch_num, feature_num, y_size, x_size, -10, 10);
360 set_values(input, input_vec);
362 network network(engine, topology);
364 network.set_input_data("input", input);
366 auto outputs = network.execute();
368 auto output = outputs.at("crop").get_memory();
369 auto output_ptr = output.pointer<int32_t>();
370 for (int b = 0; b < crop_batch_num; ++b) { //B
371 for (int f = 0; f < crop_feature_num; ++f) { //F
372 for (int y = 0; y < crop_y_size; ++y) { //Y
373 for (int x = 0; x < crop_x_size; ++x) { //X
374 int linear_id = b + batch_num * (x + x_size * (y + y_size * f));
375 int output_linear_id = b + crop_batch_num * (x + crop_x_size * (y + crop_y_size * f));
376 EXPECT_EQ(output_ptr[output_linear_id], input_vec[linear_id]);
383 TEST(crop_gpu, basic_in2x3x2x2_crop_offsets) {
384 // Reference : 1x2x2x1
390 // f0: b0: 1 2 -10 b1: 0 0 -11
391 // f0: b0: 3 4 -14 b1: 0.5 -0.5 -15
392 // f1: b0: 5 6 -12 b1: 1.5 5.2 -13
393 // f1: b0: 7 8 -16 b1: 12 8 -17
395 const auto& engine = get_test_engine();
398 auto feature_num = 2;
402 auto crop_batch_num = batch_num - 1;
403 auto crop_feature_num = feature_num;
404 auto crop_x_size = x_size - 1;
405 auto crop_y_size = y_size - 1;
407 auto batch_offset = 1;
408 auto feature_offset = 0;
412 auto input = memory::allocate(engine, { data_types::f32, format::yxfb, { tensor(spatial(x_size, y_size), feature(feature_num), batch(batch_num)) } });
415 topology.add(input_layout("input", input.get_layout()));
416 topology.add(crop("crop", "input", tensor(batch(crop_batch_num), spatial(crop_x_size, crop_y_size), feature(crop_feature_num)), { tensor(feature(0)) }));
418 std::vector<float> input_vec = { 1.f, 0.f, 5.f, 1.5f,
420 -10.f, -11.f, -12.f, -13.f,
421 3.f, 0.5f, 7.f, 12.f,
422 4.f, -0.5f, 8.f, 8.f,
423 -14.f, -15.f, -16.f, -17.f };
424 set_values(input, input_vec);
426 network network(engine, topology);
428 network.set_input_data("input", input);
430 auto outputs = network.execute();
432 auto output = outputs.at("crop").get_memory();
433 auto output_ptr = output.pointer<float>();
435 for (int b = 0; b < crop_batch_num; ++b) { //B
436 for (int f = 0; f < crop_feature_num; ++f) { //F
437 for (int y = 0; y < crop_y_size; ++y) { //Y
438 for (int x = 0; x < crop_x_size; ++x) { //X
439 int linear_id = (b + batch_offset) + batch_num * ((f + feature_offset) + feature_num * ((x + x_offset) + x_size * (y + y_offset)));
440 int output_linear_id = b + crop_batch_num * (f + crop_feature_num * (x + crop_x_size * y));
441 EXPECT_EQ(output_ptr[output_linear_id], input_vec[linear_id]);
448 TEST(crop_gpu, basic_int_in2x3x2x2_crop_offsets) {
449 // Reference : 1x2x2x1
455 // f0: b0: 1 2 -10 b1: 0 0 -11
456 // f0: b0: 3 4 -14 b1: 50 -5 -15
457 // f1: b0: 5 6 -12 b1: 15 52 -13
458 // f1: b0: 7 8 -16 b1: 12 8 -17
460 const auto& engine = get_test_engine();
463 auto feature_num = 2;
467 auto crop_batch_num = batch_num - 1;
468 auto crop_feature_num = feature_num;
469 auto crop_x_size = x_size - 1;
470 auto crop_y_size = y_size - 1;
472 auto batch_offset = 1;
473 auto feature_offset = 0;
477 auto input = memory::allocate(engine, { data_types::i32, format::yxfb,{ tensor(spatial(x_size, y_size), feature(feature_num), batch(batch_num)) } });
480 topology.add(input_layout("input", input.get_layout()));
481 topology.add(crop("crop", "input", tensor(batch(crop_batch_num), spatial(crop_x_size, crop_y_size), feature(crop_feature_num)), { tensor(feature(0)) }));
483 std::vector<int32_t> input_vec = { 1, 0, 5, 15,
488 -14, -15, -16, -17 };
489 set_values(input, input_vec);
491 network network(engine, topology);
493 network.set_input_data("input", input);
495 auto outputs = network.execute();
497 auto output = outputs.at("crop").get_memory();
498 auto output_ptr = output.pointer<int32_t>();
500 for (int b = 0; b < crop_batch_num; ++b) { //B
501 for (int f = 0; f < crop_feature_num; ++f) { //F
502 for (int y = 0; y < crop_y_size; ++y) { //Y
503 for (int x = 0; x < crop_x_size; ++x) { //X
504 int linear_id = (b + batch_offset) + batch_num * ((f + feature_offset) + feature_num * ((x + x_offset) + x_size * (y + y_offset)));
505 int output_linear_id = b + crop_batch_num * (f + crop_feature_num * (x + crop_x_size * y));
506 EXPECT_EQ(output_ptr[output_linear_id], input_vec[linear_id]);
513 TEST(crop_gpu, basic_in1x4x1x1_split) {
514 // Tests split with crop implementation
515 // _CROP_1(1x3x1x1,offset(0x0x0x0))
519 // CROP_2(1x1x1x1,offset(0x3x0x0))
521 // Reference1 : 1x3x1x1
522 // Offsets1 : 0x0x0x0
523 // Reference2 : 1x1x1x1
524 // Offsets2 : 0x3x0x0
542 const auto& engine = get_test_engine();
545 auto feature_num = 4;
549 auto crop_batch_num = 1;
550 auto crop_feature_num_1 = 3;
551 auto crop_feature_num_2 = 1;
552 auto crop_x_size = 1;
553 auto crop_y_size = 1;
554 auto feature_offset_1 = 0;
555 auto feature_offset_2 = 3;
556 auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ tensor(spatial(x_size, y_size), feature(feature_num), batch(batch_num)) } });
559 topology.add(input_layout("input", input.get_layout()));
560 topology.add(crop("crop1", "input", tensor(batch(crop_batch_num), spatial(crop_x_size, crop_y_size), feature(crop_feature_num_1)), { tensor(feature(feature_offset_1), spatial(0,0),batch(0)) }));
561 topology.add(crop("crop2", "input", tensor(batch(crop_batch_num), spatial(crop_x_size, crop_y_size), feature(crop_feature_num_2)), { tensor(feature(feature_offset_2), spatial(0,0),batch(0)) }));
563 std::vector<float> input_vec = { -1.f, 2.f, -3.f, 4.f };
564 std::vector<float> out1 = { -1.f, 2.f,-3.f };
565 std::vector<float> out2 = { 4.f, };
566 set_values(input, input_vec);
568 bo.set_option(build_option::optimize_data(true));
569 bo.set_option(build_option::outputs(topology.get_primitive_ids()));
571 network network(engine, topology, bo);
572 network.set_input_data("input", input);
573 auto outputs = network.execute();
575 auto output = outputs.at("crop1").get_memory();
576 auto output_ptr = output.pointer<float>();
578 for (size_t i = 0; i < out1.size();i++)
579 EXPECT_EQ(output_ptr[i], out1[i]);
581 std::cout << std::endl;
582 auto output_2 = outputs.at("crop2").get_memory();
583 auto output_ptr_2 = output_2.pointer<float>();
585 for (size_t i = 0; i < out2.size();i++)
586 EXPECT_EQ(output_ptr_2[i], out2[i]);
589 TEST(crop_gpu, basic_int_in1x4x1x1_split) {
590 // Tests split with crop implementation
591 // _CROP_1(1x3x1x1,offset(0x0x0x0))
595 // CROP_2(1x1x1x1,offset(0x3x0x0))
597 // Reference1 : 1x3x1x1
598 // Offsets1 : 0x0x0x0
599 // Reference2 : 1x1x1x1
600 // Offsets2 : 0x3x0x0
618 const auto& engine = get_test_engine();
621 auto feature_num = 4;
625 auto crop_batch_num = 1;
626 auto crop_feature_num_1 = 3;
627 auto crop_feature_num_2 = 1;
628 auto crop_x_size = 1;
629 auto crop_y_size = 1;
630 auto feature_offset_1 = 0;
631 auto feature_offset_2 = 3;
632 auto input = memory::allocate(engine, { data_types::i32, format::bfyx,{ tensor(spatial(x_size, y_size), feature(feature_num), batch(batch_num)) } });
635 topology.add(input_layout("input", input.get_layout()));
636 topology.add(crop("crop1", "input", tensor(batch(crop_batch_num), spatial(crop_x_size, crop_y_size), feature(crop_feature_num_1)), { tensor(feature(feature_offset_1), spatial(0,0),batch(0)) }));
637 topology.add(crop("crop2", "input", tensor(batch(crop_batch_num), spatial(crop_x_size, crop_y_size), feature(crop_feature_num_2)), { tensor(feature(feature_offset_2), spatial(0,0),batch(0)) }));
639 std::vector<int32_t> input_vec = { -1, 2, -3, 4 };
640 std::vector<int32_t> out1 = { -1, 2,-3 };
641 std::vector<int32_t> out2 = { 4, };
642 set_values(input, input_vec);
644 bo.set_option(build_option::optimize_data(true));
645 bo.set_option(build_option::outputs(topology.get_primitive_ids()));
647 network network(engine, topology, bo);
648 network.set_input_data("input", input);
649 auto outputs = network.execute();
651 auto output = outputs.at("crop1").get_memory();
652 auto output_ptr = output.pointer<int32_t>();
654 for (size_t i = 0; i < out1.size(); i++)
655 EXPECT_EQ(output_ptr[i], out1[i]);
657 std::cout << std::endl;
658 auto output_2 = outputs.at("crop2").get_memory();
659 auto output_ptr_2 = output_2.pointer<int32_t>();
661 for (size_t i = 0; i < out2.size(); i++)
662 EXPECT_EQ(output_ptr_2[i], out2[i]);
665 TEST(crop_gpu, basic_in1x4x1x1_split_w_relu) {
666 // Tests split with crop implementation
667 // _ CROP_1(1x3x1x1,offset(0x0x0x0)) --> RELU
669 // INPUT(1x4x1x1)--RELU
671 // CROP_2(1x1x1x1,offset(0x3x0x0)) --> RELU
673 // Reference1 : 1x3x1x1
674 // Offsets1 : 0x0x0x0
675 // Reference2 : 1x1x1x1
676 // Offsets2 : 0x3x0x0
694 // disable memory pool when we want to check optimized out internal results
695 engine_configuration cfg{ false, false, false, std::string(), std::string(), true /*oooq*/, std::string(),std::string(), priority_mode_types::disabled, throttle_mode_types::disabled, false /*mem_pool*/ };
696 engine engine{ cfg };
698 auto feature_num = 4;
701 auto crop_batch_num = 1;
702 auto crop_feature_num_1 = 3;
703 auto crop_feature_num_2 = 1;
704 auto crop_x_size = 1;
705 auto crop_y_size = 1;
706 auto feature_offset_1 = 0;
707 auto feature_offset_2 = 3;
708 auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ tensor(spatial(x_size, y_size), feature(feature_num), batch(batch_num)) } });
711 topology.add(input_layout("input", input.get_layout()));
712 topology.add(activation("relu", "input", activation_func::relu));
713 topology.add(crop("crop1", "relu", tensor(batch(crop_batch_num), spatial(crop_x_size, crop_y_size), feature(crop_feature_num_1)), { tensor(feature(feature_offset_1), spatial(0,0),batch(0)) }));
714 topology.add(crop("crop2", "relu", tensor(batch(crop_batch_num), spatial(crop_x_size, crop_y_size), feature(crop_feature_num_2)), { tensor(feature(feature_offset_2), spatial(0,0),batch(0)) }));
715 topology.add(activation("relu1", "crop1", activation_func::relu));
716 topology.add(activation("relu2", "crop2", activation_func::relu));
718 std::vector<float> input_vec = { -1.f, 2.f, -3.f, 4.f };
719 std::vector<float> out1 = { 0.f, 2.f,0.f };
720 std::vector<float> out2 = { 4.f, };
721 set_values(input, input_vec);
723 bo.set_option(build_option::optimize_data(true));
724 bo.set_option(build_option::debug(true)); //required to have optimized crop despite the fact that it's specified as an output
726 network network(engine, topology, bo);
727 network.set_input_data("input", input);
728 auto outputs = network.execute();
730 auto output = outputs.at("relu1").get_memory();
731 auto output_ptr = output.pointer<float>();
733 // check if crop has been executed in place
734 auto in_place = outputs.at("crop1").get_memory().is_the_same_buffer(outputs.at("relu").get_memory());
735 EXPECT_TRUE(in_place);
737 for (size_t i = 0; i < out1.size();i++)
738 EXPECT_EQ(output_ptr[i], out1[i]);
740 auto output_2 = outputs.at("relu2").get_memory();
741 auto output_ptr_2 = output_2.pointer<float>();
743 for (size_t i = 0; i < out2.size();i++)
744 EXPECT_EQ(output_ptr_2[i], out2[i]);
747 TEST(crop_gpu, basic_in3x1x2x2x1_crop_all_bfzyx) {
748 // Reference : 3x1x2x2x1
750 // Output : 3x1x2x2x1
752 const auto& engine = get_test_engine();
755 auto feature_num = 2;
760 auto crop_batch_num = batch_num - 3;
761 auto crop_feature_num = feature_num - 1;
762 auto crop_x_size = x_size - 2;
763 auto crop_y_size = y_size - 1;
764 auto crop_z_size = z_size - 1;
766 auto input = memory::allocate(engine, { data_types::f32,format::bfzyx,{ batch_num, feature_num, x_size, y_size, z_size } });
769 topology.add(input_layout("input", input.get_layout()));
770 topology.add(crop("crop", "input", { crop_batch_num, crop_feature_num, crop_x_size, crop_y_size, crop_z_size }, { 0, 0, 0, 0, 0 }));
772 std::vector<float> input_vec = generate_random_input<float>(batch_num, feature_num, y_size, x_size, -10, 10);
773 set_values(input, input_vec);
775 network network(engine, topology);
777 network.set_input_data("input", input);
779 auto outputs = network.execute();
781 auto output = outputs.at("crop").get_memory();
782 auto output_ptr = output.pointer<float>();
783 for (int b = 0; b < crop_batch_num; ++b) { //B
784 for (int f = 0; f < crop_feature_num; ++f) { //F
785 for (int z = 0; z < crop_z_size; ++z) { //Z
786 for (int y = 0; y < crop_y_size; ++y) { //Y
787 for (int x = 0; x < crop_x_size; ++x) { //X
788 int linear_id = x + x_size * (y + y_size * (z + z_size * (f + feature_num * b)));
789 int output_linear_id = x + crop_x_size * (y + crop_y_size * (z + crop_z_size * (f + crop_feature_num * b)));
790 EXPECT_EQ(output_ptr[output_linear_id], input_vec[linear_id]);