Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / TensorReduce.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/Index.h>
24 #include <nncc/core/ADT/tensor/Shape.h>
25 #include <nncc/core/ADT/tensor/Buffer.h>
26 #include <nncc/core/ADT/tensor/LexicalLayout.h>
27
28 #include <gtest/gtest.h>
29
30 using nncc::core::ADT::tensor::Index;
31 using nncc::core::ADT::tensor::Shape;
32 using nncc::core::ADT::tensor::LexicalLayout;
33 using nncc::core::ADT::tensor::make_buffer;
34
35 TEST(NodeExecution_Fixed_Reduce_Mean, f32_0)
36 {
37   // Make pull-TensorReduce(Mean) graph
38   auto g = loco::make_graph();
39   auto pull_input = g->nodes()->create<loco::Pull>();
40   pull_input->dtype(loco::DataType::FLOAT32);
41   pull_input->shape({1, 2, 2});
42   auto reduce_node = g->nodes()->create<loco::TensorReduce>();
43   reduce_node->input(pull_input);
44   reduce_node->axes()->insert(0);
45   reduce_node->axes()->insert(1);
46   reduce_node->func(loco::ReduceFunc::Mean);
47
48   // Make and assign data to pull node
49   auto pull_input_buf = make_buffer<float, LexicalLayout>({1, 2, 2});
50   pull_input_buf.at(Index{0, 0, 0}) = 1.1f;
51   pull_input_buf.at(Index{0, 0, 1}) = 2.2f;
52   pull_input_buf.at(Index{0, 1, 0}) = 5.5f;
53   pull_input_buf.at(Index{0, 1, 1}) = 6.6f;
54   auto pull_input_data = locomotiv::make_data(pull_input_buf);
55   locomotiv::annot_data(pull_input, std::move(pull_input_data));
56   locomotiv::annot_domain(pull_input, loco::Domain::Tensor);
57
58   locomotiv::NodeExecution::get().run(reduce_node);
59
60   auto kShape = Shape{1, 1, 2};
61   auto reduce_data = locomotiv::annot_data(reduce_node);
62   ASSERT_NE(reduce_data, nullptr);
63   ASSERT_EQ(loco::DataType::FLOAT32, reduce_data->dtype());
64   ASSERT_EQ(kShape, *(reduce_data->shape()));
65   ASSERT_FLOAT_EQ(3.3f, reduce_data->as_f32_bufptr()->at(Index{0, 0, 0}));
66   ASSERT_FLOAT_EQ(4.4f, reduce_data->as_f32_bufptr()->at(Index{0, 0, 1}));
67
68   ASSERT_EQ(loco::Domain::Tensor, locomotiv::annot_domain(reduce_node));
69 }
70
71 TEST(NodeExecution_Fixed_Reduce_Mean, f32_1)
72 {
73   // Make pull-TensorReduce(Mean) graph
74   auto g = loco::make_graph();
75   auto pull_input = g->nodes()->create<loco::Pull>();
76   pull_input->dtype(loco::DataType::FLOAT32);
77   pull_input->shape({1, 2, 2});
78   auto reduce_node = g->nodes()->create<loco::TensorReduce>();
79   reduce_node->input(pull_input);
80   reduce_node->axes()->insert(1);
81   reduce_node->axes()->insert(2);
82   reduce_node->func(loco::ReduceFunc::Mean);
83
84   // Make and assign data to pull node
85   auto pull_input_buf = make_buffer<float, LexicalLayout>({1, 2, 2});
86   pull_input_buf.at(Index{0, 0, 0}) = 1.1f;
87   pull_input_buf.at(Index{0, 0, 1}) = 2.2f;
88   pull_input_buf.at(Index{0, 1, 0}) = 5.5f;
89   pull_input_buf.at(Index{0, 1, 1}) = 6.6f;
90   auto pull_input_data = locomotiv::make_data(pull_input_buf);
91   locomotiv::annot_data(pull_input, std::move(pull_input_data));
92   locomotiv::annot_domain(pull_input, loco::Domain::Tensor);
93
94   locomotiv::NodeExecution::get().run(reduce_node);
95
96   auto kShape = Shape{1, 1, 1};
97   auto reduce_data = locomotiv::annot_data(reduce_node);
98   ASSERT_NE(reduce_data, nullptr);
99   ASSERT_EQ(loco::DataType::FLOAT32, reduce_data->dtype());
100   ASSERT_EQ(kShape, *(reduce_data->shape()));
101   ASSERT_FLOAT_EQ(3.85f, reduce_data->as_f32_bufptr()->at(Index{0, 0, 0}));
102
103   ASSERT_EQ(loco::Domain::Tensor, locomotiv::annot_domain(reduce_node));
104 }