Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft of rocprofiler-sdk into XLA #68

Open
wants to merge 28 commits into
base: rocm-jaxlib-v0.4.30
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
419e2de
refactoring in terms of rocprofiler-sdk
cj401-amd Oct 22, 2024
94bd785
rewrite rocm_tracer in terms of rocprofiler-sdk
cj401-amd Oct 24, 2024
2bce23e
wrappring functions from rocprofiler-sdk
cj401-amd Oct 24, 2024
4d5f45b
adapt GpuTracer in terms of rocprofiler-sdk
cj401-amd Oct 24, 2024
38e6bd9
update roctracer_wrapper and rocm_tracer
cj401-amd Oct 25, 2024
2f8c53d
update with rocprofiler-sdk (v3)
cj401-amd Oct 26, 2024
581af23
update rocm_tracer with rocprofiler-sdk
cj401-amd Oct 29, 2024
45a7fa4
fix the llvm-18 dependencies
cj401-amd Oct 29, 2024
e8f0824
test GpuTracer entry point
cj401-amd Oct 29, 2024
9bdec68
update BUILD
cj401-amd Oct 29, 2024
8f0adcc
update roctraer
cj401-amd Oct 30, 2024
f633d55
replace roctracer with rocprofiler-sdk
cj401-amd Oct 30, 2024
7f4024f
update roc_tracer in terms of rocprofiler-sdk
cj401-amd Oct 30, 2024
13e21e5
update device tracer with rocprofiler-sdk
cj401-amd Oct 30, 2024
f2a24a0
update BUILD
cj401-amd Nov 4, 2024
7758462
udpate in terms of rocprofiler-sdk
cj401-amd Nov 4, 2024
c12c1b0
add rocprofiler_configure()
cj401-amd Nov 4, 2024
2e9c8b0
update in terms of rocprofiler-sdk
cj401-amd Nov 4, 2024
4b0d958
update in terms of rocprofiler-sdk
cj401-amd Nov 4, 2024
1c1dd64
update in terms of rocprofiler-sdk
cj401-amd Nov 4, 2024
b70a671
update in terms of rocprofiler-sdk
cj401-amd Nov 7, 2024
f39ddbb
update for XEvent
cj401-amd Nov 15, 2024
030d8e8
update for XEvent
cj401-amd Nov 16, 2024
91535fb
update for XSpace and XEvent
cj401-amd Nov 19, 2024
d4ca46c
update to fill XSpace
cj401-amd Nov 20, 2024
11dc129
update with rocprofiler_force_configure
cj401-amd Nov 22, 2024
bdc7778
update to test transferring events within callback
cj401-amd Nov 25, 2024
d7c71e8
update, events appended in rocmtracer first, then add via rocmtraceco…
cj401-amd Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions third_party/tsl/third_party/gpus/rocm_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ def _rocm_include_path(repository_ctx, rocm_config, bash_bin):
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/17/include")
inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/18/include")

if int(rocm_config.rocm_version_number) >= 60200:
inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/18/include")

