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>
27 #include <gtest/gtest.h>
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;
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; }
41 template <typename T> const Buffer<T> *as_bufptr(const locomotiv::NodeData *data)
43 throw std::runtime_error("Not supported yet");
45 template <> const Buffer<int32_t> *as_bufptr<int32_t>(const locomotiv::NodeData *data)
47 return data->as_s32_bufptr();
49 template <> const Buffer<float> *as_bufptr<float>(const locomotiv::NodeData *data)
51 return data->as_f32_bufptr();
54 template <typename T> void test()
56 // Make pull-BiasEncode graph
57 auto g = loco::make_graph();
59 auto pull = g->nodes()->create<loco::Pull>();
61 pull->dtype(loco_dtype<T>());
65 auto bias_enc = g->nodes()->create<loco::BiasEncode>();
67 bias_enc->input(pull);
70 // Make and assign data to pull node
71 auto pull_buf = make_buffer<T, LexicalLayout>(Shape{1});
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);
79 locomotiv::NodeExecution::get().run(bias_enc);
82 auto bias_enc_data = locomotiv::annot_data(bias_enc);
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}));
89 ASSERT_EQ(loco::Domain::Bias, locomotiv::annot_domain(bias_enc));
93 TEST(NodeExecution_BiasEncode, s32)
100 TEST(NodeExecution_BiasEncode, f32)