Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / compiler / mir-onnx-importer / Op / Reciprocal.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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 "Reciprocal.h"
18
19 #include "ONNXHelpers.h"
20
21 #include "mir/ops/ConstantOp.h"
22 #include "mir/ops/DivOp.h"
23
24 namespace mir_onnx
25 {
26
27 static void convertReciprocal(const onnx::NodeProto &onnx_node, ConverterContext *context)
28 {
29   std::vector<mir::Operation::Output *> inputs = context->getNodeInputs(onnx_node);
30   mir::Graph *graph = context->getGraph();
31
32   assert(inputs.size() == 1);
33   auto input = inputs[0];
34
35   const float one_value = 1.0f;
36   mir::TensorVariant one_tensor({mir::DataType::FLOAT32, {}}, &one_value);
37   auto one = createOp<mir::ops::ConstantOp>(graph, one_tensor)->getOutput(0);
38   auto result = createOp<mir::ops::DivOp>(graph, input, one)->getOutput(0);
39
40   context->setNodeOutputs(onnx_node, {result});
41 }
42
43 void convertReciprocalV1(const onnx::NodeProto &onnx_node, ConverterContext *context)
44 {
45   convertReciprocal(onnx_node, context);
46 }
47
48 void convertReciprocalV6(const onnx::NodeProto &onnx_node, ConverterContext *context)
49 {
50   convertReciprocal(onnx_node, context);
51 }
52
53 } // namespace mir_onnx