2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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 #include "NodeExecution.h"
19 #include "locomotiv/NodeData.h"
20 #include "NodeDataImpl.h"
21 #include "NodeDomain.h"
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>
29 #include <gtest/gtest.h>
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;
37 test case generated from the following:
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)
44 with tf.Session() as sess:
48 TEST(NodeExecution_TensorBiasAdd, f32)
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};
55 // make BiasAdd(Pull, Const)
56 auto g = loco::make_graph();
57 Shape input_shape{1, 3, 3, 2}; // NHWC
59 auto inp = g->nodes()->create<loco::Pull>();
61 inp->dtype(loco::DataType::FLOAT32);
62 inp->shape({1, 3, 3, 2});
65 auto bias = g->nodes()->create<loco::BiasEncode>();
70 auto bias_add = g->nodes()->create<loco::BiasAdd<loco::Domain::Tensor>>();
74 bias_add->axis(3); // axis(3) means C in NHWC
77 // Make and assign data to pull node
78 auto inp_buf = make_buffer<float, LexicalLayout>(input_shape);
81 for (IndexEnumerator e{inp_buf.shape()}; e.valid(); e.advance())
83 inp_buf.at(e.current()) = in_val[n++];
87 auto bias_buf = make_buffer<float, LexicalLayout>(Shape{2});
90 for (IndexEnumerator e{bias_buf.shape()}; e.valid(); e.advance())
92 bias_buf.at(e.current()) = bias_val[n++];
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);
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);
104 locomotiv::NodeExecution::get().run(bias_add);
106 auto bias_add_data = locomotiv::annot_data(bias_add);
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()));
114 for (IndexEnumerator e{*(bias_add_data->shape())}; e.valid(); e.advance())
116 ASSERT_FLOAT_EQ(out_val[n++], bias_add_data->as_f32_bufptr()->at(e.current()));
119 ASSERT_EQ(loco::Domain::Tensor, locomotiv::annot_domain(bias_add));
123 test case generated from the following:
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)
130 with tf.Session() as sess:
134 TEST(NodeExecution_FeatureBiasAdd, f32)
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};
141 // make FeatureBiasAdd(FeatureEncode, BiasEncode)
142 auto g = loco::make_graph();
143 Shape input_shape{1, 3, 3, 2}; // NHWC
145 auto feature_encode = g->nodes()->create<loco::FeatureEncode>();
147 // setting values is ignored for testing
150 auto bias = g->nodes()->create<loco::BiasEncode>();
155 auto feature_bias_add = g->nodes()->create<loco::BiasAdd<loco::Domain::Feature>>();
157 feature_bias_add->value(feature_encode);
158 feature_bias_add->bias(bias);
161 // Make and assign data to pull node
162 auto inp_buf = make_buffer<float, LexicalLayout>(input_shape);
165 for (IndexEnumerator e{inp_buf.shape()}; e.valid(); e.advance())
167 inp_buf.at(e.current()) = in_val[n++];
171 auto bias_buf = make_buffer<float, LexicalLayout>(Shape{2});
174 for (IndexEnumerator e{bias_buf.shape()}; e.valid(); e.advance())
176 bias_buf.at(e.current()) = bias_val[n++];
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);
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);
188 locomotiv::NodeExecution::get().run(feature_bias_add);
190 auto bias_add_data = locomotiv::annot_data(feature_bias_add);
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()));
198 for (IndexEnumerator e{*(bias_add_data->shape())}; e.valid(); e.advance())
200 ASSERT_FLOAT_EQ(out_val[n++], bias_add_data->as_f32_bufptr()->at(e.current()));
203 ASSERT_EQ(loco::Domain::Feature, locomotiv::annot_domain(feature_bias_add));