Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / Reshape.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 #include "Validation.h"
22
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>
28
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;
33
34 #include <cassert>
35 #include <stdexcept>
36 #include <cstring>
37 #include <vector>
38
39 namespace
40 {
41
42 using namespace locomotiv;
43
44 void execute_node(loco::Reshape<loco::ReshapeType::Fixed> *reshape)
45 {
46   auto input_data = annot_data(reshape->input());
47
48   validate(input_data, "Input not ready");
49   validate(annot_domain(reshape->input()) == loco::Domain::Tensor,
50            "Input domain of Reshape is not Tensor");
51
52   std::unique_ptr<NodeData> reshape_data = nullptr;
53
54   switch (input_data->dtype())
55   {
56     case loco::DataType::FLOAT32:
57     {
58       auto input_bufptr = input_data->as_f32_bufptr();
59       auto *input_shape = input_data->shape();
60
61       using Shape = nncc::core::ADT::tensor::Shape;
62       std::unique_ptr<Shape> output_shape(new Shape());
63
64       output_shape->resize(reshape->rank());
65       for (uint32_t axis = 0; axis < output_shape->rank(); ++axis)
66       {
67         output_shape->dim(axis) = reshape->dim(axis).value();
68       }
69
70       auto reshape_bufptr = make_buffer<float, LexicalLayout>(*output_shape);
71
72       float *input_ptr = const_cast<float *>(input_bufptr->base());
73       uint64_t input_len = num_elements(*input_shape) * sizeof(float);
74
75       float *output_ptr = reshape_bufptr.base();
76
77       assert(input_len == num_elements(*output_shape) * sizeof(float));
78       memcpy(output_ptr, input_ptr, input_len);
79
80       reshape_data = make_data(reshape_bufptr);
81       break;
82     }
83     default:
84       throw std::runtime_error("NYI for this DataType");
85   }
86
87   assert(reshape_data != nullptr);
88   annot_data(reshape, std::move(reshape_data));
89   annot_domain(reshape, annot_domain(reshape->input()));
90 }
91
92 } // namespace
93
94 namespace locomotiv
95 {
96
97 void NodeExecution::execute(loco::Reshape<loco::ReshapeType::Fixed> *reshape)
98 {
99   execute_node(reshape);
100 }
101
102 } // namespace locomotiv