ONE - On-device Neural Engine
Loading...
Searching...
No Matches
CircleIf.cpp
Go to the documentation of this file.
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
18
21
22#include <loco.h>
23#include <oops/UserExn.h>
24
25namespace luci
26{
27
29{
30 const auto &inputs = args.op.inputs;
31 const auto *options = args.op.builtin_options.AsIfOptions();
32
33 if (inputs.size() < 2) // cond + input
34 return false;
35 if (args.op.outputs.size() < 1) // output
36 return false;
37
38 auto num_graphs = static_cast<int32_t>(args.reader.num_subgraph());
39 if (options->then_subgraph_index >= num_graphs)
40 return false;
41 if (options->else_subgraph_index >= num_graphs)
42 return false;
43
44 // input 0 should be BOOL type
45 const auto tensors = args.reader.tensors();
46 const auto tensor = tensors.at(inputs.at(0));
47 assert(tensor != nullptr);
48 if (tensor->type() != circle::TensorType_BOOL)
49 return false;
50
51 const auto shape = wrap(tensor->shape());
52 if (shape.size() != 1 && shape.size() != 0)
53 return false;
54
55 return true;
56}
57
74CircleNode *CircleIfGraphBuilder::build_node(const BuildNodeArgs &bna) const
75{
76 uint32_t input_count = bna.op.inputs.size() - 1;
77 uint32_t output_count = bna.op.outputs.size();
78
79 auto *node = bna.context->graph()->nodes()->create<CircleIf>(input_count, output_count);
80
81 node->cond(bna.input_nodes[0]);
82 for (uint32_t idx = 0; idx < input_count; ++idx)
83 {
84 node->input(idx, bna.input_nodes[idx + 1]);
85 }
86
87 const auto *options = bna.op.builtin_options.AsIfOptions();
88 node->then_branch(options->then_subgraph_index);
89 node->else_branch(options->else_subgraph_index);
90
91 return node;
92}
93
94CircleNode *CircleIfGraphBuilder::build_out(const BuildOutArgs &boa) const
95{
96 auto *nodeout = boa.node->graph()->nodes()->create<CircleIfOut>();
97
98 nodeout->input(boa.node);
99 nodeout->index(boa.index);
100
101 return nodeout;
102}
103
104} // namespace luci
NodeContext * nodes(void)
Definition Graph.h:218
Graph * graph(void)
Definition Node.h:70
Derived * create(Args &&...args)
Definition NodePool.h:37
bool validate(const ValidateArgs &args) const final
Definition CircleIf.cpp:28
IF in Circle.
Definition CircleIf.h:34
Node * cond(void) const
Definition CircleIf.h:48
VectorWrapper< T > wrap(const flatbuffers::Vector< T > *vec)