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 "NodeDataImpl.h"
20 #include "NodeDomain.h"
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>
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;
40 using namespace locomotiv;
42 void execute_node(loco::TensorBroadcast *tensor_broadcast)
44 auto input_data = annot_data(tensor_broadcast->input());
46 // Calculate output shape
47 Shape input_shape = *(input_data->shape());
49 // TODO Reuse "ShapeInferenceService"
52 output_shape.resize(input_shape.rank());
53 for (uint32_t axis = 0; axis < input_shape.rank(); ++axis)
55 if (tensor_broadcast->mapping()->defined(axis))
57 assert(input_shape.dim(axis) == 1); // Required by TensorBroadcast definition
58 output_shape.dim(axis) = tensor_broadcast->mapping()->dim(axis).value();
62 output_shape.dim(axis) = input_shape.dim(axis);
66 assert(input_shape.rank() == output_shape.rank());
68 uint32_t const rank = input_shape.rank();
70 std::unique_ptr<NodeData> output_data = nullptr;
72 switch (input_data->dtype())
74 // TODO Use type-generic implementation!
75 case loco::DataType::FLOAT32:
77 auto input_bufptr = input_data->as_f32_bufptr();
78 auto output_buf = make_buffer<float, LexicalLayout>(output_shape);
80 for (IndexEnumerator e{output_shape}; e.valid(); e.advance())
82 auto input_index = e.current();
83 const auto &output_index = e.current();
85 for (uint32_t axis = 0; axis < rank; ++axis)
87 if (tensor_broadcast->mapping()->defined(axis))
89 input_index.at(axis) = 0;
93 output_buf.at(output_index) = input_bufptr->at(input_index);
96 output_data = make_data(output_buf);
100 throw std::runtime_error("Not yet supported");
103 assert(output_data != nullptr);
104 annot_data(tensor_broadcast, std::move(output_data));
105 annot_domain(tensor_broadcast, loco::Domain::Tensor);
113 void NodeExecution::execute(loco::TensorBroadcast *tensor_broadcast)
115 execute_node(tensor_broadcast);
118 } // namespace locomotiv