ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Pack.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 <loco.h>
25#include <loco/IR/NodeShape.h>
26#include <plier/tf/Convert.h>
27
28#include <memory>
29#include <cassert>
30
31namespace
32{
33
34using namespace moco;
35
36class TFPackGraphUpdate final : public GraphUpdate
37{
38public:
39 TFPackGraphUpdate(TFPack *node, std::vector<TensorName> names) : _node(node), _names(names) {}
40
41 void input(const SymbolTable *) const override;
42
43private:
44 TFPack *_node;
45 std::vector<TensorName> _names;
46};
47
48void TFPackGraphUpdate::input(const SymbolTable *tensor_names) const
49{
50 uint32_t num_values = _names.size();
51 assert(num_values >= 1);
52
53 for (uint32_t i = 0; i < num_values; ++i)
54 {
55 auto input_node = tensor_names->node(_names[i]);
56 assert(input_node != nullptr);
57 _node->values(i, input_node);
58 }
59}
60
61} // namespace
62
63namespace moco
64{
65
66bool PackGraphBuilder::validate(const tensorflow::NodeDef &node) const
67{
68 if (!plier::tf::has_attrs(node, {"T", "N", "axis"}))
69 return false;
70
71 const int num_inputs = node.input_size();
72 return (num_inputs >= 1) && (num_inputs == plier::tf::get_int_attr(node, "N"));
73}
74
75void PackGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const
76{
77 assert(context != nullptr);
78
79 auto graph = context->graph();
80 auto tensor_names = context->tensor_names();
81 auto updates = context->updates();
82
83 const int num_inputs = node.input_size();
84 std::vector<TensorName> input_names;
85 auto pack_node = graph->nodes()->create<TFPack>(num_inputs);
86 pack_node->name(node.name());
87
88 for (int ni = 0; ni < num_inputs; ++ni)
89 {
90 input_names.push_back(TensorName(node.input(ni)));
91 }
92
93 pack_node->axis(plier::tf::get_int_attr(node, "axis"));
94
95 TensorName output_name(node.name(), 0);
96 tensor_names->enroll(output_name, pack_node);
97
98 auto update = std::make_unique<TFPackGraphUpdate>(pack_node, input_names);
99 updates->enroll(std::move(update));
100}
101
102} // namespace moco
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 Pack.cpp:66
void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final
Definition Pack.cpp:75
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...
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
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