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 void NodeExecution::execute(loco::Reshape<loco::ReshapeType::Fixed> *reshape)
44 auto input_data = annot_data(reshape->input());
46 validate(input_data, "Input not ready");
47 validate(annot_domain(reshape->input()) == loco::Domain::Tensor,
48 "Input domain of Reshape is not Tensor");
50 std::unique_ptr<NodeData> reshape_data = nullptr;
52 switch (input_data->dtype())
54 case loco::DataType::FLOAT32:
56 auto input_bufptr = input_data->as_f32_bufptr();
57 auto *input_shape = input_data->shape();
59 using Shape = nncc::core::ADT::tensor::Shape;
60 std::unique_ptr<Shape> output_shape(new Shape());
62 output_shape->resize(reshape->rank());
63 for (uint32_t axis = 0; axis < output_shape->rank(); ++axis)
65 output_shape->dim(axis) = reshape->dim(axis).value();
68 auto reshape_bufptr = make_buffer<float, LexicalLayout>(*output_shape);
70 float *input_ptr = const_cast<float *>(input_bufptr->base());
71 uint64_t input_len = num_elements(*input_shape) * sizeof(float);
73 float *output_ptr = reshape_bufptr.base();
75 assert(input_len == num_elements(*output_shape) * sizeof(float));
76 memcpy(output_ptr, input_ptr, input_len);
78 reshape_data = make_data(reshape_bufptr);
82 throw std::runtime_error("NYI for this DataType");
85 assert(reshape_data != nullptr);
86 annot_data(reshape, std::move(reshape_data));
87 annot_domain(reshape, annot_domain(reshape->input()));
90 } // namespace locomotiv