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 #include "OperationsUtils.h"
18 #define LOG_TAG "Operations"
19 
20 #include "HalInterfaces.h"
21 #include "OperationResolver.h"
22 
23 namespace android {
24 namespace nn {
25 namespace fill_op {
26 
27 constexpr uint32_t kNumInputs = 2;
28 constexpr uint32_t kDimsTensor = 0;
29 constexpr uint32_t kValueScalar = 1;
30 
31 constexpr uint32_t kNumOutputs = 1;
32 constexpr uint32_t kOutputTensor = 0;
33 
34 namespace {
35 
36 using namespace hal;
37 
38 template <typename T>
executeTyped(IOperationExecutionContext * context)39 bool executeTyped(IOperationExecutionContext* context) {
40     T* output = context->getOutputBuffer<T>(kOutputTensor);
41     const int numElements = getNumberOfElements(context->getOutputShape(kOutputTensor));
42     const T value = context->getInputValue<T>(kValueScalar);
43     for (int i = 0; i < numElements; ++i) {
44         output[i] = value;
45     }
46     return true;
47 }
48 
getValueType(OperandType outputType,OperandType * valueType)49 bool getValueType(OperandType outputType, OperandType* valueType) {
50     switch (outputType) {
51         case OperandType::TENSOR_FLOAT16:
52             *valueType = OperandType::FLOAT16;
53             return true;
54         case OperandType::TENSOR_FLOAT32:
55             *valueType = OperandType::FLOAT32;
56             return true;
57         case OperandType::TENSOR_INT32:
58             *valueType = OperandType::INT32;
59             return true;
60         default:
61             NN_RET_CHECK_FAIL() << "Unsupported value type for fill op: " << toString(outputType);
62     }
63 }
64 
65 }  // namespace
66 
validate(const IOperationValidationContext * context)67 bool validate(const IOperationValidationContext* context) {
68     NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
69     NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
70     // Check output type first because input value type is dependent on the
71     // output type.
72     OperandType outputType = context->getOutputType(kOutputTensor);
73     NN_RET_CHECK(outputType == OperandType::TENSOR_FLOAT16 ||
74                  outputType == OperandType::TENSOR_FLOAT32 ||
75                  outputType == OperandType::TENSOR_INT32)
76             << "Unsupported output type for fill op: " << toString(outputType);
77     NN_RET_CHECK(validateOutputTypes(context, {outputType}));
78 
79     OperandType valueType;
80     NN_RET_CHECK(getValueType(outputType, &valueType));
81     NN_RET_CHECK(validateInputTypes(context, {OperandType::TENSOR_INT32, valueType}));
82 
83     return validateHalVersion(context, HalVersion::V1_3);
84 }
85 
prepare(IOperationExecutionContext * context)86 bool prepare(IOperationExecutionContext* context) {
87     Shape dimsShape = context->getInputShape(kDimsTensor);
88     NN_RET_CHECK_EQ(getNumberOfDimensions(dimsShape), 1);
89 
90     Shape outputShape = context->getOutputShape(kOutputTensor);
91     outputShape.dimensions.resize(dimsShape.dimensions[0]);
92     const int32_t* dims = context->getInputBuffer<int32_t>(kDimsTensor);
93     for (int i = 0; i < dimsShape.dimensions[0]; ++i) {
94         outputShape.dimensions[i] = dims[i];
95     }
96     return context->setOutputShape(kOutputTensor, outputShape);
97 }
98 
execute(IOperationExecutionContext * context)99 bool execute(IOperationExecutionContext* context) {
100     switch (context->getInputType(kValueScalar)) {
101         case OperandType::FLOAT16:
102             return executeTyped<_Float16>(context);
103         case OperandType::FLOAT32:
104             return executeTyped<float>(context);
105         case OperandType::INT32:
106             return executeTyped<int32_t>(context);
107         default:
108             NN_RET_CHECK_FAIL() << "Unsupported value type for fill op.";
109     }
110 }
111 
112 }  // namespace fill_op
113 
114 NN_REGISTER_OPERATION(FILL, "FILL", fill_op::validate, fill_op::prepare, fill_op::execute);
115 
116 }  // namespace nn
117 }  // namespace android
118