1 /*
2 * Copyright (C) 2018 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 "Tracing.h"
23
24 #include <cmath>
25
26 namespace android {
27 namespace nn {
28 namespace neg {
29
30 constexpr char kOperationName[] = "NEG";
31
32 constexpr uint32_t kNumInputs = 1;
33 constexpr uint32_t kInputTensor = 0;
34
35 constexpr uint32_t kNumOutputs = 1;
36 constexpr uint32_t kOutputTensor = 0;
37
38 namespace {
39
40 using namespace hal;
41
42 template <typename T>
compute(const T * input,const Shape & shape,T * output)43 inline bool compute(const T* input, const Shape& shape, T* output) {
44 const auto size = getNumberOfElements(shape);
45 for (uint32_t i = 0; i < size; ++i) {
46 output[i] = -input[i];
47 }
48 return true;
49 }
50
51 } // namespace
52
validate(const IOperationValidationContext * context)53 bool validate(const IOperationValidationContext* context) {
54 NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
55 NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
56 OperandType inputType = context->getInputType(kInputTensor);
57 NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
58 inputType == OperandType::TENSOR_FLOAT32 || inputType == OperandType::TENSOR_INT32)
59 << "Unsupported tensor type for operation " << kOperationName;
60 NN_RET_CHECK(validateInputTypes(context, {inputType}));
61 NN_RET_CHECK(validateOutputTypes(context, {inputType}));
62 return validateHalVersion(context, HalVersion::V1_2);
63 }
64
prepare(IOperationExecutionContext * context)65 bool prepare(IOperationExecutionContext* context) {
66 Shape input = context->getInputShape(kInputTensor);
67 Shape output = context->getOutputShape(kOutputTensor);
68 NN_RET_CHECK(SetShape(input, &output));
69 return context->setOutputShape(kOutputTensor, output);
70 }
71
execute(IOperationExecutionContext * context)72 bool execute(IOperationExecutionContext* context) {
73 switch (context->getInputType(kInputTensor)) {
74 case OperandType::TENSOR_FLOAT16:
75 return compute(context->getInputBuffer<_Float16>(kInputTensor),
76 context->getInputShape(kInputTensor),
77 context->getOutputBuffer<_Float16>(kOutputTensor));
78 case OperandType::TENSOR_FLOAT32:
79 return compute(context->getInputBuffer<float>(kInputTensor),
80 context->getInputShape(kInputTensor),
81 context->getOutputBuffer<float>(kOutputTensor));
82 case OperandType::TENSOR_INT32:
83 return compute(context->getInputBuffer<int32_t>(kInputTensor),
84 context->getInputShape(kInputTensor),
85 context->getOutputBuffer<int32_t>(kOutputTensor));
86 default:
87 NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
88 }
89 }
90
91 } // namespace neg
92
93 NN_REGISTER_OPERATION(NEG, neg::kOperationName, neg::validate, neg::prepare, neg::execute);
94
95 } // namespace nn
96 } // namespace android
97