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 <vector>
22
23 #include "OperationResolver.h"
24 #include "Operations.h"
25 #include "Tracing.h"
26
27 namespace android {
28 namespace nn {
29 namespace squeeze {
30
31 constexpr uint32_t kNumInputs = 2;
32 constexpr uint32_t kInputTensor = 0;
33 constexpr uint32_t kSqueezeDims = 1;
34
35 constexpr uint32_t kNumOutputs = 1;
36 constexpr uint32_t kOutputTensor = 0;
37
validate(const IOperationValidationContext * context)38 Result<Version> validate(const IOperationValidationContext* context) {
39 NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
40 NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
41 OperandType inputType = context->getInputType(kInputTensor);
42 NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
43 inputType == OperandType::TENSOR_FLOAT32 ||
44 inputType == OperandType::TENSOR_QUANT8_ASYMM ||
45 inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
46 << "Unsupported input operand type for SQUEEZE op: " << inputType;
47
48 Version minSupportedVersion;
49 if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
50 minSupportedVersion = Version::ANDROID_R;
51 } else if (inputType == OperandType::TENSOR_FLOAT16) {
52 minSupportedVersion = Version::ANDROID_Q;
53 } else {
54 minSupportedVersion = Version::ANDROID_P;
55 }
56
57 NN_RET_CHECK(validateInputTypes(context, {
58 inputType,
59 OperandType::TENSOR_INT32,
60 }));
61 NN_RET_CHECK(validateOutputTypes(context, {inputType}));
62 const Shape& input = context->getInputShape(kInputTensor);
63 if (hasKnownRank(input)) {
64 NN_RET_CHECK_LE(getNumberOfDimensions(input), 4);
65 }
66 return minSupportedVersion;
67 }
68
69 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
prepare(IOperationExecutionContext * context)70 bool prepare(IOperationExecutionContext* context) {
71 // Only the squeeze dims tensor can be omitted.
72 NN_RET_CHECK(!context->isOmittedInput(kInputTensor));
73 NN_RET_CHECK(!context->isOmittedOutput(kOutputTensor));
74
75 const int32_t* squeezeDims = context->getInputBuffer<int32_t>(kSqueezeDims);
76 const Shape inputShape = context->getInputShape(kInputTensor);
77 const Shape squeezeDimsShape = context->getInputShape(kSqueezeDims);
78 int32_t numInputDims = static_cast<int32_t>(getNumberOfDimensions(inputShape));
79
80 NN_RET_CHECK_LE(getNumberOfDimensions(inputShape), 4);
81
82 // squeezeDims need to be provided as a 1-D int32 tensor.
83 NN_OPS_CHECK(squeezeDimsShape.type == OperandType::TENSOR_INT32);
84 NN_OPS_CHECK(getNumberOfDimensions(squeezeDimsShape) == 1);
85
86 std::vector<bool> shouldSqueeze(numInputDims, false);
87 int32_t numDimsSqueezed = 0;
88
89 if (context->isOmittedInput(kSqueezeDims)) {
90 // If squeezeDims is omitted, all dims with value 1 will be squeezed.
91 for (int32_t idx = 0; idx < numInputDims; ++idx) {
92 if (getSizeOfDimension(inputShape, idx) == 1) {
93 shouldSqueeze[idx] = true;
94 ++numDimsSqueezed;
95 }
96 }
97 } else {
98 int32_t squeezeDimsSize = static_cast<int32_t>(getSizeOfDimension(squeezeDimsShape, 0));
99 for (int32_t idx = 0; idx < squeezeDimsSize; ++idx) {
100 int32_t current =
101 squeezeDims[idx] < 0 ? squeezeDims[idx] + numInputDims : squeezeDims[idx];
102 NN_OPS_CHECK(current >= 0 && current < numInputDims &&
103 getSizeOfDimension(inputShape, current) == 1);
104 if (!shouldSqueeze[current]) ++numDimsSqueezed;
105 shouldSqueeze[current] = true;
106 }
107 }
108
109 // Sets output dimensions.
110 std::vector<uint32_t> outDims(numInputDims - numDimsSqueezed);
111 if (numInputDims == numDimsSqueezed) {
112 // Handle edge case where squeeze removes all dimensions.
113 outDims.push_back(1);
114 } else {
115 for (int32_t inIdx = 0, outIdx = 0; inIdx < numInputDims; ++inIdx) {
116 if (!shouldSqueeze[inIdx]) {
117 outDims[outIdx++] = getSizeOfDimension(inputShape, inIdx);
118 }
119 }
120 }
121 Shape outputShape(inputShape);
122 outputShape.dimensions = outDims;
123
124 return context->setOutputShape(kOutputTensor, outputShape);
125 }
126
execute(IOperationExecutionContext * context)127 bool execute(IOperationExecutionContext* context) {
128 switch (context->getInputType(kInputTensor)) {
129 case OperandType::TENSOR_FLOAT16:
130 case OperandType::TENSOR_FLOAT32:
131 case OperandType::TENSOR_QUANT8_ASYMM:
132 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
133 return copyData(context->getInputBuffer(kInputTensor),
134 context->getInputShape(kInputTensor),
135 context->getOutputBuffer(kOutputTensor),
136 context->getOutputShape(kOutputTensor));
137 default:
138 NN_RET_CHECK_FAIL() << "Unsupported tensor type for SQUEEZE op.";
139 }
140 }
141 #endif // NN_INCLUDE_CPU_IMPLEMENTATION
142
143 } // namespace squeeze
144
145 NN_REGISTER_OPERATION(SQUEEZE, "SQUEEZE", squeeze::validate, squeeze::prepare, squeeze::execute,
146 .allowOmittedOperand = true);
147
148 } // namespace nn
149 } // namespace android
150