Skip to content

Commit 6327a71

Browse files
cyyeverpytorchmergebot
authored andcommitted
[Environment Variable][2/N] Use thread-safe setenv wrapper (pytorch#124485)
This follows pytorch#119449 to make setenv thread-safe. Pull Request resolved: pytorch#124485 Approved by: https://github.com/eqy
1 parent 6dcd773 commit 6327a71

File tree

4 files changed

+18
-55
lines changed

4 files changed

+18
-55
lines changed

aten/src/ATen/cuda/detail/CUDAHooks.cpp

+4-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <ATen/detail/CUDAHooksInterface.h>
1515
#include <ATen/native/cuda/CuFFTPlanCache.h>
1616
#include <c10/util/Exception.h>
17+
#include <c10/util/env.h>
1718
#include <c10/cuda/CUDACachingAllocator.h>
1819
#include <c10/cuda/CUDAFunctions.h>
1920
#include <c10/util/irange.h>
@@ -79,19 +80,6 @@ struct _Initializer {
7980
} initializer;
8081
} // anonymous namespace
8182

82-
// Sets the CUDA_MODULE_LOADING environment variable
83-
// if it's not set by the user.
84-
void maybe_set_cuda_module_loading(const std::string &def_value) {
85-
auto value = std::getenv("CUDA_MODULE_LOADING");
86-
if (!value) {
87-
#ifdef _WIN32
88-
auto env_var = "CUDA_MODULE_LOADING=" + def_value;
89-
_putenv(env_var.c_str());
90-
#else
91-
setenv("CUDA_MODULE_LOADING", def_value.c_str(), 1);
92-
#endif
93-
}
94-
}
9583

