ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Squeeze.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 <loco.h>
24#include <plier/tf/Convert.h>
25#include <oops/UserExn.h>
26
27#include <memory>
28
29namespace
30{
31using namespace moco;
32
36class SqueezeGraphUpdate final : public GraphUpdate
37{
38public:
39 SqueezeGraphUpdate(TFSqueeze *node, const TensorName &&input_name)
40 : _node(node), _input_name(input_name)
41 {
42 // DO NOTHING
43 }
44
45 void input(const SymbolTable *) const override;
46
47private:
48 TFSqueeze *_node;
49 const TensorName _input_name;
50};
51
52void SqueezeGraphUpdate::input(const SymbolTable *table) const
53{
54 loco::Node *input_node = table->node(_input_name);
55 _node->input(input_node);
56}
57
58} // namespace
59
60namespace moco
61{
62
63bool SqueezeGraphBuilder::validate(const tensorflow::NodeDef &node) const
64{
65 if (node.input_size() != 1)
66 return false;
67
68 if (!plier::tf::has_attrs(node, {"T"}))
69 return false;
70
71 if (plier::tf::has_attrs(node, {"axis"}))
72 {
73 // TODO support 'axis' attribute
74 oops::UserExn("Squeeze: Unsupported 'axis' attribute", node.name());
75 }
76
77 return true;
78}
79
80void SqueezeGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const
81{
82 assert(context != nullptr);
83
84 loco::Graph *graph = context->graph();
85 SymbolTable *tensor_names = context->tensor_names();
86 UpdateQueue *updates = context->updates();
87
88 // TODO support 'axis' attribute
89 assert(!plier::tf::has_attrs(node, {"axis"}));
90
91 std::vector<int64_t> squeeze_dims;
92 if (plier::tf::has_attrs(node, {"squeeze_dims"}))
93 {
94 auto squeeze_dim_list = plier::tf::get_list_attr(node, {"squeeze_dims"});
95 // TODO assert squeeze_dims are mutually different?
96 squeeze_dims = plier::tf::as_int64_list(squeeze_dim_list);
97 }
98 // Note that it is possible that NodeDef does not have squeeze_dims attribute.
99 // In that case, TFSqueeze also has empty squeeze_dims,
100
101 // creating TF dialect Squeeze node
102 auto tf_squeeze = graph->nodes()->create<TFSqueeze>();
103 tf_squeeze->name(node.name());
104 tf_squeeze->squeeze_dims(squeeze_dims);
105
106 TensorName output_name(node.name(), 0);
107 tensor_names->enroll(output_name, tf_squeeze);
108
109 auto update = std::make_unique<SqueezeGraphUpdate>(tf_squeeze, TensorName(node.input(0)));
110 updates->enroll(std::move(update));
111}
112
113} // namespace moco
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.
void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override
Definition Squeeze.cpp:80
bool validate(const tensorflow::NodeDef &) const override
Definition Squeeze.cpp:63
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
FeatureShapeUpdater update(loco::FeatureShape &feature_shape)
bool has_attrs(const tensorflow::NodeDef &node, const std::vector< std::string > &attr_names)
Definition Convert.cpp:35
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