2 // Copyright (c) 2018 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 ///////////////////////////////////////////////////////////////////////////////////////////////////
19 #include <gtest/gtest.h>
20 #include <api/CPP/memory.hpp>
21 #include <api/CPP/input_layout.hpp>
22 #include "api/CPP/mvn.hpp"
23 #include "api/CPP/reorder.hpp"
24 #include <api/CPP/topology.hpp>
25 #include <api/CPP/network.hpp>
26 #include <api/CPP/engine.hpp>
27 #include "test_utils/test_utils.h"
30 #include "test_utils.h"
32 using namespace cldnn;
34 class mvn_gpu_test : public ::testing::TestWithParam<cldnn::format>
39 void mvn_compute_mean_accross_channels_bfyx(cldnn::memory &output, bool normalize_variance)
41 using namespace tests;
43 const auto output_desc = generic_test::get_linear_memory_desc(output.get_layout());
45 auto output_sizes = output.get_layout().size.sizes();
47 uint32_t batch_size = output_sizes[0];
48 uint32_t feature_size = output_sizes[1];
49 uint32_t y_size = output_sizes[3];
50 uint32_t x_size = output_sizes[2];
52 auto buff = output.pointer<T>();
54 float err_margin = output.get_layout().data_type == data_types::f32 ? 1e-03F : 1e-02F;
56 for (uint32_t b = 0; b < batch_size; ++b)
60 for (uint32_t f = 0; f < feature_size; ++f)
62 for (uint32_t y = 0; y < y_size; ++y)
64 for (uint32_t x = 0; x < x_size; ++x)
66 size_t data_index = generic_test::get_linear_index(output.get_layout(), b, f, y, x, output_desc);
67 float data = static_cast<float>(buff[data_index]);
69 if (normalize_variance)
70 variance += data*data;
74 sum /= feature_size * y_size * x_size;
75 T result_sum = static_cast<T>(sum);
76 EXPECT_NEAR(result_sum, 0.f, err_margin);
78 if (normalize_variance)
80 variance /= feature_size * y_size * x_size;
81 T result_variance = static_cast<T>(variance);
82 EXPECT_NEAR(result_variance, 1.f, err_margin);
88 void mvn_compute_mean_within_channels_bfyx(cldnn::memory &output, bool normalize_variance)
90 using namespace tests;
92 const auto output_desc = generic_test::get_linear_memory_desc(output.get_layout());
94 auto output_sizes = output.get_layout().size.sizes();
96 uint32_t batch_size = output_sizes[0];
97 uint32_t feature_size = output_sizes[1];
98 uint32_t y_size = output_sizes[3];
99 uint32_t x_size = output_sizes[2];
101 auto buff = output.pointer<T>();
103 float err_margin = output.get_layout().data_type == data_types::f32 ? 1e-03F : 1e-02F;
105 for (uint32_t b = 0; b < batch_size; ++b)
107 for (uint32_t f = 0; f < feature_size; ++f)
110 float variance = 0.f;
111 for (uint32_t y = 0; y < y_size; ++y)
113 for (uint32_t x = 0; x < x_size; ++x)
115 size_t data_index = generic_test::get_linear_index(output.get_layout(), b, f, y, x, output_desc);
116 float data = static_cast<float>(buff[data_index]);
118 if (normalize_variance)
119 variance += data*data;
122 sum /= y_size * x_size;
123 T result_sum = static_cast<T>(sum);
124 EXPECT_NEAR(result_sum, 0.f, err_margin);
126 if (normalize_variance)
128 variance /= y_size * x_size;
129 T result_variance = static_cast<T>(variance);
130 EXPECT_NEAR(result_variance, 1.f, err_margin);
136 TEST(mvn_gpu_test, mvn_test_across_channels_bfyx)
138 //mvn accross channels fp32 test with normalize_variance set to false
139 using namespace cldnn;
140 using namespace tests;
142 const auto& engine = get_test_engine();
144 auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 7, 10, 17, 13 } });
146 tests::set_random_values<float>(input, true, 8, 100);
149 topology.add(input_layout("input", input.get_layout()));
150 topology.add(mvn("mvn", "input", true, false));
152 network network(engine, topology);
154 network.set_input_data("input", input);
156 auto outputs = network.execute();
157 EXPECT_EQ(outputs.size(), size_t(1));
158 EXPECT_EQ(outputs.begin()->first, "mvn");
160 auto output = outputs.begin()->second.get_memory();
161 mvn_compute_mean_accross_channels_bfyx<float>(output, false);
164 TEST(mvn_gpu_test, mvn_test_across_channels_bfyx_fp16)
166 //mvn accross channels fp16 test with normalize_variance set to false
167 using namespace cldnn;
168 using namespace tests;
170 const auto& engine = get_test_engine();
172 auto input = memory::allocate(engine, { data_types::f16, format::bfyx,{ 7, 10, 17, 13 } });
174 tests::set_random_values<FLOAT16>(input, true, 8, 100);
177 topology.add(input_layout("input", input.get_layout()));
178 topology.add(mvn("mvn", "input", true, false));
180 network network(engine, topology);
182 network.set_input_data("input", input);
184 auto outputs = network.execute();
185 EXPECT_EQ(outputs.size(), size_t(1));
186 EXPECT_EQ(outputs.begin()->first, "mvn");
188 auto output = outputs.begin()->second.get_memory();
189 mvn_compute_mean_accross_channels_bfyx<FLOAT16>(output, false);
192 TEST(mvn_gpu_test, mvn_test_across_channels_bfyx_normalize_variance)
194 //mvn accross channels fp32 test with normalize_variance set to true
195 using namespace cldnn;
196 using namespace tests;
198 const auto& engine = get_test_engine();
200 auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 7, 10, 17, 13 } });
202 tests::set_random_values<float>(input, true, 8, 100);
205 topology.add(input_layout("input", input.get_layout()));
206 topology.add(mvn("mvn", "input", true, true));
208 network network(engine, topology);
210 network.set_input_data("input", input);
212 auto outputs = network.execute();
213 EXPECT_EQ(outputs.size(), size_t(1));
214 EXPECT_EQ(outputs.begin()->first, "mvn");
216 auto output = outputs.begin()->second.get_memory();
217 mvn_compute_mean_accross_channels_bfyx<float>(output, true);
220 TEST(mvn_gpu_test, mvn_test_across_channels_bfyx_normalize_variance_fp16)
222 //mvn accross channels fp16 test with normalize_variance set to true
223 using namespace cldnn;
224 using namespace tests;
226 const auto& engine = get_test_engine();
228 auto input = memory::allocate(engine, { data_types::f16, format::bfyx,{ 7, 10, 17, 13 } });
230 tests::set_random_values<FLOAT16>(input, true, 8, 100);
233 topology.add(input_layout("input", input.get_layout()));
234 topology.add(mvn("mvn", "input", true, true));
236 network network(engine, topology);
238 network.set_input_data("input", input);
240 auto outputs = network.execute();
241 EXPECT_EQ(outputs.size(), size_t(1));
242 EXPECT_EQ(outputs.begin()->first, "mvn");
244 auto output = outputs.begin()->second.get_memory();
245 mvn_compute_mean_accross_channels_bfyx<FLOAT16>(output, true);
248 TEST(mvn_gpu_test, mvn_test_within_channels_bfyx)
250 //mvn within channels fp32 test with normalize_variance set to false
251 using namespace cldnn;
252 using namespace tests;
254 const auto& engine = get_test_engine();
256 auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 7, 10, 17, 13 } });
258 tests::set_random_values<float>(input, true, 8, 100);
261 topology.add(input_layout("input", input.get_layout()));
262 topology.add(mvn("mvn", "input", false, false));
264 network network(engine, topology);
266 network.set_input_data("input", input);
268 auto outputs = network.execute();
269 EXPECT_EQ(outputs.size(), size_t(1));
270 EXPECT_EQ(outputs.begin()->first, "mvn");
272 auto output = outputs.begin()->second.get_memory();
273 mvn_compute_mean_within_channels_bfyx<float>(output, false);
276 TEST(mvn_gpu_test, mvn_test_within_channels_bfyx_fp16)
278 //mvn within channels fp16 test with normalize_variance set to false
279 using namespace cldnn;
280 using namespace tests;
282 const auto& engine = get_test_engine();
284 auto input = memory::allocate(engine, { data_types::f16, format::bfyx,{ 7, 10, 17, 13 } });
286 tests::set_random_values<FLOAT16>(input, true, 8, 100);
289 topology.add(input_layout("input", input.get_layout()));
290 topology.add(mvn("mvn", "input", false, false));
292 network network(engine, topology);
294 network.set_input_data("input", input);
296 auto outputs = network.execute();
297 EXPECT_EQ(outputs.size(), size_t(1));
298 EXPECT_EQ(outputs.begin()->first, "mvn");
300 auto output = outputs.begin()->second.get_memory();
301 mvn_compute_mean_within_channels_bfyx<FLOAT16>(output, false);
304 TEST(mvn_gpu_test, mvn_test_within_channels_bfyx_normalize_variance)
306 //mvn within channels fp32 test with normalize_variance set to true
307 using namespace cldnn;
308 using namespace tests;
310 const auto& engine = get_test_engine();
312 auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 7, 10, 17, 13 } });
314 tests::set_random_values<float>(input, true, 8, 100);
317 topology.add(input_layout("input", input.get_layout()));
318 topology.add(mvn("mvn", "input", false, true));
320 network network(engine, topology);
322 network.set_input_data("input", input);
324 auto outputs = network.execute();
325 EXPECT_EQ(outputs.size(), size_t(1));
326 EXPECT_EQ(outputs.begin()->first, "mvn");
328 auto output = outputs.begin()->second.get_memory();
329 mvn_compute_mean_within_channels_bfyx<float>(output, true);
332 TEST(mvn_gpu_test, mvn_test_within_channels_bfyx_normalize_variance_fp16)
334 //mvn within channels fp16 test with normalize_variance set to true
335 using namespace cldnn;
336 using namespace tests;
338 const auto& engine = get_test_engine();
340 auto input = memory::allocate(engine, { data_types::f16, format::bfyx,{ 7, 10, 17, 13 } });
342 tests::set_random_values<FLOAT16>(input, true, 8, 100);
345 topology.add(input_layout("input", input.get_layout()));
346 topology.add(mvn("mvn", "input", false, true));
348 network network(engine, topology);
350 network.set_input_data("input", input);
352 auto outputs = network.execute();
353 EXPECT_EQ(outputs.size(), size_t(1));
354 EXPECT_EQ(outputs.begin()->first, "mvn");
356 auto output = outputs.begin()->second.get_memory();
357 mvn_compute_mean_within_channels_bfyx<FLOAT16>(output, true);