ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Reshape.cpp
Go to the documentation of this file.
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
28
33
34#include <cassert>
35#include <stdexcept>
36#include <cstring>
37#include <vector>
38
39namespace
40{
41
42using namespace locomotiv;
43
44void 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");
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
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
94namespace locomotiv
95{
96
97void NodeExecution::execute(loco::Reshape<loco::ReshapeType::Fixed> *reshape)
98{
99 execute_node(reshape);
100}
101
102} // namespace locomotiv
void resize(int dimensions_count)
Definition Tensor.h:121
const luci_interpreter::RuntimeShape output_shape
bool validate(Code *code)
list input_data
Definition infer.py:29
void annot_domain(loco::Node *node, const loco::Domain &domain)
Wrapper to annotate domain to node. Cannot annotate unknown domain.
std::unique_ptr< NodeData > make_data(const NodeData::Buffer< DT > &buffer)
Copy buffer to make NodeData.
uint32_t num_elements(const Shape &shape)
The number of elements of a feature map of a given shape.
Definition Shape.h:59
Buffer< T > make_buffer(const Shape &shape)
Definition Buffer.h:47
uint64_t num_elements(const Shape &)
Definition Shape.cpp:51
Definition Shape.h:28