[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
This commit is contained in:
cyy 2024-11-18 21:45:32 +00:00 committed by PyTorch MergeBot
parent c62da98c1a
commit 8cd7ad8b48
6 changed files with 48 additions and 45 deletions

View File

@ -1,10 +1,9 @@
#include <ATen/core/dispatch/Dispatcher.h>
#include <ATen/core/PythonOpRegistrationTrampoline.h> #include <ATen/core/PythonOpRegistrationTrampoline.h>
#include <chrono> #include <ATen/core/dispatch/Dispatcher.h>
#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
@ -17,18 +16,18 @@ TORCH_SDT_DEFINE_SEMAPHORE(operator_end)
#endif #endif
bool show_dispatch_trace() { 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 (envar.has_value()) {
if (strcmp(envar, "0") == 0) { if (envar == "0") {
return false; return false;
} }
if (strcmp(envar, "1") == 0) { if (envar == "1") {
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, envar.value(),
" valid values are 0 or 1."); " valid values are 0 or 1.");
} }

View File

@ -13,6 +13,7 @@
#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
@ -433,8 +434,8 @@ void TuningContext::EnableTunableOp(bool value) {
} }
bool TuningContext::IsTunableOpEnabled() const { bool TuningContext::IsTunableOpEnabled() const {
static const char *env = std::getenv("PYTORCH_TUNABLEOP_ENABLED"); static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ENABLED");
if (env != nullptr && strcmp(env, "1") == 0) { if (env == "1") {
return true; return true;
} }
return enable_; return enable_;
@ -460,16 +461,16 @@ void TuningContext::EnableRecordUntuned(bool value) {
} }
bool TuningContext::IsTuningEnabled() const { bool TuningContext::IsTuningEnabled() const {
static const char *env = std::getenv("PYTORCH_TUNABLEOP_TUNING"); static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_TUNING");
if (env != nullptr && strcmp(env, "0") == 0) { if (env == "0") {
return false; return false;
} }
return tuning_enable_; return tuning_enable_;
} }
bool TuningContext::IsRecordUntunedEnabled() const { bool TuningContext::IsRecordUntunedEnabled() const {
static const char *env = std::getenv("PYTORCH_TUNABLEOP_RECORD_UNTUNED"); static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_RECORD_UNTUNED");
if (env != nullptr && strcmp(env, "1") == 0) { if (env == "1") {
return true; return true;
} }
return record_untuned_enable_; return record_untuned_enable_;
@ -477,8 +478,8 @@ bool TuningContext::IsRecordUntunedEnabled() const {
std::ofstream& TuningContext::GetUntunedFile(){ std::ofstream& TuningContext::GetUntunedFile(){
if (!untuned_file_.is_open()) { if (!untuned_file_.is_open()) {
const char *env = std::getenv("PYTORCH_TUNABLEOP_UNTUNED_FILENAME"); const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_UNTUNED_FILENAME");
std::string filename = (env == nullptr) ? "tunableop_untuned.csv" : env; std::string filename = (!env.has_value()) ? "tunableop_untuned.csv" : env.value();
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('.');
@ -515,9 +516,9 @@ void TuningContext::SetMaxTuningDurationMs(int max_duration_ms) {
} }
int TuningContext::GetMaxTuningDurationMs() const { int TuningContext::GetMaxTuningDurationMs() const {
static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_TUNING_DURATION_MS"); static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_TUNING_DURATION_MS");
if (env != nullptr) { if (env.has_value()) {
int val = atoi(env); int val = stoi(env.value());
return val < 0 ? 0 : val; return val < 0 ? 0 : val;
} }
return max_tuning_duration_ms_; return max_tuning_duration_ms_;
@ -528,9 +529,9 @@ void TuningContext::SetMaxTuningIterations(int max_iter) {
} }
int TuningContext::GetMaxTuningIterations() const { int TuningContext::GetMaxTuningIterations() const {
static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_TUNING_ITERATIONS"); static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_TUNING_ITERATIONS");
if (env != nullptr) { if (env.has_value()) {
int val = atoi(env); int val = stoi(env.value());
return val < 0 ? 0 : val; return val < 0 ? 0 : val;
} }
return max_tuning_iterations_; return max_tuning_iterations_;
@ -541,9 +542,9 @@ void TuningContext::SetMaxWarmupDurationMs(int max_duration_ms) {
} }
int TuningContext::GetMaxWarmupDurationMs() const { int TuningContext::GetMaxWarmupDurationMs() const {
static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_WARMUP_DURATION_MS"); static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_WARMUP_DURATION_MS");
if (env != nullptr) { if (env.has_value()) {
int val = atoi(env); int val = stoi(env.value());
return val < 0 ? 0 : val; return val < 0 ? 0 : val;
} }
return max_warmup_duration_ms_; return max_warmup_duration_ms_;
@ -554,9 +555,9 @@ void TuningContext::SetMaxWarmupIterations(int max_iter) {
} }
int TuningContext::GetMaxWarmupIterations() const { int TuningContext::GetMaxWarmupIterations() const {
static const char *env = std::getenv("PYTORCH_TUNABLEOP_MAX_WARMUP_ITERATIONS"); static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_MAX_WARMUP_ITERATIONS");
if (env != nullptr) { if (env.has_value()) {
int val = atoi(env); int val = stoi(env.value());
return val < 0 ? 0 : val; return val < 0 ? 0 : val;
} }
return max_warmup_iterations_; return max_warmup_iterations_;
@ -567,8 +568,8 @@ void TuningContext::EnableICacheFlush(bool value) {
} }
bool TuningContext::IsICacheFlushEnabled() const { bool TuningContext::IsICacheFlushEnabled() const {
static const char *env = std::getenv("PYTORCH_TUNABLEOP_ICACHE_FLUSH_ENABLED"); static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ICACHE_FLUSH_ENABLED");
if (env != nullptr && strcmp(env, "0") == 0) { if (env == "0") {
return false; return false;
} }
return icache_flush_; return icache_flush_;
@ -579,10 +580,10 @@ void TuningContext::SetRotatingBufferSize(int size) {
} }
int TuningContext::GetRotatingBufferSize() const { int TuningContext::GetRotatingBufferSize() const {
static const char *env = std::getenv("PYTORCH_TUNABLEOP_ROTATING_BUFFER_SIZE"); static const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_ROTATING_BUFFER_SIZE");
if (env != nullptr) { if (env.has_value()) {
constexpr int MB = 1024 * 1024; 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 return val < 0 ? 0 : val * MB; // env var is specified as MB, returned as bytes
} }
else { else {
@ -602,8 +603,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 char *env = std::getenv("PYTORCH_TUNABLEOP_FILENAME"); const auto env = c10::utils::get_env("PYTORCH_TUNABLEOP_FILENAME");
std::string filename = (env == nullptr) ? "tunableop_results.csv" : env; std::string filename = (!env.has_value()) ? "tunableop_results.csv" : env.value();
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 char* ptr = std::getenv("TORCH_MKLDNN_MATMUL_MIN_DIM"); const auto ptr = c10::utils::get_env("TORCH_MKLDNN_MATMUL_MIN_DIM");
return ptr != nullptr ? std::atoi(ptr) : default_min_dim; return ptr.has_value() ? std::stoi(ptr.value()) : 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 char* ptr = std::getenv("TORCH_MKLDNN_MATMUL_MIN_SIZE"); const auto ptr = c10::utils::get_env("TORCH_MKLDNN_MATMUL_MIN_SIZE");
return ptr != nullptr ? std::atoi(ptr) : default_min_size; return ptr.has_value() ? std::stoi(ptr.value()) : default_min_size;
}(); }();
return value; return value;
} }

View File

@ -1,3 +1,4 @@
#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")
@ -76,9 +77,9 @@ namespace torch::lazy {
std::string& getLTCForceFallback() { std::string& getLTCForceFallback() {
static std::string config; static std::string config;
static bool _ignore = [&]() { static bool _ignore = [&]() {
char* envptr = std::getenv("LTC_FORCE_FALLBACK"); auto env = c10::utils::get_env("LTC_FORCE_FALLBACK");
if (envptr) { if (env) {
config = std::string(envptr); config = std::move(env.value());
} }
return true; return true;
}(); }();

View File

@ -1,3 +1,4 @@
#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>
@ -17,8 +18,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 char* env = std::getenv(name); const auto env = c10::utils::get_env(name);
return env != nullptr ? env : defval; return env.has_value() ? env.value() : defval;
} }
DebugUtil::GraphFormat DefaultGraphFormat() { DebugUtil::GraphFormat DefaultGraphFormat() {

View File

@ -1,3 +1,4 @@
#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>
@ -58,7 +59,7 @@ Shape Shape::with_symbolic_dims(
} }
bool symbolicShapeEnabled() { 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; return enabled || FLAGS_ltc_enable_symbolic_shapes;
} }