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 "LogisticOperationConverter.h"
18 
19 #include <vector>
20 
21 #include "OperationConverterResolver.h"
22 #include "SubGraphContext.h"
23 
24 namespace android {
25 namespace nn {
26 
getLogisticInputs(const Operation & operation,SubGraphContext * context) const27 Result<std::vector<int32_t>> LogisticOperationConverter::getLogisticInputs(
28         const Operation& operation, SubGraphContext* context) const {
29     NN_TRY(context->createTensorFlatbufferFromOperand(operation.inputs[kInputTensorIdx]));
30     std::vector<int32_t> inputs{
31             context->getTensorIdxFromOperandIdx(operation.inputs[kInputTensorIdx])};
32     return inputs;
33 }
34 
getLogisticOutputs(const Operation & operation,SubGraphContext * context) const35 Result<std::vector<int32_t>> LogisticOperationConverter::getLogisticOutputs(
36         const Operation& operation, SubGraphContext* context) const {
37     NN_TRY(context->createTensorFlatbufferFromOperand(operation.outputs[kOutputTensorIdx]));
38     std::vector<int32_t> outputs{
39             context->getTensorIdxFromOperandIdx(operation.outputs[kOutputTensorIdx])};
40     return outputs;
41 }
42 
convert(const Operation & operation,SubGraphContext * context) const43 Result<void> LogisticOperationConverter::convert(const Operation& operation,
44                                                  SubGraphContext* context) const {
45     // add opcode for LOGISTIC if not added yet
46     uint32_t opCodeIdx = context->addOpCode(OperationType::LOGISTIC);
47 
48     std::vector<int32_t> inputs = NN_TRY(getLogisticInputs(operation, context));
49     std::vector<int32_t> outputs = NN_TRY(getLogisticOutputs(operation, context));
50 
51     auto optionsFlatbuffer = tflite::CreateLogSoftmaxOptions(context->getBuilder());
52     auto operatorFlatbuffer = tflite::CreateOperatorDirect(
53             context->getBuilder() /* builder */, opCodeIdx /* opcode_index */, &inputs /* inputs */,
54             &outputs /* outputs */,
55             tflite::BuiltinOptions::BuiltinOptions_LogSoftmaxOptions /* builtin_options_type */,
56             optionsFlatbuffer.Union() /* builtin_options */);
57     context->addOperatorFlatbuffer(operatorFlatbuffer);
58 
59     return {};
60 }
61 
62 NN_REGISTER_OPERATION_CONVERTER(LOGISTIC, LogisticOperationConverter);
63 
64 }  // namespace nn
65 }  // namespace android