Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / BiasEncode.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 using nncc::core::ADT::tensor::Buffer;
34
35 namespace
36 {
37 template <typename T> loco::DataType loco_dtype() { throw std::runtime_error("Not supported yet"); }
38 template <> loco::DataType loco_dtype<int32_t>() { return loco::DataType::S32; }
39 template <> loco::DataType loco_dtype<float>() { return loco::DataType::FLOAT32; }
40
41 template <typename T> const Buffer<T> *as_bufptr(const locomotiv::NodeData *data)
42 {
43   throw std::runtime_error("Not supported yet");
44 }
45 template <> const Buffer<int32_t> *as_bufptr<int32_t>(const locomotiv::NodeData *data)
46 {
47   return data->as_s32_bufptr();
48 }
49 template <> const Buffer<float> *as_bufptr<float>(const locomotiv::NodeData *data)
50 {
51   return data->as_f32_bufptr();
52 }
53
54 template <typename T> void test()
55 {
56   // Make pull-BiasEncode graph
57   auto g = loco::make_graph();
58
59   auto pull = g->nodes()->create<loco::Pull>();
60   {
61     pull->dtype(loco_dtype<T>());
62     pull->shape({1});
63   }
64
65   auto bias_enc = g->nodes()->create<loco::BiasEncode>();
66   {
67     bias_enc->input(pull);
68   }
69
70   // Make and assign data to pull node
71   auto pull_buf = make_buffer<T, LexicalLayout>(Shape{1});
72   {
73     pull_buf.at(Index{0}) = static_cast<T>(100);
74     auto pull_data = locomotiv::make_data(pull_buf);
75     locomotiv::annot_data(pull, std::move(pull_data));
76     locomotiv::annot_domain(pull, loco::Domain::Tensor);
77   }
78
79   locomotiv::NodeExecution::get().run(bias_enc);
80
81   // check
82   auto bias_enc_data = locomotiv::annot_data(bias_enc);
83
84   ASSERT_NE(bias_enc_data, nullptr);
85   ASSERT_EQ(loco_dtype<T>(), bias_enc_data->dtype());
86   ASSERT_EQ(Shape{1}, *(bias_enc_data->shape()));
87   ASSERT_EQ(pull_buf.at(Index{0}), as_bufptr<T>(bias_enc_data)->at(Index{0}));
88
89   ASSERT_EQ(loco::Domain::Bias, locomotiv::annot_domain(bias_enc));
90 }
91 } // namespace
92
93 TEST(NodeExecution_BiasEncode, s32)
94 {
95   test<int32_t>();
96
97   SUCCEED();
98 }
99
100 TEST(NodeExecution_BiasEncode, f32)
101 {
102   test<float>();
103
104   SUCCEED();
105 }