Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions third_party/xla_client/record_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,13 @@ RecordReader::RecordReader(std::string path, const string& compression,
reader_.reset(new tensorflow::io::RecordReader(file_.get(), options));
}

bool RecordReader::Read(std::string* value) {
// We need to pass a tensorflow::tstring here, which will ultimately result in
// making a copy. Hopefully the tensorflow string story will end with a nice
// outcome.
tensorflow::tstring tvalue;
bool RecordReader::Read(Data* value) {
std::lock_guard<std::mutex> slock(lock_);
xla::Status status = reader_->ReadRecord(&offset_, &tvalue);
xla::Status status = reader_->ReadRecord(&offset_, value);
if (tensorflow::errors::IsOutOfRange(status)) {
return false;
}
XLA_CHECK_OK(status) << path_ << " offset " << offset_;
*value = tvalue;
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion third_party/xla_client/record_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ namespace util {

class RecordReader {
public:
using Data = tensorflow::tstring;

RecordReader(std::string path, const std::string& compression,
int64 buffer_size);

const std::string& path() const { return path_; }

bool Read(std::string* value);
bool Read(Data* value);

private:
std::string path_;
Expand Down
10 changes: 5 additions & 5 deletions torch_xla/csrc/init_python_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ std::shared_ptr<xla::util::RecordReader> CreateRecordReader(
}

bool RecordRead(const std::shared_ptr<xla::util::RecordReader>& reader,
std::string* value) {
xla::util::RecordReader::Data* value) {
NoGilSection nogil;
return reader->Read(value);
}
Expand All @@ -286,12 +286,12 @@ py::object RecordReadExample(
return std::vector<int64_t>({size});
};

std::string value;
xla::util::RecordReader::Data value;
if (!RecordRead(reader, &value)) {
return py::none();
}
tensorflow::Example exmsg;
if (!exmsg.ParseFromString(value)) {
if (!exmsg.ParseFromArray(value.data(), value.size())) {
XLA_ERROR() << "Unable to parse TF example from " << reader->path();
}
auto example = py::dict();
Expand Down Expand Up @@ -592,11 +592,11 @@ void InitXlaModuleBindings(py::module m) {
m.def(
"_xla_tfrecord_read",
[](const std::shared_ptr<xla::util::RecordReader>& reader) -> py::object {
std::string record;
xla::util::RecordReader::Data record;
if (!RecordRead(reader, &record)) {
return py::none();
}
return py::bytes(record);
return py::bytes(record.data(), record.size());
});
m.def("_xla_tfexample_read",
[](const std::shared_ptr<xla::util::RecordReader>& reader) {
Expand Down