Skip to content

Commit f7e621c

Browse files
naromero77amdpytorchmergebot
authored andcommitted
[ROCm] TunableOp do not log during exit (#142818)
Depending on the order of static object destruction, the TunableOp logger is not available. Pull Request resolved: #142818 Approved by: https://github.com/jeffdaily
1 parent 233853a commit f7e621c

File tree

2 files changed

+64
-37
lines changed

2 files changed

+64
-37
lines changed

aten/src/ATen/cuda/tunable/Tunable.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,13 @@ TuningContext::TuningContext() :
391391
rotating_buffer_size_{-1},
392392
filename_{},
393393
untuned_file_{},
394-
results_count_from_input_file_{0}
394+
results_count_from_input_file_{0},
395+
is_shutting_down_{false}
395396
{
396397
}
397398

398399
TuningContext::~TuningContext() {
400+
is_shutting_down_ = true;
399401
if (!manager_initialized_) {
400402
// TuningResultsManager was never initialized, no tuning requested or performed.
401403
// This can happen in a DDP job where a python process spawns other workers
@@ -740,4 +742,48 @@ bool TuningContext::WriteFile(const std::string& filename_) {
740742
return true;
741743
}
742744

745+
namespace {
746+
747+
struct MaybeDelete {
748+
bool owns_pointer;
749+
void operator()(std::ostream* os) const { if (owns_pointer) delete os; }
750+
};
751+
752+
using OstreamPtr = std::unique_ptr<std::ostream, MaybeDelete>;
753+
754+
inline OstreamPtr get_stream(const std::string& filename) {
755+
if (filename == "out") {
756+
return OstreamPtr { &std::cout, MaybeDelete {false} };
757+
}
758+
else if (filename == "err") {
759+
return OstreamPtr { &std::cerr, MaybeDelete {false} };
760+
}
761+
else {
762+
return OstreamPtr { new std::ofstream {filename.c_str()}, MaybeDelete {true} };
763+
}
764+
}
765+
766+
} // anonymous namespace
767+
768+
std::string TuningContext::GetLogFilename() const {
769+
static const auto env_file = c10::utils::get_env("PYTORCH_TUNABLEOP_VERBOSE_FILENAME");
770+
static std::string val_file = env_file.has_value() ? env_file.value() : "err";
771+
return val_file;
772+
}
773+
774+
int TuningContext::GetLogLevel() const {
775+
static const auto env_verbose = c10::utils::get_env("PYTORCH_TUNABLEOP_VERBOSE");
776+
static int val_verbose = env_verbose.has_value() ? stoi(env_verbose.value()) : 0;
777+
return val_verbose;
778+
}
779+
780+
bool TuningContext::GetLogOkay() const {
781+
return !is_shutting_down_;
782+
}
783+
784+
std::ostream& TuningContext::GetLog() const {
785+
static auto streamptr = get_stream(GetLogFilename());
786+
return *streamptr;
787+
}
788+
743789
} // namespace at::cuda::tunable

aten/src/ATen/cuda/tunable/Tunable.h

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <c10/util/CallOnce.h>
1313
#include <c10/util/StringUtil.h>
14+
#include <c10/util/env.h>
1415

1516
#include <fstream>
1617
#include <functional>
@@ -22,46 +23,13 @@
2223
#include <unordered_set>
2324
#include <utility>
2425

25-
namespace at::cuda::tunable {
26-
27-
namespace detail {
28-
29-
struct MaybeDelete {
30-
bool owns_pointer;
31-
void operator()(std::ostream* os) const { if (owns_pointer) delete os; }
32-
};
33-
34-
using OstreamPtr = std::unique_ptr<std::ostream, MaybeDelete>;
35-
36-
inline OstreamPtr get_stream(const std::string& filename) {
37-
if (filename == "out") {
38-
return OstreamPtr { &std::cout, MaybeDelete {false} };
39-
}
40-
else if (filename == "err") {
41-
return OstreamPtr { &std::cerr, MaybeDelete {false} };
42-
}
43-
else {
44-
return OstreamPtr { new std::ofstream {filename.c_str()}, MaybeDelete {true} };
45-
}
46-
}
47-
48-
}
49-
50-
template<class... Types>
51-
static void TunableLog(int level, Types... args) {
52-
static const char *env_file = getenv("PYTORCH_TUNABLEOP_VERBOSE_FILENAME");
53-
static const char *env_verbose = getenv("PYTORCH_TUNABLEOP_VERBOSE");
54-
static int level_user = env_verbose ? atoi(env_verbose) : 0;
55-
static auto streamptr = detail::get_stream(env_file ? env_file : "err");
56-
if (level_user >= level) {
57-
(*streamptr) << c10::str(args...) << std::endl;
58-
}
59-
}
60-
#define TUNABLE_LOGV(LEVEL, ...) TunableLog(LEVEL, __VA_ARGS__)
26+
#define TUNABLE_LOGV(LEVEL, ...) getTuningContext()->Log(LEVEL, __VA_ARGS__)
6127
#define TUNABLE_LOG1(...) TUNABLE_LOGV(1, __VA_ARGS__)
6228
#define TUNABLE_LOG2(...) TUNABLE_LOGV(2, __VA_ARGS__)
6329
#define TUNABLE_LOG3(...) TUNABLE_LOGV(3, __VA_ARGS__)
6430

31+
namespace at::cuda::tunable {
32+
6533
enum TORCH_CUDA_CPP_API TuningStatus {
6634
OK = 0,
6735
FAIL = 1,
@@ -219,7 +187,19 @@ class TORCH_CUDA_CPP_API TuningContext {
219187
bool ReadFile(const std::string& filename={});
220188
bool WriteFile(const std::string& filename={});
221189

190+
template<class... Types>
191+
void Log(int level, Types... args) {
192+
if (GetLogOkay() && GetLogLevel() >= level) {
193+
GetLog() << c10::str(args...) << std::endl;
194+
}
195+
}
196+
222197
private:
198+
std::string GetLogFilename() const;
199+
int GetLogLevel() const;
200+
bool GetLogOkay() const;
201+
std::ostream& GetLog() const;
202+
223203
bool enable_;
224204
bool tuning_enable_;
225205
bool record_untuned_enable_;
@@ -238,6 +218,7 @@ class TORCH_CUDA_CPP_API TuningContext {
238218
std::string filename_;
239219
std::ofstream untuned_file_;
240220
size_t results_count_from_input_file_;
221+
bool is_shutting_down_;
241222
};
242223

243224
TORCH_CUDA_CPP_API TuningContext* getTuningContext();

0 commit comments

Comments
 (0)