pytorch
aa857850 - Add check_env, getenv api (#59052)

Commit
3 years ago
Add check_env, getenv api (#59052) Summary: Related Issue: https://github.com/pytorch/pytorch/issues/57691 This PR introduces an API for checking environment variables: ```c++ optional<bool> check_env(const char *name) ``` Reads the environment variable name and returns - `optional<true>`, if set equal to "1" - `optional<false>`, if set equal to "0" - `nullopt`, otherwise Issues a warning if the environment variable was set to any value other than 0 or 1 Pull Request resolved: https://github.com/pytorch/pytorch/pull/59052 Test Plan: Manually run the following test case: - Apply this diff to the repo ``` diff --git a/torch/csrc/Exceptions.cpp b/torch/csrc/Exceptions.cpp index d008643f70..990d254f0d 100644 --- a/torch/csrc/Exceptions.cpp +++ b/torch/csrc/Exceptions.cpp @@ -9,6 +9,9 @@ #include <torch/csrc/THP.h> +#include <c10/util/Optional.h> +#include <c10/util/env.h> + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) PyObject *THPException_FatalError; @@ -23,18 +26,7 @@ bool THPException_init(PyObject *module) namespace torch { static bool compute_cpp_stack_traces_enabled() { - auto envar = std::getenv("TORCH_SHOW_CPP_STACKTRACES"); - if (envar) { - if (strcmp(envar, "0") == 0) { - return false; - } - if (strcmp(envar, "1") == 0) { - return true; - } - TORCH_WARN("ignoring invalid value for TORCH_SHOW_CPP_STACKTRACES: ", envar, - " valid values are 0 or 1."); - } - return false; + return c10::utils::check_env("TORCH_SHOW_CPP_STACKTRACES").value_or(false); } bool get_cpp_stacktraces_enabled() { ``` This patch replaces the prior `std::getenv` usage in `torch/csrc/Exceptions.cpp` to use the new api. - Run the following python3 script ```python import torch print(torch.__version__) # should print local version (not release) a1 = torch.tensor([1,2,3]) a2 = torch.tensor([2]) a1 @ a2 ``` using the following commands ```bash python3 test.py # should not output CPP trace TORCH_SHOW_CPP_STACKTRACES=1 python3 test.py # should output CPP trace ``` Reviewed By: ngimel Differential Revision: D28799873 Pulled By: 1ntEgr8 fbshipit-source-id: 3e23353f48679ba8ce0364c049420ba4ff86ff09
Author
Parents
Loading