Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / TensorBroadcast.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 "NodeDataImpl.h"
20 #include "NodeDomain.h"
21
22 #include <nncc/core/ADT/tensor/Shape.h>
23 #include <nncc/core/ADT/tensor/Buffer.h>
24 #include <nncc/core/ADT/tensor/Index.h>
25 #include <nncc/core/ADT/tensor/IndexEnumerator.h>
26 #include <nncc/core/ADT/tensor/LexicalLayout.h>
27
28 using nncc::core::ADT::tensor::Index;
29 using nncc::core::ADT::tensor::IndexEnumerator;
30 using nncc::core::ADT::tensor::LexicalLayout;
31 using nncc::core::ADT::tensor::make_buffer;
32 using nncc::core::ADT::tensor::Shape;
33
34 #include <cassert>
35 #include <stdexcept>
36
37 namespace
38 {
39
40 using namespace locomotiv;
41
42 void execute_node(loco::TensorBroadcast *tensor_broadcast)
43 {
44   auto input_data = annot_data(tensor_broadcast->input());
45
46   // Calculate output shape
47   Shape input_shape = *(input_data->shape());
48
49   // TODO Reuse "ShapeInferenceService"
50   Shape output_shape;
51
52   output_shape.resize(input_shape.rank());
53   for (uint32_t axis = 0; axis < input_shape.rank(); ++axis)
54   {
55     if (tensor_broadcast->mapping()->defined(axis))
56     {
57       assert(input_shape.dim(axis) == 1); // Required by TensorBroadcast definition
58       output_shape.dim(axis) = tensor_broadcast->mapping()->dim(axis).value();
59     }
60     else
61     {
62       output_shape.dim(axis) = input_shape.dim(axis);
63     }
64   }
65
66   assert(input_shape.rank() == output_shape.rank());
67
68   uint32_t const rank = input_shape.rank();
69
70   std::unique_ptr<NodeData> output_data = nullptr;
71
72   switch (input_data->dtype())
73   {
74     // TODO Use type-generic implementation!
75     case loco::DataType::FLOAT32:
76     {
77       auto input_bufptr = input_data->as_f32_bufptr();
78       auto output_buf = make_buffer<float, LexicalLayout>(output_shape);
79
80       for (IndexEnumerator e{output_shape}; e.valid(); e.advance())
81       {
82         auto input_index = e.current();
83         const auto &output_index = e.current();
84
85         for (uint32_t axis = 0; axis < rank; ++axis)
86         {
87           if (tensor_broadcast->mapping()->defined(axis))
88           {
89             input_index.at(axis) = 0;
90           }
91         }
92
93         output_buf.at(output_index) = input_bufptr->at(input_index);
94       }
95
96       output_data = make_data(output_buf);
97       break;
98     }
99     default:
100       throw std::runtime_error("Not yet supported");
101   }
102
103   assert(output_data != nullptr);
104   annot_data(tensor_broadcast, std::move(output_data));
105   annot_domain(tensor_broadcast, loco::Domain::Tensor);
106 }
107
108 } // namespace
109
110 namespace locomotiv
111 {
112
113 void NodeExecution::execute(loco::TensorBroadcast *tensor_broadcast)
114 {
115   execute_node(tensor_broadcast);
116 }
117
118 } // namespace locomotiv