[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 <chrono>
#include <ATen/core/dispatch/Dispatcher.h>
#include <list>
#include <sstream>
#include <utility>
#include <c10/util/env.h>
#ifdef FBCODE_CAFFE2
#include <c10/util/static_tracepoint.h>
#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.");
}

View File

@ -13,6 +13,7 @@
#include <ATen/cuda/tunable/Tunable.h>
#include <c10/util/Exception.h>
#include <c10/util/StringUtil.h>
#include <c10/util/env.h>
#include <torch/version.h>
#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();

View File

@ -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;
}

View File

@ -1,3 +1,4 @@
#include <c10/util/env.h>
#include <torch/csrc/lazy/core/config.h>
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;
}();

View File

@ -1,3 +1,4 @@
#include <c10/util/env.h>
#include <c10/util/irange.h>
#include <torch/csrc/lazy/core/debug_util.h>
@ -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() {

View File

@ -1,3 +1,4 @@
#include <c10/util/env.h>
#include <c10/util/irange.h>
#include <torch/csrc/lazy/core/shape.h>
#include <torch/csrc/lazy/core/tensor.h>
@ -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;
}