ONE - On-device Neural Engine
Loading...
Searching...
No Matches
CircleWhile.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.AsWhileOptions();
32
33 if (inputs.size() != args.op.outputs.size())
34 return false;
35
36 auto num_graphs = static_cast<int32_t>(args.reader.num_subgraph());
37 if (options->cond_subgraph_index >= num_graphs)
38 return false;
39 if (options->body_subgraph_index >= num_graphs)
40 return false;
41
42 return true;
43}
44
61CircleNode *CircleWhileGraphBuilder::build(const circle::OperatorT &op,
62 GraphBuilderContext *context) const
63{
64 assert(context != nullptr);
65
66 auto graph = context->graph();
67
68 const std::vector<int32_t> &inputs = op.inputs;
69 const std::vector<int32_t> &outputs = op.outputs;
70 const auto tensors = context->reader()->tensors();
71 const auto opcodes = context->reader()->opcodes();
72
73 std::vector<CircleNode *> input_nodes;
74 for (const int32_t input_tensor_index : inputs)
75 {
76 auto input_node = context->nodefinder()->node(input_tensor_index);
77 assert(input_node != nullptr);
78 input_nodes.push_back(input_node);
79 }
80
81 uint32_t input_count = inputs.size();
82 uint32_t output_count = outputs.size();
83
84 // Create CircleWhile
85 CircleWhile *node = graph->nodes()->create<CircleWhile>(input_count, output_count);
86
87 for (uint32_t idx = 0; idx < input_count; ++idx)
88 {
89 node->input(idx, input_nodes[idx]);
90 }
91
92 const auto *options = op.builtin_options.AsWhileOptions();
93 node->cond_branch(options->cond_subgraph_index);
94 node->body_branch(options->body_subgraph_index);
95
96 assert(outputs.size() > 0);
97 {
98 // Lets use name of output 0 as While name
99 const auto output_tensor = tensors[outputs[0]];
100 assert(output_tensor != nullptr);
101 node->name(tensor_name(output_tensor));
102 assert(opcodes[op.opcode_index] != nullptr);
103 node->op_version(opcodes[op.opcode_index]->version());
104
105 // NOTE We don't set quantization for While itself but to virtual outputs
106 }
107
108 // Create virtual outputs of While
109 for (uint32_t n = 0; n < output_count; ++n)
110 {
111 const auto output_tensor = tensors[outputs[n]];
112 assert(output_tensor != nullptr);
113
114 auto *nodeout = graph->nodes()->create<CircleWhileOut>();
115
116 nodeout->input(node);
117 nodeout->index(n);
118
119 copy_tensor_attributes(output_tensor, nodeout);
120
121 // Note: leave shape_status to UNKNOWN to run shape inference
122
123 context->nodefinder()->enroll(outputs[n], nodeout);
124 }
125
126 return node;
127}
128
129} // namespace luci
CircleOperatorCodes opcodes() const
CircleTensors tensors() const
CircleNode * build(const circle::OperatorT &op, GraphBuilderContext *context) const final
While Node builder.
bool validate(const ValidateArgs &args) const final
WHILE in Circle.
Definition CircleWhile.h:34
Node * input(uint32_t index) const
Definition CircleWhile.h:51
int32_t cond_branch(void) const
Definition CircleWhile.h:55
int32_t body_branch(void) const
Definition CircleWhile.h:58
Virtual CIRCLEWHILEOUT in Circle.
loco::Node * input(void) const
Class to store context to build loco graph IR from TensorFlow.
IndexNodeFinder * nodefinder()
void enroll(TensorIndex idx, CircleNode *node)
CircleNode * node(TensorIndex idx) const
const char * tensor_name(const circle::Tensor *tensor)
void copy_tensor_attributes(const circle::Tensor *tensor, CircleNode *node)
Copy common tensor attributes such as name, type, etc. to node.
CircleInput * input_node(loco::Graph *g, const loco::GraphInputIndex &index)
Find a Pull node with a given input index.
int32_t op_version(void) const
NodeName name(void) const