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"
21 #include "Validation.h"
23 #include <nncc/core/ADT/tensor/Shape.h>
24 #include <nncc/core/ADT/tensor/Buffer.h>
25 #include <nncc/core/ADT/tensor/Index.h>
26 #include <nncc/core/ADT/tensor/IndexEnumerator.h>
27 #include <nncc/core/ADT/tensor/LexicalLayout.h>
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::num_elements;
42 using namespace locomotiv;
44 void execute_node(loco::Reshape<loco::ReshapeType::Fixed> *reshape)
46 auto input_data = annot_data(reshape->input());
48 validate(input_data, "Input not ready");
49 validate(annot_domain(reshape->input()) == loco::Domain::Tensor,
50 "Input domain of Reshape is not Tensor");
52 std::unique_ptr<NodeData> reshape_data = nullptr;
54 switch (input_data->dtype())
56 case loco::DataType::FLOAT32:
58 auto input_bufptr = input_data->as_f32_bufptr();
59 auto *input_shape = input_data->shape();
61 using Shape = nncc::core::ADT::tensor::Shape;
62 std::unique_ptr<Shape> output_shape(new Shape());
64 output_shape->resize(reshape->rank());
65 for (uint32_t axis = 0; axis < output_shape->rank(); ++axis)
67 output_shape->dim(axis) = reshape->dim(axis).value();
70 auto reshape_bufptr = make_buffer<float, LexicalLayout>(*output_shape);
72 float *input_ptr = const_cast<float *>(input_bufptr->base());
73 uint64_t input_len = num_elements(*input_shape) * sizeof(float);
75 float *output_ptr = reshape_bufptr.base();
77 assert(input_len == num_elements(*output_shape) * sizeof(float));
78 memcpy(output_ptr, input_ptr, input_len);
80 reshape_data = make_data(reshape_bufptr);
84 throw std::runtime_error("NYI for this DataType");
87 assert(reshape_data != nullptr);
88 annot_data(reshape, std::move(reshape_data));
89 annot_domain(reshape, annot_domain(reshape->input()));
97 void NodeExecution::execute(loco::Reshape<loco::ReshapeType::Fixed> *reshape)
99 execute_node(reshape);
102 } // namespace locomotiv