Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / TensorConcat.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
27 #include <gtest/gtest.h>
28
29 using nncc::core::ADT::tensor::Index;
30 using nncc::core::ADT::tensor::Shape;
31 using nncc::core::ADT::tensor::LexicalLayout;
32 using nncc::core::ADT::tensor::make_buffer;
33
34 TEST(NodeExecution_TensorConcat, f32)
35 {
36   // Make (pull, pull)-concat graph
37   auto g = loco::make_graph();
38   auto pull_l = g->nodes()->create<loco::Pull>();
39   pull_l->dtype(loco::DataType::FLOAT32);
40   pull_l->shape({1, 2});
41   auto pull_r = g->nodes()->create<loco::Pull>();
42   pull_r->dtype(loco::DataType::FLOAT32);
43   pull_r->shape({1, 2});
44   auto tconcat = g->nodes()->create<loco::TensorConcat>();
45   tconcat->lhs(pull_l);
46   tconcat->rhs(pull_r);
47   tconcat->axis(0);
48
49   // Make and assign data to pull node
50   auto pull_l_buf = make_buffer<float, LexicalLayout>(Shape{1, 2});
51   pull_l_buf.at(Index{0, 0}) = -1.0f;
52   pull_l_buf.at(Index{0, 1}) = -2.0f;
53   auto pull_r_buf = make_buffer<float, LexicalLayout>(Shape{1, 2});
54   pull_r_buf.at(Index{0, 0}) = 3.0f;
55   pull_r_buf.at(Index{0, 1}) = 4.0f;
56
57   auto pull_l_data = locomotiv::make_data(pull_l_buf);
58   locomotiv::annot_data(pull_l, std::move(pull_l_data));
59   locomotiv::annot_domain(pull_l, loco::Domain::Tensor);
60   auto pull_r_data = locomotiv::make_data(pull_r_buf);
61   locomotiv::annot_data(pull_r, std::move(pull_r_data));
62   locomotiv::annot_domain(pull_r, loco::Domain::Tensor);
63
64   locomotiv::NodeExecution::get().run(tconcat);
65
66   auto concat_data = locomotiv::annot_data(tconcat);
67   ASSERT_NE(concat_data, nullptr);
68   ASSERT_EQ(loco::DataType::FLOAT32, concat_data->dtype());
69   ASSERT_EQ((Shape{2, 2}), (*(concat_data->shape())));
70   ASSERT_FLOAT_EQ(-1.0f, concat_data->as_f32_bufptr()->at(Index{0, 0}));
71   ASSERT_FLOAT_EQ(-2.0f, concat_data->as_f32_bufptr()->at(Index{0, 1}));
72   ASSERT_FLOAT_EQ(3.0f, concat_data->as_f32_bufptr()->at(Index{1, 0}));
73   ASSERT_FLOAT_EQ(4.0f, concat_data->as_f32_bufptr()->at(Index{1, 1}));
74
75   ASSERT_EQ(loco::Domain::Tensor, locomotiv::annot_domain(tconcat));
76 }
77
78 TEST(NodeExecution_TensorConcat, f32_2)
79 {
80   // Make (pull, pull)-concat graph
81   auto g = loco::make_graph();
82   auto pull_l = g->nodes()->create<loco::Pull>();
83   pull_l->dtype(loco::DataType::FLOAT32);
84   pull_l->shape({1, 2});
85   auto pull_r = g->nodes()->create<loco::Pull>();
86   pull_r->dtype(loco::DataType::FLOAT32);
87   pull_r->shape({3, 2});
88   auto tconcat = g->nodes()->create<loco::TensorConcat>();
89   tconcat->lhs(pull_l);
90   tconcat->rhs(pull_r);
91   tconcat->axis(0);
92
93   // Make and assign data to pull node
94   auto pull_l_buf = make_buffer<float, LexicalLayout>(Shape{1, 2});
95   pull_l_buf.at(Index{0, 0}) = -1.0f;
96   pull_l_buf.at(Index{0, 1}) = -2.0f;
97   auto pull_r_buf = make_buffer<float, LexicalLayout>(Shape{3, 2});
98   pull_r_buf.at(Index{0, 0}) = 3.0f;
99   pull_r_buf.at(Index{0, 1}) = 4.0f;
100   pull_r_buf.at(Index{1, 0}) = -3.0f;
101   pull_r_buf.at(Index{1, 1}) = -4.0f;
102   pull_r_buf.at(Index{2, 0}) = 5.0f;
103   pull_r_buf.at(Index{2, 1}) = 6.0f;
104
105   auto pull_l_data = locomotiv::make_data(pull_l_buf);
106   locomotiv::annot_data(pull_l, std::move(pull_l_data));
107   locomotiv::annot_domain(pull_l, loco::Domain::Tensor);
108   auto pull_r_data = locomotiv::make_data(pull_r_buf);
109   locomotiv::annot_data(pull_r, std::move(pull_r_data));
110   locomotiv::annot_domain(pull_r, loco::Domain::Tensor);
111
112   locomotiv::NodeExecution::get().run(tconcat);
113
114   auto concat_data = locomotiv::annot_data(tconcat);
115   ASSERT_NE(concat_data, nullptr);
116   ASSERT_EQ(loco::DataType::FLOAT32, concat_data->dtype());
117   ASSERT_EQ((Shape{4, 2}), (*(concat_data->shape())));
118   ASSERT_FLOAT_EQ(-1.0f, concat_data->as_f32_bufptr()->at(Index{0, 0}));
119   ASSERT_FLOAT_EQ(-2.0f, concat_data->as_f32_bufptr()->at(Index{0, 1}));
120   ASSERT_FLOAT_EQ(3.0f, concat_data->as_f32_bufptr()->at(Index{1, 0}));
121   ASSERT_FLOAT_EQ(4.0f, concat_data->as_f32_bufptr()->at(Index{1, 1}));
122   ASSERT_FLOAT_EQ(-3.0f, concat_data->as_f32_bufptr()->at(Index{2, 0}));
123   ASSERT_FLOAT_EQ(-4.0f, concat_data->as_f32_bufptr()->at(Index{2, 1}));
124   ASSERT_FLOAT_EQ(5.0f, concat_data->as_f32_bufptr()->at(Index{3, 0}));
125   ASSERT_FLOAT_EQ(6.0f, concat_data->as_f32_bufptr()->at(Index{3, 1}));
126
127   ASSERT_EQ(loco::Domain::Tensor, locomotiv::annot_domain(tconcat));
128 }