ONE - On-device Neural Engine
Loading...
Searching...
No Matches
moco::MaxPoolGraphBuilder Class Reference

#include <MaxPool.h>

Collaboration diagram for moco::MaxPoolGraphBuilder:

Public Member Functions

bool validate (const tensorflow::NodeDef &) const final
 
void build (const tensorflow::NodeDef &, GraphBuilderContext *) const final
 
- Public Member Functions inherited from moco::GraphBuilder
virtual ~GraphBuilder ()
 

Detailed Description

Definition at line 25 of file MaxPool.h.

Member Function Documentation

◆ build()

void moco::MaxPoolGraphBuilder::build ( const tensorflow::NodeDef &  node,
GraphBuilderContext context 
) const
finalvirtual

Implements moco::GraphBuilder.

Definition at line 97 of file MaxPool.cpp.

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}
A neural network graph.
Definition Graph.h:161
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 *.
Class to store GraphUpdate objects.
void enroll(std::unique_ptr< GraphUpdate > &&update)
Registers GraphUpdate objects.
std::string str_toupper(std::string s)
Definition Convert.cpp:27
FeatureShapeUpdater update(loco::FeatureShape &feature_shape)
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

References plier::tf::as_int64_list(), moco::SymbolTable::enroll(), moco::UpdateQueue::enroll(), plier::tf::get_list_attr(), plier::tf::get_string_attr(), moco::GraphBuilderContext::graph(), moco::TFNode::name(), moco::str_toupper(), moco::GraphBuilderContext::tensor_names(), moco::update(), and moco::GraphBuilderContext::updates().

◆ validate()

bool moco::MaxPoolGraphBuilder::validate ( const tensorflow::NodeDef &  node) const
finalvirtual

Implements moco::GraphBuilder.

Definition at line 65 of file MaxPool.cpp.

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}
Exception to user.
Definition UserExn.h:42
bool has_attrs(const tensorflow::NodeDef &node, const std::vector< std::string > &attr_names)
Definition Convert.cpp:35

References plier::tf::as_int64_list(), plier::tf::get_list_attr(), plier::tf::get_string_attr(), and plier::tf::has_attrs().


The documentation for this class was generated from the following files: