From 2675ef8758cf4939bd93fd75a9acb64f3cf4e204 Mon Sep 17 00:00:00 2001 From: PyTorch MergeBot Date: Wed, 13 Nov 2024 16:50:00 +0000 Subject: [PATCH] Revert " [Environment Variable][5/N] Use thread-safe getenv functions (#139762)" This reverts commit 43f0fe60a36dc7e3bd8f77a2451bde81496679b0. Reverted https://github.com/pytorch/pytorch/pull/139762 on behalf of https://github.com/malfet due to One of these diffs had incorrect downstream optional handling, we must reaudit all of these diffs ([comment](https://github.com/pytorch/pytorch/pull/139762#issuecomment-2474174813)) --- aten/src/ATen/core/dispatch/Dispatcher.cpp | 15 +++--- aten/src/ATen/cuda/tunable/Tunable.cpp | 55 +++++++++++----------- aten/src/ATen/native/LinearAlgebra.cpp | 8 ++-- torch/csrc/lazy/core/config.cpp | 7 ++- torch/csrc/lazy/core/debug_util.cpp | 5 +- torch/csrc/lazy/core/shape.cpp | 3 +- 6 files changed, 45 insertions(+), 48 deletions(-) diff --git a/aten/src/ATen/core/dispatch/Dispatcher.cpp b/aten/src/ATen/core/dispatch/Dispatcher.cpp index 75f19e72275..922bbab67ed 100644 --- a/aten/src/ATen/core/dispatch/Dispatcher.cpp +++ b/aten/src/ATen/core/dispatch/Dispatcher.cpp @@ -1,9 +1,10 @@ -#include #include +#include +#include #include +#include #include -#include #ifdef FBCODE_CAFFE2 #include #endif @@ -16,18 +17,18 @@ TORCH_SDT_DEFINE_SEMAPHORE(operator_end) #endif bool show_dispatch_trace() { - static const auto envar = c10::utils::get_env("TORCH_SHOW_DISPATCH_TRACE"); + static auto envar = std::getenv("TORCH_SHOW_DISPATCH_TRACE"); - if (envar.has_value()) { - if (envar == "0") { + if (envar) { + if (strcmp(envar, "0") == 0) { return false; } - if (envar == "1") { + if (strcmp(envar, "1") == 0) { return true; } TORCH_WARN( "ignoring invalid value for TORCH_SHOW_DISPATCH_TRACE: ", - envar.value(), + envar, " valid values are 0 or 1."); } diff --git a/aten/src/ATen/cuda/tunable/Tunable.cpp b/aten/src/ATen/cuda/tunable/Tunable.cpp index 5d1a04e3e4f..318d08189f4 100644 --- a/aten/src/ATen/cuda/tunable/Tunable.cpp +++ b/aten/src/ATen/cuda/tunable/Tunable.cpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #ifndef _WIN32 @@ -434,8 +433,8 @@ void TuningContext::EnableTunableOp(bool value) { } bool TuningContext::IsTunableOpEnabled() const { - static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ENABLED"); - if (env == "1") { + static const char *env = std::getenv("PYTORCH_TUNABLEOP_ENABLED"); + if (env != nullptr && strcmp(env, "1") == 0) { return true; } return enable_; @@ -461,16 +460,16 @@ void TuningContext::EnableRecordUntuned(bool value) { } bool TuningContext::IsTuningEnabled() const { - static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_TUNING"); - if (env == "0") { + static const char *env = std::getenv("PYTORCH_TUNABLEOP_TUNING"); + if (env != nullptr && strcmp(env, "0") == 0) { return false; } return tuning_enable_; } bool TuningContext::IsRecordUntunedEnabled() const { - static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_RECORD_UNTUNED"); - if (env == "1") { + static const char *env = std::getenv("PYTORCH_TUNABLEOP_RECORD_UNTUNED"); + if (env != nullptr && strcmp(env, "1") == 0) { return true; } return record_untuned_enable_; @@ -478,8 +477,8 @@ bool TuningContext::IsRecordUntunedEnabled() const { std::ofstream& TuningContext::GetUntunedFile(){ if (!untuned_file_.is_open()) { - const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_UNTUNED_FILENAME"); - std::string filename = (!env.has_value()) ? "tunableop_untuned.csv" : env.value(); + const char *env = std::getenv("PYTORCH_TUNABLEOP_UNTUNED_FILENAME"); + std::string filename = (env == nullptr) ? "tunableop_untuned.csv" : env; std::string device = c10::str(int(c10::cuda::current_device())); std::size_t found = filename.rfind('.'); @@ -516,9 +515,9 @@ void TuningContext::SetMaxTuningDurationMs(int max_duration_ms) { } int TuningContext::GetMaxTuningDurationMs() const { - static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_TUNING_DURATION_MS"); - if (env.has_value()) { - int val = stoi(env.value()); + static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_TUNING_DURATION_MS"); + if (env != nullptr) { + int val = atoi(env); return val < 0 ? 0 : val; } return max_tuning_duration_ms_; @@ -529,9 +528,9 @@ void TuningContext::SetMaxTuningIterations(int max_iter) { } int TuningContext::GetMaxTuningIterations() const { - static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_TUNING_ITERATIONS"); - if (env.has_value()) { - int val = stoi(env.value()); + static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_TUNING_ITERATIONS"); + if (env != nullptr) { + int val = atoi(env); return val < 0 ? 0 : val; } return max_tuning_iterations_; @@ -542,9 +541,9 @@ void TuningContext::SetMaxWarmupDurationMs(int max_duration_ms) { } int TuningContext::GetMaxWarmupDurationMs() const { - static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_WARMUP_DURATION_MS"); - if (env.has_value()) { - int val = stoi(env.value()); + static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_WARMUP_DURATION_MS"); + if (env != nullptr) { + int val = atoi(env); return val < 0 ? 0 : val; } return max_warmup_duration_ms_; @@ -555,9 +554,9 @@ void TuningContext::SetMaxWarmupIterations(int max_iter) { } int TuningContext::GetMaxWarmupIterations() const { - static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_WARMUP_ITERATIONS"); - if (env.has_value()) { - int val = stoi(env.value()); + static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_WARMUP_ITERATIONS"); + if (env != nullptr) { + int val = atoi(env); return val < 0 ? 0 : val; } return max_warmup_iterations_; @@ -568,8 +567,8 @@ void TuningContext::EnableICacheFlush(bool value) { } bool TuningContext::IsICacheFlushEnabled() const { - static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ICACHE_FLUSH_ENABLED"); - if (env == "0") { + static const char *env = std::getenv("PYTORCH_TUNABLEOP_ICACHE_FLUSH_ENABLED"); + if (env != nullptr && strcmp(env, "0") == 0) { return false; } return icache_flush_; @@ -580,10 +579,10 @@ void TuningContext::SetRotatingBufferSize(int size) { } int TuningContext::GetRotatingBufferSize() const { - static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ROTATING_BUFFER_SIZE"); - if (env.has_value()) { + static const char *env = std::getenv("PYTORCH_TUNABLEOP_ROTATING_BUFFER_SIZE"); + if (env != nullptr) { constexpr int MB = 1024 * 1024; - int val = stoi(env.value()); + int val = atoi(env); return val < 0 ? 0 : val * MB; // env var is specified as MB, returned as bytes } else { @@ -603,8 +602,8 @@ TuningResultsManager& TuningContext::GetTuningResultsManager() { manager_initialized_ = true; if (GetFilename().empty()) { // if SetFilename() was not already called, call it now with the default or env var - const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_FILENAME"); - std::string filename = (!env.has_value()) ? "tunableop_results.csv" : env.value(); + const char *env = std::getenv("PYTORCH_TUNABLEOP_FILENAME"); + std::string filename = (env == nullptr) ? "tunableop_results.csv" : env; SetFilename(filename, true); } auto filename = GetFilename(); diff --git a/aten/src/ATen/native/LinearAlgebra.cpp b/aten/src/ATen/native/LinearAlgebra.cpp index 599b16bab3d..6109717ff2c 100644 --- a/aten/src/ATen/native/LinearAlgebra.cpp +++ b/aten/src/ATen/native/LinearAlgebra.cpp @@ -1364,8 +1364,8 @@ static inline int64_t get_mkldnn_matmul_min_dim() { //it's enabled on all Neoverse cpus. return is_arm_neoverse() ? 8 : 0; }(); - const auto ptr = c10::utils::get_env("TORCH_MKLDNN_MATMUL_MIN_DIM"); - return ptr.has_value() ? std::stoi(ptr.value()) : default_min_dim; + const char* ptr = std::getenv("TORCH_MKLDNN_MATMUL_MIN_DIM"); + return ptr != nullptr ? std::atoi(ptr) : default_min_dim; }(); return value; } @@ -1378,8 +1378,8 @@ static inline int64_t get_mkldnn_matmul_min_size() { // it's enabled on all Neoverse cpus. return is_arm_neoverse() ? 8 * 1024 : 0; }(); - const auto ptr = c10::utils::get_env("TORCH_MKLDNN_MATMUL_MIN_SIZE"); - return ptr.has_value() ? std::stoi(ptr.value()) : default_min_size; + const char* ptr = std::getenv("TORCH_MKLDNN_MATMUL_MIN_SIZE"); + return ptr != nullptr ? std::atoi(ptr) : default_min_size; }(); return value; } diff --git a/torch/csrc/lazy/core/config.cpp b/torch/csrc/lazy/core/config.cpp index 00bf247cabd..09c9347dee4 100644 --- a/torch/csrc/lazy/core/config.cpp +++ b/torch/csrc/lazy/core/config.cpp @@ -1,4 +1,3 @@ -#include #include C10_DEFINE_bool(torch_lazy_ir_debug, false, "Enable lazy tensor IR debugging") @@ -77,9 +76,9 @@ namespace torch::lazy { std::string& getLTCForceFallback() { static std::string config; static bool _ignore = [&]() { - auto env = c10::utils::get_env("LTC_FORCE_FALLBACK"); - if (env) { - config = std::move(env.value()); + char* envptr = std::getenv("LTC_FORCE_FALLBACK"); + if (envptr) { + config = std::string(envptr); } return true; }(); diff --git a/torch/csrc/lazy/core/debug_util.cpp b/torch/csrc/lazy/core/debug_util.cpp index 8105760174e..2ddaceab71a 100644 --- a/torch/csrc/lazy/core/debug_util.cpp +++ b/torch/csrc/lazy/core/debug_util.cpp @@ -1,4 +1,3 @@ -#include #include #include @@ -18,8 +17,8 @@ namespace torch::lazy { namespace { std::string GetEnvString(const char* name, const std::string& defval) { - const auto env = c10::utils::get_env(name); - return env.has_value() ? env.value() : defval; + const char* env = std::getenv(name); + return env != nullptr ? env : defval; } DebugUtil::GraphFormat DefaultGraphFormat() { diff --git a/torch/csrc/lazy/core/shape.cpp b/torch/csrc/lazy/core/shape.cpp index f59684d4ffc..fbdb38cbdec 100644 --- a/torch/csrc/lazy/core/shape.cpp +++ b/torch/csrc/lazy/core/shape.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -59,7 +58,7 @@ Shape Shape::with_symbolic_dims( } bool symbolicShapeEnabled() { - static const bool enabled = c10::utils::has_env("LTC_ENABLE_SYMBOLIC_SHAPES"); + static bool enabled = std::getenv("LTC_ENABLE_SYMBOLIC_SHAPES") != nullptr; return enabled || FLAGS_ltc_enable_symbolic_shapes; }