Skip to content

Commit 3c3fa42

Browse files
briancoutinhofacebook-github-bot
authored andcommitted
Enable init for daemon cases, remove event profiler code (#1035)
Summary: Pull Request resolved: #1035 Cleans up the initialization for kineto * The current method to init kineto for CUDA builds is to add a callback on CUDA context. But this leads to CUPTI being enabled right from the start. * For the case where profiling daemon is enabled (dynolog), this PR initialized the profiler and config loader always, for both CPU and CUDA builds. This should be safe to do as kineto_init now happens when torch is imported. * Still leaving the CUPTI callback approach above for non dynolog/daemon use cases, this leaves behavior inside Meta unchanged. * Let's start cleaning up Even profiler. From here on is offiically turned off. Will start nuking the files soon. Reviewed By: sanrise Differential Revision: D69285243 fbshipit-source-id: bd04c4837da479599db978178849e121f2a90af1
1 parent 9678759 commit 3c3fa42

File tree

1 file changed

+56
-93
lines changed

1 file changed

+56
-93
lines changed

libkineto/src/init.cpp

Lines changed: 56 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ namespace KINETO_NAMESPACE {
3636
#if __linux__ || defined(HAS_CUPTI)
3737
static bool initialized = false;
3838

39-
static void initProfilersCPU() {
39+
static void initProfilers() {
4040
if (!initialized) {
4141
libkineto::api().initProfilerIfRegistered();
42+
libkineto::api().configLoader().initBaseConfig();
4243
initialized = true;
4344
VLOG(0) << "libkineto profilers activated";
4445
}
@@ -47,11 +48,6 @@ static void initProfilersCPU() {
4748
#endif // __linux__ || defined(HAS_CUPTI)
4849

4950
#ifdef HAS_CUPTI
50-
static std::mutex& initEventMutex() {
51-
static std::mutex initMutex_;
52-
return initMutex_;
53-
}
54-
5551
bool enableEventProfiler() {
5652
if (getenv("KINETO_ENABLE_EVENT_PROFILER") != nullptr) {
5753
return true;
@@ -60,28 +56,15 @@ bool enableEventProfiler() {
6056
}
6157
}
6258

63-
static void initProfilers(
59+
static void initProfilersCallback(
6460
CUpti_CallbackDomain /*domain*/,
6561
CUpti_CallbackId /*cbid*/,
66-
const CUpti_CallbackData* cbInfo) {
62+
const CUpti_CallbackData* /*cbInfo*/) {
6763
VLOG(0) << "CUDA Context created";
68-
initProfilersCPU();
64+
initProfilers();
6965

70-
if (!enableEventProfiler()) {
71-
VLOG(0) << "Kineto EventProfiler disabled, skipping start";
72-
return;
73-
} else {
74-
std::lock_guard<std::mutex> lock(initEventMutex());
75-
CUpti_ResourceData* d = (CUpti_ResourceData*)cbInfo;
76-
CUcontext ctx = d->context;
77-
ConfigLoader& config_loader = libkineto::api().configLoader();
78-
config_loader.initBaseConfig();
79-
auto config = config_loader.getConfigCopy();
80-
if (config->eventProfilerEnabled()) {
81-
// This function needs to be called under lock.
82-
EventProfilerController::start(ctx, config_loader);
83-
LOG(INFO) << "Kineto EventProfiler started";
84-
}
66+
if (enableEventProfiler()) {
67+
LOG(WARNING) << "Event Profiler is no longer supported in kineto";
8568
}
8669
}
8770

@@ -98,17 +81,39 @@ static bool shouldPreloadCuptiInstrumentation() {
9881
#endif
9982
}
10083

101-
static void stopProfiler(
102-
CUpti_CallbackDomain /*domain*/,
103-
CUpti_CallbackId /*cbid*/,
104-
const CUpti_CallbackData* cbInfo) {
105-
VLOG(0) << "CUDA Context destroyed";
106-
std::lock_guard<std::mutex> lock(initEventMutex());
107-
CUpti_ResourceData* d = (CUpti_ResourceData*)cbInfo;
108-
CUcontext ctx = d->context;
109-
// This function needs to be called under lock.
110-
EventProfilerController::stopIfEnabled(ctx);
111-
LOG(INFO) << "Kineto EventProfiler stopped";
84+
bool setupCuptiInitCallback(bool logOnError) {
85+
// libcupti will be lazily loaded on this call.
86+
// If it is not available (e.g. CUDA is not installed),
87+
// then this call will return an error and we just abort init.
88+
auto cbapi = CuptiCallbackApi::singleton();
89+
cbapi->initCallbackApi();
90+
91+
bool status = false;
92+
93+
if (cbapi->initSuccess()) {
94+
const CUpti_CallbackDomain domain = CUPTI_CB_DOMAIN_RESOURCE;
95+
status = cbapi->registerCallback(
96+
domain,
97+
CuptiCallbackApi::RESOURCE_CONTEXT_CREATED,
98+
initProfilersCallback);
99+
if (status) {
100+
status = cbapi->enableCallback(
101+
domain, CuptiCallbackApi::RESOURCE_CONTEXT_CREATED);
102+
}
103+
}
104+
105+
if (!cbapi->initSuccess() || !status) {
106+
if (logOnError) {
107+
CUPTI_CALL(cbapi->getCuptiStatus());
108+
LOG(WARNING) << "CUPTI initialization failed - "
109+
<< "CUDA profiler activities will be missing";
110+
LOG(INFO)
111+
<< "If you see CUPTI_ERROR_INSUFFICIENT_PRIVILEGES, refer to "
112+
<< "https://developer.nvidia.com/nvidia-development-tools-solutions-err-nvgpuctrperm-cupti";
113+
}
114+
}
115+
116+
return status;
112117
}
113118

114119
static std::unique_ptr<CuptiRangeProfilerInit> rangeProfilerInit;
@@ -120,7 +125,6 @@ static std::unique_ptr<CuptiRangeProfilerInit> rangeProfilerInit;
120125
using namespace KINETO_NAMESPACE;
121126
extern "C" {
122127

123-
// Return true if no CUPTI errors occurred during init
124128
void libkineto_init(bool cpuOnly, bool logOnError) {
125129
// Start with initializing the log level
126130
const char* logLevelEnv = getenv("KINETO_LOG_LEVEL");
@@ -139,60 +143,22 @@ void libkineto_init(bool cpuOnly, bool logOnError) {
139143
#endif
140144

141145
#ifdef HAS_CUPTI
142-
if (!cpuOnly) {
143-
// libcupti will be lazily loaded on this call.
144-
// If it is not available (e.g. CUDA is not installed),
145-
// then this call will return an error and we just abort init.
146-
auto cbapi = CuptiCallbackApi::singleton();
147-
cbapi->initCallbackApi();
148-
bool status = false;
149-
bool initRangeProfiler = true;
150-
151-
if (cbapi->initSuccess()) {
152-
const CUpti_CallbackDomain domain = CUPTI_CB_DOMAIN_RESOURCE;
153-
status = cbapi->registerCallback(
154-
domain, CuptiCallbackApi::RESOURCE_CONTEXT_CREATED, initProfilers);
155-
if (status) {
156-
status = cbapi->enableCallback(
157-
domain, CuptiCallbackApi::RESOURCE_CONTEXT_CREATED);
158-
}
159-
160-
// Register stopProfiler callback only for event profiler.
161-
// This callback is not required for activities tracing.
162-
if (enableEventProfiler()) {
163-
if (status) {
164-
status = cbapi->registerCallback(
165-
domain,
166-
CuptiCallbackApi::RESOURCE_CONTEXT_DESTROYED,
167-
stopProfiler);
168-
}
169-
if (status) {
170-
status = cbapi->enableCallback(
171-
domain, CuptiCallbackApi::RESOURCE_CONTEXT_DESTROYED);
172-
}
173-
}
174-
}
146+
bool initRangeProfiler = true;
175147

176-
if (!cbapi->initSuccess() || !status) {
177-
initRangeProfiler = false;
178-
cpuOnly = true;
179-
if (logOnError) {
180-
CUPTI_CALL(cbapi->getCuptiStatus());
181-
LOG(WARNING) << "CUPTI initialization failed - "
182-
<< "CUDA profiler activities will be missing";
183-
LOG(INFO)
184-
<< "If you see CUPTI_ERROR_INSUFFICIENT_PRIVILEGES, refer to "
185-
<< "https://developer.nvidia.com/nvidia-development-tools-solutions-err-nvgpuctrperm-cupti";
186-
}
187-
}
148+
if (!cpuOnly && !libkineto::isDaemonEnvVarSet()) {
149+
bool success = setupCuptiInitCallback(logOnError);
150+
cpuOnly = !success;
151+
initRangeProfiler = success;
152+
}
188153

189-
// initialize CUPTI Range Profiler API
190-
if (initRangeProfiler) {
191-
rangeProfilerInit = std::make_unique<CuptiRangeProfilerInit>();
192-
}
154+
// Initialize CUPTI Range Profiler API
155+
// Note: the following is a no-op if Range Profiler is not supported
156+
// currently it is only enabled in fbcode.
157+
if (!cpuOnly && initRangeProfiler) {
158+
rangeProfilerInit = std::make_unique<CuptiRangeProfilerInit>();
193159
}
194160

195-
if (shouldPreloadCuptiInstrumentation()) {
161+
if (!cpuOnly && shouldPreloadCuptiInstrumentation()) {
196162
CuptiActivityApi::forceLoadCupti();
197163
}
198164
#endif // HAS_CUPTI
@@ -224,13 +190,10 @@ void libkineto_init(bool cpuOnly, bool logOnError) {
224190
#endif // HAS_XPUPTI
225191

226192
#if __linux__
227-
// When CUDA/GPU is used the profiler initialization happens on the
228-
// creation of the first CUDA stream (see initProfilers()).
229-
// This section bootstraps the profiler and its connection to a profiling
230-
// daemon in the CPU only case.
231-
if (cpuOnly && getenv(kUseDaemonEnvVar) != nullptr) {
232-
initProfilersCPU();
233-
libkineto::api().configLoader().initBaseConfig();
193+
// For open source users that would like to connect to a profiling daemon
194+
// we should always initialize the profiler at this point.
195+
if (libkineto::isDaemonEnvVarSet()) {
196+
initProfilers();
234197
}
235198
#endif
236199
}

0 commit comments

Comments
 (0)