5412f9fa76f576f1bad7e847cbd28febcd7cb44f
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / tests / test_cases / binary_convolution_gpu_test.cpp
1 /*
2 // Copyright (c) 2019 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/memory.hpp>
21 #include <api/input_layout.hpp>
22 #include "api/binary_convolution.hpp"
23 #include "api/reorder.hpp"
24 #include <api/topology.hpp>
25 #include <api/network.hpp>
26 #include <api/engine.hpp>
27 #include "test_utils/test_utils.h"
28 #include <iostream>
29 #include <api/data.hpp>
30 #include <src/include/to_string_utils.h>
31 #include "float16.h"
32 #include "test_utils.h"
33
34 using namespace cldnn;
35 using namespace tests;
36
37 // Batch, groups, IC, IW, IH, OC, OW, OH, KH, KW, SH, SW, PH, PW
38 struct TestParams {
39     int b;
40     int g;
41
42     int ic;
43     int ih;
44     int iw;
45
46     int oc;
47     int oh;
48     int ow;
49
50     int kh;
51     int kw;
52
53     int sh;
54     int sw;
55
56     int ph;
57     int pw;
58
59     float pad_value;
60     data_types dt;
61     std::string name;
62
63     bool isConsistent() const
64     {
65         bool res = true;
66
67         res &= (((iw - kw + 2*pw) / sw + 1) == ow);
68         res &= (((ih - kh + 2*ph) / sh + 1) == oh);
69         return res;
70     }
71
72     friend ::std::ostream& operator<<(::std::ostream& os, const TestParams& p) {
73         return os << "Params: [ b=" << p.b
74                   << "; g=" << p.g
75                   << "; src=[" << p.ic << "; " << p.ih << "; " << p.iw << "]"
76                   << "; dst=[" << p.oc << "; " << p.oh << "; " << p.ow << "]"
77                   << "; k=[" << p.kh << "; " << p.kw << "]"
78                   << "; stride=[" << p.sh << "; " << p.sw << "]"
79                   << "; pad=[" << p.ph << "; " << p.pw << "]"
80                   << "; pad_value=" << p.pad_value
81                   << "; name=" << p.name
82                   << "]";
83     }
84     friend void PrintTo(const TestParams& p, ::std::ostream* os) {
85         *os << p;
86     }
87 };
88
89 static void fill(cldnn::memory& mem) {
90     auto ptr = mem.pointer<uint32_t>();
91     for (size_t i = 0; i < div_up(mem.get_layout().count(), 32); i++) {
92         ptr[i] = (uint32_t)rand() % (1 << 31);
93     }
94 }
95
96 template <typename data_t_src, typename data_t_wei,
97           typename data_t_acc, typename data_t_dst>
98 void compute_ref_conv_bin(const cldnn::memory &src,
99                           const cldnn::memory &weights,
100                           cldnn::memory &dst,
101                           TestParams &p)
102 {
103     auto src_data     = src.pointer<data_t_src>();
104     auto weights_data = weights.pointer<data_t_wei>();
105     auto dst_data     = dst.pointer<data_t_dst>();
106
107     bool with_groups = p.g > 1;
108     int pack_size = sizeof(data_t_src) * 8;
109
110     int B = p.b;
111     int NG = p.g;
112     int IC = p.ic;
113     int IH = p.ih;
114     int IW = p.iw;
115
116     int OC = p.oc;
117     int OH = p.oh;
118     int OW = p.ow;
119
120     int KH = p.kh;
121     int KW = p.kw;
122
123     int SH = p.sh;
124     int SW = p.sw;
125
126     int PH = p.ph;
127     int PW = p.pw;
128
129     auto extract_bit = [&](data_t_src val, data_t_src bit) -> data_t_src {
130         return (data_t_src)((val >> bit) & 0x1);
131     };
132
133     auto ker = [=](data_t_acc &d, int g, int mb, int oc,int oh, int ow, int& ks) {
134         for (int ic = 0; ic < IC / NG; ++ic) {
135             for (int kh = 0; kh < KH; ++kh)
136                 for (int kw = 0; kw < KW; ++kw) {
137                     const int ih = oh * SH - PH + kh;
138                     const int iw = ow * SW - PW + kw;
139
140                     int widx =   g * OC / NG *IC / NG * KH * KW
141                                  + oc * IC / NG * KH * KW
142                                  + ic * KH * KW
143                                  + kh * KW
144                                  + kw;
145                     int iidx = -1;
146                     uint8_t w = extract_bit(weights_data[widx / pack_size], widx % pack_size);
147                     uint8_t s = 0;
148
149                     if ((ih < 0 || ih >= IH || iw < 0 || iw >= IW))
150                     {
151                         if (p.pad_value == 0.0f)
152                             continue;
153                         else
154                             s = (p.pad_value == -1.0f) ? 0 : 1;
155                     }
156                     else
157                     {
158                         if (ic == 0) ks++;
159                         iidx = mb * div_up(IC, pack_size) * IH * IW
160                                + g * div_up(IC, pack_size) / NG * IH * IW
161                                + (ic/pack_size) * IH * IW
162                                + ih * IW
163                                + iw;
164
165                         s = extract_bit(src_data[iidx], ic % pack_size);
166                     }
167                     d += (data_t_acc)(s ^ w);
168                 }
169         }
170     };
171
172     for (int g = 0; g < NG; g++) {
173         for (int b = 0; b < B; b++) {
174             for (int oc = 0; oc < OC / NG; oc++) {
175                 for (int oh = 0; oh < OH; oh++) {
176                     for (int ow = 0; ow < OW; ow++) {
177                         data_t_acc a = 0;
178                         int ks = 0;
179                         ker(a, g, b, oc, oh, ow, ks);
180                         int dst_off = b * OC * OH* OW
181                                       + g * OC / NG * OH * OW
182                                       + oc * OH * OW
183                                       + oh * OW
184                                       + ow;
185                         if (p.pad_value == 0.0f)
186                             dst_data[dst_off] =(data_t_dst)(IC*ks - 2*a);
187                         else
188                             dst_data[dst_off] = (data_t_dst)(IC*KH*KW - 2*a);
189                     }
190                 }
191             }
192         }
193     }
194 }
195
196 class binary_convolution_test : public ::testing::TestWithParam<TestParams>
197 {
198     void SetUp()
199     {
200         std::cout << GetParam() << std::endl;
201         ASSERT_TRUE(GetParam().isConsistent());
202     }
203 };
204
205 TEST_P(binary_convolution_test, conv)
206 {
207     const auto& engine = get_test_engine();
208     cldnn::build_options options;
209     options.set_option(cldnn::build_option::optimize_data(true));
210     topology topology_bin;
211
212     std::string weights_suffix = "_w_";
213
214     std::string input_name = "input";
215     std::string output_name = "conv";
216
217     TestParams p = GetParam();
218
219     cldnn::tensor stride = cldnn::tensor{cldnn::batch(1), cldnn::feature(1), cldnn::spatial(p.sw, p.sh)};
220     cldnn::tensor pad = cldnn::tensor{cldnn::batch(0), cldnn::feature(0), cldnn::spatial(-p.pw, -p.ph)};
221     cldnn::tensor dilation = {1,1,1,1};
222
223     cldnn::tensor is_size{ cldnn::batch(p.b),
224                            cldnn::feature(p.ic),
225                            cldnn::spatial(p.iw, p.ih) };
226     cldnn::tensor wei_size{ cldnn::batch(p.oc),
227                             cldnn::feature(p.ic),
228                             cldnn::spatial(p.kw, p.kh) };
229     cldnn::tensor os_size{ cldnn::batch(p.b),
230                             cldnn::feature(p.oc),
231                             cldnn::spatial(p.ow, p.oh)};
232
233     auto input       = memory::allocate(engine, { cldnn::data_types::bin, cldnn::format::b_fs_yx_32fp, is_size });
234     auto weights     = memory::allocate(engine, { cldnn::data_types::bin, cldnn::format::bfyx, wei_size });
235     auto output_ref  = memory::allocate(engine, { cldnn::data_types::f32, cldnn::format::bfyx, os_size });
236
237     fill(input);
238     fill(weights);
239
240     compute_ref_conv_bin<uint32_t, uint32_t, int32_t, float>(input, weights, output_ref, p);
241
242 //    print_bin_blob(input,"input");
243 //    print_bin_blob_packed(input,"input");
244 //    print_bin_blob(weights, "weights");
245 //    print_blob(output_ref, "ref_out");
246
247     topology_bin.add(input_layout(input_name, input.get_layout()));
248     topology_bin.add(data(output_name + weights_suffix, weights));
249
250     topology_bin.add(binary_convolution(output_name, input_name, {output_name + weights_suffix},
251                                         stride, pad, dilation, os_size, 1, p.pad_value, p.dt));
252
253     network network_bin(engine, topology_bin, options);
254     network_bin.set_input_data(input_name, input);
255
256     std::map<primitive_id, network_output> outputs = network_bin.execute();
257     auto outputMemory = outputs.at(output_name).get_memory();
258
259     for (size_t i = 0; i < output_ref.count(); i++) {
260         if (p.dt == data_types::f32)
261         {
262             auto ref = output_ref.pointer<float>();
263             auto opt = outputMemory.pointer<float>();
264
265             ASSERT_EQ(ref[i], opt[i]) << i;
266         }
267         else if (p.dt == data_types::f16)
268         {
269             auto ref = output_ref.pointer<float>();
270             auto opt = outputMemory.pointer<uint16_t>();
271
272             ASSERT_EQ(ref[i], float16_to_float32(opt[i])) << i;
273         }
274     }
275 }
276
277 // Batch, groups, IC, IW, IH, OC, OW, OH, KH, KW, SH, SW, PH, PW
278 INSTANTIATE_TEST_CASE_P(BinaryConvTest, binary_convolution_test, ::testing::Values(
279         TestParams{1, 1,  16,2,2,   4,2,2, 3,3, 1,1, 1,1, -1.0f, data_types::f32, "small"},
280         TestParams{1, 1,  17,2,2,   4,2,2, 3,3, 1,1, 1,1, -1.0f, data_types::f32, "small"},
281         TestParams{1, 1,  17,2,2,   4,2,2, 3,3, 1,1, 1,1,  0.0f, data_types::f32, "small"},
282         TestParams{1, 1,  17,2,2,   4,2,2, 3,3, 1,1, 1,1,  1.0f, data_types::f32, "small"},
283         TestParams{1, 1,  16,2,2,  16,2,2, 3,3, 1,1, 1,1,  1.0f, data_types::f32, "small"},
284         TestParams{1, 1,  32,2,2,  32,2,2, 3,3, 1,1, 1,1,  1.0f, data_types::f32, "small"},
285         TestParams{1, 1,  32,2,2,  32,2,2, 1,1, 1,1, 0,0,  1.0f, data_types::f32, "small"},
286         TestParams{1, 1, 128,2,2, 128,2,2, 1,1, 1,1, 0,0, -1.0f, data_types::f32, "small"},
287         TestParams{1, 1,  16,4,3,   4,4,3, 1,1, 1,1, 0,0, -1.0f, data_types::f32, "small"},
288         TestParams{1, 1,  16,2,2,   4,2,2, 3,3, 1,1, 1,1, -1.0f, data_types::f16, "small"},
289         TestParams{1, 1,  17,2,2,   4,2,2, 3,3, 1,1, 1,1, -1.0f, data_types::f16, "small"},
290         TestParams{1, 1,  17,2,2,   4,2,2, 3,3, 1,1, 1,1,  0.0f, data_types::f16, "small"},
291         TestParams{1, 1,  17,2,2,   4,2,2, 3,3, 1,1, 1,1,  1.0f, data_types::f16, "small"},
292         TestParams{1, 1,  16,2,2,  16,2,2, 3,3, 1,1, 1,1,  1.0f, data_types::f16, "small"},
293         TestParams{1, 1,  32,2,2,  32,2,2, 3,3, 1,1, 1,1,  1.0f, data_types::f16, "small"},
294         TestParams{1, 1,  32,2,2,  32,2,2, 1,1, 1,1, 0,0,  1.0f, data_types::f16, "small"},
295         TestParams{1, 1, 128,2,2, 128,2,2, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "small"},
296         TestParams{1, 1,  16,4,3,   4,4,3, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "small"},
297         TestParams{1, 1,  9,16,32, 17,8,16, 7,7, 2,2, 3,3, -1.0f, data_types::f16, "small"},
298         TestParams{1, 1,  9,16,32, 17,8,16, 7,7, 2,2, 3,3, 1.0f, data_types::f16, "small"},
299
300         // Resnet-18 3x3
301         TestParams{1, 1,  64,56,56,  64,56,56, 3,3, 1,1, 1,1, -1.0f, data_types::f16, "resnet18_0"},
302         TestParams{1, 1,  64,56,56, 128,28,28, 3,3, 2,2, 1,1, -1.0f, data_types::f16, "resnet18_1"},
303         TestParams{1, 1, 128,28,28, 128,28,28, 3,3, 1,1, 1,1, -1.0f, data_types::f16, "resnet18_2"},
304         TestParams{1, 1, 128,28,28, 256,14,14, 3,3, 2,2, 1,1, -1.0f, data_types::f16, "resnet18_3"},
305         TestParams{1, 1, 256,14,14, 256,14,14, 3,3, 1,1, 1,1, -1.0f, data_types::f16, "resnet18_4"},
306         TestParams{1, 1, 256,14,14, 512, 7, 7, 3,3, 2,2, 1,1, -1.0f, data_types::f16, "resnet18_5"},
307         TestParams{1, 1, 512, 7, 7, 512, 7, 7, 3,3, 1,1, 1,1, -1.0f, data_types::f16, "resnet18_6"},
308         // Resnet-50
309         TestParams{1, 1, 64,56,56, 64,56,56, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "resnet50_0"},
310         TestParams{1, 1, 64,56,56, 256,56,56, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "resnet50_1"},
311         TestParams{1, 1, 256,56,56, 128,28,28, 1,1, 2,2, 0,0, -1.0f, data_types::f16, "resnet50_2"},
312         TestParams{1, 1, 128,28,28, 512,28,28, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "resnet50_3"},
313         TestParams{1, 1, 512,28,28, 128,28,28, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "resnet50_4"},
314         TestParams{1, 1, 512,28,28, 256,14,14, 1,1, 2,2, 0,0, -1.0f, data_types::f16, "resnet50_5"},
315         TestParams{1, 1, 256,14,14, 1024,14,14, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "resnet50_6"},
316         TestParams{1, 1, 1024,14,14, 256,14,14, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "resnet50_7"},
317         TestParams{1, 1, 1024,14,14, 512,7,7, 1,1, 2,2, 0,0, -1.0f, data_types::f16, "resnet50_8"},
318         TestParams{1, 1, 512,7,7, 2048,7,7, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "resnet50_9"},
319         TestParams{1, 1, 2048,7,7, 512,7,7, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "resnet50_10"},
320         // Mobilenet-ssd-vd
321         TestParams{1, 1,  56,96,168, 112,96,168, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv2_2_sep_BIN"}, // back_bone_seq_conv2_2_sep_BIN
322         TestParams{1, 1, 112,96,168, 112,96,168, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv3_1_sep_BIN"}, // back_bone_seq_conv3_1_sep_BIN
323         TestParams{1, 1,  112,48,84, 208,48, 84, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv3_2_sep_BIN"}, // back_bone_seq_conv3_2_sep_BIN
324         TestParams{1, 1,  208,48,84, 216,48, 84, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv4_1_sep_BIN"}, // back_bone_seq_conv4_1_sep_BIN
325         TestParams{1, 1,  216,24,42, 328,24, 42, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv4_2_sep_BIN"}, // back_bone_seq_conv4_2_sep_BIN
326         TestParams{1, 1,  328,24,42, 288,24, 42, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv5_1_sep_BIN"}, // back_bone_seq_conv5_1_sep_BIN
327         TestParams{1, 1,  288,24,42, 288,24, 42, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv5_2_sep_BIN"}, // back_bone_seq_conv5_2_sep_BIN
328         TestParams{1, 1,  288,24,42, 240,24, 42, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv5_3_sep_BIN"}, // back_bone_seq_conv5_3_sep_BIN
329         TestParams{1, 1,  240,24,42, 264,24, 42, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv5_4_sep_BIN"}, // back_bone_seq_conv5_4_sep_BIN
330         TestParams{1, 1,  264,24,42, 192,24, 42, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv5_5_sep_BIN"}, // back_bone_seq_conv5_5_sep_BIN
331         TestParams{1, 1,  192,12,21, 208,12, 21, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv5_6_sep_BIN"}, // back_bone_seq_conv5_6_sep_BIN
332         TestParams{1, 1,  208,12,21,  88,12, 21, 1,1, 1,1, 0,0, -1.0f, data_types::f16, "conv6_sep_BN"} // back_bone_seq_conv6_sep_BN
333 ),);
334
335 template <typename T>
336 static void set_binary_values(const cldnn::memory& mem, std::vector<T> args) {
337     auto ptr = mem.pointer<T>();
338
339     auto it = ptr.begin();
340     for (auto x : args)
341         *it++ = x;
342 }
343
344 TEST(binary_convolution, basic_convolution_1x1_single_packed_channel)
345 {
346     const auto& engine = get_test_engine();
347
348     auto input = memory::allocate(engine, { data_types::bin, format::b_fs_yx_32fp, { 1, 16, 2, 2 } });
349     auto weights = memory::allocate(engine, { data_types::bin, format::bfyx, { 4, 16, 1, 1 } });
350
351     // 0 0 1 0  0 1 0 0  1 0 1 0  1 0 1 0
352     // 1 0 0 0  0 1 1 0  0 1 1 0  1 0 1 0
353     // 1 1 0 0  1 0 1 1  1 1 1 1  1 0 1 0
354     // 0 0 0 0  0 0 0 0  0 0 0 0  0 0 0 1
355     set_binary_values<uint32_t>(input, { 21796, 22113, 24531, 32768 });
356
357     // 1 1 1 1  1 1 1 1  1 1 1 1  1 1 1 1
358     // 0 1 0 1  0 1 0 1  1 0 1 0  1 0 1 0
359     // 1 0 1 0  1 0 1 0  0 1 0 1  0 1 0 1
360     // 0 0 0 0  0 0 0 0  0 0 0 0  0 0 0 0
361     set_binary_values<uint16_t>(weights, { 65535, 21930, 43605, 0 });
362
363     // 16 - 2*popcount(1 1 0 1  1 0 1 1  0 1 0 1  0 1 0 1) = -4
364     // 16 - 2*popcount(0 1 1 1  1 0 0 1  1 0 0 1  0 1 0 1) = -2
365     // 16 - 2*popcount(0 0 1 1  0 1 0 0  0 0 0 0  0 1 0 1) = 6
366     // 16 - 2*popcount(1 1 1 1  1 1 1 1  1 1 1 1  1 1 1 0) = -14
367
368     // 16 - 2*popcount(0 1 1 1  0 0 0 1  0 0 0 0  0 0 0 0) = 8
369     // 16 - 2*popcount(1 1 0 1  0 0 1 1  1 1 0 0  0 0 0 0) = 2
370     // 16 - 2*popcount(1 0 0 1  1 1 1 0  0 1 0 1  0 0 0 0) = 2
371     // 16 - 2*popcount(0 1 0 1  0 1 0 1  1 0 1 0  1 0 1 1) = -2
372
373     // 16 - 2*popcount(1 0 0 0  1 1 1 0  1 1 1 1  1 1 1 1) = -8
374     // 16 - 2*popcount(0 0 1 0  1 1 0 0  0 0 1 1  1 1 1 1) = -2
375     // 16 - 2*popcount(0 1 1 0  0 0 0 1  1 0 1 0  1 1 1 1) = -2
376     // 16 - 2*popcount(1 0 1 0  1 0 1 0  0 1 0 1  0 1 0 0) = 2
377
378     // 16 - 2*popcount(0 0 1 0  0 1 0 0  1 0 1 0  1 0 1 0) = 4
379     // 16 - 2*popcount(1 0 0 0  0 1 1 0  0 1 1 0  1 0 1 0) = 2
380     // 16 - 2*popcount(1 1 0 0  1 0 1 1  1 1 1 1  1 0 1 0) = -6
381     // 16 - 2*popcount(0 0 0 0  0 0 0 0  0 0 0 0  0 0 0 1) = 14
382     VF<float> output_vec = {
383             -4.0f, -2.0f,  6.0f, -14.0f,
384              8.0f,  2.0f,  2.0f,  -2.0f,
385             -8.0f, -2.0f, -2.0f,   2.0f,
386              4.0f,  2.0f, -6.0f,  14.0f };
387
388     topology topology(
389             input_layout("input", input.get_layout()),
390             data("weights", weights),
391             binary_convolution("binary_conv", "input", { "weights" },
392                                { 1,1,1,1 },
393                                { 0,0,0,0 },
394                                { 1,1,1,1 },
395                                { 1,4,2,2 },
396                                0, 0.0f,
397                                data_types::f32,
398                                padding{ { 0,0,0,0 }, 0 })
399     );
400
401     cldnn::build_options options;
402     options.set_option(cldnn::build_option::optimize_data(true));
403
404     network network(engine, topology, options);
405     network.set_input_data("input", input);
406
407     auto outputs = network.execute();
408     EXPECT_EQ(outputs.size(), size_t(1));
409     EXPECT_EQ(outputs.begin()->first, "binary_conv");
410
411     auto output_memory = outputs.at("binary_conv").get_memory();
412     auto output_layout = output_memory.get_layout();
413     auto output_ptr = output_memory.pointer<float>();
414
415     EXPECT_EQ(output_layout.format, format::bfyx);
416     EXPECT_EQ(output_layout.data_type, data_types::f32);
417     EXPECT_EQ(output_layout.size.batch[0], 1);
418     EXPECT_EQ(output_layout.size.feature[0], 4);
419     EXPECT_EQ(output_layout.size.spatial[1], 2);
420     EXPECT_EQ(output_layout.size.spatial[0], 2);
421
422     for (size_t i = 0; i < output_layout.count(); i++)
423     {
424         EXPECT_EQ(output_ptr[i], output_vec[i]) << "index="<< i;
425     }
426 }
427
428 TEST(binary_convolution, basic_convolution_1x1_single_packed_channel_fp16) {
429     const auto& engine = get_test_engine();
430
431     auto input = memory::allocate(engine, { data_types::bin, format::b_fs_yx_32fp, { 1, 16, 2, 2 } });
432     auto weights = memory::allocate(engine, { data_types::bin, format::bfyx, { 4, 16, 1, 1 } });
433
434     // 0 0 1 0  0 1 0 0  1 0 1 0  1 0 1 0
435     // 1 0 0 0  0 1 1 0  0 1 1 0  1 0 1 0
436     // 1 1 0 0  1 0 1 1  1 1 1 1  1 0 1 0
437     // 0 0 0 0  0 0 0 0  0 0 0 0  0 0 0 1
438     set_binary_values<uint32_t>(input, { 21796, 22113, 24531, 32768 });
439
440     // 1 1 1 1  1 1 1 1  1 1 1 1  1 1 1 1
441     // 0 1 0 1  0 1 0 1  1 0 1 0  1 0 1 0
442     // 1 0 1 0  1 0 1 0  0 1 0 1  0 1 0 1
443     // 0 0 0 0  0 0 0 0  0 0 0 0  0 0 0 0
444     set_binary_values<uint16_t>(weights, { 65535, 21930, 43605, 0 });
445
446     // 16 - 2*popcount(1 1 0 1  1 0 1 1  0 1 0 1  0 1 0 1) = -4
447     // 16 - 2*popcount(0 1 1 1  1 0 0 1  1 0 0 1  0 1 0 1) = -2
448     // 16 - 2*popcount(0 0 1 1  0 1 0 0  0 0 0 0  0 1 0 1) = 6
449     // 16 - 2*popcount(1 1 1 1  1 1 1 1  1 1 1 1  1 1 1 0) = -14
450
451     // 16 - 2*popcount(0 1 1 1  0 0 0 1  0 0 0 0  0 0 0 0) = 8
452     // 16 - 2*popcount(1 1 0 1  0 0 1 1  1 1 0 0  0 0 0 0) = 2
453     // 16 - 2*popcount(1 0 0 1  1 1 1 0  0 1 0 1  0 0 0 0) = 2
454     // 16 - 2*popcount(0 1 0 1  0 1 0 1  1 0 1 0  1 0 1 1) = -2
455
456     // 16 - 2*popcount(1 0 0 0  1 1 1 0  1 1 1 1  1 1 1 1) = -8
457     // 16 - 2*popcount(0 0 1 0  1 1 0 0  0 0 1 1  1 1 1 1) = -2
458     // 16 - 2*popcount(0 1 1 0  0 0 0 1  1 0 1 0  1 1 1 1) = -2
459     // 16 - 2*popcount(1 0 1 0  1 0 1 0  0 1 0 1  0 1 0 0) = 2
460
461     // 16 - 2*popcount(0 0 1 0  0 1 0 0  1 0 1 0  1 0 1 0) = 4
462     // 16 - 2*popcount(1 0 0 0  0 1 1 0  0 1 1 0  1 0 1 0) = 2
463     // 16 - 2*popcount(1 1 0 0  1 0 1 1  1 1 1 1  1 0 1 0) = -6
464     // 16 - 2*popcount(0 0 0 0  0 0 0 0  0 0 0 0  0 0 0 1) = 14
465     VF<float> output_vec = {
466             -4.0f, -2.0f,  6.0f, -14.0f,
467              8.0f,  2.0f,  2.0f,  -2.0f,
468             -8.0f, -2.0f, -2.0f,   2.0f,
469              4.0f,  2.0f, -6.0f,  14.0f };
470
471     topology topology(
472             input_layout("input", input.get_layout()),
473             data("weights", weights),
474             binary_convolution("binary_conv", "input", { "weights" },
475                                { 1,1,1,1 },
476                                { 0,0,0,0 },
477                                { 1,1,1,1 },
478                                { 1,4,2,2 },
479                                0, 0.0f,
480                                data_types::f16,
481                                padding{ { 0,0,0,0 }, 0 })
482     );
483
484     cldnn::build_options options;
485     options.set_option(cldnn::build_option::optimize_data(true));
486
487     network network(engine, topology, options);
488     network.set_input_data("input", input);
489
490     auto outputs = network.execute();
491     EXPECT_EQ(outputs.size(), size_t(1));
492     EXPECT_EQ(outputs.begin()->first, "binary_conv");
493
494     auto output_memory = outputs.at("binary_conv").get_memory();
495     auto output_layout = output_memory.get_layout();
496     auto output_ptr = output_memory.pointer<uint16_t>();
497
498     EXPECT_EQ(output_layout.format, format::bfyx);
499     EXPECT_EQ(output_layout.data_type, data_types::f16);
500     EXPECT_EQ(output_layout.size.batch[0], 1);
501     EXPECT_EQ(output_layout.size.feature[0], 4);
502     EXPECT_EQ(output_layout.size.spatial[1], 2);
503     EXPECT_EQ(output_layout.size.spatial[0], 2);
504
505     for (size_t i = 0; i < output_layout.count(); i++)
506     {
507         EXPECT_EQ(float16_to_float32(output_ptr[i]), output_vec[i]) << "index="<< i;
508     }
509 }
510