"git@developer.sourcefind.cn:OpenDAS/ktransformers.git" did not exist on "59b0631e333e0cbbc51819c2cb0094ad933e0bf1"
Unverified Commit 5be4f414 authored by peterjc123's avatar peterjc123 Committed by GitHub
Browse files

Improve read_file for Windows (#2768)

* Improve read_file for Windows

* Fix lint

* Use _stat64 instead

* Apply suggesion

* Fix lint
parent f1f6a972
...@@ -215,10 +215,6 @@ class ImageTester(unittest.TestCase): ...@@ -215,10 +215,6 @@ class ImageTester(unittest.TestCase):
data = read_file(fpath) data = read_file(fpath)
expected = torch.tensor(list(content), dtype=torch.uint8) expected = torch.tensor(list(content), dtype=torch.uint8)
self.assertTrue(data.equal(expected)) self.assertTrue(data.equal(expected))
# Windows holds into the file until the tensor is alive
# so need to del the tensor before deleting the file see
# https://github.com/pytorch/vision/issues/2743#issuecomment-703817293
del data
os.unlink(fpath) os.unlink(fpath)
with self.assertRaisesRegex( with self.assertRaisesRegex(
......
#include "read_write_file_cpu.h" #include "read_write_file_cpu.h"
// According to
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions?view=vs-2019,
// we should use _stat64 for 64-bit file size on Windows.
#ifdef _WIN32
#define VISION_STAT _stat64
#else
#define VISION_STAT stat
#endif
torch::Tensor read_file(std::string filename) { torch::Tensor read_file(std::string filename) {
// CHECK if this only works on Windows for files smaller than 2GB struct VISION_STAT stat_buf;
// https://stackoverflow.com/questions/5840148/how-can-i-get-a-files-size-in-c int rc = VISION_STAT(filename.c_str(), &stat_buf);
struct stat stat_buf;
int rc = stat(filename.c_str(), &stat_buf);
// errno is a variable defined in errno.h // errno is a variable defined in errno.h
TORCH_CHECK( TORCH_CHECK(
rc == 0, "[Errno ", errno, "] ", strerror(errno), ": '", filename, "'"); rc == 0, "[Errno ", errno, "] ", strerror(errno), ": '", filename, "'");
...@@ -13,8 +20,14 @@ torch::Tensor read_file(std::string filename) { ...@@ -13,8 +20,14 @@ torch::Tensor read_file(std::string filename) {
TORCH_CHECK(size > 0, "Expected a non empty file"); TORCH_CHECK(size > 0, "Expected a non empty file");
#ifdef _WIN32
auto data =
torch::from_file(filename, /*shared=*/false, /*size=*/size, torch::kU8)
.clone();
#else
auto data = auto data =
torch::from_file(filename, /*shared=*/false, /*size=*/size, torch::kU8); torch::from_file(filename, /*shared=*/false, /*size=*/size, torch::kU8);
#endif
return data; return data;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment