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