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