ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Softmax.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#include <plier/tf/Convert.h>
23
24#include <memory>
25
26namespace
27{
28using namespace moco;
29
33class SoftmaxGraphUpdate final : public GraphUpdate
34{
35public:
36 SoftmaxGraphUpdate(TFSoftmax *node, const TensorName &&input_name)
37 : _node(node), _input_name(input_name)
38 {
39 // DO NOTHING
40 }
41
42 void input(const SymbolTable *) const override;
43
44private:
45 TFSoftmax *_node;
46 const TensorName _input_name;
47};
48
49void SoftmaxGraphUpdate::input(const SymbolTable *table) const
50{
51 loco::Node *input_node = table->node(_input_name);
52 _node->logits(input_node);
53}
54
55} // namespace
56
57namespace moco
58{
59
60bool SoftmaxGraphBuilder::validate(const tensorflow::NodeDef &node) const
61{
62 if (node.input_size() != 1)
63 return false;
64
65 return plier::tf::has_attrs(node, {"T"});
66}
67
68void SoftmaxGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const
69{
70 assert(context != nullptr);
71
72 loco::Graph *graph = context->graph();
73 SymbolTable *tensor_names = context->tensor_names();
74 UpdateQueue *updates = context->updates();
75
76 // creating TF dialect Softmax node
77 auto tf_softmax = graph->nodes()->create<TFSoftmax>();
78 tf_softmax->name(node.name());
79
80 TensorName output_name(node.name(), 0);
81 tensor_names->enroll(output_name, tf_softmax);
82
83 auto update = std::make_unique<SoftmaxGraphUpdate>(tf_softmax, TensorName(node.input(0)));
84 updates->enroll(std::move(update));
85}
86
87} // 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.
bool validate(const tensorflow::NodeDef &) const override
Definition Softmax.cpp:60
void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override
Definition Softmax.cpp:68
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.
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
NodeName name(void) const
Definition TFNodeDecl.h:50