forked from Samsung/ONE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelData.cpp
More file actions
180 lines (150 loc) · 4.63 KB
/
ModelData.cpp
File metadata and controls
180 lines (150 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ModelData.h"
#include <mio/circle/schema_generated.h>
#include <luci/Importer.h>
#include <luci/CircleExporter.h>
#include <luci/CircleFileExpContract.h>
#include <fstream>
#include <vector>
using namespace circle_resizer;
namespace
{
std::vector<uint8_t> read_model(const std::string &model_path)
{
std::ifstream file_stream(model_path, std::ios::in | std::ios::binary | std::ifstream::ate);
if (!file_stream.is_open())
{
throw std::runtime_error("Failed to open file: " + model_path);
}
std::streamsize size = file_stream.tellg();
file_stream.seekg(0, std::ios::beg);
std::vector<uint8_t> buffer(size);
if (!file_stream.read(reinterpret_cast<char *>(buffer.data()), size))
{
throw std::runtime_error("Failed to read file: " + model_path);
}
return buffer;
}
std::unique_ptr<luci::Module> load_module(const std::vector<uint8_t> &model_buffer)
{
flatbuffers::Verifier verifier{model_buffer.data(), model_buffer.size()};
if (!circle::VerifyModelBuffer(verifier))
{
throw std::runtime_error("Verification of the model failed");
}
const luci::GraphBuilderSource *source_ptr = &luci::GraphBuilderRegistry::get();
luci::Importer importer(source_ptr);
return importer.importModule(model_buffer.data(), model_buffer.size());
}
class BufferModelContract : public luci::CircleExporter::Contract
{
public:
BufferModelContract(luci::Module *module)
: _module(module), _buffer{std::make_unique<std::vector<uint8_t>>()}
{
}
luci::Module *module() const override { return _module; }
bool store(const char *ptr, const size_t size) const override
{
_buffer->resize(size);
std::copy(ptr, ptr + size, _buffer->begin());
return true;
}
std::vector<uint8_t> get_buffer() { return *_buffer; }
private:
luci::Module *_module;
std::unique_ptr<std::vector<uint8_t>> _buffer;
};
template <typename NodeType>
std::vector<Shape> extract_shapes(const std::vector<loco::Node *> &nodes)
{
std::vector<Shape> shapes;
for (const auto &loco_node : nodes)
{
std::vector<Dim> dims;
const auto circle_node = loco::must_cast<const NodeType *>(loco_node);
for (uint32_t dim_idx = 0; dim_idx < circle_node->rank(); dim_idx++)
{
if (circle_node->dim(dim_idx).known())
{
const int32_t dim_val = circle_node->dim(dim_idx).value();
dims.push_back(Dim{dim_val});
}
else
{
dims.push_back(Dim{-1});
}
}
shapes.push_back(Shape{dims});
}
return shapes;
}
} // namespace
ModelData::ModelData(const std::vector<uint8_t> &buffer)
: _buffer{buffer}, _module{load_module(buffer)}
{
}
ModelData::ModelData(const std::string &model_path) : ModelData(read_model(model_path)) {}
void ModelData::invalidate_module() { _module_invalidated = true; }
void ModelData::invalidate_buffer() { _buffer_invalidated = true; }
std::vector<uint8_t> &ModelData::buffer()
{
if (_buffer_invalidated)
{
luci::CircleExporter exporter;
BufferModelContract contract(module());
if (!exporter.invoke(&contract))
{
throw std::runtime_error("Exporting buffer from the model failed");
}
_buffer = contract.get_buffer();
_buffer_invalidated = false;
}
return _buffer;
}
luci::Module *ModelData::module()
{
if (_module_invalidated)
{
_module = load_module(_buffer);
_module_invalidated = false;
}
return _module.get();
}
void ModelData::save(std::ostream &stream)
{
auto &buff = buffer();
stream.write(reinterpret_cast<const char *>(buff.data()), buff.size());
if (!stream.good())
{
throw std::runtime_error("Failed to write to output stream");
}
}
void ModelData::save(const std::string &output_path)
{
std::ofstream out_stream(output_path, std::ios::out | std::ios::binary);
save(out_stream);
}
std::vector<Shape> ModelData::input_shapes()
{
return extract_shapes<luci::CircleInput>(loco::input_nodes(module()->graph()));
}
std::vector<Shape> ModelData::output_shapes()
{
return extract_shapes<luci::CircleOutput>(loco::output_nodes(module()->graph()));
}
ModelData::~ModelData() = default;