Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / EltwiseDiv.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 x = 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 y = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
42                 shape=[1, 3, 3, 2], dtype=tf.float32)
43 out = tf.div(x, y)
44
45 with tf.Session() as sess:
46     print(sess.run(out))
47 */
48 TEST(NodeExecution_EltwiseDiv, f32)
49 {
50   float x_val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
51   float y_val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
52   float out_val[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
53
54   // make EltwiseDiv(Pull, Pull)
55   auto g = loco::make_graph();
56   Shape input_shape{1, 3, 3, 2}; // NHWC
57
58   auto inp_lhs = g->nodes()->create<loco::Pull>();
59   {
60     inp_lhs->dtype(loco::DataType::FLOAT32);
61     inp_lhs->shape({1, 3, 3, 2});
62   }
63
64   auto inp_rhs = g->nodes()->create<loco::Pull>();
65   {
66     inp_rhs->dtype(loco::DataType::FLOAT32);
67     inp_rhs->shape({1, 3, 3, 2});
68   }
69
70   auto eltwise_div = g->nodes()->create<loco::EltwiseDiv>();
71   {
72     eltwise_div->lhs(inp_lhs);
73     eltwise_div->rhs(inp_rhs);
74   }
75
76   // Make and assign data to two pull nodes
77   auto inp_lhs_buf = make_buffer<float, LexicalLayout>(input_shape);
78   {
79     int n = 0;
80     for (IndexEnumerator e{inp_lhs_buf.shape()}; e.valid(); e.advance())
81     {
82       inp_lhs_buf.at(e.current()) = x_val[n++];
83     }
84   }
85
86   auto inp_rhs_buf = make_buffer<float, LexicalLayout>(input_shape);
87   {
88     int n = 0;
89     for (IndexEnumerator e{inp_rhs_buf.shape()}; e.valid(); e.advance())
90     {
91       inp_rhs_buf.at(e.current()) = y_val[n++];
92     }
93   }
94
95   auto inp_lhs_data = locomotiv::make_data(inp_lhs_buf);
96   locomotiv::annot_data(inp_lhs, std::move(inp_lhs_data));
97   locomotiv::annot_domain(inp_lhs, loco::Domain::Tensor);
98
99   auto inp_rhs_data = locomotiv::make_data(inp_rhs_buf);
100   locomotiv::annot_data(inp_rhs, std::move(inp_rhs_data));
101   locomotiv::annot_domain(inp_rhs, loco::Domain::Tensor);
102
103   // run the network
104   locomotiv::NodeExecution::get().run(eltwise_div);
105
106   // get result
107   auto eltwise_div_data = locomotiv::annot_data(eltwise_div);
108
109   // comparing the result
110   ASSERT_NE(eltwise_div_data, nullptr);
111   ASSERT_EQ(loco::DataType::FLOAT32, eltwise_div_data->dtype());
112   ASSERT_EQ(Shape({1, 3, 3, 2}), *(eltwise_div_data->shape()));
113
114   uint32_t n = 0;
115   for (IndexEnumerator e{*(eltwise_div_data->shape())}; e.valid(); e.advance())
116   {
117     ASSERT_FLOAT_EQ(out_val[n++], eltwise_div_data->as_f32_bufptr()->at(e.current()));
118   }
119
120   ASSERT_EQ(loco::Domain::Tensor, locomotiv::annot_domain(eltwise_div));
121 }