From 8cd7ad8b48a187df36bdc9ac1fcaf25db950eed6 Mon Sep 17 00:00:00 2001 From: cyy Date: Mon, 18 Nov 2024 21:45:32 +0000 Subject: [PATCH] [Reland][Environment Variable][5/N] Use thread-safe getenv functions (#140594) Reland of #139762 with no bug found. Pull Request resolved: https://github.com/pytorch/pytorch/pull/140594 Approved by: https://github.com/ezyang --- 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, 48 insertions(+), 45 deletions(-) diff --git a/aten/src/ATen/core/dispatch/Dispatcher.cpp b/aten/src/ATen/core/dispatch/Dispatcher.cpp index 922bbab67eda..75f19e722754 100644 --- a/aten/src/ATen/core/dispatch/Dispatcher.cpp +++ b/aten/src/ATen/core/dispatch/Dispatcher.cpp @@ -1,10 +1,9 @@ -#include #include -#include +#include #include -#include #include +#include #ifdef FBCODE_CAFFE2 #include #endif @@ -17,18 +16,18 @@ TORCH_SDT_DEFINE_SEMAPHORE(operator_end) #endif bool show_dispatch_trace() { - static auto envar = std::getenv("TORCH_SHOW_DISPATCH_TRACE"); + static const auto envar = c10::utils::get_env("TORCH_SHOW_DISPATCH_TRACE"); - if (envar) { - if (strcmp(envar, "0") == 0) { + if (envar.has_value()) { + if (envar == "0") { return false; } - if (strcmp(envar, "1") == 0) { + if (envar == "1") { return true; } TORCH_WARN( "ignoring invalid value for TORCH_SHOW_DISPATCH_TRACE: ", - envar, + envar.value(), " 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 318d08189f4e..5d1a04e3e4f0 100644 --- a/aten/src/ATen/cuda/tunable/Tunable.cpp +++ b/aten/src/ATen/cuda/tunable/Tunable.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #ifndef _WIN32 @@ -433,8 +434,8 @@ void TuningContext::EnableTunableOp(bool value) { } bool TuningContext::IsTunableOpEnabled() const { - static const char *env = std::getenv("PYTORCH_TUNABLEOP_ENABLED"); - if (env != nullptr && strcmp(env, "1") == 0) { + static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ENABLED"); + if (env == "1") { return true; } return enable_; @@ -460,16 +461,16 @@ void TuningContext::EnableRecordUntuned(bool value) { } bool TuningContext::IsTuningEnabled() const { - static const char *env = std::getenv("PYTORCH_TUNABLEOP_TUNING"); - if (env != nullptr && strcmp(env, "0") == 0) { + static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_TUNING"); + if (env == "0") { return false; } return tuning_enable_; } bool TuningContext::IsRecordUntunedEnabled() const { - static const char *env = std::getenv("PYTORCH_TUNABLEOP_RECORD_UNTUNED"); - if (env != nullptr && strcmp(env, "1") == 0) { + static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_RECORD_UNTUNED"); + if (env == "1") { return true; } return record_untuned_enable_; @@ -477,8 +478,8 @@ bool TuningContext::IsRecordUntunedEnabled() const { std::ofstream& TuningContext::GetUntunedFile(){ if (!untuned_file_.is_open()) { - const char *env = std::getenv("PYTORCH_TUNABLEOP_UNTUNED_FILENAME"); - std::string filename = (env == nullptr) ? "tunableop_untuned.csv" : env; + const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_UNTUNED_FILENAME"); + std::string filename = (!env.has_value()) ? "tunableop_untuned.csv" : env.value(); std::string device = c10::str(int(c10::cuda::current_device())); std::size_t found = filename.rfind('.'); @@ -515,9 +516,9 @@ void TuningContext::SetMaxTuningDurationMs(int max_duration_ms) { } int TuningContext::GetMaxTuningDurationMs() const { - static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_TUNING_DURATION_MS"); - if (env != nullptr) { - int val = atoi(env); + static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_TUNING_DURATION_MS"); + if (env.has_value()) { + int val = stoi(env.value()); return val < 0 ? 0 : val; } return max_tuning_duration_ms_; @@ -528,9 +529,9 @@ void TuningContext::SetMaxTuningIterations(int max_iter) { } int TuningContext::GetMaxTuningIterations() const { - static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_TUNING_ITERATIONS"); - if (env != nullptr) { - int val = atoi(env); + static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_TUNING_ITERATIONS"); + if (env.has_value()) { + int val = stoi(env.value()); return val < 0 ? 0 : val; } return max_tuning_iterations_; @@ -541,9 +542,9 @@ void TuningContext::SetMaxWarmupDurationMs(int max_duration_ms) { } int TuningContext::GetMaxWarmupDurationMs() const { - static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_WARMUP_DURATION_MS"); - if (env != nullptr) { - int val = atoi(env); + static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_WARMUP_DURATION_MS"); + if (env.has_value()) { + int val = stoi(env.value()); return val < 0 ? 0 : val; } return max_warmup_duration_ms_; @@ -554,9 +555,9 @@ void TuningContext::SetMaxWarmupIterations(int max_iter) { } int TuningContext::GetMaxWarmupIterations() const { - static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_WARMUP_ITERATIONS"); - if (env != nullptr) { - int val = atoi(env); + static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_WARMUP_ITERATIONS"); + if (env.has_value()) { + int val = stoi(env.value()); return val < 0 ? 0 : val; } return max_warmup_iterations_; @@ -567,8 +568,8 @@ void TuningContext::EnableICacheFlush(bool value) { } bool TuningContext::IsICacheFlushEnabled() const { - static const char *env = std::getenv("PYTORCH_TUNABLEOP_ICACHE_FLUSH_ENABLED"); - if (env != nullptr && strcmp(env, "0") == 0) { + static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ICACHE_FLUSH_ENABLED"); + if (env == "0") { return false; } return icache_flush_; @@ -579,10 +580,10 @@ void TuningContext::SetRotatingBufferSize(int size) { } int TuningContext::GetRotatingBufferSize() const { - static const char *env = std::getenv("PYTORCH_TUNABLEOP_ROTATING_BUFFER_SIZE"); - if (env != nullptr) { + static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ROTATING_BUFFER_SIZE"); + if (env.has_value()) { constexpr int MB = 1024 * 1024; - int val = atoi(env); + int val = stoi(env.value()); return val < 0 ? 0 : val * MB; // env var is specified as MB, returned as bytes } else { @@ -602,8 +603,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 char *env = std::getenv("PYTORCH_TUNABLEOP_FILENAME"); - std::string filename = (env == nullptr) ? "tunableop_results.csv" : env; + const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_FILENAME"); + std::string filename = (!env.has_value()) ? "tunableop_results.csv" : env.value(); SetFilename(filename, true); } auto filename = GetFilename(); diff --git a/aten/src/ATen/native/LinearAlgebra.cpp b/aten/src/ATen/native/LinearAlgebra.cpp index 6109717ff2c3..599b16bab3dd 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 char* ptr = std::getenv("TORCH_MKLDNN_MATMUL_MIN_DIM"); - return ptr != nullptr ? std::atoi(ptr) : default_min_dim; + const auto ptr = c10::utils::get_env("TORCH_MKLDNN_MATMUL_MIN_DIM"); + return ptr.has_value() ? std::stoi(ptr.value()) : 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 char* ptr = std::getenv("TORCH_MKLDNN_MATMUL_MIN_SIZE"); - return ptr != nullptr ? std::atoi(ptr) : default_min_size; + const auto ptr = c10::utils::get_env("TORCH_MKLDNN_MATMUL_MIN_SIZE"); + return ptr.has_value() ? std::stoi(ptr.value()) : default_min_size; }(); return value; } diff --git a/torch/csrc/lazy/core/config.cpp b/torch/csrc/lazy/core/config.cpp index 09c9347dee45..00bf247cabdc 100644 --- a/torch/csrc/lazy/core/config.cpp +++ b/torch/csrc/lazy/core/config.cpp @@ -1,3 +1,4 @@ +#include #include C10_DEFINE_bool(torch_lazy_ir_debug, false, "Enable lazy tensor IR debugging") @@ -76,9 +77,9 @@ namespace torch::lazy { std::string& getLTCForceFallback() { static std::string config; static bool _ignore = [&]() { - char* envptr = std::getenv("LTC_FORCE_FALLBACK"); - if (envptr) { - config = std::string(envptr); + auto env = c10::utils::get_env("LTC_FORCE_FALLBACK"); + if (env) { + config = std::move(env.value()); } return true; }(); diff --git a/torch/csrc/lazy/core/debug_util.cpp b/torch/csrc/lazy/core/debug_util.cpp index 2ddaceab71a1..8105760174ee 100644 --- a/torch/csrc/lazy/core/debug_util.cpp +++ b/torch/csrc/lazy/core/debug_util.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -17,8 +18,8 @@ namespace torch::lazy { namespace { std::string GetEnvString(const char* name, const std::string& defval) { - const char* env = std::getenv(name); - return env != nullptr ? env : defval; + const auto env = c10::utils::get_env(name); + return env.has_value() ? env.value() : defval; } DebugUtil::GraphFormat DefaultGraphFormat() { diff --git a/torch/csrc/lazy/core/shape.cpp b/torch/csrc/lazy/core/shape.cpp index fbdb38cbdec1..f59684d4ffc2 100644 --- a/torch/csrc/lazy/core/shape.cpp +++ b/torch/csrc/lazy/core/shape.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -58,7 +59,7 @@ Shape Shape::with_symbolic_dims( } bool symbolicShapeEnabled() { - static bool enabled = std::getenv("LTC_ENABLE_SYMBOLIC_SHAPES") != nullptr; + static const bool enabled = c10::utils::has_env("LTC_ENABLE_SYMBOLIC_SHAPES"); return enabled || FLAGS_ltc_enable_symbolic_shapes; }