1 /*
2 * Copyright (C) 2022 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 "AddOperationConverter.h"
18
19 #include <vector>
20
21 #include "OperationConverterResolver.h"
22 #include "SubGraphContext.h"
23
24 namespace android {
25 namespace nn {
26
convert(const Operation & operation,SubGraphContext * context) const27 Result<void> AddOperationConverter::convert(const Operation& operation,
28 SubGraphContext* context) const {
29 const Model::Subgraph* subgraph = context->getSubgraph();
30
31 // add opcode for ADD if not added yet
32 uint32_t opCodeIdx = context->addOpCode(OperationType::ADD);
33
34 std::vector<int32_t> inputs = NN_TRY(getArithmeticInputs(operation, context));
35 std::vector<int32_t> outputs = NN_TRY(getArithmeticOutputs(operation, context));
36
37 int baseOptionsIdx = 2;
38
39 // activation
40 const Operand& activationOperand =
41 subgraph->operands[operation.inputs[baseOptionsIdx + kActivationOffset]];
42 NN_RET_CHECK(isOperandConstant(activationOperand));
43 FusedActivationFunc activation = static_cast<FusedActivationFunc>(
44 context->getConstantScalar<int32_t>(activationOperand));
45
46 auto optionsFlatbuffer = tflite::CreateAddOptions(
47 context->getBuilder(),
48 NN_TRY(getTfliteActivation(activation)) /* fused_activation_function */);
49 auto operatorFlatbuffer = tflite::CreateOperatorDirect(
50 context->getBuilder() /* builder */, opCodeIdx /* opcode_index */, &inputs /* inputs */,
51 &outputs /* outputs */,
52 tflite::BuiltinOptions::BuiltinOptions_AddOptions /* builtin_options_type */,
53 optionsFlatbuffer.Union() /* builtin_options */);
54 context->addOperatorFlatbuffer(operatorFlatbuffer);
55
56 return {};
57 }
58
59 NN_REGISTER_OPERATION_CONVERTER(ADD, AddOperationConverter);
60
61 } // namespace nn
62 } // namespace android