ONE - On-device Neural Engine
Loading...
Searching...
No Matches
SquaredDifference.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 <loco.h>
22
23#include <memory>
24
25namespace
26{
27
28using namespace moco;
29
33class TFSquaredDifferenceGraphUpdate final : public GraphUpdate
34{
35public:
36 TFSquaredDifferenceGraphUpdate(TFSquaredDifference *node, std::vector<TensorName> names)
37 : _node(node), _names(names)
38 {
39 }
40
41 void input(const SymbolTable *) const override;
42
43private:
45 std::vector<TensorName> _names;
46};
47
48void TFSquaredDifferenceGraphUpdate::input(const SymbolTable *table) const
49{
50 assert(_names.size() == 2);
51
52 _node->x(table->node(_names[0]));
53 _node->y(table->node(_names[1]));
54}
55
56} // namespace
57
58namespace moco
59{
60
61bool SquaredDifferenceGraphBuilder::validate(const tensorflow::NodeDef &node) const
62{
63 return node.input_size() == 2;
64}
65
66void SquaredDifferenceGraphBuilder::build(const tensorflow::NodeDef &node,
67 GraphBuilderContext *context) const
68{
69 assert(context != nullptr);
70
71 loco::Graph *graph = context->graph();
72 SymbolTable *tensor_names = context->tensor_names();
73 UpdateQueue *updates = context->updates();
74
75 // creating TF dialect SquaredDifference node
76 auto tf_sqdiff = graph->nodes()->create<TFSquaredDifference>();
77 tf_sqdiff->name(node.name());
78
79 // register string-name to node
80 TensorName output_name(node.name(), 0);
81 tensor_names->enroll(output_name, tf_sqdiff);
82
83 std::vector<TensorName> add_input_names;
84 add_input_names.push_back(TensorName(node.input(0))); // x
85 add_input_names.push_back(TensorName(node.input(1))); // y
86
87 // Queue node input update
88 auto tf_sqrt_update =
89 std::make_unique<TFSquaredDifferenceGraphUpdate>(tf_sqdiff, add_input_names);
90 updates->enroll(std::move(tf_sqrt_update));
91}
92
93} // namespace moco
A neural network graph.
Definition Graph.h:161
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 override
void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override
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
NodeName name(void) const
Definition TFNodeDecl.h:50