Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / luci / import / src / Nodes / CircleOneHot.cpp
1 /*
2  * Copyright (c) 2020 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 "luci/Import/Nodes/CircleOneHot.h"
18
19 #include <luci/IR/Nodes/CircleOneHot.h>
20
21 #include <loco.h>
22 #include <oops/UserExn.h>
23
24 namespace luci
25 {
26
27 bool CircleOneHotGraphBuilder::validate(const ValidateArgs &args) const
28 {
29   const auto &inputs = args.op.inputs;
30   const auto &outputs = args.op.outputs;
31   const auto *options = args.op.builtin_options.AsOneHotOptions();
32
33   // Only 4 Input come refered from
34   if (inputs.size() != 4)
35     return false;
36
37   if (outputs.size() != 1)
38     return false;
39
40   const auto &tensors = args.reader.tensors();
41   const auto &indices = tensors.at(inputs.at(0));
42   const auto &depth = tensors.at(inputs.at(1));
43   const auto &on_value = tensors.at(inputs.at(2));
44   const auto &off_value = tensors.at(inputs.at(3));
45
46   if (options->axis < -1 || options->axis > static_cast<int32_t>(indices->shape.size()))
47     return false;
48   if (depth->shape.size() != 0)
49     return false;
50   if (on_value->shape.size() != 0)
51     return false;
52   if (off_value->shape.size() != 0)
53     return false;
54   if (on_value->type != off_value->type)
55     return false;
56
57   return true;
58 }
59
60 CircleNode *CircleOneHotGraphBuilder::build_node(const circle::OperatorT &op,
61                                                  const std::vector<CircleNode *> &inputs,
62                                                  loco::Graph *graph) const
63 {
64   auto *node = graph->nodes()->create<CircleOneHot>();
65
66   node->indices(inputs.at(0));
67   node->depth(inputs.at(1));
68   node->on_value(inputs.at(2));
69   node->off_value(inputs.at(3));
70
71   const auto *options = op.builtin_options.AsOneHotOptions();
72   node->axis(options->axis);
73
74   return node;
75 }
76
77 } // namespace luci