ONE - On-device Neural Engine
Loading...
Searching...
No Matches
CustomopConfLoader.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 "CustomopConfLoader.h"
18
19#include <loco.h>
20#include <cwrap/Fildes.h>
21#include <angkor/TensorShape.h>
22
23#include <CustomOpInfo.pb.h>
24
25#include <google/protobuf/io/zero_copy_stream_impl.h>
26#include <google/protobuf/text_format.h>
27
28#include <fcntl.h>
29
30#include <limits> // std::numeric_limits
31
32namespace
33{
34bool load_text(const cwrap::Fildes &fildes, tf2tflite::CustomOpInfoDef &def)
35{
36 google::protobuf::io::FileInputStream fis(fildes.get());
37
38 return google::protobuf::TextFormat::Parse(&fis, &def);
39}
40
41angkor::TensorShape convert_shape(const tf2tflite::ShapeProto &shape)
42{
43 angkor::TensorShape to_shape;
44
45 int64_t rank64 = shape.dim_size();
46 assert(rank64 < std::numeric_limits<uint32_t>::max());
47
48 int32_t rank = static_cast<int32_t>(rank64);
49 to_shape.resize(rank);
50
51 for (int32_t d = 0; d < rank; d++)
52 {
53 int64_t dim_value = shape.dim(d).size();
54 assert(dim_value >= 0ULL);
55 assert(dim_value < std::numeric_limits<uint32_t>::max());
56
57 uint32_t dim_value32 = static_cast<uint32_t>(dim_value);
58 to_shape.dim(d) = dim_value32;
59 }
60
61 return to_shape;
62}
63
64loco::DataType convert_dtype(const tf2tflite::DataType &dtype)
65{
66 if (dtype == tf2tflite::DT_FLOAT)
67 return loco::DataType::FLOAT32;
68 else if (dtype == tf2tflite::DT_INT32)
69 return loco::DataType::S32;
70 else
71 throw std::runtime_error("Not yet supported datatype. Cannot convert.");
72}
73
74// Note : the following functions look similar with plier::tf::Convert.h.
75// However, the schema is different.(not "tensorflow::..." but "tf2tflite::...")
76// So, plier::tf cannot be used.
77loco::DataType get_dtype_attr(const tf2tflite::CustomOpDef &custom_op)
78{
79 std::string type_attr_name("dtype");
80
81 assert(custom_op.attr().count(type_attr_name) > 0);
82 const auto &attr = custom_op.attr().at(type_attr_name);
83 assert(attr.value_case() == tf2tflite::AttrValue::kType);
84 auto dtype_def = attr.type();
85
86 return convert_dtype(dtype_def);
87}
88
89angkor::TensorShape get_shape_attr(const tf2tflite::CustomOpDef &custom_op)
90{
91 std::string shape_attr_name("output_shape");
92
93 assert(custom_op.attr().count(shape_attr_name) > 0);
94 const auto &attr = custom_op.attr().at(shape_attr_name);
95 assert(attr.value_case() == tf2tflite::AttrValue::kShape);
96 auto shape_def = attr.shape();
97
98 return convert_shape(shape_def);
99}
100
101void add_customop(tf2tflite::CustomOpInfoDef &def, moco::ModelSignature &sig)
102{
103 for (const auto &custom_op : def.custom_op())
104 {
105 sig.add_customop(custom_op.op());
106
107 auto name = custom_op.name();
108
109 // setting dtype and shape to ModelSignature
110 sig.dtype(name, get_dtype_attr(custom_op));
111 sig.shape(name, get_shape_attr(custom_op));
112 }
113}
114
115} // namespace
116
117namespace tf2tflite
118{
119
120void load_customop_conf(const std::string &path, moco::ModelSignature &sig)
121{
122 CustomOpInfoDef def;
123
124 cwrap::Fildes fildes{open(path.c_str(), O_RDONLY)};
125
126 if (fildes.get() < 0)
127 {
128 throw std::runtime_error{"Error: " + path + " not found"};
129 }
130
131 if (!load_text(fildes, def))
132 {
133 throw std::runtime_error{"Error: Failed to parse prototxt " + path};
134 }
135
136 add_customop(def, sig);
137}
138
139} // namespace tf2tflite
POSIX File Descriptor.
Definition Fildes.h:29
int get(void) const
Definition Fildes.cpp:74
uint32_t & dim(uint32_t axis)
Definition Shape.cpp:42
Shape & resize(uint32_t size)
Definition Shape.cpp:36
DataType
"scalar" value type
Definition DataType.h:27
const tensorflow::TensorShapeProto & get_shape_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
Definition Convert.cpp:52
void load_customop_conf(const std::string &path, moco::ModelSignature &sig)
Loads customop.conf into ModelSignature.
Class to store information to run a model. Normally this info comes from users via CLI params or conf...
void dtype(const std::string &node_name, loco::DataType dtype)
Adds node name and its dtype provided from user.
void add_customop(const std::string &op)
Adds customop op type (not name of node) provided from user.
void shape(const std::string &node_name, const angkor::TensorShape &shape)
Adds node name and its shape provided from user.