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 void NodeExecution::execute(loco::TensorBroadcast *tensor_broadcast)
42 auto input_data = annot_data(tensor_broadcast->input());
44 // Calculate output shape
45 Shape input_shape = *(input_data->shape());
47 // TODO Reuse "ShapeInferenceService"
50 output_shape.resize(input_shape.rank());
51 for (uint32_t axis = 0; axis < input_shape.rank(); ++axis)
53 if (tensor_broadcast->mapping()->defined(axis))
55 assert(input_shape.dim(axis) == 1); // Required by TensorBroadcast definition
56 output_shape.dim(axis) = tensor_broadcast->mapping()->dim(axis).value();
60 output_shape.dim(axis) = input_shape.dim(axis);
64 assert(input_shape.rank() == output_shape.rank());
66 uint32_t const rank = input_shape.rank();
68 std::unique_ptr<NodeData> output_data = nullptr;
70 switch (input_data->dtype())
72 // TODO Use type-generic implementation!
73 case loco::DataType::FLOAT32:
75 auto input_bufptr = input_data->as_f32_bufptr();
76 auto output_buf = make_buffer<float, LexicalLayout>(output_shape);
78 for (IndexEnumerator e{output_shape}; e.valid(); e.advance())
80 auto input_index = e.current();
81 const auto &output_index = e.current();
83 for (uint32_t axis = 0; axis < rank; ++axis)
85 if (tensor_broadcast->mapping()->defined(axis))
87 input_index.at(axis) = 0;
91 output_buf.at(output_index) = input_bufptr->at(input_index);
94 output_data = make_data(output_buf);
98 throw std::runtime_error("Not yet supported");
101 assert(output_data != nullptr);
102 annot_data(tensor_broadcast, std::move(output_data));
103 annot_domain(tensor_broadcast, loco::Domain::Tensor);
106 } // namespace locomotiv