Skip to content

Commit f47cc3e

Browse files
authored
[PROTON] Reworked the mechanism for finding libraries for profiling backends. (#5751)
We should be able to use the default CUPTI path when Triton is installed, whether in development mode or within site packages. Additionally, we resolved incorrect architecture-to-package name mappings in setup.py.
1 parent 924468e commit f47cc3e

File tree

13 files changed

+69
-49
lines changed

13 files changed

+69
-49
lines changed

python/setup.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ def download_and_copy(name, src_func, dst_path, variable, version, url_func):
315315
base_dir = os.path.dirname(__file__)
316316
system = platform.system()
317317
arch = platform.machine()
318-
arch = {"arm64": "aarch64"}.get(arch, arch)
318+
# NOTE: This might be wrong for jetson if both grace chips and jetson chips return aarch64
319+
arch = {"arm64": "sbsa", "aarch64": "sbsa"}.get(arch, arch)
319320
supported = {"Linux": "linux", "Darwin": "linux"}
320321
url = url_func(supported[system], arch, version)
321322
src_path = src_func(supported[system], arch, version)
@@ -407,11 +408,7 @@ def get_proton_cmake_args(self):
407408
if cupti_include_dir == "":
408409
cupti_include_dir = os.path.join(get_base_dir(), "third_party", "nvidia", "backend", "include")
409410
cmake_args += ["-DCUPTI_INCLUDE_DIR=" + cupti_include_dir]
410-
cupti_lib_dir = get_env_with_keys(["TRITON_CUPTI_LIB_PATH"])
411-
if cupti_lib_dir == "":
412-
cupti_lib_dir = os.path.join(get_base_dir(), "third_party", "nvidia", "backend", "lib", "cupti")
413-
cmake_args += ["-DCUPTI_LIB_DIR=" + cupti_lib_dir]
414-
roctracer_include_dir = get_env_with_keys(["ROCTRACER_INCLUDE_PATH"])
411+
roctracer_include_dir = get_env_with_keys(["TRITON_ROCTRACER_INCLUDE_PATH"])
415412
if roctracer_include_dir == "":
416413
roctracer_include_dir = os.path.join(get_base_dir(), "third_party", "amd", "backend", "include")
417414
cmake_args += ["-DROCTRACER_INCLUDE_DIR=" + roctracer_include_dir]

third_party/proton/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ if(APPLE)
2929
set(PROTON_PYTHON_LDFLAGS "-undefined dynamic_lookup")
3030
endif()
3131

32-
if(DEFINED CUPTI_LIB_DIR)
33-
message(STATUS "CUPTI lib directory: ${CUPTI_LIB_DIR}")
34-
add_compile_definitions(CUPTI_LIB_DIR=${CUPTI_LIB_DIR})
35-
endif()
36-
3732
include_directories(${CUPTI_INCLUDE_DIR})
3833
include_directories(SYSTEM ${ROCTRACER_INCLUDE_DIR})
3934
target_compile_definitions(proton PRIVATE __HIP_PLATFORM_AMD__)

third_party/proton/csrc/Proton.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ void initProton(pybind11::module &&m) {
1515

1616
m.def("start",
1717
[](const std::string &path, const std::string &contextSourceName,
18-
const std::string &dataName, const std::string &profilerName) {
18+
const std::string &dataName, const std::string &profilerName,
19+
const std::string &profilerPath) {
1920
auto sessionId = SessionManager::instance().addSession(
20-
path, profilerName, contextSourceName, dataName);
21+
path, profilerName, profilerPath, contextSourceName, dataName);
2122
SessionManager::instance().activateSession(sessionId);
2223
return sessionId;
2324
});

third_party/proton/csrc/include/Driver/Dispatch.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ namespace proton {
4444

4545
struct ExternLibBase {
4646
using RetType = int; // Generic type, can be overridden in derived structs
47-
static constexpr const char *name = ""; // Placeholder
48-
static constexpr const char *defaultDir = ""; // Placeholder
49-
static constexpr RetType success = 0; // Placeholder
47+
static constexpr const char *name = ""; // Placeholder
48+
static constexpr RetType success = 0; // Placeholder
5049
ExternLibBase() = delete;
5150
ExternLibBase(const ExternLibBase &) = delete;
5251
ExternLibBase &operator=(const ExternLibBase &) = delete;
5352
static inline void *lib{nullptr};
53+
static inline std::string defaultDir{""};
5454
};
5555

5656
template <typename ExternLib> class Dispatch {
@@ -59,25 +59,24 @@ template <typename ExternLib> class Dispatch {
5959

6060
static void init(const char *name, void **lib) {
6161
if (*lib == nullptr) {
62-
// First reuse the existing handle
63-
*lib = dlopen(name, RTLD_NOLOAD);
64-
}
65-
if (*lib == nullptr) {
66-
// If not found, try to load it from LD_LIBRARY_PATH
67-
*lib = dlopen(name, RTLD_LOCAL | RTLD_LAZY);
68-
}
69-
if (*lib == nullptr) {
70-
// If still not found, try to load it from the default path
62+
// If not found, try to load it from the default path
7163
auto dir = std::string(ExternLib::defaultDir);
7264
if (dir.length() > 0) {
7365
auto fullPath = dir + "/" + name;
7466
*lib = dlopen(fullPath.c_str(), RTLD_LOCAL | RTLD_LAZY);
67+
} else {
68+
// Only if the default path is not set, we try to load it from the
69+
// system.
70+
// First reuse the existing handle
71+
*lib = dlopen(name, RTLD_NOLOAD);
72+
if (*lib == nullptr) {
73+
// If not found, try to load it from LD_LIBRARY_PATH
74+
*lib = dlopen(name, RTLD_LOCAL | RTLD_LAZY);
75+
}
7576
}
7677
}
7778
if (*lib == nullptr) {
78-
throw std::runtime_error("Could not find `" + std::string(name) +
79-
"`. Make sure it is in your "
80-
"LD_LIBRARY_PATH.");
79+
throw std::runtime_error("Could not load `" + std::string(name) + "`");
8180
}
8281
}
8382

third_party/proton/csrc/include/Driver/GPU/CuptiApi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "cupti.h"
55
#include "cupti_pcsampling.h"
6+
#include <string>
67

78
namespace proton {
89

@@ -106,6 +107,8 @@ CUptiResult pcSamplingStart(CUpti_PCSamplingStartParams *pParams);
106107
template <bool CheckSuccess>
107108
CUptiResult pcSamplingStop(CUpti_PCSamplingStopParams *pParams);
108109

110+
void setLibPath(const std::string &path);
111+
109112
} // namespace cupti
110113

111114
} // namespace proton

third_party/proton/csrc/include/Profiler/GPUProfiler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class GPUProfiler : public Profiler,
4141
}
4242
bool isPCSamplingEnabled() const { return pcSamplingEnabled; }
4343

44+
ConcreteProfilerT &setLibPath(const std::string &libPath) {
45+
pImpl->setLibPath(libPath);
46+
return dynamic_cast<ConcreteProfilerT &>(*this);
47+
}
48+
4449
protected:
4550
// OpInterface
4651
void startOp(const Scope &scope) override {
@@ -136,6 +141,7 @@ class GPUProfiler : public Profiler,
136141
: profiler(profiler) {}
137142
virtual ~GPUProfilerPimplInterface() = default;
138143

144+
virtual void setLibPath(const std::string &libPath) = 0;
139145
virtual void doStart() = 0;
140146
virtual void doFlush() = 0;
141147
virtual void doStop() = 0;

third_party/proton/csrc/include/Session/Session.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class SessionManager : public Singleton<SessionManager> {
7272
~SessionManager() = default;
7373

7474
size_t addSession(const std::string &path, const std::string &profilerName,
75+
const std::string &profilerPath,
7576
const std::string &contextSourceName,
7677
const std::string &dataName);
7778

@@ -103,6 +104,7 @@ class SessionManager : public Singleton<SessionManager> {
103104
private:
104105
std::unique_ptr<Session> makeSession(size_t id, const std::string &path,
105106
const std::string &profilerName,
107+
const std::string &profilerPath,
106108
const std::string &contextSourceName,
107109
const std::string &dataName);
108110

third_party/proton/csrc/lib/Driver/GPU/CuptiApi.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@ namespace proton {
66

77
namespace cupti {
88

9-
#define STRINGIFY(x) #x
10-
#define TOSTRING(x) STRINGIFY(x)
119
struct ExternLibCupti : public ExternLibBase {
1210
using RetType = CUptiResult;
1311
static constexpr const char *name = "libcupti.so";
14-
#ifdef CUPTI_LIB_DIR
15-
static constexpr const char *defaultDir = TOSTRING(CUPTI_LIB_DIR);
16-
#else
17-
static constexpr const char *defaultDir = "";
18-
#endif
12+
static inline std::string defaultDir = "";
1913
static constexpr RetType success = CUPTI_SUCCESS;
2014
static void *lib;
2115
};
@@ -116,6 +110,8 @@ DEFINE_DISPATCH(ExternLibCupti, pcSamplingStart, cuptiPCSamplingStart,
116110
DEFINE_DISPATCH(ExternLibCupti, pcSamplingStop, cuptiPCSamplingStop,
117111
CUpti_PCSamplingStopParams *);
118112

113+
void setLibPath(const std::string &path) { ExternLibCupti::defaultDir = path; }
114+
119115
} // namespace cupti
120116

121117
} // namespace proton

third_party/proton/csrc/lib/Profiler/Cupti/CuptiProfiler.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ struct CuptiProfiler::CuptiProfilerPimpl
198198
: GPUProfiler<CuptiProfiler>::GPUProfilerPimplInterface(profiler) {}
199199
virtual ~CuptiProfilerPimpl() = default;
200200

201+
void setLibPath(const std::string &libPath) override {
202+
cupti::setLibPath(libPath);
203+
}
201204
void doStart() override;
202205
void doFlush() override;
203206
void doStop() override;

third_party/proton/csrc/lib/Profiler/RocTracer/RoctracerProfiler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ struct RoctracerProfiler::RoctracerProfilerPimpl
189189
: GPUProfiler<RoctracerProfiler>::GPUProfilerPimplInterface(profiler) {}
190190
virtual ~RoctracerProfilerPimpl() = default;
191191

192+
void setLibPath(const std::string &libPath) override {}
192193
void doStart() override;
193194
void doFlush() override;
194195
void doStop() override;

0 commit comments

Comments
 (0)