ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Identity.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#include <loco.h>
23
24#include <memory>
25#include <vector>
26
27namespace
28{
29
30using namespace moco;
31
32class TFIdentityGraphUpdate final : public GraphUpdate
33{
34public:
35 TFIdentityGraphUpdate(TFIdentity *node, const std::vector<TensorName> &names)
36 : _node(node), _names(names)
37 {
38 }
39
40 void input(const SymbolTable *) const override;
41
42private:
43 TFIdentity *_node;
44 const std::vector<TensorName> _names;
45};
46
47void TFIdentityGraphUpdate::input(const SymbolTable *tensor_names) const
48{
49 for (auto &name : _names)
50 {
51 loco::Node *target = tensor_names->node(name);
52 _node->input(target);
53 }
54}
55
56} // namespace
57
58namespace moco
59{
60
61bool IdentityGraphBuilder::validate(const tensorflow::NodeDef &node) const
62{
63 if (node.input_size() < 1) // from TensorFlow lite toco
64 return false;
65
66 return true;
67}
68
69void IdentityGraphBuilder::build(const tensorflow::NodeDef &node,
70 GraphBuilderContext *context) const
71{
72 loco::Graph *graph = context->graph();
73 SymbolTable *tensor_names = context->tensor_names();
74 UpdateQueue *updates = context->updates();
75
76 // Create a Identity node
77 auto identity_node = graph->nodes()->create<TFIdentity>();
78 identity_node->name(node.name());
79
80 // register string-name to node
81 TensorName output_name(node.name(), 0);
82 tensor_names->enroll(output_name, identity_node);
83
84 // Queue node input update
85 // TODO: Check if we really need multiple input handlings
86 std::vector<TensorName> names;
87 for (int i = 0; i < node.input_size(); ++i)
88 {
89 names.emplace_back(TensorName(node.input(i)));
90 }
91 auto update = std::make_unique<TFIdentityGraphUpdate>(identity_node, names);
92 updates->enroll(std::move(update));
93}
94
95} // 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 final
Definition Identity.cpp:69
bool validate(const tensorflow::NodeDef &) const final
Definition Identity.cpp:61
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.
Definition Log.h:23
FeatureShapeUpdater update(loco::FeatureShape &feature_shape)
Option< std::string > target(optname("--target"), overview("select target language to emit for given architecture." "Valid values are '" NNC_TARGET_ARM_CPP "', '" NNC_TARGET_X86_CPP "', '" NNC_TARGET_ARM_GPU_CPP "', '" NNC_TARGET_INTERPRETER "'"), std::string(), optional(false), optvalues(NNC_TARGET_ARM_CPP "," NNC_TARGET_X86_CPP "," NNC_TARGET_ARM_GPU_CPP "," NNC_TARGET_INTERPRETER), nullptr, separators("="))
Definition Options.h:47
NodeName name(void) const
Definition TFNodeDecl.h:50