9684
// NB: deleter is dynamic, because we need it to live in a separate
9785
// compilation unit (alt is to have another method in hooks, but
@@ -102,7 +90,9 @@ void CUDAHooks::initCUDA() const {
10290
// have a chance to enable vitals.
10391
at::vitals::VitalsAPI.setVital("CUDA", "used", "true", /* force = */ true);
10492

105-
maybe_set_cuda_module_loading("LAZY");
93+
// Sets the CUDA_MODULE_LOADING environment variable
94+
// if it's not set by the user.
95+
c10::utils::set_env("CUDA_MODULE_LOADING", "LAZY", false);
10696
const auto num_devices = c10::cuda::device_count_ensure_non_zero();
10797
c10::cuda::CUDACachingAllocator::init(num_devices);
10898
at::cuda::detail::init_p2p_access_cache(num_devices);

aten/src/ATen/test/vitals.cpp

+5-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <ATen/ATen.h>
55
#include <ATen/core/Vitals.h>
6+
#include <c10/util/env.h>
67
#include <c10/util/irange.h>
78
#include <cstdlib>
89

@@ -15,11 +16,7 @@ TEST(Vitals, Basic) {
1516
std::streambuf* sbuf = std::cout.rdbuf();
1617
std::cout.rdbuf(buffer.rdbuf());
1718
{
18-
#ifdef _WIN32
19-
_putenv("TORCH_VITAL=1");
20-
#else
21-
setenv("TORCH_VITAL", "1", 1);
22-
#endif
19+
c10::utils::set_env("TORCH_VITAL", "1");
2320
TORCH_VITAL_DEFINE(Testing);
2421
TORCH_VITAL(Testing, Attribute0) << 1;
2522
TORCH_VITAL(Testing, Attribute1) << "1";
@@ -44,11 +41,7 @@ TEST(Vitals, MultiString) {
4441
std::streambuf* sbuf = std::cout.rdbuf();
4542
std::cout.rdbuf(buffer.rdbuf());
4643
{
47-
#ifdef _WIN32
48-
_putenv("TORCH_VITAL=1");
49-
#else
50-
setenv("TORCH_VITAL", "1", 1);
51-
#endif
44+
c10::utils::set_env("TORCH_VITAL", "1");
5245
TORCH_VITAL_DEFINE(Testing);
5346
TORCH_VITAL(Testing, Attribute0) << 1 << " of " << 2;
5447
TORCH_VITAL(Testing, Attribute1) << 1;
@@ -69,15 +62,7 @@ TEST(Vitals, OnAndOff) {
6962
std::streambuf* sbuf = std::cout.rdbuf();
7063
std::cout.rdbuf(buffer.rdbuf());
7164
{
72-
#ifdef _WIN32
73-
if (i) {
74-
_putenv("TORCH_VITAL=1");
75-
} else {
76-
_putenv("TORCH_VITAL=0");
77-
}
78-
#else
79-
setenv("TORCH_VITAL", i ? "1" : "", 1);
80-
#endif
65+
c10::utils::set_env("TORCH_VITAL", i ? "1" : "0");
8166
TORCH_VITAL_DEFINE(Testing);
8267
TORCH_VITAL(Testing, Attribute0) << 1;
8368
}
@@ -100,11 +85,7 @@ TEST(Vitals, APIVitals) {
10085
std::streambuf* sbuf = std::cout.rdbuf();
10186
std::cout.rdbuf(buffer.rdbuf());
10287
{
103-
#ifdef _WIN32
104-
_putenv("TORCH_VITAL=1");
105-
#else
106-
setenv("TORCH_VITAL", "1", 1);
107-
#endif
88+
c10::utils::set_env("TORCH_VITAL", "1");
10889
APIVitals api_vitals;
10990
rvalue = api_vitals.setVital("TestingSetVital", "TestAttr", "TestValue");
11091
}

test/cpp/api/dispatch.cpp

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#include <gtest/gtest.h>
22

33
#include <ATen/native/Pow.h>
4+
#include <c10/util/env.h>
45
#include <c10/util/irange.h>
56
#include <test/cpp/api/support.h>
67
#include <torch/torch.h>
78
#include <torch/types.h>
89
#include <torch/utils.h>
910
#include <cstdlib>
10-
#include <iostream>
11-
#include <type_traits>
1211
#include <vector>
1312

1413
struct DispatchTest : torch::test::SeedingFixture {};
@@ -18,11 +17,7 @@ TEST_F(DispatchTest, TestAVX2) {
1817
const std::vector<int> result{1, 4, 27, 256};
1918
const auto vals_tensor = torch::tensor(ints);
2019
const auto pows_tensor = torch::tensor(ints);
21-
#ifdef _WIN32
22-
_putenv("ATEN_CPU_CAPABILITY=avx2");
23-
#else
24-
setenv("ATEN_CPU_CAPABILITY", "avx2", 1);
25-
#endif
20+
c10::utils::set_env("ATEN_CPU_CAPABILITY", "avx2");
2621
const auto actual_pow_avx2 = vals_tensor.pow(pows_tensor);
2722
for (const auto i : c10::irange(4)) {
2823
ASSERT_EQ(result[i], actual_pow_avx2[i].item<int>());
@@ -34,11 +29,7 @@ TEST_F(DispatchTest, TestAVX512) {
3429
const std::vector<int> result{1, 4, 27, 256};
3530
const auto vals_tensor = torch::tensor(ints);
3631
const auto pows_tensor = torch::tensor(ints);
37-
#ifdef _WIN32
38-
_putenv("ATEN_CPU_CAPABILITY=avx512");
39-
#else
40-
setenv("ATEN_CPU_CAPABILITY", "avx512", 1);
41-
#endif
32+
c10::utils::set_env("ATEN_CPU_CAPABILITY", "avx512");
4233
const auto actual_pow_avx512 = vals_tensor.pow(pows_tensor);
4334
for (const auto i : c10::irange(4)) {
4435
ASSERT_EQ(result[i], actual_pow_avx512[i].item<int>());
@@ -50,11 +41,7 @@ TEST_F(DispatchTest, TestDefault) {
5041
const std::vector<int> result{1, 4, 27, 256};
5142
const auto vals_tensor = torch::tensor(ints);
5243
const auto pows_tensor = torch::tensor(ints);
53-
#ifdef _WIN32
54-
_putenv("ATEN_CPU_CAPABILITY=default");
55-
#else
56-
setenv("ATEN_CPU_CAPABILITY", "default", 1);
57-
#endif
44+
c10::utils::set_env("ATEN_CPU_CAPABILITY", "default");
5845
const auto actual_pow_default = vals_tensor.pow(pows_tensor);
5946
for (const auto i : c10::irange(4)) {
6047
ASSERT_EQ(result[i], actual_pow_default[i].item<int>());

test/test_cuda.py

+5
Original file line numberDiff line numberDiff line change
@@ -5364,6 +5364,11 @@ def test_cuda_autocast_deprecated_warning(self):
53645364
with torch.cuda.amp.autocast():
53655365
_ = torch.ones(10)
53665366

5367+
def test_cuda_module_loading_env(self):
5368+
torch.cuda.init()
5369+
val = os.environ.get("CUDA_MODULE_LOADING", "")
5370+
self.assertEqual(val, "LAZY")
5371+
53675372

53685373
instantiate_parametrized_tests(TestCuda)
53695374
instantiate_parametrized_tests(TestCudaMallocAsync)

0 commit comments

Comments
 (0)