Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / EltwiseSqrt.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 #include <cmath>
30 #include <limits>
31
32 using nncc::core::ADT::tensor::Index;
33 using nncc::core::ADT::tensor::Shape;
34 using nncc::core::ADT::tensor::LexicalLayout;
35 using nncc::core::ADT::tensor::make_buffer;
36
37 TEST(NodeExecution_EltwiseSqrt, f32)
38 {
39   // Make Pull-EltwiseSqrt graph
40   auto g = loco::make_graph();
41   auto pull = g->nodes()->create<loco::Pull>();
42   pull->dtype(loco::DataType::FLOAT32);
43   pull->shape({4});
44   auto sqrt = g->nodes()->create<loco::EltwiseSqrt>();
45   sqrt->input(pull);
46
47   // Make and assign data to Pull node
48   auto pull_buf = make_buffer<float, LexicalLayout>(Shape{4});
49   pull_buf.at(Index{0}) = 4.0f;
50   pull_buf.at(Index{1}) = 9.0f;
51   pull_buf.at(Index{2}) = 0.0f;
52   pull_buf.at(Index{3}) = -1.0f;
53   auto pull_data = locomotiv::make_data(pull_buf);
54   locomotiv::annot_data(pull, std::move(pull_data));
55   locomotiv::annot_domain(pull, loco::Domain::Tensor);
56
57   locomotiv::NodeExecution::get().run(sqrt);
58
59   auto sqrt_data = locomotiv::annot_data(sqrt);
60   ASSERT_NE(sqrt_data, nullptr);
61   ASSERT_EQ(loco::DataType::FLOAT32, sqrt_data->dtype());
62   ASSERT_EQ(Shape{4}, *(sqrt_data->shape()));
63   ASSERT_FLOAT_EQ(2.0f, sqrt_data->as_f32_bufptr()->at(Index{0}));
64   ASSERT_FLOAT_EQ(3.0f, sqrt_data->as_f32_bufptr()->at(Index{1}));
65   ASSERT_FLOAT_EQ(0.0f, sqrt_data->as_f32_bufptr()->at(Index{2}));
66   ASSERT_TRUE(std::isnan(sqrt_data->as_f32_bufptr()->at(Index{3})));
67
68   ASSERT_EQ(loco::Domain::Tensor, locomotiv::annot_domain(sqrt));
69 }