Repository navigation
Expand file tree
/
Copy pathrecord_io_wrapper.cc
More file actions
329 lines (285 loc) · 11.6 KB
/
Copy pathrecord_io_wrapper.cc
File metadata and controls
329 lines (285 loc) · 11.6 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
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
/* Copyright 2019 The TensorFlow Authors. 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 <memory>
#include <string>
#include "absl/memory/memory.h"
#include "pybind11/pybind11.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/io/record_reader.h"
#include "tensorflow/core/lib/io/record_writer.h"
#include "tensorflow/core/lib/io/zlib_compression_options.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/file_system.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/python/lib/core/pybind11_absl.h"
#include "tensorflow/python/lib/core/pybind11_status.h"
namespace {
namespace py = ::pybind11;
class PyRecordReader {
public:
// NOTE(sethtroisi): At this time PyRecordReader doesn't benefit from taking
// RecordReaderOptions, if this changes the API can be updated at that time.
static tensorflow::Status New(const std::string& filename,
const std::string& compression_type,
PyRecordReader** out) {
std::unique_ptr<tensorflow::RandomAccessFile> file;
TF_RETURN_IF_ERROR(
tensorflow::Env::Default()->NewRandomAccessFile(filename, &file));
auto options =
tensorflow::io::RecordReaderOptions::CreateRecordReaderOptions(
compression_type);
options.buffer_size = kReaderBufferSize;
auto reader =
absl::make_unique<tensorflow::io::RecordReader>(file.get(), options);
*out = new PyRecordReader(std::move(file), std::move(reader));
return tensorflow::Status::OK();
}
PyRecordReader() = delete;
~PyRecordReader() { Close(); }
tensorflow::Status ReadNextRecord(tensorflow::tstring* out) {
if (IsClosed()) {
return tensorflow::errors::FailedPrecondition("Reader is closed.");
}
return reader_->ReadRecord(&offset_, out);
}
bool IsClosed() const { return file_ == nullptr && reader_ == nullptr; }
void Close() {
reader_ = nullptr;
file_ = nullptr;
}
private:
static constexpr tensorflow::uint64 kReaderBufferSize = 16 * 1024 * 1024;
PyRecordReader(std::unique_ptr<tensorflow::RandomAccessFile> file,
std::unique_ptr<tensorflow::io::RecordReader> reader)
: offset_(0), file_(std::move(file)), reader_(std::move(reader)) {}
tensorflow::uint64 offset_;
std::unique_ptr<tensorflow::RandomAccessFile> file_;
std::unique_ptr<tensorflow::io::RecordReader> reader_;
TF_DISALLOW_COPY_AND_ASSIGN(PyRecordReader);
};
class PyRecordRandomReader {
public:
static tensorflow::Status New(const std::string& filename,
PyRecordRandomReader** out) {
std::unique_ptr<tensorflow::RandomAccessFile> file;
TF_RETURN_IF_ERROR(
tensorflow::Env::Default()->NewRandomAccessFile(filename, &file));
auto options =
tensorflow::io::RecordReaderOptions::CreateRecordReaderOptions("");
options.buffer_size = kReaderBufferSize;
auto reader =
absl::make_unique<tensorflow::io::RecordReader>(file.get(), options);
*out = new PyRecordRandomReader(std::move(file), std::move(reader));
return tensorflow::Status::OK();
}
PyRecordRandomReader() = delete;
~PyRecordRandomReader() { Close(); }
tensorflow::Status ReadRecord(tensorflow::uint64* offset,
tensorflow::tstring* out) {
if (IsClosed()) {
return tensorflow::errors::FailedPrecondition(
"Random TFRecord Reader is closed.");
}
return reader_->ReadRecord(offset, out);
}
bool IsClosed() const { return file_ == nullptr && reader_ == nullptr; }
void Close() {
reader_ = nullptr;
file_ = nullptr;
}
private:
static constexpr tensorflow::uint64 kReaderBufferSize = 16 * 1024 * 1024;
PyRecordRandomReader(std::unique_ptr<tensorflow::RandomAccessFile> file,
std::unique_ptr<tensorflow::io::RecordReader> reader)
: file_(std::move(file)), reader_(std::move(reader)) {}
std::unique_ptr<tensorflow::RandomAccessFile> file_;
std::unique_ptr<tensorflow::io::RecordReader> reader_;
TF_DISALLOW_COPY_AND_ASSIGN(PyRecordRandomReader);
};
class PyRecordWriter {
public:
static tensorflow::Status New(
const std::string& filename,
const tensorflow::io::RecordWriterOptions& options,
PyRecordWriter** out) {
std::unique_ptr<tensorflow::WritableFile> file;
TF_RETURN_IF_ERROR(
tensorflow::Env::Default()->NewWritableFile(filename, &file));
auto writer =
absl::make_unique<tensorflow::io::RecordWriter>(file.get(), options);
*out = new PyRecordWriter(std::move(file), std::move(writer));
return tensorflow::Status::OK();
}
PyRecordWriter() = delete;
~PyRecordWriter() { Close(); }
tensorflow::Status WriteRecord(tensorflow::StringPiece record) {
if (IsClosed()) {
return tensorflow::errors::FailedPrecondition("Writer is closed.");
}
return writer_->WriteRecord(record);
}
tensorflow::Status Flush() {
if (IsClosed()) {
return tensorflow::errors::FailedPrecondition("Writer is closed.");
}
auto status = writer_->Flush();
if (status.ok()) {
// Per the RecordWriter contract, flushing the RecordWriter does not
// flush the underlying file. Here we need to do both.
return file_->Flush();
}
return status;
}
bool IsClosed() const { return file_ == nullptr && writer_ == nullptr; }
tensorflow::Status Close() {
if (writer_ != nullptr) {
auto status = writer_->Close();
writer_ = nullptr;
if (!status.ok()) return status;
}
if (file_ != nullptr) {
auto status = file_->Close();
file_ = nullptr;
if (!status.ok()) return status;
}
return tensorflow::Status::OK();
}
private:
PyRecordWriter(std::unique_ptr<tensorflow::WritableFile> file,
std::unique_ptr<tensorflow::io::RecordWriter> writer)
: file_(std::move(file)), writer_(std::move(writer)) {}
std::unique_ptr<tensorflow::WritableFile> file_;
std::unique_ptr<tensorflow::io::RecordWriter> writer_;
TF_DISALLOW_COPY_AND_ASSIGN(PyRecordWriter);
};
PYBIND11_MODULE(_pywrap_record_io, m) {
py::class_<PyRecordReader>(m, "RecordIterator")
.def(py::init(
[](const std::string& filename, const std::string& compression_type) {
tensorflow::Status status;
PyRecordReader* self = nullptr;
{
py::gil_scoped_release release;
status = PyRecordReader::New(filename, compression_type, &self);
}
MaybeRaiseRegisteredFromStatus(status);
return self;
}))
.def("__iter__", [](const py::object& self) { return self; })
.def("__next__",
[](PyRecordReader* self) {
if (self->IsClosed()) {
throw py::stop_iteration();
}
tensorflow::tstring record;
tensorflow::Status status;
{
py::gil_scoped_release release;
status = self->ReadNextRecord(&record);
}
if (tensorflow::errors::IsOutOfRange(status)) {
// Don't close because the file being read could be updated
// in-between
// __next__ calls.
throw py::stop_iteration();
}
MaybeRaiseRegisteredFromStatus(status);
return py::bytes(record);
})
.def("close", [](PyRecordReader* self) { self->Close(); });
py::class_<PyRecordRandomReader>(m, "RandomRecordReader")
.def(py::init([](const std::string& filename) {
tensorflow::Status status;
PyRecordRandomReader* self = nullptr;
{
py::gil_scoped_release release;
status = PyRecordRandomReader::New(filename, &self);
}
MaybeRaiseRegisteredFromStatus(status);
return self;
}))
.def("read",
[](PyRecordRandomReader* self, tensorflow::uint64 offset) {
tensorflow::uint64 temp_offset = offset;
tensorflow::tstring record;
tensorflow::Status status;
{
py::gil_scoped_release release;
status = self->ReadRecord(&temp_offset, &record);
}
if (tensorflow::errors::IsOutOfRange(status)) {
throw py::index_error(tensorflow::strings::StrCat(
"Out of range at reading offset ", offset));
}
MaybeRaiseRegisteredFromStatus(status);
return py::make_tuple(py::bytes(record), temp_offset);
})
.def("close", [](PyRecordRandomReader* self) { self->Close(); });
using tensorflow::io::ZlibCompressionOptions;
py::class_<ZlibCompressionOptions>(m, "ZlibCompressionOptions")
.def_readwrite("flush_mode", &ZlibCompressionOptions::flush_mode)
.def_readwrite("input_buffer_size",
&ZlibCompressionOptions::input_buffer_size)
.def_readwrite("output_buffer_size",
&ZlibCompressionOptions::output_buffer_size)
.def_readwrite("window_bits", &ZlibCompressionOptions::window_bits)
.def_readwrite("compression_level",
&ZlibCompressionOptions::compression_level)
.def_readwrite("compression_method",
&ZlibCompressionOptions::compression_method)
.def_readwrite("mem_level", &ZlibCompressionOptions::mem_level)
.def_readwrite("compression_strategy",
&ZlibCompressionOptions::compression_strategy);
using tensorflow::io::RecordWriterOptions;
py::class_<RecordWriterOptions>(m, "RecordWriterOptions")
.def(py::init(&RecordWriterOptions::CreateRecordWriterOptions))
.def_readonly("compression_type", &RecordWriterOptions::compression_type)
.def_readonly("zlib_options", &RecordWriterOptions::zlib_options);
using tensorflow::MaybeRaiseRegisteredFromStatus;
py::class_<PyRecordWriter>(m, "RecordWriter")
.def(py::init(
[](const std::string& filename, const RecordWriterOptions& options) {
PyRecordWriter* self = nullptr;
tensorflow::Status status;
{
py::gil_scoped_release release;
status = PyRecordWriter::New(filename, options, &self);
}
MaybeRaiseRegisteredFromStatus(status);
return self;
}))
.def("__enter__", [](const py::object& self) { return self; })
.def("__exit__",
[](PyRecordWriter* self, py::args) {
MaybeRaiseRegisteredFromStatus(self->Close());
})
.def(
"write",
[](PyRecordWriter* self, tensorflow::StringPiece record) {
tensorflow::Status status;
{
py::gil_scoped_release release;
status = self->WriteRecord(record);
}
MaybeRaiseRegisteredFromStatus(status);
},
py::arg("record"))
.def("flush",
[](PyRecordWriter* self) {
MaybeRaiseRegisteredFromStatus(self->Flush());
})
.def("close", [](PyRecordWriter* self) {
MaybeRaiseRegisteredFromStatus(self->Close());
});
}
} // namespace