ONE - On-device Neural Engine
Loading...
Searching...
No Matches
MaxPool.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
20
21#include <moco/Names.h>
22
23#include "Convert.h"
24
25#include <loco.h>
27#include <plier/tf/Convert.h>
28#include <oops/UserExn.h>
29
30#include <memory>
31#include <cassert>
32#include <stdexcept>
33
34namespace
35{
36
37using namespace moco;
38
39class TFMaxPoolGraphUpdate final : public GraphUpdate
40{
41public:
42 TFMaxPoolGraphUpdate(TFMaxPool *node, const TensorName &name)
43 : _maxpool_node(node), _input_name(name)
44 {
45 }
46
47 void input(const SymbolTable *) const override;
48
49private:
50 TFMaxPool *_maxpool_node;
51 const TensorName _input_name;
52};
53
54void TFMaxPoolGraphUpdate::input(const SymbolTable *node_table) const
55{
56 loco::Node *input_node = node_table->node(_input_name);
57 _maxpool_node->input(input_node);
58}
59
60} // namespace
61
62namespace moco
63{
64
65bool MaxPoolGraphBuilder::validate(const tensorflow::NodeDef &node) const
66{
67 // note: even though "data_format" is not entered when a model is written,
68 // TF seems to generate "data_format" field into a pb file
69 if (!plier::tf::has_attrs(node, {"T", "data_format", "ksize", "padding", "strides"}))
70 return false;
71
72 auto data_layout = plier::tf::get_string_attr(node, "data_format");
73 if (!(data_layout == "NHWC" || data_layout == "NCHW"))
74 {
75 throw oops::UserExn("MaxPool Unsupported data_format", node.name());
76 }
77
78 auto tf_ksize = plier::tf::get_list_attr(node, "ksize");
79 auto ksize = plier::tf::as_int64_list(tf_ksize);
80 if (ksize.size() != 4)
81 {
82 // TODO support ksize length for 1 and 2
83 throw oops::UserExn("MaxPool ksize requires rank 4", node.name());
84 }
85
86 auto tf_strides = plier::tf::get_list_attr(node, "strides");
87 auto strides = plier::tf::as_int64_list(tf_strides);
88 if (strides.size() != 4)
89 {
90 // TODO support strides length for 1 and 2
91 throw oops::UserExn("MaxPool strides requires rank 4", node.name());
92 }
93
94 return true;
95}
96
97void MaxPoolGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const
98{
99 assert(context != nullptr);
100
101 loco::Graph *graph = context->graph();
102 SymbolTable *tensor_names = context->tensor_names();
103 UpdateQueue *updates = context->updates();
104
105 // name of loco nodes
106 ::std::string node_name = node.name();
107
108 // tensorflow data_format: one of NHWC or NCHW.
109 auto data_layout = plier::tf::get_string_attr(node, "data_format");
110 auto maxPool_node = graph->nodes()->create<TFMaxPool>();
111 maxPool_node->name(node.name());
112 maxPool_node->data_layout(data_layout);
113
114 // padding
115 auto padding = moco::str_toupper(plier::tf::get_string_attr(node, "padding"));
116 maxPool_node->padding(padding);
117
118 // ksize
119 auto tf_ksize = plier::tf::get_list_attr(node, "ksize");
120 auto ksize = plier::tf::as_int64_list(tf_ksize);
121 assert(ksize.size() == 4);
122 maxPool_node->ksize(ksize);
123
124 // strides
125 auto tf_strides = plier::tf::get_list_attr(node, "strides");
126 auto strides = plier::tf::as_int64_list(tf_strides);
127 assert(strides.size() == 4);
128 maxPool_node->strides(strides);
129
130 // To set the input node of encode_node with node_name
131 TensorName output_name(node_name, 0);
132 tensor_names->enroll(output_name, maxPool_node);
133
134 // Record ifm inputs to featureEncode_node
135 auto update = std::make_unique<TFMaxPoolGraphUpdate>(maxPool_node, TensorName(node.input(0)));
136
137 updates->enroll(std::move(update));
138}
139
140} // namespace moco
141
142// TODO Consider a case when TF MaxPool is for 3D.
143// MaxPool works for 2D and other Dimensions, such as 3D
144// So, in future, some other GraphBuilder decide if MaxPoolGraphBuilder is used or
145// other GraphBuilder is used for TF MaxPool
A neural network graph.
Definition Graph.h:161
Logical unit of computation.
Definition Node.h:54
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
Definition MaxPool.cpp:65
void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final
Definition MaxPool.cpp:97
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...
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
std::string str_toupper(std::string s)
Definition Convert.cpp:27
FeatureShapeUpdater update(loco::FeatureShape &feature_shape)
bool has_attrs(const tensorflow::NodeDef &node, const std::vector< std::string > &attr_names)
Definition Convert.cpp:35
const std::string & get_string_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
Definition Convert.cpp:79
std::vector< int64_t > as_int64_list(const tensorflow::AttrValue_ListValue &lv)
Definition Convert.cpp:111
const tensorflow::AttrValue_ListValue & get_list_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
Definition Convert.cpp:70
NodeName name(void) const
Definition TFNodeDecl.h:50