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))
This commit is contained in:
PyTorch MergeBot
2024-11-13 16:50:00 +00:00
parent 3d618019fb
commit 2675ef8758
6 changed files with 45 additions and 48 deletions

View File

@ -1,9 +1,10 @@
#include <ATen/core/PythonOpRegistrationTrampoline.h>
#include <ATen/core/dispatch/Dispatcher.h> #include <ATen/core/dispatch/Dispatcher.h>
#include <ATen/core/PythonOpRegistrationTrampoline.h>
#include <chrono>
#include <list> #include <list>
#include <sstream>
#include <utility> #include <utility>
#include <c10/util/env.h>
#ifdef FBCODE_CAFFE2 #ifdef FBCODE_CAFFE2
#include <c10/util/static_tracepoint.h> #include <c10/util/static_tracepoint.h>
#endif #endif
@ -16,18 +17,18 @@ TORCH_SDT_DEFINE_SEMAPHORE(operator_end)
#endif #endif
bool show_dispatch_trace() { 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) {
if (envar == "0") { if (strcmp(envar, "0") == 0) {
return false; return false;
} }
if (envar == "1") { if (strcmp(envar, "1") == 0) {
return true; return true;
} }
TORCH_WARN( TORCH_WARN(
"ignoring invalid value for TORCH_SHOW_DISPATCH_TRACE: ", "ignoring invalid value for TORCH_SHOW_DISPATCH_TRACE: ",
envar.value(), envar,
" valid values are 0 or 1."); " valid values are 0 or 1.");
} }

View File

