Revert "[Environment Variable][1/N] Use thread-safe env variable API in c10 (#119449)"

This reverts commit b51f66c1950a582dd18d1b2ee67df840a8c4dbbe.

Reverted https://github.com/pytorch/pytorch/pull/119449 on behalf of https://github.com/malfet due to Broke gcc9 builds ([comment](https://github.com/pytorch/pytorch/pull/119449#issuecomment-2064936414))
This commit is contained in:
PyTorch MergeBot
2024-04-18 18:53:59 +00:00
parent 89407eca3b
commit 61bc188f42
12 changed files with 60 additions and 163 deletions

View File

@ -3,7 +3,6 @@
#include <c10/core/alignment.h>
#include <c10/util/Flags.h>
#include <c10/util/Logging.h>
#include <c10/util/env.h>
#include <c10/util/irange.h>
#include <c10/util/numa.h>
@ -54,8 +53,8 @@ void memset_junk(void* data, size_t num) {
#if defined(__linux__) && !defined(__ANDROID__)
static inline bool is_thp_alloc_enabled() {
static bool value = [&] {
auto env = c10::utils::check_env("THP_MEM_ALLOC_ENABLE");
return env.has_value() ? env.value() : 0;
const char* ptr = std::getenv("THP_MEM_ALLOC_ENABLE");
return ptr != nullptr ? std::atoi(ptr) : 0;
}();
return value;
}

View File

@ -234,7 +234,7 @@ size_t CUDAAllocatorConfig::parseAllocatorConfig(
return i;
}
void CUDAAllocatorConfig::parseArgs(const std::optional<std::string>& env) {
void CUDAAllocatorConfig::parseArgs(const char* env) {
// If empty, set the default values
m_max_split_size = std::numeric_limits<size_t>::max();
m_roundup_power2_divisions.assign(kRoundUpPowerOfTwoIntervals, 0);
@ -242,16 +242,16 @@ void CUDAAllocatorConfig::parseArgs(const std::optional<std::string>& env) {
bool used_cudaMallocAsync = false;
bool used_native_specific_option = false;
if (!env.has_value()) {
if (env == nullptr) {
return;
}
{
std::lock_guard<std::mutex> lock(m_last_allocator_settings_mutex);
m_last_allocator_settings = env.value();
m_last_allocator_settings = env;
}
std::vector<std::string> config;
lexArgs(env.value().c_str(), config);
lexArgs(env, config);
for (size_t i = 0; i < config.size(); i++) {
std::string_view config_item_view(config[i]);

View File

@ -2,7 +2,6 @@
#include <c10/cuda/CUDAMacros.h>
#include <c10/util/Exception.h>
#include <c10/util/env.h>
#include <atomic>
#include <cstddef>
@ -73,13 +72,14 @@ class C10_CUDA_API CUDAAllocatorConfig {
static CUDAAllocatorConfig& instance() {
static CUDAAllocatorConfig* s_instance = ([]() {
auto inst = new CUDAAllocatorConfig();
inst->parseArgs(c10::utils::get_env("PYTORCH_CUDA_ALLOC_CONF"));
const char* env = getenv("PYTORCH_CUDA_ALLOC_CONF");
inst->parseArgs(env);
return inst;
})();
return *s_instance;
}
void parseArgs(const std::optional<std::string>& env);
void parseArgs(const char* env);
private:
CUDAAllocatorConfig();

View File

@ -8,7 +8,6 @@
#include <c10/util/CallOnce.h>
#include <c10/util/ScopeExit.h>
#include <c10/util/UniqueVoidPtr.h>
#include <c10/util/env.h>
#include <c10/util/flat_hash_map.h>
#include <c10/util/hash.h>
#include <c10/util/irange.h>
@ -2832,7 +2831,7 @@ class DeviceCachingAllocator {
// errors, since the caching allocator foils cuda-memcheck.
bool forceUncachedAllocator() {
static bool force_uncached =
c10::utils::has_env("PYTORCH_NO_CUDA_MEMORY_CACHING");
getenv("PYTORCH_NO_CUDA_MEMORY_CACHING") != nullptr;
return force_uncached;
}
@ -3364,9 +3363,9 @@ struct BackendStaticInitializer {
// version checks, to CUDAAllocatorConfig's runtime doublecheck. If this
// works, maybe we should move all of CUDAAllocatorConfig here?
CUDAAllocator* parseEnvForBackend() {
const auto val = c10::utils::get_env("PYTORCH_CUDA_ALLOC_CONF");
if (val.has_value()) {
const std::string& config = val.value();
const char* val = getenv("PYTORCH_CUDA_ALLOC_CONF");
if (val != nullptr) {
const std::string config(val);
std::regex exp("[\\s,]+");
std::sregex_token_iterator it(config.begin(), config.end(), exp, -1);

View File

@ -3,7 +3,6 @@
#include <c10/cuda/CUDAFunctions.h>
#include <c10/util/Backtrace.h>
#include <c10/util/Exception.h>
#include <c10/util/env.h>
#include <c10/util/irange.h>
#include <cuda_runtime.h>
@ -81,8 +80,8 @@ bool dsa_check_if_all_devices_support_managed_memory() {
}
bool env_flag_set(const char* env_var_name) {
const auto env_flag = c10::utils::check_env(env_var_name);
return env_flag.has_value() && env_flag.value();
const char* const env_string = std::getenv(env_var_name);
return (env_string == nullptr) ? false : std::strcmp(env_string, "0");
}
/// Deleter for UVM/managed memory pointers

View File

@ -1,14 +1,12 @@
#include <c10/cuda/CUDAMiscFunctions.h>
#include <c10/util/env.h>
#include <cstdlib>
namespace c10::cuda {
// NOLINTNEXTLINE(bugprone-exception-escape,-warnings-as-errors)
const char* get_cuda_check_suffix() noexcept {
static auto device_blocking_flag =
c10::utils::check_env("CUDA_LAUNCH_BLOCKING");
static char* device_blocking_flag = getenv("CUDA_LAUNCH_BLOCKING");
static bool blocking_enabled =
(device_blocking_flag.has_value() && device_blocking_flag.value());
(device_blocking_flag && atoi(device_blocking_flag));
if (blocking_enabled) {
return "";
} else {

View File

@ -1,8 +1,9 @@
#include <c10/util/DeadlockDetection.h>
#include <c10/util/env.h>
#include <gtest/gtest.h>
#include <cstdlib>
using namespace ::testing;
using namespace c10::impl;
@ -22,7 +23,7 @@ TEST(DeadlockDetection, basic) {
#ifndef _WIN32
TEST(DeadlockDetection, disable) {
c10::utils::set_env("TORCH_DISABLE_DEADLOCK_DETECTION", "1");
setenv("TORCH_DISABLE_DEADLOCK_DETECTION", "1", 1);
DummyPythonGILHooks hooks;
SetPythonGILHooks(&hooks);
SetPythonGILHooks(&hooks);

View File

@ -1,5 +1,6 @@
#include <c10/util/DeadlockDetection.h>
#include <c10/util/env.h>
#include <cstdlib>
namespace c10::impl {
@ -7,7 +8,7 @@ namespace {
PythonGILHooks* python_gil_hooks = nullptr;
bool disable_detection() {
return c10::utils::has_env("TORCH_DISABLE_DEADLOCK_DETECTION");
return std::getenv("TORCH_DISABLE_DEADLOCK_DETECTION") != nullptr;
}
} // namespace

View File

@ -1,7 +1,6 @@
#include <c10/util/Backtrace.h>
#include <c10/util/Flags.h>
#include <c10/util/Logging.h>
#include <c10/util/env.h>
#ifdef FBCODE_CAFFE2
#include <folly/synchronization/SanitizeThread.h>
#endif
@ -11,6 +10,7 @@
#endif
#include <algorithm>
#include <cstdlib>
#include <iostream>
// Common code that we use regardless of whether we use glog or not.
@ -94,8 +94,8 @@ using DDPUsageLoggerType = std::function<void(const DDPLoggingData&)>;
namespace {
bool IsAPIUsageDebugMode() {
auto val = c10::utils::get_env("PYTORCH_API_USAGE_STDERR");
return val.has_value() && !val.value().empty(); // any non-empty value
const char* val = getenv("PYTORCH_API_USAGE_STDERR");
return val && *val; // any non-empty value
}
void APIUsageDebug(const string& event) {
@ -438,10 +438,10 @@ namespace c10::detail {
namespace {
void setLogLevelFlagFromEnv() {
auto level_env = c10::utils::get_env("TORCH_CPP_LOG_LEVEL");
const char* level_str = std::getenv("TORCH_CPP_LOG_LEVEL");
// Not set, fallback to the default level (i.e. WARNING).
std::string level{level_env.has_value() ? level_env.value() : ""};
std::string level{level_str != nullptr ? level_str : ""};
if (level.empty()) {
return;
}

View File

@ -1,108 +0,0 @@
#include <c10/util/Exception.h>
#include <c10/util/env.h>
#include <fmt/format.h>
#include <cstdlib>
#include <shared_mutex>
namespace c10::utils {
static std::shared_mutex env_mutex;
// Set an environment variable.
void set_env(const char* name, const char* value, bool overwrite) {
std::lock_guard lk(env_mutex);
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
#ifdef _MSC_VER
if (!overwrite) {
// NOLINTNEXTLINE(concurrency-mt-unsafe)
if (std::getenv(name) != nullptr) {
return;
}
}
auto full_env_variable = fmt::format("{}={}", name, value);
// NOLINTNEXTLINE(concurrency-mt-unsafe)
auto err = putenv(full_env_variable.c_str());
TORCH_INTERNAL_ASSERT(
err == 0,
"putenv failed for environment \"",
name,
"\", the error is: ",
err);
#else
// NOLINTNEXTLINE(concurrency-mt-unsafe)
auto err = setenv(name, value, static_cast<int>(overwrite));
TORCH_INTERNAL_ASSERT(
err == 0,
"setenv failed for environment \"",
name,
"\", the error is: ",
err);
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
return;
}
// Checks an environment variable is set.
bool has_env(const char* name) noexcept {
std::shared_lock lk(env_mutex);
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
// NOLINTNEXTLINE(concurrency-mt-unsafe)
auto envar = std::getenv(name);
#ifdef _MSC_VER
#pragma warning(pop)
#endif
return envar != nullptr;
}
// Reads an environment variable and returns the content if it is set
std::optional<std::string> get_env(const char* name) noexcept {
std::shared_lock lk(env_mutex);
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
// NOLINTNEXTLINE(concurrency-mt-unsafe)
auto envar = std::getenv(name);
#ifdef _MSC_VER
#pragma warning(pop)
#endif
if (envar != nullptr) {
return std::string(envar);
}
return std::nullopt;
}
// Reads an environment variable and returns
// - optional<true>, if set equal to "1"
// - optional<false>, if set equal to "0"
// - nullopt, otherwise
//
// NB:
// Issues a warning if the value of the environment variable is not 0 or 1.
std::optional<bool> check_env(const char* name) {
auto env_opt = get_env(name);
if (env_opt.has_value()) {
if (*env_opt == "0") {
return false;
}
if (*env_opt == "1") {
return true;
}
TORCH_WARN(
"Ignoring invalid value for boolean flag ",
name,
": ",
*env_opt,
"valid values are 0 or 1.");
}
return std::nullopt;
}
} // namespace c10::utils

View File

@ -1,20 +1,11 @@
#pragma once
#include <c10/macros/Export.h>
#include <c10/util/Exception.h>
#include <cstdlib>
#include <cstring>
#include <optional>
#include <string>
namespace c10::utils {
// Set an environment variable.
C10_API void set_env(
const char* name,
const char* value,
bool overwrite = true);
// Checks an environment variable is set.
C10_API bool has_env(const char* name) noexcept;
// Reads an environment variable and returns
// - optional<true>, if set equal to "1"
// - optional<false>, if set equal to "0"
@ -22,10 +13,29 @@ C10_API bool has_env(const char* name) noexcept;
//
// NB:
// Issues a warning if the value of the environment variable is not 0 or 1.
C10_API std::optional<bool> check_env(const char* name);
// Reads the value of an environment variable if it is set.
// However, check_env should be used if the value is assumed to be a flag.
C10_API std::optional<std::string> get_env(const char* name) noexcept;
inline std::optional<bool> check_env(const char* name) {
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
auto envar = std::getenv(name);
#ifdef _MSC_VER
#pragma warning(pop)
#endif
if (envar) {
if (strcmp(envar, "0") == 0) {
return false;
}
if (strcmp(envar, "1") == 0) {
return true;
}
TORCH_WARN(
"Ignoring invalid value for boolean flag ",
name,
": ",
envar,
"valid values are 0 or 1.");
}
return std::nullopt;
}
} // namespace c10::utils

View File

@ -1,5 +1,4 @@
#include <c10/util/Exception.h>
#include <c10/util/env.h>
#include <c10/util/tempfile.h>
#include <fmt/format.h>
@ -23,11 +22,10 @@ static std::string make_filename(std::string_view name_prefix) {
// We see if any of these environment variables is set and use their value, or
// else default the temporary directory to `/tmp`.
std::string tmp_directory = "/tmp";
const char* tmp_directory = "/tmp";
for (const char* variable : {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}) {
auto path_opt = c10::utils::get_env(variable);
if (path_opt.has_value()) {
tmp_directory = path_opt.value();
if (const char* path = getenv(variable)) {
tmp_directory = path;
break;
}
}