1 /*
2 * Copyright (C) 2019 The Android Open Source Project
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 // Contains the implementation of the operations.
18
19 #define LOG_TAG "Operations"
20
21 #include "Squeeze.h"
22
23 #include <vector>
24
25 #include "OperationResolver.h"
26 #include "Operations.h"
27 #include "Tracing.h"
28
29 namespace android {
30 namespace nn {
31 namespace squeeze {
32
33 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
prepare(IOperationExecutionContext * context)34 bool prepare(IOperationExecutionContext* context) {
35 // Only the squeeze dims tensor can be omitted.
36 NN_RET_CHECK(!context->isOmittedInput(kInputTensor));
37 NN_RET_CHECK(!context->isOmittedOutput(kOutputTensor));
38
39 const int32_t* squeezeDims = context->getInputBuffer<int32_t>(kSqueezeDims);
40 const Shape inputShape = context->getInputShape(kInputTensor);
41 const Shape squeezeDimsShape = context->getInputShape(kSqueezeDims);
42 int32_t numInputDims = static_cast<int32_t>(getNumberOfDimensions(inputShape));
43
44 NN_RET_CHECK_LE(getNumberOfDimensions(inputShape), 4u);
45
46 // squeezeDims need to be provided as a 1-D int32 tensor.
47 NN_OPS_CHECK(squeezeDimsShape.type == OperandType::TENSOR_INT32);
48 NN_OPS_CHECK(getNumberOfDimensions(squeezeDimsShape) == 1);
49
50 std::vector<bool> shouldSqueeze(numInputDims, false);
51 int32_t numDimsSqueezed = 0;
52
53 if (context->isOmittedInput(kSqueezeDims)) {
54 // If squeezeDims is omitted, all dims with value 1 will be squeezed.
55 for (int32_t idx = 0; idx < numInputDims; ++idx) {
56 if (getSizeOfDimension(inputShape, idx) == 1) {
57 shouldSqueeze[idx] = true;
58 ++numDimsSqueezed;
59 }
60 }
61 } else {
62 int32_t squeezeDimsSize = static_cast<int32_t>(getSizeOfDimension(squeezeDimsShape, 0));
63 for (int32_t idx = 0; idx < squeezeDimsSize; ++idx) {
64 int32_t current =
65 squeezeDims[idx] < 0 ? squeezeDims[idx] + numInputDims : squeezeDims[idx];
66 NN_OPS_CHECK(current >= 0 && current < numInputDims &&
67 getSizeOfDimension(inputShape, current) == 1);
68 if (!shouldSqueeze[current]) ++numDimsSqueezed;
69 shouldSqueeze[current] = true;
70 }
71 }
72
73 // Sets output dimensions.
74 std::vector<uint32_t> outDims(numInputDims - numDimsSqueezed);
75 if (numInputDims == numDimsSqueezed) {
76 // Handle edge case where squeeze removes all dimensions.
77 outDims.push_back(1);
78 } else {
79 for (int32_t inIdx = 0, outIdx = 0; inIdx < numInputDims; ++inIdx) {
80 if (!shouldSqueeze[inIdx]) {
81 outDims[outIdx++] = getSizeOfDimension(inputShape, inIdx);
82 }
83 }
84 }
85 Shape outputShape(inputShape);
86 outputShape.dimensions = outDims;
87
88 return context->setOutputShape(kOutputTensor, outputShape);
89 }
90
execute(IOperationExecutionContext * context)91 bool execute(IOperationExecutionContext* context) {
92 switch (context->getInputType(kInputTensor)) {
93 case OperandType::TENSOR_FLOAT16:
94 case OperandType::TENSOR_FLOAT32:
95 case OperandType::TENSOR_QUANT8_ASYMM:
96 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
97 return copyData(context->getInputBuffer(kInputTensor),
98 context->getInputShape(kInputTensor),
99 context->getOutputBuffer(kOutputTensor),
100 context->getOutputShape(kOutputTensor));
101 default:
102 NN_RET_CHECK_FAIL() << "Unsupported tensor type for SQUEEZE op.";
103 }
104 }
105 #endif // NN_INCLUDE_CPU_IMPLEMENTATION
106
107 } // namespace squeeze
108
109 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(SQUEEZE, squeeze::prepare, squeeze::execute,
110 .allowOmittedOperand = true);
111
112 } // namespace nn
113 } // namespace android
114