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 #define LOG_TAG "Operations"
18
19 #include "LegacyUtils.h"
20 #include "OperationResolver.h"
21 #include "OperationsUtils.h"
22
23 namespace android {
24 namespace nn {
25 namespace rank_op {
26
27 constexpr uint32_t kNumInputs = 1;
28 constexpr uint32_t kInputTensor = 0;
29
30 constexpr uint32_t kNumOutputs = 1;
31 constexpr uint32_t kOutputScalar = 0;
32
validate(const IOperationValidationContext * context)33 Result<Version> validate(const IOperationValidationContext* context) {
34 NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
35 NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
36 OperandType inputType = context->getInputType(kInputTensor);
37 NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
38 inputType == OperandType::TENSOR_FLOAT32 ||
39 inputType == OperandType::TENSOR_INT32 ||
40 inputType == OperandType::TENSOR_QUANT8_ASYMM ||
41 inputType == OperandType::TENSOR_QUANT16_SYMM ||
42 inputType == OperandType::TENSOR_BOOL8 ||
43 inputType == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL ||
44 inputType == OperandType::TENSOR_QUANT16_ASYMM ||
45 inputType == OperandType::TENSOR_QUANT8_SYMM ||
46 inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
47 << "Incorrect input type for a RANK op: " << inputType;
48 NN_RET_CHECK(validateOutputTypes(context, {OperandType::INT32}));
49 return Version::ANDROID_R;
50 }
51
prepare(IOperationExecutionContext * context)52 bool prepare(IOperationExecutionContext* context) {
53 Shape output = context->getOutputShape(kOutputScalar);
54 return context->setOutputShape(kOutputScalar, output);
55 }
56
execute(IOperationExecutionContext * context)57 bool execute(IOperationExecutionContext* context) {
58 *context->getOutputBuffer<int32_t>(kOutputScalar) =
59 getNumberOfDimensions(context->getInputShape(kInputTensor));
60 return true;
61 }
62
63 } // namespace rank_op
64
65 NN_REGISTER_OPERATION(RANK, "RANK", rank_op::validate, rank_op::prepare, rank_op::execute);
66
67 } // namespace nn
68 } // namespace android
69