ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Reshape.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright (C) 2017 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "Reshape.h"
19#include "Operand.h"
20#include "Assert.h"
21
22#include <cstring>
23
24bool reshapePrepare(const Shape &input, const int32_t *targetDims, const int32_t targetDimsSize,
25 Shape *output)
26{
27 // Reshape allows one of the targetDims components to have the
28 // special -1 value, meaning it will be calculated automatically based on the
29 // input. Here we calculate what that dimension should be so that the number
30 // of output elements in the same as the number of input elements.
31 int32_t numInputElements = (int32_t)getNumberOfElements(input);
32
33 std::vector<uint32_t> outDims(targetDimsSize);
34 int32_t numOutputElements = 1;
35 int32_t strechDim = -1;
36 for (int32_t i = 0; i < targetDimsSize; ++i)
37 {
38 int32_t value = targetDims[i];
39 if (value == -1)
40 {
41 ASSERT(strechDim == -1);
42 strechDim = i;
43 }
44 else
45 {
46 numOutputElements *= value;
47 outDims[i] = (uint32_t)value;
48 }
49 }
50 if (strechDim != -1)
51 {
52 int32_t strechValue = numInputElements / numOutputElements;
53 outDims[strechDim] = (uint32_t)strechValue;
54 numOutputElements *= strechValue;
55 }
56
57 ASSERT(numInputElements == numOutputElements);
58
59 output->type = input.type;
60 output->dimensions = outDims;
61 output->offset = input.offset;
62 output->scale = input.scale;
63
64 return true;
65}
66
67bool reshapeGeneric(const void *inputData, const Shape &inputShape, void *outputData,
68 const Shape &outputShape)
69{
70 size_t count = sizeOfData(inputShape.type, inputShape.dimensions);
71 memcpy(outputData, inputData, count);
72 return true;
73}
#define ASSERT(v)
Definition Assert.h:24
uint32_t sizeOfData(const Operand &operand)
Definition Operand.h:56
uint32_t getNumberOfElements(const Shape &shape)
Definition Shape.cpp:48
bool reshapeGeneric(const void *inputData, const Shape &inputShape, void *outputData, const Shape &outputShape)
Definition Reshape.cpp:67
bool reshapePrepare(const Shape &input, const int32_t *targetDims, const int32_t targetDimsSize, Shape *output)
Definition Reshape.cpp:24
Definition Shape.h:28
OperandType type
Definition Shape.h:29
std::vector< uint32_t > dimensions
Definition Shape.h:30