@ -13,7 +13,6 @@
#include <ATen/cuda/tunable/Tunable.h> #include <ATen/cuda/tunable/Tunable.h>
#include <c10/util/Exception.h> #include <c10/util/Exception.h>
#include <c10/util/StringUtil.h> #include <c10/util/StringUtil.h>
#include <c10/util/env.h>
#include <torch/version.h> #include <torch/version.h>
#ifndef _WIN32 #ifndef _WIN32
@ -434,8 +433,8 @@ void TuningContext::EnableTunableOp(bool value) {
} }
bool TuningContext::IsTunableOpEnabled() const { bool TuningContext::IsTunableOpEnabled() const {
static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ENABLED"); static const char *env = std::getenv("PYTORCH_TUNABLEOP_ENABLED");
if (env == "1") { if (env != nullptr && strcmp(env, "1") == 0) {
return true; return true;
} }
return enable_; return enable_;
@ -461,16 +460,16 @@ void TuningContext::EnableRecordUntuned(bool value) {
} }
bool TuningContext::IsTuningEnabled() const { bool TuningContext::IsTuningEnabled() const {
static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_TUNING"); static const char *env = std::getenv("PYTORCH_TUNABLEOP_TUNING");
if (env == "0") { if (env != nullptr && strcmp(env, "0") == 0) {
return false; return false;
} }
return tuning_enable_; return tuning_enable_;
} }
bool TuningContext::IsRecordUntunedEnabled() const { bool TuningContext::IsRecordUntunedEnabled() const {
static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_RECORD_UNTUNED"); static const char *env = std::getenv("PYTORCH_TUNABLEOP_RECORD_UNTUNED");
if (env == "1") { if (env != nullptr && strcmp(env, "1") == 0) {
return true; return true;
} }
return record_untuned_enable_; return record_untuned_enable_;
@ -478,8 +477,8 @@ bool TuningContext::IsRecordUntunedEnabled() const {
std::ofstream& TuningContext::GetUntunedFile(){ std::ofstream& TuningContext::GetUntunedFile(){
if (!untuned_file_.is_open()) { if (!untuned_file_.is_open()) {
const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_UNTUNED_FILENAME"); const char *env = std::getenv("PYTORCH_TUNABLEOP_UNTUNED_FILENAME");
std::string filename = (!env.has_value()) ? "tunableop_untuned.csv" : env.value(); std::string filename = (env == nullptr) ? "tunableop_untuned.csv" : env;
std::string device = c10::str(int(c10::cuda::current_device())); std::string device = c10::str(int(c10::cuda::current_device()));
std::size_t found = filename.rfind('.'); std::size_t found = filename.rfind('.');
@ -516,9 +515,9 @@ void TuningContext::SetMaxTuningDurationMs(int max_duration_ms) {
} }
int TuningContext::GetMaxTuningDurationMs() const { int TuningContext::GetMaxTuningDurationMs() const {
static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_TUNING_DURATION_MS"); static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_TUNING_DURATION_MS");
if (env.has_value()) { if (env != nullptr) {
int val = stoi(env.value()); int val = atoi(env);
return val < 0 ? 0 : val; return val < 0 ? 0 : val;
} }
return max_tuning_duration_ms_; return max_tuning_duration_ms_;
@ -529,9 +528,9 @@ void TuningContext::SetMaxTuningIterations(int max_iter) {
} }
int TuningContext::GetMaxTuningIterations() const { int TuningContext::GetMaxTuningIterations() const {
static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_TUNING_ITERATIONS"); static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_TUNING_ITERATIONS");
if (env.has_value()) { if (env != nullptr) {
int val = stoi(env.value()); int val = atoi(env);
return val < 0 ? 0 : val; return val < 0 ? 0 : val;
} }
return max_tuning_iterations_; return max_tuning_iterations_;
@ -542,9 +541,9 @@ void TuningContext::SetMaxWarmupDurationMs(int max_duration_ms) {
} }
int TuningContext::GetMaxWarmupDurationMs() const { int TuningContext::GetMaxWarmupDurationMs() const {
static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_WARMUP_DURATION_MS"); static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_WARMUP_DURATION_MS");
if (env.has_value()) { if (env != nullptr) {
int val = stoi(env.value()); int val = atoi(env);
return val < 0 ? 0 : val; return val < 0 ? 0 : val;
} }
return max_warmup_duration_ms_; return max_warmup_duration_ms_;
@ -555,9 +554,9 @@ void TuningContext::SetMaxWarmupIterations(int max_iter) {
} }
int TuningContext::GetMaxWarmupIterations() const { int TuningContext::GetMaxWarmupIterations() const {
static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_WARMUP_ITERATIONS"); static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_WARMUP_ITERATIONS");
if (env.has_value()) { if (env != nullptr) {
int val = stoi(env.value()); int val = atoi(env);
return val < 0 ? 0 : val; return val < 0 ? 0 : val;
} }
return max_warmup_iterations_; return max_warmup_iterations_;
@ -568,8 +567,8 @@ void TuningContext::EnableICacheFlush(bool value) {
} }
bool TuningContext::IsICacheFlushEnabled() const { bool TuningContext::IsICacheFlushEnabled() const {
static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ICACHE_FLUSH_ENABLED"); static const char *env = std::getenv("PYTORCH_TUNABLEOP_ICACHE_FLUSH_ENABLED");
if (env == "0") { if (env != nullptr && strcmp(env, "0") == 0) {
return false; return false;
} }
return icache_flush_; return icache_flush_;
@ -580,10 +579,10 @@ void TuningContext::SetRotatingBufferSize(int size) {
} }
int TuningContext::GetRotatingBufferSize() const { int TuningContext::GetRotatingBufferSize() const {
static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ROTATING_BUFFER_SIZE"); static const char *env = std::getenv("PYTORCH_TUNABLEOP_ROTATING_BUFFER_SIZE");
if (env.has_value()) { if (env != nullptr) {
constexpr int MB = 1024 * 1024; 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 return val < 0 ? 0 : val * MB; // env var is specified as MB, returned as bytes
} }
else { else {
@ -603,8 +602,8 @@ TuningResultsManager& TuningContext::GetTuningResultsManager() {
manager_initialized_ = true; manager_initialized_ = true;
if (GetFilename().empty()) { if (GetFilename().empty()) {
// if SetFilename() was not already called, call it now with the default or env var // 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"); const char *env = std::getenv("PYTORCH_TUNABLEOP_FILENAME");
std::string filename = (!env.has_value()) ? "tunableop_results.csv" : env.value(); std::string filename = (env == nullptr) ? "tunableop_results.csv" : env;
SetFilename(filename, true); SetFilename(filename, true);
} }
auto filename = GetFilename(); auto filename = GetFilename();

View File

@ -1364,8 +1364,8 @@ static inline int64_t get_mkldnn_matmul_min_dim() {
//it's enabled on all Neoverse cpus. //it's enabled on all Neoverse cpus.
return is_arm_neoverse() ? 8 : 0; return is_arm_neoverse() ? 8 : 0;
}(); }();
const auto ptr = c10::utils::get_env("TORCH_MKLDNN_MATMUL_MIN_DIM"); const char* ptr = std::getenv("TORCH_MKLDNN_MATMUL_MIN_DIM");
return ptr.has_value() ? std::stoi(ptr.value()) : default_min_dim; return ptr != nullptr ? std::atoi(ptr) : default_min_dim;
}(); }();
return value; return value;
} }
@ -1378,8 +1378,8 @@ static inline int64_t get_mkldnn_matmul_min_size() {
// it's enabled on all Neoverse cpus. // it's enabled on all Neoverse cpus.
return is_arm_neoverse() ? 8 * 1024 : 0; return is_arm_neoverse() ? 8 * 1024 : 0;
}(); }();
const auto ptr = c10::utils::get_env("TORCH_MKLDNN_MATMUL_MIN_SIZE"); const char* ptr = std::getenv("TORCH_MKLDNN_MATMUL_MIN_SIZE");
return ptr.has_value() ? std::stoi(ptr.value()) : default_min_size; return ptr != nullptr ? std::atoi(ptr) : default_min_size;
}(); }();
return value; return value;
} }

View File

@ -1,4 +1,3 @@
#include <c10/util/env.h>
#include <torch/csrc/lazy/core/config.h> #include <torch/csrc/lazy/core/config.h>
C10_DEFINE_bool(torch_lazy_ir_debug, false, "Enable lazy tensor IR debugging") C10_DEFINE_bool(torch_lazy_ir_debug, false, "Enable lazy tensor IR debugging")
@ -77,9 +76,9 @@ namespace torch::lazy {
std::string& getLTCForceFallback() { std::string& getLTCForceFallback() {
static std::string config; static std::string config;
static bool _ignore = [&]() { static bool _ignore = [&]() {
auto env = c10::utils::get_env("LTC_FORCE_FALLBACK"); char* envptr = std::getenv("LTC_FORCE_FALLBACK");
if (env) { if (envptr) {
config = std::move(env.value()); config = std::string(envptr);
} }
return true; return true;
}(); }();

View File

@ -1,4 +1,3 @@
#include <c10/util/env.h>
#include <c10/util/irange.h> #include <c10/util/irange.h>
#include <torch/csrc/lazy/core/debug_util.h> #include <torch/csrc/lazy/core/debug_util.h>
@ -18,8 +17,8 @@ namespace torch::lazy {
namespace { namespace {
std::string GetEnvString(const char* name, const std::string& defval) { std::string GetEnvString(const char* name, const std::string& defval) {
const auto env = c10::utils::get_env(name); const char* env = std::getenv(name);
return env.has_value() ? env.value() : defval; return env != nullptr ? env : defval;
} }
DebugUtil::GraphFormat DefaultGraphFormat() { DebugUtil::GraphFormat DefaultGraphFormat() {

View File

@ -1,4 +1,3 @@
#include <c10/util/env.h>
#include <c10/util/irange.h> #include <c10/util/irange.h>
#include <torch/csrc/lazy/core/shape.h> #include <torch/csrc/lazy/core/shape.h>
#include <torch/csrc/lazy/core/tensor.h> #include <torch/csrc/lazy/core/tensor.h>
@ -59,7 +58,7 @@ Shape Shape::with_symbolic_dims(
} }
bool symbolicShapeEnabled() { 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; return enabled || FLAGS_ltc_enable_symbolic_shapes;
} }