ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Concat.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
26#include <memory>
27#include <cassert>
28
29namespace
30{
31
32using namespace moco;
33
34class TFConcatV2GraphUpdate final : public GraphUpdate
35{
36public:
37 TFConcatV2GraphUpdate(TFConcatV2 *node, std::vector<TensorName> names)
38 : _node(node), _names(names)
39 {
40 }
41
42 void input(const SymbolTable *) const override;
43
44private:
45 TFConcatV2 *_node;
46 std::vector<TensorName> _names;
47};
48
49void TFConcatV2GraphUpdate::input(const SymbolTable *tensor_names) const
50{
51 uint32_t num_values = _names.size() - 1; // exclude axis
52 assert(num_values >= 1);
53
54 for (uint32_t i = 0; i < num_values; ++i)
55 {
56 auto input_node = tensor_names->node(_names[i]);
57 assert(input_node != nullptr);
58 _node->values(i, input_node);
59 }
60 auto axis_node = tensor_names->node(_names[num_values]);
61 assert(axis_node != nullptr);
62 _node->axis(axis_node);
63}
64
65} // namespace
66
67namespace moco
68{
69
70bool ConcatV2GraphBuilder::validate(const tensorflow::NodeDef &node) const
71{
72 if (!plier::tf::has_attrs(node, {"T", "N", "Tidx"}))
73 return false;
74
75 // Concat node SHOULD have 3 or more inputs, that is 2 + axis
76 const int num_inputs = node.input_size() - 1;
77 return (num_inputs >= 2) && (num_inputs == plier::tf::get_int_attr(node, "N"));
78}
79
80void ConcatV2GraphBuilder::build(const tensorflow::NodeDef &node,
81 GraphBuilderContext *context) const
82{
83 assert(context != nullptr);
84
85 auto graph = context->graph();
86 auto tensor_names = context->tensor_names();
87 auto updates = context->updates();
88
89 const int num_inputs = node.input_size() - 1;
90 std::vector<TensorName> input_names;
91 auto concat_node = graph->nodes()->create<TFConcatV2>(num_inputs);
92 concat_node->name(node.name());
93
94 for (int ni = 0; ni < num_inputs; ++ni)
95 {
96 input_names.push_back(TensorName(node.input(ni)));
97 }
98 // last one is the axis
99 input_names.push_back(TensorName(node.input(num_inputs)));
100
101 // register string-name to the last node as output of concat(s)
102 TensorName output_name(node.name(), 0);
103 tensor_names->enroll(output_name, concat_node);
104
105 auto update = std::make_unique<TFConcatV2GraphUpdate>(concat_node, input_names);
106 updates->enroll(std::move(update));
107}
108
109} // namespace moco
bool validate(const tensorflow::NodeDef &) const final
Definition Concat.cpp:70
void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final
Definition Concat.cpp:80
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.
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