From 46503a7ac0862698f3266f270f5ecf12c756dcdf Mon Sep 17 00:00:00 2001 From: Dmytro Dzhulgakov Date: Wed, 13 Feb 2019 19:28:05 -0800 Subject: [PATCH] Trim libshm deps, move tempfile.h to c10 (#17019) Summary: libshm_manager doesn't need to depend on all of libtorch. It only uses tiny tempfile.h which can be moved to c10. I could just duplicate the file too, but it's not worth it as c10 is small enough. Pull Request resolved: https://github.com/pytorch/pytorch/pull/17019 Differential Revision: D14052688 Pulled By: dzhulgakov fbshipit-source-id: 8797d15f8c7c49c49d40b7ab2f43aa3bf6becb0c --- c10/test/util/tempfile_test.cpp | 9 +++++++++ {torch/csrc/utils => c10/util}/tempfile.h | 7 ++----- test/cpp/api/misc.cpp | 8 -------- test/cpp/api/serialize.cpp | 14 ++++++++------ test/cpp/common/support.h | 2 -- torch/lib/libshm/manager.cpp | 5 ++--- 6 files changed, 21 insertions(+), 24 deletions(-) create mode 100644 c10/test/util/tempfile_test.cpp rename {torch/csrc/utils => c10/util}/tempfile.h (96%) diff --git a/c10/test/util/tempfile_test.cpp b/c10/test/util/tempfile_test.cpp new file mode 100644 index 0000000..806cebd --- /dev/null +++ b/c10/test/util/tempfile_test.cpp @@ -0,0 +1,9 @@ +#include +#include + +#if !defined(_WIN32) +TEST(TempFileTest, MatchesExpectedPattern) { + c10::TempFile pattern = c10::make_tempfile("test-pattern-"); + ASSERT_NE(pattern.name.find("test-pattern-"), std::string::npos); +} +#endif // !defined(_WIN32) diff --git a/torch/csrc/utils/tempfile.h b/c10/util/tempfile.h similarity index 96% rename from torch/csrc/utils/tempfile.h rename to c10/util/tempfile.h index b5b9383..98ac885 100644 --- a/torch/csrc/utils/tempfile.h +++ b/c10/util/tempfile.h @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -16,8 +15,7 @@ #include #endif -namespace torch { -namespace utils { +namespace c10 { namespace detail { // Creates the filename pattern passed to and completed by `mkstemp`. // Returns std::vector because `mkstemp` needs a (non-const) `char*` and @@ -102,5 +100,4 @@ inline TempFile make_tempfile(std::string name_prefix = "torch-file-") { } AT_ERROR("Error generating temporary file: ", std::strerror(errno)); } -} // namespace utils -} // namespace torch +} // namespace c10 diff --git a/test/cpp/api/misc.cpp b/test/cpp/api/misc.cpp index 097a649..69e7a8c 100644 --- a/test/cpp/api/misc.cpp +++ b/test/cpp/api/misc.cpp @@ -1,6 +1,5 @@ #include -#include #include #include #include @@ -52,10 +51,3 @@ TEST(NNInitTest, CanInitializeTensorThatRequiresGrad) { "has been used in an in-place operation"); ASSERT_EQ(torch::nn::init::ones_(tensor).sum().item(), 12); } - -#if !defined(_WIN32) -TEST(TempFileTest, MatchesExpectedPattern) { - torch::utils::TempFile pattern = torch::utils::make_tempfile("test-pattern-"); - ASSERT_NE(pattern.name.find("test-pattern-"), std::string::npos); -} -#endif // !defined(_WIN32) diff --git a/test/cpp/api/serialize.cpp b/test/cpp/api/serialize.cpp index dab0dc4..d6be300 100644 --- a/test/cpp/api/serialize.cpp +++ b/test/cpp/api/serialize.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -54,7 +56,7 @@ TEST(SerializeTest, BasicToFile) { auto x = torch::randn({5, 5}); - auto tempfile = torch::utils::make_tempfile(); + auto tempfile = c10::make_tempfile(); torch::save(x, tempfile.name); torch::Tensor y; @@ -135,7 +137,7 @@ TEST(SerializeTest, XOR) { epoch++; } - auto tempfile = torch::utils::make_tempfile(); + auto tempfile = c10::make_tempfile(); torch::save(model, tempfile.name); torch::load(model2, tempfile.name); @@ -149,7 +151,7 @@ TEST(SerializeTest, Optim) { auto model3 = Linear(5, 2); // Models 1, 2, 3 will have the same parameters. - auto model_tempfile = torch::utils::make_tempfile(); + auto model_tempfile = c10::make_tempfile(); torch::save(model1, model_tempfile.name); torch::load(model2, model_tempfile.name); torch::load(model3, model_tempfile.name); @@ -194,7 +196,7 @@ TEST(SerializeTest, Optim) { // Do 2 steps of model 3 while saving the optimizer step(optim3, model3); - auto optim_tempfile = torch::utils::make_tempfile(); + auto optim_tempfile = c10::make_tempfile(); torch::save(optim3, optim_tempfile.name); torch::load(optim3_2, optim_tempfile.name); step(optim3_2, model3); @@ -253,7 +255,7 @@ TEST(SerializeTest, XOR_CUDA) { epoch++; } - auto tempfile = torch::utils::make_tempfile(); + auto tempfile = c10::make_tempfile(); torch::save(model, tempfile.name); torch::load(model2, tempfile.name); @@ -264,7 +266,7 @@ TEST(SerializeTest, XOR_CUDA) { loss = getLoss(model2, 100, true); ASSERT_LT(loss.item(), 0.1); - auto tempfile2 = torch::utils::make_tempfile(); + auto tempfile2 = c10::make_tempfile(); torch::save(model2, tempfile2.name); torch::load(model3, tempfile2.name); diff --git a/test/cpp/common/support.h b/test/cpp/common/support.h index 43c59f2..30f54b8 100644 --- a/test/cpp/common/support.h +++ b/test/cpp/common/support.h @@ -1,7 +1,5 @@ #pragma once -#include - #include #include diff --git a/torch/lib/libshm/manager.cpp b/torch/lib/libshm/manager.cpp index c315979..acba9e4 100644 --- a/torch/lib/libshm/manager.cpp +++ b/torch/lib/libshm/manager.cpp @@ -9,8 +9,7 @@ #include #include -#include -#include +#include #include #include @@ -87,7 +86,7 @@ int main(int argc, char *argv[]) { std::unique_ptr srv_socket; const auto tempfile = - torch::utils::try_make_tempfile(/*name_prefix=*/"torch-shm-file-"); + c10::try_make_tempfile(/*name_prefix=*/"torch-shm-file-"); try { if (!tempfile.has_value()) { throw std::runtime_error( -- 2.7.4