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 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)
45 with tf.Session() as sess:
48 TEST(NodeExecution_EltwiseDiv, f32)
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};
54 // make EltwiseDiv(Pull, Pull)
55 auto g = loco::make_graph();
56 Shape input_shape{1, 3, 3, 2}; // NHWC
58 auto inp_lhs = g->nodes()->create<loco::Pull>();
60 inp_lhs->dtype(loco::DataType::FLOAT32);
61 inp_lhs->shape({1, 3, 3, 2});
64 auto inp_rhs = g->nodes()->create<loco::Pull>();
66 inp_rhs->dtype(loco::DataType::FLOAT32);
67 inp_rhs->shape({1, 3, 3, 2});
70 auto eltwise_div = g->nodes()->create<loco::EltwiseDiv>();
72 eltwise_div->lhs(inp_lhs);
73 eltwise_div->rhs(inp_rhs);
76 // Make and assign data to two pull nodes
77 auto inp_lhs_buf = make_buffer<float, LexicalLayout>(input_shape);
80 for (IndexEnumerator e{inp_lhs_buf.shape()}; e.valid(); e.advance())
82 inp_lhs_buf.at(e.current()) = x_val[n++];
86 auto inp_rhs_buf = make_buffer<float, LexicalLayout>(input_shape);
89 for (IndexEnumerator e{inp_rhs_buf.shape()}; e.valid(); e.advance())
91 inp_rhs_buf.at(e.current()) = y_val[n++];
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);
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);
104 locomotiv::NodeExecution::get().run(eltwise_div);
107 auto eltwise_div_data = locomotiv::annot_data(eltwise_div);
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()));
115 for (IndexEnumerator e{*(eltwise_div_data->shape())}; e.valid(); e.advance())
117 ASSERT_FLOAT_EQ(out_val[n++], eltwise_div_data->as_f32_bufptr()->at(e.current()));
120 ASSERT_EQ(loco::Domain::Tensor, locomotiv::annot_domain(eltwise_div));