Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / BiasAdd.test.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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 #include "NodeExecution.h"
18
19 #include "locomotiv/NodeData.h"
20 #include "NodeDataImpl.h"
21 #include "NodeDomain.h"
22
23 #include <nncc/core/ADT/tensor/Shape.h>
24 #include <nncc/core/ADT/tensor/Buffer.h>
25 #include <nncc/core/ADT/tensor/LexicalLayout.h>
26 #include <nncc/core/ADT/tensor/Index.h>
27 #include <nncc/core/ADT/tensor/IndexEnumerator.h>
28
29 #include <gtest/gtest.h>
30
31 using nncc::core::ADT::tensor::Shape;
32 using nncc::core::ADT::tensor::LexicalLayout;
33 using nncc::core::ADT::tensor::make_buffer;
34 using nncc::core::ADT::tensor::IndexEnumerator;
35
36 /*
37 test case generated from the following:
38
39   inp = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
40                     shape=[1, 3, 3, 2], dtype=tf.float32)
41   bias = tf.constant([1.1, 2.1], shape=[2], dtype=tf.float32)
42   out = tf.nn.bias_add(inp, bias)
43
44   with tf.Session() as sess:
45       print(sess.run(out))
46  */
47
48 TEST(NodeExecution_TensorBiasAdd, f32)
49 {
50   float in_val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
51   float bias_val[] = {1.1, 2.1};
52   float out_val[] = {2.1,  4.1,  4.1,  6.1,  6.1,  8.1,  8.1,  10.1, 10.1,
53                      12.1, 12.1, 14.1, 14.1, 16.1, 16.1, 18.1, 18.1, 20.1};
54
55   // make BiasAdd(Pull, Const)
56   auto g = loco::make_graph();
57   Shape input_shape{1, 3, 3, 2}; // NHWC
58
59   auto inp = g->nodes()->create<loco::Pull>();
60   {
61     inp->dtype(loco::DataType::FLOAT32);
62     inp->shape({1, 3, 3, 2});
63   }
64
65   auto bias = g->nodes()->create<loco::BiasEncode>();
66   {
67     // nothing to do
68   }
69
70   auto bias_add = g->nodes()->create<loco::BiasAdd<loco::Domain::Tensor>>();
71   {
72     bias_add->value(inp);
73     bias_add->bias(bias);
74     bias_add->axis(3); // axis(3) means C in NHWC
75   }
76
77   // Make and assign data to pull node
78   auto inp_buf = make_buffer<float, LexicalLayout>(input_shape);
79   {
80     int n = 0;
81     for (IndexEnumerator e{inp_buf.shape()}; e.valid(); e.advance())
82     {
83       inp_buf.at(e.current()) = in_val[n++];
84     }
85   }
86
87   auto bias_buf = make_buffer<float, LexicalLayout>(Shape{2});
88   {
89     int n = 0;
90     for (IndexEnumerator e{bias_buf.shape()}; e.valid(); e.advance())
91     {
92       bias_buf.at(e.current()) = bias_val[n++];
93     }
94   }
95
96   auto inp_data = locomotiv::make_data(inp_buf);
97   locomotiv::annot_data(inp, std::move(inp_data));
98   locomotiv::annot_domain(inp, loco::Domain::Tensor);
99
100   auto bias_data = locomotiv::make_data(bias_buf);
101   locomotiv::annot_data(bias, std::move(bias_data));
102   locomotiv::annot_domain(bias, loco::Domain::Bias);
103
104   locomotiv::NodeExecution::get().run(bias_add);
105
106   auto bias_add_data = locomotiv::annot_data(bias_add);
107
108   // comparing the result
109   ASSERT_NE(bias_add_data, nullptr);
110   ASSERT_EQ(loco::DataType::FLOAT32, bias_add_data->dtype());
111   ASSERT_EQ(Shape({1, 3, 3, 2}), *(bias_add_data->shape()));
112
113   uint32_t n = 0;
114   for (IndexEnumerator e{*(bias_add_data->shape())}; e.valid(); e.advance())
115   {
116     ASSERT_FLOAT_EQ(out_val[n++], bias_add_data->as_f32_bufptr()->at(e.current()));
117   }
118
119   ASSERT_EQ(loco::Domain::Tensor, locomotiv::annot_domain(bias_add));
120 }
121
122 /*
123 test case generated from the following:
124
125   inp = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
126                     shape=[1, 3, 3, 2], dtype=tf.float32)
127   bias = tf.constant([1.1, 2.1], shape=[2], dtype=tf.float32)
128   out = tf.nn.bias_add(inp, bias)
129
130   with tf.Session() as sess:
131       print(sess.run(out))
132  */
133
134 TEST(NodeExecution_FeatureBiasAdd, f32)
135 {
136   float in_val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
137   float bias_val[] = {1.1, 2.1};
138   float out_val[] = {2.1,  4.1,  4.1,  6.1,  6.1,  8.1,  8.1,  10.1, 10.1,
139                      12.1, 12.1, 14.1, 14.1, 16.1, 16.1, 18.1, 18.1, 20.1};
140
141   // make FeatureBiasAdd(FeatureEncode, BiasEncode)
142   auto g = loco::make_graph();
143   Shape input_shape{1, 3, 3, 2}; // NHWC
144
145   auto feature_encode = g->nodes()->create<loco::FeatureEncode>();
146   {
147     // setting values is ignored for testing
148   }
149
150   auto bias = g->nodes()->create<loco::BiasEncode>();
151   {
152     // nothing to do
153   }
154
155   auto feature_bias_add = g->nodes()->create<loco::BiasAdd<loco::Domain::Feature>>();
156   {
157     feature_bias_add->value(feature_encode);
158     feature_bias_add->bias(bias);
159   }
160
161   // Make and assign data to pull node
162   auto inp_buf = make_buffer<float, LexicalLayout>(input_shape);
163   {
164     int n = 0;
165     for (IndexEnumerator e{inp_buf.shape()}; e.valid(); e.advance())
166     {
167       inp_buf.at(e.current()) = in_val[n++];
168     }
169   }
170
171   auto bias_buf = make_buffer<float, LexicalLayout>(Shape{2});
172   {
173     int n = 0;
174     for (IndexEnumerator e{bias_buf.shape()}; e.valid(); e.advance())
175     {
176       bias_buf.at(e.current()) = bias_val[n++];
177     }
178   }
179
180   auto inp_data = locomotiv::make_data(inp_buf);
181   locomotiv::annot_data(feature_encode, std::move(inp_data));
182   locomotiv::annot_domain(feature_encode, loco::Domain::Feature);
183
184   auto bias_data = locomotiv::make_data(bias_buf);
185   locomotiv::annot_data(bias, std::move(bias_data));
186   locomotiv::annot_domain(bias, loco::Domain::Bias);
187
188   locomotiv::NodeExecution::get().run(feature_bias_add);
189
190   auto bias_add_data = locomotiv::annot_data(feature_bias_add);
191
192   // comparing the result
193   ASSERT_NE(bias_add_data, nullptr);
194   ASSERT_EQ(loco::DataType::FLOAT32, bias_add_data->dtype());
195   ASSERT_EQ(Shape({1, 3, 3, 2}), *(bias_add_data->shape()));
196
197   uint32_t n = 0;
198   for (IndexEnumerator e{*(bias_add_data->shape())}; e.valid(); e.advance())
199   {
200     ASSERT_FLOAT_EQ(out_val[n++], bias_add_data->as_f32_bufptr()->at(e.current()));
201   }
202
203   ASSERT_EQ(loco::Domain::Feature, locomotiv::annot_domain(feature_bias_add));
204 }