-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathfile_utils.cc
More file actions
279 lines (250 loc) · 9.26 KB
/
file_utils.cc
File metadata and controls
279 lines (250 loc) · 9.26 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file file_utils.cc
*/
#include "file_utils.h"
#include <dmlc/json.h>
#include <dmlc/memory_io.h>
#include <tvm/ffi/function.h>
#include <tvm/ffi/reflection/registry.h>
#include <tvm/runtime/logging.h>
#include <tvm/runtime/serializer.h>
#include <fstream>
#include <unordered_map>
#include <utility>
#include <vector>
namespace tvm {
namespace runtime {
void FunctionInfo::Save(dmlc::JSONWriter* writer) const {
std::vector<std::string> sarg_types(arg_types.size());
for (size_t i = 0; i < arg_types.size(); ++i) {
sarg_types[i] = DLDataTypeToString(arg_types[i]);
}
writer->BeginObject();
writer->WriteObjectKeyValue("name", name);
writer->WriteObjectKeyValue("arg_types", sarg_types);
writer->WriteObjectKeyValue("storage_scopes", storage_scopes);
writer->WriteObjectKeyValue("launch_param_tags", launch_param_tags);
std::vector<int> iarg_extra_tags(arg_extra_tags.size());
for (size_t i = 0; i < arg_extra_tags.size(); ++i) {
iarg_extra_tags[i] = static_cast<int>(arg_extra_tags[i]);
}
writer->WriteObjectKeyValue("arg_extra_tags", iarg_extra_tags);
writer->EndObject();
}
void FunctionInfo::Load(dmlc::JSONReader* reader) {
dmlc::JSONObjectReadHelper helper;
std::vector<std::string> sarg_types;
helper.DeclareField("name", &name);
helper.DeclareField("arg_types", &sarg_types);
helper.DeclareOptionalField("storage_scopes", &storage_scopes);
helper.DeclareOptionalField("launch_param_tags", &launch_param_tags);
helper.DeclareOptionalField("thread_axis_tags",
&launch_param_tags); // for backward compatibility
std::vector<int> iarg_extra_tags;
helper.DeclareOptionalField("arg_extra_tags", &iarg_extra_tags);
arg_extra_tags.resize(iarg_extra_tags.size());
for (size_t i = 0; i < arg_extra_tags.size(); ++i) {
arg_extra_tags[i] = static_cast<ArgExtraTags>(iarg_extra_tags[i]);
}
helper.ReadAllFields(reader);
arg_types.resize(sarg_types.size());
for (size_t i = 0; i < arg_types.size(); ++i) {
arg_types[i] = StringToDLDataType(sarg_types[i]);
}
}
void FunctionInfo::Save(dmlc::Stream* writer) const {
writer->Write(name);
writer->Write(arg_types);
writer->Write(storage_scopes);
writer->Write(launch_param_tags);
writer->Write(arg_extra_tags);
}
bool FunctionInfo::Load(dmlc::Stream* reader) {
if (!reader->Read(&name)) return false;
if (!reader->Read(&arg_types)) return false;
if (!reader->Read(&storage_scopes)) return false;
if (!reader->Read(&launch_param_tags)) return false;
if (!reader->Read(&arg_extra_tags)) return false;
return true;
}
std::string GetFileFormat(const std::string& file_name, const std::string& format) {
std::string fmt = format;
if (fmt.length() == 0) {
size_t pos = file_name.find_last_of(".");
if (pos != std::string::npos) {
return file_name.substr(pos + 1, file_name.length() - pos - 1);
} else {
return "";
}
} else {
return format;
}
}
std::string GetCacheDir() {
char* env_cache_dir;
if ((env_cache_dir = getenv("TVM_CACHE_DIR"))) return env_cache_dir;
if ((env_cache_dir = getenv("XDG_CACHE_HOME"))) {
return std::string(env_cache_dir) + "/tvm";
}
if ((env_cache_dir = getenv("HOME"))) {
return std::string(env_cache_dir) + "/.cache/tvm";
}
return ".";
}
std::string GetFileBasename(const std::string& file_name) {
size_t last_slash = file_name.find_last_of("/");
if (last_slash == std::string::npos) return file_name;
return file_name.substr(last_slash + 1);
}
std::string GetMetaFilePath(const std::string& file_name) {
size_t pos = file_name.find_last_of(".");
if (pos != std::string::npos) {
return file_name.substr(0, pos) + ".tvm_meta.json";
} else {
return file_name + ".tvm_meta.json";
}
}
void LoadBinaryFromFile(const std::string& file_name, std::string* data) {
std::ifstream fs(file_name, std::ios::in | std::ios::binary);
ICHECK(!fs.fail()) << "Cannot open " << file_name;
// get its size:
fs.seekg(0, std::ios::end);
size_t size = static_cast<size_t>(fs.tellg());
fs.seekg(0, std::ios::beg);
data->resize(size);
fs.read(&(*data)[0], size);
}
void SaveBinaryToFile(const std::string& file_name, const std::string& data) {
std::ofstream fs(file_name, std::ios::out | std::ios::binary);
ICHECK(!fs.fail()) << "Cannot open " << file_name;
fs.write(&data[0], data.length());
}
void SaveMetaDataToFile(const std::string& file_name,
const std::unordered_map<std::string, FunctionInfo>& fmap) {
std::string version = "0.1.0";
std::ofstream fs(file_name.c_str());
ICHECK(!fs.fail()) << "Cannot open file " << file_name;
dmlc::JSONWriter writer(&fs);
writer.BeginObject();
writer.WriteObjectKeyValue("tvm_version", version);
writer.WriteObjectKeyValue("func_info", fmap);
writer.EndObject();
fs.close();
}
void LoadMetaDataFromFile(const std::string& file_name,
std::unordered_map<std::string, FunctionInfo>* fmap) {
std::ifstream fs(file_name.c_str());
ICHECK(!fs.fail()) << "Cannot open file " << file_name;
std::string version;
dmlc::JSONReader reader(&fs);
dmlc::JSONObjectReadHelper helper;
helper.DeclareField("tvm_version", &version);
helper.DeclareField("func_info", fmap);
helper.ReadAllFields(&reader);
fs.close();
}
void RemoveFile(const std::string& file_name) {
// FIXME: This doesn't check the return code.
std::remove(file_name.c_str());
}
void CopyFile(const std::string& src_file_name, const std::string& dest_file_name) {
std::ifstream src(src_file_name, std::ios::binary);
ICHECK(src) << "Unable to open source file '" << src_file_name << "'";
std::ofstream dest(dest_file_name, std::ios::binary | std::ios::trunc);
ICHECK(dest) << "Unable to destination source file '" << src_file_name << "'";
dest << src.rdbuf();
src.close();
dest.close();
ICHECK(dest) << "File-copy operation failed."
<< " src='" << src_file_name << "'"
<< " dest='" << dest_file_name << "'";
}
ffi::Map<ffi::String, Tensor> LoadParams(const std::string& param_blob) {
dmlc::MemoryStringStream strm(const_cast<std::string*>(¶m_blob));
return LoadParams(&strm);
}
ffi::Map<ffi::String, Tensor> LoadParams(dmlc::Stream* strm) {
ffi::Map<ffi::String, Tensor> params;
uint64_t header, reserved;
ICHECK(strm->Read(&header)) << "Invalid parameters file format";
ICHECK(header == kTVMTensorListMagic) << "Invalid parameters file format";
ICHECK(strm->Read(&reserved)) << "Invalid parameters file format";
std::vector<std::string> names;
ICHECK(strm->Read(&names)) << "Invalid parameters file format";
uint64_t sz;
strm->Read(&sz);
size_t size = static_cast<size_t>(sz);
ICHECK(size == names.size()) << "Invalid parameters file format";
for (size_t i = 0; i < size; ++i) {
// The data_entry is allocated on device, Tensor.load always load the array into CPU.
Tensor temp;
temp.Load(strm);
params.Set(names[i], temp);
}
return params;
}
void SaveParams(dmlc::Stream* strm, const ffi::Map<ffi::String, Tensor>& params) {
std::vector<std::string> names;
std::vector<const DLTensor*> arrays;
for (auto& p : params) {
names.push_back(p.first);
arrays.push_back(p.second.operator->());
}
uint64_t header = kTVMTensorListMagic, reserved = 0;
strm->Write(header);
strm->Write(reserved);
strm->Write(names);
{
uint64_t sz = static_cast<uint64_t>(arrays.size());
strm->Write(sz);
for (size_t i = 0; i < sz; ++i) {
tvm::runtime::SaveDLTensor(strm, arrays[i]);
}
}
}
std::string SaveParams(const ffi::Map<ffi::String, Tensor>& params) {
std::string bytes;
dmlc::MemoryStringStream strm(&bytes);
dmlc::Stream* fo = &strm;
SaveParams(fo, params);
return bytes;
}
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef()
.def("runtime.SaveParams",
[](const ffi::Map<ffi::String, Tensor>& params) {
std::string s = ::tvm::runtime::SaveParams(params);
return ffi::Bytes(std::move(s));
})
.def("runtime.SaveParamsToFile",
[](const ffi::Map<ffi::String, Tensor>& params, const ffi::String& path) {
tvm::runtime::SimpleBinaryFileStream strm(path, "wb");
SaveParams(&strm, params);
})
.def("runtime.LoadParams", [](const ffi::Bytes& s) { return ::tvm::runtime::LoadParams(s); })
.def("runtime.LoadParamsFromFile", [](const ffi::String& path) {
tvm::runtime::SimpleBinaryFileStream strm(path, "rb");
return LoadParams(&strm);
});
}
} // namespace runtime
} // namespace tvm