ONE - On-device Neural Engine
Loading...
Searching...
No Matches
StridedSlice.cpp
Go to the documentation of this file.
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
18
21
22#include <moco/Names.h>
23
24#include "Convert.h"
25
26#include <loco.h>
27#include <plier/tf/Convert.h>
28#include <oops/UserExn.h>
29
30#include <memory>
31
32namespace
33{
34using namespace moco;
35
36class TFStridedSliceGraphUpdate final : public GraphUpdate
37{
38public:
39 TFStridedSliceGraphUpdate(TFStridedSlice *node, std::vector<TensorName> names)
40 : _node(node), _names(names)
41 {
42 }
43
44 void input(const SymbolTable *) const override;
45
46private:
47 TFStridedSlice *_node;
48 std::vector<TensorName> _names;
49};
50
51void TFStridedSliceGraphUpdate::input(const SymbolTable *node_table) const
52{
53 // TODO support size 3 where strides is None
54 assert(_names.size() == 4);
55
56 auto input_node = node_table->node(_names[0]);
57 auto begin_node = node_table->node(_names[1]);
58 auto end_node = node_table->node(_names[2]);
59 auto strides_node = node_table->node(_names[3]);
60 assert(input_node != nullptr);
61 assert(begin_node != nullptr);
62 assert(end_node != nullptr);
63 assert(strides_node != nullptr);
64
65 _node->input(input_node);
66 _node->begin(begin_node);
67 _node->end(end_node);
68 _node->strides(strides_node);
69
70 // TODO move validation codes to some suitable place
71 // Run basic validation
72
73 // TODO support full mask features
74 if (_node->begin_mask() != 0 || _node->end_mask() != 0 || _node->ellipsis_mask() != 0 ||
75 _node->new_axis_mask() != 0 || _node->shrink_axis_mask() != 1)
76 {
77 throw oops::UserExn("Mask attributes are not supported for now: ", _node->name());
78 }
79
80 // Only Const are supported for now
81 auto const_input = dynamic_cast<moco::TFConst *>(_node->input());
82 auto const_begin = dynamic_cast<moco::TFConst *>(_node->begin());
83 auto const_end = dynamic_cast<moco::TFConst *>(_node->end());
84 auto const_strides = dynamic_cast<moco::TFConst *>(_node->strides());
85 if (const_input == nullptr || const_begin == nullptr || const_end == nullptr ||
86 const_strides == nullptr)
87 {
88 throw oops::UserExn("Only Const inputs are supported for now: ", _node->name());
89 }
90
91 // TODO support S64
92 if (const_begin->dtype() != loco::DataType::S32 || const_end->dtype() != loco::DataType::S32 ||
93 const_strides->dtype() != loco::DataType::S32)
94 {
95 throw oops::UserExn("Only Const types of INT32 are supported for begin/end/strides for now: ",
96 _node->name());
97 }
98
99 // Input Rank should match number of elements of the begin/end/strides
100 auto rin = const_input->rank();
101 if (rin != const_begin->size<loco::DataType::S32>() ||
102 rin != const_end->size<loco::DataType::S32>() ||
103 rin != const_strides->size<loco::DataType::S32>())
104 {
105 throw oops::UserExn("Ranks for inputs should be same: ", _node->name());
106 }
107
108 // TODO support strides type of S64
109 // TODO support other strides value
110 // Only support stride 1 for now
111 uint32_t elements = const_strides->size<loco::DataType::S32>();
112 for (uint32_t e = 0; e < elements; ++e)
113 {
114 if (const_strides->at<loco::DataType::S32>(e) != 1)
115 {
116 throw oops::UserExn("Only stride 1 is supported for now: ", _node->name());
117 }
118 }
119}
120
121} // namespace
122
123namespace moco
124{
125
126bool StridedSliceGraphBuilder::validate(const tensorflow::NodeDef &node) const
127{
128 // TODO support node.input_size() == 3 where strides is None
129 if (node.input_size() != 4)
130 return false;
131
132 if (!plier::tf::has_attrs(node, {"T", "Index", "begin_mask", "end_mask", "ellipsis_mask",
133 "new_axis_mask", "shrink_axis_mask"}))
134 return false;
135
136 return true;
137}
138
139void StridedSliceGraphBuilder::build(const tensorflow::NodeDef &node,
140 GraphBuilderContext *context) const
141{
142 assert(context != nullptr);
143
144 loco::Graph *graph = context->graph();
145 SymbolTable *tensor_names = context->tensor_names();
146 UpdateQueue *updates = context->updates();
147
148 std::string node_name = node.name();
149
150 auto stridedslice = graph->nodes()->create<TFStridedSlice>();
151 stridedslice->name(node_name);
152
153 // read attributes
154 auto begin_mask = plier::tf::get_int_attr(node, "begin_mask");
155 auto end_mask = plier::tf::get_int_attr(node, "end_mask");
156 auto ellipsis_mask = plier::tf::get_int_attr(node, "ellipsis_mask");
157 auto new_axis_mask = plier::tf::get_int_attr(node, "new_axis_mask");
158 auto shrink_axis_mask = plier::tf::get_int_attr(node, "shrink_axis_mask");
159
160 stridedslice->begin_mask(begin_mask);
161 stridedslice->end_mask(end_mask);
162 stridedslice->ellipsis_mask(ellipsis_mask);
163 stridedslice->new_axis_mask(new_axis_mask);
164 stridedslice->shrink_axis_mask(shrink_axis_mask);
165
166 // TODO support general mask values: we support only this limited case for now
167 assert(begin_mask == 0);
168 assert(end_mask == 0);
169 assert(ellipsis_mask == 0);
170 assert(new_axis_mask == 0);
171 assert(shrink_axis_mask == 1);
172
173 // save the name for graph link updates
174 TensorName output_name(node_name, 0);
175 tensor_names->enroll(output_name, stridedslice);
176
177 std::vector<TensorName> input_names;
178 input_names.push_back(TensorName(node.input(0))); // input
179 input_names.push_back(TensorName(node.input(1))); // begin
180 input_names.push_back(TensorName(node.input(2))); // end
181 input_names.push_back(TensorName(node.input(3))); // strides
182
183 auto tfconv2d_update = std::make_unique<TFStridedSliceGraphUpdate>(stridedslice, input_names);
184
185 updates->enroll(std::move(tfconv2d_update));
186}
187
188} // namespace moco
A neural network graph.
Definition Graph.h:161
Class to store context to build loco graph IR from TensorFlow.
Interface to connect the graph.
virtual void input(const SymbolTable *) const =0
Do the graph input connections using the SymbolTable.
bool validate(const tensorflow::NodeDef &) const final
void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final
Class to store and query loco::Node* with string name key.
void enroll(const TensorName &tensor_name, loco::Node *node)
Registers a name with corresponding loco::Node *.
loco::Node * node(const TensorName &tensor_name) const
Queries enrolled(registered) with name and return node if found Will throw runtime_error if not found...
IR for tf.constant.
Definition TFConst.h:67
Class to store GraphUpdate objects.
void enroll(std::unique_ptr< GraphUpdate > &&update)
Registers GraphUpdate objects.
Exception to user.
Definition UserExn.h:42
CircleInput * input_node(loco::Graph *g, const loco::GraphInputIndex &index)
Find a Pull node with a given input index.
Definition Log.h:23
bool has_attrs(const tensorflow::NodeDef &node, const std::vector< std::string > &attr_names)
Definition Convert.cpp:35
int64_t get_int_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
Definition Convert.cpp:87
NodeName name(void) const
Definition TFNodeDecl.h:50