# Support hcc based off clang 10.0.0 (for ROCm 3.3)
inc_dirs.append(rocm_toolkit_path + "/hcc/compiler/lib/clang/10.0.0/include/")
inc_dirs.append(rocm_toolkit_path + "/hcc/lib/clang/10.0.0/include")
Expand Down
3 changes: 2 additions & 1 deletion third_party/tsl/tsl/platform/default/dso_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ absl::StatusOr<void*> GetHipsolverDsoHandle() {
#endif

absl::StatusOr<void*> GetRoctracerDsoHandle() {
return GetDsoHandle("roctracer64", "");
// return GetDsoHandle("roctracer64", "");
return GetDsoHandle("rocprofiler-sdk", "");
}

absl::StatusOr<void*> GetHipsparseDsoHandle() {
Expand Down
4 changes: 3 additions & 1 deletion xla/backends/profiler/gpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tsl_gpu_library(
":cupti_collector",
":cupti_tracer",
":cupti_wrapper",
":rocm_collector",
# ":rocm_collector",
":rocm_tracer",
],
deps = [
Expand Down Expand Up @@ -213,6 +213,7 @@ tsl_gpu_library(
],
)


tsl_gpu_library(
name = "rocm_collector",
srcs = if_rocm(["rocm_collector.cc"]),
Expand Down Expand Up @@ -256,6 +257,7 @@ tsl_gpu_library(
visibility = ["//visibility:public"],
deps = [
":rocm_collector",
"//xla/backends/profiler/gpu/common:common_headers",
"//xla/stream_executor/rocm:roctracer_wrapper",
"@com_google_absl//absl/container:fixed_array",
"@com_google_absl//absl/container:flat_hash_map",
Expand Down
26 changes: 26 additions & 0 deletions xla/backends/profiler/gpu/common/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@local_config_rocm//rocm:build_defs.bzl", "if_rocm")
load(
"@tsl//tsl/platform:build_config.bzl",
"tf_additional_device_tracer_srcs",
)
load("@tsl//tsl/profiler/builds:build_config.bzl", "tf_profiler_copts")
load("//xla/tests:build_defs.bzl", "xla_test")
load(
"//xla/tsl:tsl.bzl",
"internal_visibility",
"tsl_copts",
"tsl_gpu_library",
)

cc_library(
name = "common_headers",
hdrs = [
"call_stack.hpp",
"defines.hpp",
"filesystem.hpp",
"name_info.hpp",
],
visibility = ["//visibility:public"],
deps = [
"//xla/stream_executor/rocm:roctracer_wrapper"]
)
97 changes: 97 additions & 0 deletions xla/backends/profiler/gpu/common/call_stack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "filesystem.hpp"

#include <cstdint>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

#include "tsl/platform/env.h"
#include "tsl/platform/errors.h"
#include "tsl/platform/logging.h"
#include "tsl/platform/macros.h"


namespace xla {
namespace common {
struct source_location
{
std::string function = {};
std::string file = {};
uint32_t line = 0;
std::string context = {};
};

using call_stack_t = std::vector<source_location>;

inline void
print_call_stack(std::string ofname,
const call_stack_t& _call_stack,
const char* env_variable = "ROCPROFILER_SAMPLE_OUTPUT_FILE")
{
if(auto* eofname = getenv(env_variable)) ofname = eofname;

std::ostream* ofs = nullptr;
auto cleanup = std::function<void(std::ostream*&)>{};

if(ofname == "stdout")
ofs = &std::cout;
else if(ofname == "stderr")
ofs = &std::cerr;
else
{
ofs = new std::ofstream{ofname};
if(ofs && *ofs)
cleanup = [](std::ostream*& _os) { delete _os; };
else
{
std::cerr << "Error outputting to " << ofname << ". Redirecting to stderr...\n";
ofname = "stderr";
ofs = &std::cerr;
}
}

// LOG(INFO) << "Outputting collected data to " << ofname;

size_t n = 0;
for(const auto& itr : _call_stack)
{
*ofs << std::left << std::setw(2) << ++n << "/" << std::setw(2) << _call_stack.size()
<< " [" << common::fs::path{itr.file}.filename() << ":" << itr.line << "] "
<< std::setw(20) << itr.function;
if(!itr.context.empty()) *ofs << " :: " << itr.context;
*ofs << "\n";
}

*ofs << std::flush;

if(cleanup) cleanup(ofs);
}
} // namespace common
} // namespace xla
83 changes: 83 additions & 0 deletions xla/backends/profiler/gpu/common/defines.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "xla/stream_executor/rocm/roctracer_wrapper.h"
namespace se = ::stream_executor;

#define ROCPROFILER_VAR_NAME_COMBINE(X, Y) X##Y
#define ROCPROFILER_VARIABLE(X, Y) ROCPROFILER_VAR_NAME_COMBINE(X, Y)

#define ROCPROFILER_WARN(result) \
{ \
rocprofiler_status_t ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__) = result; \
if(ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__) != ROCPROFILER_STATUS_SUCCESS) \
{ \
std::string status_msg = \
se::wrap::rocprofiler_get_status_string(ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__)); \
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] " << #result \
<< " returned error code " << ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__) \
<< ": " << status_msg << ". This is just a warning!" << std::endl; \
} \
}

#define ROCPROFILER_CHECK(result) \
{ \
rocprofiler_status_t ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__) = result; \
if(ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__) != ROCPROFILER_STATUS_SUCCESS) \
{ \
std::string status_msg = \
se::wrap::rocprofiler_get_status_string(ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__)); \
std::stringstream errmsg{}; \
errmsg << "[" << __FILE__ << ":" << __LINE__ << "] " << #result \
<< " failed with error code " << ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__) \
<< " :: " << status_msg; \
throw std::runtime_error(errmsg.str()); \
} \
}

