forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a hook (wrapper) for non-std stream reader in PyTorchStreamRea…
…der (pytorch#15551) Summary: To implement a stream is very annoying, since it is closely defined with the underlying storage streambuffer. So in this PR, we add ReadAdapterInterface and PyTorchStreamReader will use it. We implement IStreamAdapter as a wrapper of std::istream. And keep the user interface unchanged. Pull Request resolved: pytorch#15551 Reviewed By: zrphercule Differential Revision: D13568907 Pulled By: houseroad fbshipit-source-id: 93708cb801248a6c101f35cb14d1631029365c3c
- Loading branch information
1 parent
1488c5d
commit a918f1d
Showing
13 changed files
with
229 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "caffe2/serialize/file_adapter.h" | ||
#include <c10/util/Exception.h> | ||
#include "caffe2/core/common.h" | ||
|
||
namespace caffe2 { | ||
namespace serialize { | ||
|
||
FileAdapter::FileAdapter(const std::string& file_name) { | ||
file_stream_.open(file_name, std::ifstream::in | std::ifstream::binary); | ||
if (!file_stream_) { | ||
AT_ERROR("open file failed, file path: ", file_name); | ||
} | ||
istream_adapter_ = caffe2::make_unique<IStreamAdapter>(&file_stream_); | ||
} | ||
|
||
size_t FileAdapter::size() const { | ||
return istream_adapter_->size(); | ||
} | ||
|
||
size_t FileAdapter::read(uint64_t pos, void* buf, size_t n, const char* what) | ||
const { | ||
return istream_adapter_->read(pos, buf, n, what); | ||
} | ||
|
||
FileAdapter::~FileAdapter() {} | ||
|
||
} // namespace serialize | ||
} // namespace caffe2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
#include <fstream> | ||
#include <memory> | ||
|
||
#include <c10/macros/Macros.h> | ||
#include "caffe2/serialize/istream_adapter.h" | ||
#include "caffe2/serialize/read_adapter_interface.h" | ||
|
||
namespace caffe2 { | ||
namespace serialize { | ||
|
||
class FileAdapter final : public ReadAdapterInterface { | ||
public: | ||
C10_DISABLE_COPY_AND_ASSIGN(FileAdapter); | ||
explicit FileAdapter(const std::string& file_name); | ||
size_t size() const override; | ||
size_t read(uint64_t pos, void* buf, size_t n, const char* what = "") | ||
const override; | ||
~FileAdapter(); | ||
|
||
private: | ||
std::ifstream file_stream_; | ||
std::unique_ptr<IStreamAdapter> istream_adapter_; | ||
}; | ||
|
||
} // namespace serialize | ||
} // namespace caffe2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "caffe2/serialize/istream_adapter.h" | ||
#include <c10/util/Exception.h> | ||
|
||
namespace caffe2 { | ||
namespace serialize { | ||
|
||
IStreamAdapter::IStreamAdapter(std::istream* istream) : istream_(istream) {} | ||
|
||
size_t IStreamAdapter::size() const { | ||
auto prev_pos = istream_->tellg(); | ||
validate("getting the current position"); | ||
istream_->seekg(0, istream_->end); | ||
validate("seeking to end"); | ||
auto result = istream_->tellg(); | ||
validate("getting size"); | ||
istream_->seekg(prev_pos); | ||
validate("seeking to the original position"); | ||
return result; | ||
} | ||
|
||
size_t IStreamAdapter::read(uint64_t pos, void* buf, size_t n, const char* what) | ||
const { | ||
istream_->seekg(pos); | ||
validate(what); | ||
istream_->read(static_cast<char*>(buf), n); | ||
validate(what); | ||
return n; | ||
} | ||
|
||
void IStreamAdapter::validate(const char* what) const { | ||
if (!*istream_) { | ||
AT_ERROR("istream reader failed: ", what, "."); | ||
} | ||
} | ||
|
||
IStreamAdapter::~IStreamAdapter() {} | ||
|
||
} // namespace serialize | ||
} // namespace caffe2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
#include <istream> | ||
|
||
#include <c10/macros/Macros.h> | ||
|
||
#include "caffe2/serialize/read_adapter_interface.h" | ||
|
||
namespace caffe2 { | ||
namespace serialize { | ||
|
||
// this is a reader implemented by std::istream | ||
class IStreamAdapter final : public ReadAdapterInterface { | ||
public: | ||
C10_DISABLE_COPY_AND_ASSIGN(IStreamAdapter); | ||
explicit IStreamAdapter(std::istream* istream); | ||
size_t size() const override; | ||
size_t read(uint64_t pos, void* buf, size_t n, const char* what = "") | ||
const override; | ||
~IStreamAdapter(); | ||
|
||
private: | ||
std::istream* istream_; | ||
void validate(const char* what) const; | ||
}; | ||
|
||
} // namespace serialize | ||
} // namespace caffe2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "caffe2/serialize/read_adapter_interface.h" | ||
|
||
namespace caffe2 { | ||
namespace serialize { | ||
|
||
ReadAdapterInterface::~ReadAdapterInterface() {} | ||
|
||
} // namespace serialize | ||
} // namespace caffe2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
|
||
namespace caffe2 { | ||
namespace serialize { | ||
|
||
// this is the interface for the (file/stream/memory) reader in | ||
// PyTorchStreamReader. with this interface, we can extend the support | ||
// besides standard istream | ||
class ReadAdapterInterface { | ||
public: | ||
virtual size_t size() const = 0; | ||
virtual size_t read(uint64_t pos, void* buf, size_t n, const char* what = "") | ||
const = 0; | ||
virtual ~ReadAdapterInterface(); | ||
}; | ||
|
||
} // namespace serialize | ||
} // namespace caffe2 |
Oops, something went wrong.