ONE - On-device Neural Engine
Loading...
Searching...
No Matches
COpCall.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
17#include "COpCall.h"
18
19#include "Convert.h"
20
21#include <locoex/COpCall.h>
22#include <locoex/COpAttrTypes.h>
23#include <moco/Names.h>
24#include <moco/tf/Frontend.h>
25#include <loco.h>
26#include <oops/UserExn.h>
27
28#include <memory>
29#include <vector>
30#include <cassert>
31#include <stdexcept>
32
33namespace
34{
35
36class COpCallGraphUpdate final : public moco::GraphUpdate
37{
38public:
39 COpCallGraphUpdate(locoex::COpCall *node, const std::vector<moco::TensorName> &input_names)
40 : _node(node), _input_names(input_names)
41 {
42 }
43
44 void input(const moco::SymbolTable *) const override;
45
46private:
47 locoex::COpCall *_node;
48 const std::vector<moco::TensorName> _input_names;
49};
50
51void COpCallGraphUpdate::input(const moco::SymbolTable *tensor_names) const
52{
53 for (int n = 0; n < _input_names.size(); n++)
54 {
55 loco::Node *target = tensor_names->node(_input_names.at(n));
56 _node->input(n, target);
57 }
58}
59
60} // namespace
61
62namespace moco
63{
64namespace tf
65{
66
67bool COpCallGraphBuilder::validate(const tensorflow::NodeDef &tf_node) const { return true; }
68
69void COpCallGraphBuilder::build(const tensorflow::NodeDef &tf_node,
70 GraphBuilderContext *context) const
71{
72 assert(context != nullptr);
73
74 loco::Graph *graph = context->graph();
75 SymbolTable *tensor_names = context->tensor_names();
76 UpdateQueue *updates = context->updates();
77
78 // Create a "COpCall" node for CustomOp and set attributes
79 auto call_node = graph->nodes()->create<locoex::COpCall>(tf_node.input_size());
80 {
81 call_node->op(tf_node.op());
82 call_node->name(tf_node.name());
83 call_node->dtype(_signature->dtype(tf_node.name()));
84
85 auto shape = _signature->shape(tf_node.name());
86 call_node->rank(shape->rank());
87 for (int d = 0; d < shape->rank(); d++)
88 call_node->dim(d) = shape->dim(d);
89
90 for (auto iter = tf_node.attr().begin(); iter != tf_node.attr().end(); iter++)
91 {
92 auto name = iter->first;
93 auto val = iter->second;
94
95 if (val.value_case() == tensorflow::AttrValue::kF)
96 {
97 call_node->attr(name, std::make_unique<locoex::COpAttrFloat>(val.f()));
98 }
99 else if (val.value_case() == tensorflow::AttrValue::kI)
100 {
101 call_node->attr(name, std::make_unique<locoex::COpAttrInt>(val.i()));
102 }
103 // TODO define more types
104 else
105 {
106 throw oops::UserExn("Unsupported attribute type", tf_node.name());
107 }
108 }
109 }
110
111 // register this node with its name
112 TensorName output_name(tf_node.name(), 0);
113 tensor_names->enroll(output_name, call_node);
114
115 // Queue node input update
116 std::vector<TensorName> input_names;
117 for (int i = 0; i < tf_node.input_size(); ++i)
118 {
119 input_names.emplace_back(TensorName(tf_node.input(i)));
120 }
121 auto update = std::make_unique<COpCallGraphUpdate>(call_node, input_names);
122 updates->enroll(std::move(update));
123}
124
125} // namespace tf
126} // namespace moco
A neural network graph.
Definition Graph.h:161
Logical unit of computation.
Definition Node.h:54
Class to calls custom operation.
Definition COpCall.h:38
void op(const std::string &op)
Definition COpCall.h:43
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...
Class to store GraphUpdate objects.
void enroll(std::unique_ptr< GraphUpdate > &&update)
Registers GraphUpdate objects.
bool validate(const tensorflow::NodeDef &) const override
Definition COpCall.cpp:67
void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override
Definition COpCall.cpp:69
Exception to user.
Definition UserExn.h:42
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
void dtype(const std::string &node_name, loco::DataType dtype)
Adds node name and its dtype provided from user.
void shape(const std::string &node_name, const angkor::TensorShape &shape)
Adds node name and its shape provided from user.