#define ROCPROFILER_CALL(result, msg) \
{ \
rocprofiler_status_t ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__) = result; \
if(ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__) != ROCPROFILER_STATUS_SUCCESS) \
{ \
std::string status_msg = \
se::wrap::rocprofiler_get_status_string(ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__)); \
std::cerr << "[" #result "][" << __FILE__ << ":" << __LINE__ << "] " << msg \
<< " failed with error code " << ROCPROFILER_VARIABLE(CHECKSTATUS, __LINE__) \
<< ": " << status_msg << std::endl; \
std::stringstream errmsg{}; \
errmsg << "[" #result "][" << __FILE__ << ":" << __LINE__ << "] " << msg " failure (" \
<< status_msg << ")"; \
} \
}

#if HIP_VERSION >= 60300000
# define HIP_HOST_ALLOC_FUNC hipExtHostAlloc
# define HIP_HOST_FREE_FUNC hipFreeHost
#else
# define HIP_HOST_ALLOC_FUNC hipHostMalloc
# define HIP_HOST_FREE_FUNC hipHostFree
#endif

// throw std::runtime_error(errmsg.str());
78 changes: 78 additions & 0 deletions xla/backends/profiler/gpu/common/filesystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#if !defined(ROCPROFILER_SAMPLES_HAS_GHC_LIB_FILESYSTEM)
# if defined __has_include
# if __has_include(<ghc/filesystem.hpp>)
# define ROCPROFILER_SAMPLES_HAS_GHC_LIB_FILESYSTEM 1
# else
# define ROCPROFILER_SAMPLES_HAS_GHC_LIB_FILESYSTEM 0
# endif
# else
# define ROCPROFILER_SAMPLES_HAS_GHC_LIB_FILESYSTEM 0
# endif
#endif

#if ROCPROFILER_SAMPLES_HAS_GHC_LIB_FILESYSTEM == 0
# if defined __has_include
# if __has_include(<version>)
# include <version>
# endif
# endif

# if defined(__cpp_lib_filesystem)
# define ROCPROFILER_SAMPLES_HAS_CPP_LIB_FILESYSTEM 1
# else
# if defined __has_include
# if __has_include(<filesystem>)
# define ROCPROFILER_SAMPLES_HAS_CPP_LIB_FILESYSTEM 1
# endif
# endif
# endif
#endif

// include the correct filesystem header
#if defined(ROCPROFILER_SAMPLES_HAS_GHC_LIB_FILESYSTEM) && \
ROCPROFILER_SAMPLES_HAS_GHC_LIB_FILESYSTEM > 0
# include <ghc/filesystem.hpp>
#elif defined(ROCPROFILER_SAMPLES_HAS_CPP_LIB_FILESYSTEM) && \
ROCPROFILER_SAMPLES_HAS_CPP_LIB_FILESYSTEM > 0
# include <filesystem>
#else
# include <experimental/filesystem>
#endif

namespace xla {
namespace common {
#if defined(ROCPROFILER_SAMPLES_HAS_GHC_LIB_FILESYSTEM) && \
ROCPROFILER_SAMPLES_HAS_GHC_LIB_FILESYSTEM > 0
namespace fs = ::ghc::filesystem; // NOLINT(misc-unused-alias-decls)
#elif defined(ROCPROFILER_SAMPLES_HAS_CPP_LIB_FILESYSTEM) && \
ROCPROFILER_SAMPLES_HAS_CPP_LIB_FILESYSTEM > 0
namespace fs = ::std::filesystem; // NOLINT(misc-unused-alias-decls)
#else
namespace fs = ::std::experimental::filesystem; // NOLINT(misc-unused-alias-decls)
#endif
} // namespace common
} // namespace xla
54 changes: 54 additions & 0 deletions xla/backends/profiler/gpu/common/name_info.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "xla/stream_executor/rocm/roctracer_wrapper.h"

#include "defines.hpp"

#include <iostream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>

namespace xla {
namespace common {
using callback_name_info = rocprofiler::sdk::callback_name_info;
using buffer_name_info = rocprofiler::sdk::buffer_name_info;

inline auto
get_buffer_tracing_names()
{
return rocprofiler::sdk::get_buffer_tracing_names();
}

inline auto
get_callback_id_names()
{
return rocprofiler::sdk::get_callback_tracing_names();
}
} // namespace common
} // namespace xla
Loading
Loading