Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / tests / test_cases / mvn_gpu_test.cpp
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
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
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18
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"
28 #include <iostream>
29 #include "float16.h"
30 #include "test_utils.h"
31
32 using namespace cldnn;
33
34 class mvn_gpu_test : public ::testing::TestWithParam<cldnn::format>
35 {
36 };
37
38 template <typename T>
39 void mvn_compute_mean_accross_channels_bfyx(cldnn::memory &output, bool normalize_variance)
40 {
41     using namespace tests;
42
43     const auto output_desc = generic_test::get_linear_memory_desc(output.get_layout());
44
45     auto output_sizes = output.get_layout().size.sizes();
46
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];
51
52     auto buff = output.pointer<T>();
53
54     float err_margin = output.get_layout().data_type == data_types::f32 ? 1e-03F : 1e-02F;
55
56     for (uint32_t b = 0; b < batch_size; ++b)
57     {
58         float sum = 0.f;
59         float variance = 0.f;
60         for (uint32_t f = 0; f < feature_size; ++f)
61         {
62             for (uint32_t y = 0; y < y_size; ++y)
63             {
64                 for (uint32_t x = 0; x < x_size; ++x)
65                 {
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]);
68                     sum += data;
69                     if (normalize_variance)
70                         variance += data*data;
71                 }
72             }
73         }
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);
77
78         if (normalize_variance)
79         {
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);
83         }
84     }
85 }
86
87 template <typename T>
88 void mvn_compute_mean_within_channels_bfyx(cldnn::memory &output, bool normalize_variance)
89 {
90     using namespace tests;
91
92     const auto output_desc = generic_test::get_linear_memory_desc(output.get_layout());
93
94     auto output_sizes = output.get_layout().size.sizes();
95
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];
100
101     auto buff = output.pointer<T>();
102
103     float err_margin = output.get_layout().data_type == data_types::f32 ? 1e-03F : 1e-02F;
104
105     for (uint32_t b = 0; b < batch_size; ++b)
106     {
107         for (uint32_t f = 0; f < feature_size; ++f)
108         {
109             float sum = 0.f;
110             float variance = 0.f;
111             for (uint32_t y = 0; y < y_size; ++y)
112             {
113                 for (uint32_t x = 0; x < x_size; ++x)
114                 {
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]);
117                     sum += data;
118                     if (normalize_variance)
119                         variance += data*data;
120                 }
121             }
122             sum /= y_size * x_size;
123             T result_sum = static_cast<T>(sum);
124             EXPECT_NEAR(result_sum, 0.f, err_margin);
125
126             if (normalize_variance)
127             {
128                 variance /= y_size * x_size;
129                 T result_variance = static_cast<T>(variance);
130                 EXPECT_NEAR(result_variance, 1.f, err_margin);
131             }
132         }
133     }
134 }
135
136 TEST(mvn_gpu_test, mvn_test_across_channels_bfyx)
137 {
138     //mvn accross channels fp32 test with normalize_variance set to false
139     using namespace cldnn;
140     using namespace tests;
141
142     const auto& engine = get_test_engine();
143
144     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 7, 10, 17, 13 } });
145
146     tests::set_random_values<float>(input, true, 8, 100);
147
148     topology topology;
149     topology.add(input_layout("input", input.get_layout()));
150     topology.add(mvn("mvn", "input", true, false));
151
152     network network(engine, topology);
153
154     network.set_input_data("input", input);
155
156     auto outputs = network.execute();
157     EXPECT_EQ(outputs.size(), size_t(1));
158     EXPECT_EQ(outputs.begin()->first, "mvn");
159
160     auto output = outputs.begin()->second.get_memory();
161     mvn_compute_mean_accross_channels_bfyx<float>(output, false);
162 }
163
164 TEST(mvn_gpu_test, mvn_test_across_channels_bfyx_fp16)
165 {
166     //mvn accross channels fp16 test with normalize_variance set to false
167     using namespace cldnn;
168     using namespace tests;
169
170     const auto& engine = get_test_engine();
171
172     auto input = memory::allocate(engine, { data_types::f16, format::bfyx,{ 7, 10, 17, 13 } });
173
174     tests::set_random_values<FLOAT16>(input, true, 8, 100);
175
176     topology topology;
177     topology.add(input_layout("input", input.get_layout()));
178     topology.add(mvn("mvn", "input", true, false));
179
180     network network(engine, topology);
181
182     network.set_input_data("input", input);
183
184     auto outputs = network.execute();
185     EXPECT_EQ(outputs.size(), size_t(1));
186     EXPECT_EQ(outputs.begin()->first, "mvn");
187
188     auto output = outputs.begin()->second.get_memory();
189     mvn_compute_mean_accross_channels_bfyx<FLOAT16>(output, false);
190 }
191
192 TEST(mvn_gpu_test, mvn_test_across_channels_bfyx_normalize_variance)
193 {
194     //mvn accross channels fp32 test with normalize_variance set to true
195     using namespace cldnn;
196     using namespace tests;
197
198     const auto& engine = get_test_engine();
199
200     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 7, 10, 17, 13 } });
201
202     tests::set_random_values<float>(input, true, 8, 100);
203
204     topology topology;
205     topology.add(input_layout("input", input.get_layout()));
206     topology.add(mvn("mvn", "input", true, true));
207
208     network network(engine, topology);
209
210     network.set_input_data("input", input);
211
212     auto outputs = network.execute();
213     EXPECT_EQ(outputs.size(), size_t(1));
214     EXPECT_EQ(outputs.begin()->first, "mvn");
215
216     auto output = outputs.begin()->second.get_memory();
217     mvn_compute_mean_accross_channels_bfyx<float>(output, true);
218 }
219
220 TEST(mvn_gpu_test, mvn_test_across_channels_bfyx_normalize_variance_fp16)
221 {
222     //mvn accross channels fp16 test with normalize_variance set to true
223     using namespace cldnn;
224     using namespace tests;
225
226     const auto& engine = get_test_engine();
227
228     auto input = memory::allocate(engine, { data_types::f16, format::bfyx,{ 7, 10, 17, 13 } });
229
230     tests::set_random_values<FLOAT16>(input, true, 8, 100);
231
232     topology topology;
233     topology.add(input_layout("input", input.get_layout()));
234     topology.add(mvn("mvn", "input", true, true));
235
236     network network(engine, topology);
237
238     network.set_input_data("input", input);
239
240     auto outputs = network.execute();
241     EXPECT_EQ(outputs.size(), size_t(1));
242     EXPECT_EQ(outputs.begin()->first, "mvn");
243
244     auto output = outputs.begin()->second.get_memory();
245     mvn_compute_mean_accross_channels_bfyx<FLOAT16>(output, true);
246 }
247
248 TEST(mvn_gpu_test, mvn_test_within_channels_bfyx)
249 {
250     //mvn within channels fp32 test with normalize_variance set to false
251     using namespace cldnn;
252     using namespace tests;
253
254     const auto& engine = get_test_engine();
255
256     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 7, 10, 17, 13 } });
257
258     tests::set_random_values<float>(input, true, 8, 100);
259
260     topology topology;
261     topology.add(input_layout("input", input.get_layout()));
262     topology.add(mvn("mvn", "input", false, false));
263
264     network network(engine, topology);
265
266     network.set_input_data("input", input);
267
268     auto outputs = network.execute();
269     EXPECT_EQ(outputs.size(), size_t(1));
270     EXPECT_EQ(outputs.begin()->first, "mvn");
271
272     auto output = outputs.begin()->second.get_memory();
273     mvn_compute_mean_within_channels_bfyx<float>(output, false);
274 }
275
276 TEST(mvn_gpu_test, mvn_test_within_channels_bfyx_fp16)
277 {
278     //mvn within channels fp16 test with normalize_variance set to false
279     using namespace cldnn;
280     using namespace tests;
281
282     const auto& engine = get_test_engine();
283
284     auto input = memory::allocate(engine, { data_types::f16, format::bfyx,{ 7, 10, 17, 13 } });
285
286     tests::set_random_values<FLOAT16>(input, true, 8, 100);
287
288     topology topology;
289     topology.add(input_layout("input", input.get_layout()));
290     topology.add(mvn("mvn", "input", false, false));
291
292     network network(engine, topology);
293
294     network.set_input_data("input", input);
295
296     auto outputs = network.execute();
297     EXPECT_EQ(outputs.size(), size_t(1));
298     EXPECT_EQ(outputs.begin()->first, "mvn");
299
300     auto output = outputs.begin()->second.get_memory();
301     mvn_compute_mean_within_channels_bfyx<FLOAT16>(output, false);
302 }
303
304 TEST(mvn_gpu_test, mvn_test_within_channels_bfyx_normalize_variance)
305 {
306     //mvn within channels fp32 test with normalize_variance set to true
307     using namespace cldnn;
308     using namespace tests;
309
310     const auto& engine = get_test_engine();
311
312     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 7, 10, 17, 13 } });
313
314     tests::set_random_values<float>(input, true, 8, 100);
315
316     topology topology;
317     topology.add(input_layout("input", input.get_layout()));
318     topology.add(mvn("mvn", "input", false, true));
319
320     network network(engine, topology);
321
322     network.set_input_data("input", input);
323
324     auto outputs = network.execute();
325     EXPECT_EQ(outputs.size(), size_t(1));
326     EXPECT_EQ(outputs.begin()->first, "mvn");
327
328     auto output = outputs.begin()->second.get_memory();
329     mvn_compute_mean_within_channels_bfyx<float>(output, true);
330 }
331
332 TEST(mvn_gpu_test, mvn_test_within_channels_bfyx_normalize_variance_fp16)
333 {
334     //mvn within channels fp16 test with normalize_variance set to true
335     using namespace cldnn;
336     using namespace tests;
337
338     const auto& engine = get_test_engine();
339
340     auto input = memory::allocate(engine, { data_types::f16, format::bfyx,{ 7, 10, 17, 13 } });
341
342     tests::set_random_values<FLOAT16>(input, true, 8, 100);
343
344     topology topology;
345     topology.add(input_layout("input", input.get_layout()));
346     topology.add(mvn("mvn", "input", false, true));
347
348     network network(engine, topology);
349
350     network.set_input_data("input", input);
351
352     auto outputs = network.execute();
353     EXPECT_EQ(outputs.size(), size_t(1));
354     EXPECT_EQ(outputs.begin()->first, "mvn");
355
356     auto output = outputs.begin()->second.get_memory();
357     mvn_compute_mean_within_channels_bfyx<FLOAT16>(output, true);
358 }