010ca68215ce695cee73c0af6c11a76142e3b5c5
[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 locomotiv
38 {
39
40 void NodeExecution::execute(loco::TensorBroadcast *tensor_broadcast)
41 {
42   auto input_data = annot_data(tensor_broadcast->input());
43
44   // Calculate output shape
45   Shape input_shape = *(input_data->shape());
46
47   // TODO Reuse "ShapeInferenceService"
48   Shape output_shape;
49
50   output_shape.resize(input_shape.rank());
51   for (uint32_t axis = 0; axis < input_shape.rank(); ++axis)
52   {
53     if (tensor_broadcast->mapping()->defined(axis))
54     {
55       assert(input_shape.dim(axis) == 1); // Required by TensorBroadcast definition
56       output_shape.dim(axis) = tensor_broadcast->mapping()->dim(axis).value();
57     }
58     else
59     {
60       output_shape.dim(axis) = input_shape.dim(axis);
61     }
62   }
63
64   assert(input_shape.rank() == output_shape.rank());
65
66   uint32_t const rank = input_shape.rank();
67
68   std::unique_ptr<NodeData> output_data = nullptr;
69
70   switch (input_data->dtype())
71   {
72     // TODO Use type-generic implementation!
73     case loco::DataType::FLOAT32:
74     {
75       auto input_bufptr = input_data->as_f32_bufptr();
76       auto output_buf = make_buffer<float, LexicalLayout>(output_shape);
77
78       for (IndexEnumerator e{output_shape}; e.valid(); e.advance())
79       {
80         auto input_index = e.current();
81         const auto &output_index = e.current();
82
83         for (uint32_t axis = 0; axis < rank; ++axis)
84         {
85           if (tensor_broadcast->mapping()->defined(axis))
86           {
87             input_index.at(axis) = 0;
88           }
89         }
90
91         output_buf.at(output_index) = input_bufptr->at(input_index);
92       }
93
94       output_data = make_data(output_buf);
95       break;
96     }
97     default:
98       throw std::runtime_error("Not yet supported");
99   }
100
101   assert(output_data != nullptr);
102   annot_data(tensor_broadcast, std::move(output_data));
103   annot_domain(tensor_broadcast, loco::Domain::Tensor);
104 }
105
106 } // namespace locomotiv