ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Relu.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 <cassert>
26#include <stdexcept>
27
28namespace
29{
30
31using namespace moco;
32
33class TFReluGraphUpdate final : public GraphUpdate
34{
35public:
36 TFReluGraphUpdate(TFRelu *node, const TensorName &&name) : _node(node), _name(name) {}
37
38 void input(const SymbolTable *) const override;
39
40private:
41 TFRelu *_node;
42 const TensorName _name;
43};
44
45void TFReluGraphUpdate::input(const SymbolTable *table) const
46{
47 loco::Node *target = table->node(_name);
48 _node->features(target);
49}
50
51} // namespace
52
53namespace moco
54{
55
56bool ReluGraphBuilder::validate(const tensorflow::NodeDef &node) const
57{
58 // ReLU node SHOULD have only one input
59 if (node.input_size() != 1)
60 return false;
61
62 return true;
63}
64
65void ReluGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const
66{
67 assert(context != nullptr);
68
69 loco::Graph *graph = context->graph();
70 SymbolTable *tensor_names = context->tensor_names();
71 UpdateQueue *updates = context->updates();
72
73 // Create a "TFRelu" node for Relu
74 auto relu_node = graph->nodes()->create<TFRelu>();
75 relu_node->name(node.name());
76
77 // register string-name to node
78 TensorName output_name(node.name(), 0);
79 tensor_names->enroll(output_name, relu_node);
80
81 // Queue node input update
82 auto update = std::make_unique<TFReluGraphUpdate>(relu_node, TensorName(node.input(0)));
83 updates->enroll(std::move(update));
84}
85
86} // 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 Relu.cpp:65
bool validate(const tensorflow::NodeDef &) const final
Definition Relu.cpp:56
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