forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization.cpp
412 lines (380 loc) · 12.8 KB
/
serialization.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <torch/csrc/python_headers.h>
#include <system_error>
#include <vector>
#include <ATen/ops/from_blob.h>
#include <c10/core/CPUAllocator.h>
#include <torch/csrc/THP.h>
#include <torch/csrc/serialization.h>
template <class io>
Py_ssize_t doPartialRead(io fildes, void* buf, size_t nbytes);
template <class io>
Py_ssize_t doPartialWrite(io fildes, void* buf, size_t nbytes);
static Py_ssize_t doPartialPythonReadBuffered(
PyObject* fildes,
void* buf,
size_t nbytes);
static Py_ssize_t doPartialPythonReadInto(
PyObject* fildes,
void* buf,
size_t nbytes);
static Py_ssize_t doPartialPythonWrite(
PyObject* fildes,
void* buf,
size_t nbytes);
template <>
Py_ssize_t doPartialRead<int>(int fildes, void* buf, size_t nbytes) {
return read(fildes, buf, nbytes);
}
template <>
Py_ssize_t doPartialRead<PyObject*>(
PyObject* fildes,
void* buf,
size_t nbytes) {
// Try to use fildes.readinto() instead of fildes.read()
// because it is more memory efficient.
// TODO: Stop calling PyObject_HasAttrString() in a loop on our read loop
auto has_readinto = PyObject_HasAttrString(fildes, "readinto") == 1;
if (has_readinto) {
return doPartialPythonReadInto(fildes, buf, nbytes);
}
return doPartialPythonReadBuffered(fildes, buf, nbytes);
}
template <>
Py_ssize_t doPartialWrite<int>(int fildes, void* buf, size_t nbytes) {
return write(fildes, buf, nbytes);
}
template <>
Py_ssize_t doPartialWrite<PyObject*>(
PyObject* fildes,
void* buf,
size_t nbytes) {
return doPartialPythonWrite(fildes, buf, nbytes);
}
static inline bool isUnsupportedOperation() {
THPObjectPtr io(PyImport_ImportModule("io"));
if (!io)
throw python_error();
THPObjectPtr exception(PyObject_GetAttrString(io, "UnsupportedOperation"));
if (!exception)
throw python_error();
return PyErr_ExceptionMatches(exception.get());
}
// Call Python fildes.read(nbytes) and copy it to buf.
static inline Py_ssize_t doPartialPythonReadBuffered(
PyObject* fildes,
void* buf,
size_t raw_nbytes) {
// If we request a large amount of data, f.read() will internally try to
// allocate a buffer of that size. This is counterproductive, because
// it's not the buffer we ultimately want to write the data into. Read
// less than that and avoid allocating too much extra memory.
// TODO: Maybe 260 KB is a bit small...
const size_t nbytes = std::min<size_t>(raw_nbytes, 262144u); // 2^18 (~260 KB)
THPObjectPtr r(PyObject_CallMethod(fildes, "read", "i", nbytes));
if (!r)
throw python_error();
auto size = PyBytes_GET_SIZE(r.get());
const void* py_buf = PyBytes_AsString(r.get());
// we read EOF
if (size == 0) {
return 0;
}
// Slurp it into the buffer we actually want
memcpy(buf, py_buf, size);
return size;
}
// Either does fildes.readinto(buf) or fildes.write(buf)
static inline Py_ssize_t doPartialPythonIO(
PyObject* fildes,
void* buf,
size_t nbytes,
bool is_read) {
auto rw_flag = is_read ? PyBUF_WRITE : PyBUF_READ;
THPObjectPtr memview(PyMemoryView_FromMemory(
reinterpret_cast<char*>(buf), static_cast<Py_ssize_t>(nbytes), rw_flag));
if (!memview)
throw python_error();
std::string method = "write";
if (is_read) {
method = "readinto";
}
THPObjectPtr r(
PyObject_CallMethod(fildes, method.c_str(), "O", memview.get()));
if (r) {
return PyLong_AsSsize_t(r.get());
}
// fildes.readinto can return UnsupportedOperation so fall back to
// fildes.read.
if (is_read && isUnsupportedOperation()) {
PyErr_Clear();
return doPartialPythonReadBuffered(fildes, buf, nbytes);
}
throw python_error();
}
// Call Python fildes.readinto(buf)
static Py_ssize_t doPartialPythonReadInto(
PyObject* fildes,
void* buf,
size_t nbytes) {
return doPartialPythonIO(fildes, buf, nbytes, /* is_read */ true);
}
// Call Python fildes.write(buf)
static Py_ssize_t doPartialPythonWrite(
PyObject* fildes,
void* buf,
size_t nbytes) {
return doPartialPythonIO(fildes, buf, nbytes, /* is_read */ false);
}
// Requires that we read EXACTLY nbytes; fails if we don't.
template <typename io>
void doRead(io fildes, void* raw_buf, size_t nbytes) {
char* buf = static_cast<char*>(raw_buf);
while (nbytes > 0) {
errno = 0; // doPartialRead may not set errno
// we read in 1GB blocks to avoid bugs on Mac OS X Lion
// see https://github.com/pytorch/pytorch/issues/1031 for more details
Py_ssize_t r =
doPartialRead(fildes, buf, std::min<size_t>(nbytes, 1073741824));
if (r < 0) {
int err = errno;
TORCH_INTERNAL_ASSERT(
err != 0, "read(): impossible! r < 0, but no errno was set");
TORCH_INTERNAL_ASSERT(
err != EAGAIN,
"read(): non-blocking fd ",
fildes,
" read EAGAIN; cowardly refusing to spin-wait");
if (err == EINTR) {
continue;
} else {
TORCH_CHECK(
false, "read(): fd ", fildes, " failed with ", strerror(err));
}
} else if (r == 0) {
break;
}
buf += r;
// This is guaranteed by POSIX, but I just want to be double-sure
// to not underflow a signed integer.
AT_ASSERT(static_cast<size_t>(r) <= nbytes);
nbytes -= r;
}
if (nbytes != 0) {
TORCH_CHECK(
false,
"unexpected EOF, expected ",
nbytes,
" more bytes. The file might be corrupted.");
}
}
template <typename io>
void doWrite(io fildes, void* raw_buf, size_t nbytes) {
char* buf = static_cast<char*>(raw_buf);
while (nbytes > 0) {
errno = 0; // doPartialWrite may not set errno
// we write in 1GB blocks to avoid bugs on Mac OS X Lion
// see https://github.com/pytorch/pytorch/issues/1031 for more details
Py_ssize_t r =
doPartialWrite(fildes, buf, std::min<size_t>(nbytes, 1073741824));
if (r < 0) {
int err = errno;
TORCH_INTERNAL_ASSERT(
err != 0, "write(): impossible! r < 0, but no errno was set");
TORCH_INTERNAL_ASSERT(
err != EAGAIN,
"write(): non-blocking fd ",
fildes,
" read EAGAIN; cowardly refusing to spin-wait");
if (err == EINTR) {
continue;
} else {
TORCH_CHECK(
false, "write(): fd ", fildes, " failed with ", strerror(err));
}
}
buf += r;
AT_ASSERT(static_cast<size_t>(r) <= nbytes);
nbytes -= r;
}
}
// save_save is necessary since the old eager format saved storages as
// [size + data], but the v1.5 eager format removes this since size is saved in
// the filesize.
template <class io>
void THPStorage_writeFileRaw(
c10::StorageImpl* self,
io fd,
bool save_size,
uint64_t element_size) {
c10::DeviceGuard guard(self->device());
uint8_t* data{};
at::Tensor cpu_tensor;
size_t size_bytes = self->nbytes();
size_t numel = size_bytes / element_size;
if (self->device_type() == at::kCPU) {
// We are using a mutable pointer here because we're ultimately
// calling into a Python API that requires that, even though it
// won't mutate the data.
data = static_cast<uint8_t*>(self->mutable_data());
} else {
// Here we use a tensor.to() to impl D2H for all non-CPU device.
auto device_tensor = at::from_blob(
self->mutable_data(),
{static_cast<int64_t>(size_bytes)},
{1},
nullptr,
at::device(self->device()).dtype(c10::kByte),
{self->device()});
cpu_tensor = device_tensor.to(at::kCPU);
data = (uint8_t*)cpu_tensor.data_ptr();
}
if (save_size) {
if (torch::utils::THP_nativeByteOrder() ==
torch::utils::THPByteOrder::THP_LITTLE_ENDIAN)
doWrite(fd, &numel, sizeof(int64_t));
else {
int64_t nsize{}; // convert big endian cpu to little endian storage
torch::utils::THP_encodeBuffer(
(uint8_t*)&nsize,
(const int64_t*)&numel,
torch::utils::THPByteOrder::THP_LITTLE_ENDIAN,
1);
doWrite(fd, &nsize, sizeof(int64_t));
}
}
// fast track for bytes and little endian
if (element_size == 1 ||
torch::utils::THP_nativeByteOrder() ==
torch::utils::THPByteOrder::THP_LITTLE_ENDIAN) {
doWrite(fd, data, size_bytes);
} else {
size_t buffer_size = std::min(numel, (size_t)5000);
std::vector<uint8_t> le_buffer;
le_buffer.resize(buffer_size * element_size);
for (size_t i = 0; i < numel; i += buffer_size) {
size_t to_convert = std::min(numel - i, buffer_size);
if (element_size == 2) {
torch::utils::THP_encodeBuffer(
le_buffer.data(),
(const int16_t*)data + i,
torch::utils::THPByteOrder::THP_LITTLE_ENDIAN,
to_convert);
} else if (element_size == 4) {
torch::utils::THP_encodeBuffer(
le_buffer.data(),
(const int32_t*)data + i,
torch::utils::THPByteOrder::THP_LITTLE_ENDIAN,
to_convert);
} else if (element_size == 8) {
torch::utils::THP_encodeBuffer(
le_buffer.data(),
(const int64_t*)data + i,
torch::utils::THPByteOrder::THP_LITTLE_ENDIAN,
to_convert);
}
doWrite(fd, le_buffer.data(), to_convert * element_size);
}
}
}
template void THPStorage_writeFileRaw<int>(
c10::StorageImpl* self,
int fd,
bool save_size,
uint64_t element_size);
template void THPStorage_writeFileRaw<PyObject*>(
c10::StorageImpl* self,
PyObject* fd,
bool save_size,
uint64_t element_size);
template <class io>
c10::intrusive_ptr<c10::StorageImpl> THPStorage_readFileRaw(
io file,
c10::intrusive_ptr<c10::StorageImpl> storage,
uint64_t element_size) {
c10::OptionalDeviceGuard guard;
if (storage.defined()) {
guard.reset_device(storage->device());
}
int64_t size{};
doRead(file, &size, sizeof(int64_t));
if (torch::utils::THP_nativeByteOrder() ==
torch::utils::THPByteOrder::THP_BIG_ENDIAN) {
int64_t tsize = size; // convert little endian storage to big endian cpu
torch::utils::THP_decodeBuffer(&size, (const uint8_t*)&tsize, true, 1);
}
size_t nbytes = element_size * size;
if (!storage.defined()) {
storage = c10::make_intrusive<at::StorageImpl>(
c10::StorageImpl::use_byte_size_t(),
nbytes,
c10::GetDefaultCPUAllocator(),
/*resizable=*/true);
} else {
size_t _storage_nbytes = storage->nbytes();
TORCH_CHECK(
_storage_nbytes == nbytes,
"storage has wrong byte size: expected %ld got %ld",
nbytes,
_storage_nbytes);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
std::unique_ptr<char[]> cpu_data;
uint8_t* data{};
if (storage->device_type() == at::kCPU) {
data = static_cast<uint8_t*>(storage->mutable_data());
} else {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
cpu_data = std::unique_ptr<char[]>(new char[nbytes]);
data = (uint8_t*)cpu_data.get();
}
// fast track for bytes and little endian
if (element_size == 1 ||
torch::utils::THP_nativeByteOrder() ==
torch::utils::THPByteOrder::THP_LITTLE_ENDIAN) {
doRead(file, data, storage->nbytes());
} else {
int64_t buffer_size = std::min(size, (int64_t)5000);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
std::unique_ptr<uint8_t[]> le_buffer(
new uint8_t[buffer_size * element_size]);
for (int64_t i = 0; i < size; i += buffer_size) {
size_t to_convert = std::min(size - i, buffer_size);
doRead(file, le_buffer.get(), element_size * to_convert);
// NOLINTNEXTLINE(bugprone-branch-clone)
if (element_size == 2) {
torch::utils::THP_decodeBuffer(
(int16_t*)data + i, le_buffer.get(), true, to_convert);
} else if (element_size == 4) {
torch::utils::THP_decodeBuffer(
(int32_t*)data + i, le_buffer.get(), true, to_convert);
} else if (element_size == 8) {
torch::utils::THP_decodeBuffer(
(int64_t*)data + i, le_buffer.get(), true, to_convert);
}
}
}
if (storage->device_type() != at::kCPU) {
// Here we use a tensor.copy_() to impl H2D for all non-CPU device.
auto cpu_tensor = at::from_blob(
(void*)data,
{static_cast<int64_t>(nbytes)},
at::device(at::kCPU).dtype(c10::kByte));
auto device_tensor = at::from_blob(
storage->mutable_data(),
{static_cast<int64_t>(nbytes)},
{1},
nullptr,
at::device(storage->device()).dtype(c10::kByte),
{storage->device()});
device_tensor.copy_(cpu_tensor);
}
return storage;
}
template c10::intrusive_ptr<c10::StorageImpl> THPStorage_readFileRaw<int>(
int fd,
c10::intrusive_ptr<c10::StorageImpl> storage,
uint64_t element_size);
template c10::intrusive_ptr<c10::StorageImpl> THPStorage_readFileRaw<PyObject*>(
PyObject* fd,
c10::intrusive_ptr<c10::StorageImpl> storage,
uint64_t element_size);