Skip to content

Commit 7ca621c

Browse files
shawnwang18Google-ML-Automation
authored andcommitted
PR #33505: [XLA:GPU] Dump command buffer contents to folder specified by --xla-dump-to through dump.h
Imported from GitHub PR #33505 📝 Summary of Changes This PR migrates the command buffer content's dump through dump.h Copybara import of the project: -- 0b870f2 by Shawn Wang <[email protected]>: Dump command buffer contents to folder specified by --xla-dump-to through dump.h -- 123c254 by Shawn Wang <[email protected]>: clang format fix Merging this change closes #33505 COPYBARA_INTEGRATE_REVIEW=#33505 from shawnwang18:shawnw/stringfy_cmd_buffer 123c254 PiperOrigin-RevId: 828357446
1 parent 7d6b101 commit 7ca621c

File tree

8 files changed

+41
-8
lines changed

8 files changed

+41
-8
lines changed

xla/stream_executor/command_buffer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ class CommandBuffer {
311311
// Returns command buffer state.
312312
virtual State state() const = 0;
313313

314+
virtual std::string ToString() const = 0;
315+
314316
//--------------------------------------------------------------------------//
315317
// Command buffer tracing API
316318
//--------------------------------------------------------------------------//

xla/stream_executor/cuda/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,7 @@ cc_library(
15591559
"@com_google_absl//absl/types:span",
15601560
"@local_config_cuda//cuda:cuda_headers",
15611561
"@tsl//tsl/platform:casts",
1562+
"@tsl//tsl/platform:path",
15621563
],
15631564
)
15641565

xla/stream_executor/cuda/cuda_command_buffer.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ limitations under the License.
5353
#include "xla/tsl/platform/errors.h"
5454
#include "xla/tsl/platform/statusor.h"
5555
#include "tsl/platform/casts.h"
56+
#include "tsl/platform/path.h"
5657

5758
namespace stream_executor::gpu {
5859
namespace {
@@ -831,4 +832,25 @@ absl::Status CudaCommandBuffer::CheckCanBeUpdated() {
831832
return absl::OkStatus();
832833
}
833834

835+
std::string CudaCommandBuffer::ToString() const {
836+
std::string path = tsl::io::GetTempFilename(/*extension=*/"dot");
837+
#if CUDA_VERSION >= 12000
838+
int flags = CU_GRAPH_DEBUG_DOT_FLAGS_VERBOSE;
839+
auto dot_print_status =
840+
cuda::ToStatus(cuGraphDebugDotPrint(graph_, path.c_str(), flags),
841+
"Failed to print gpu graph debug file");
842+
if (!dot_print_status.ok()) {
843+
return std::string(dot_print_status.message());
844+
}
845+
std::string dot_file_contents;
846+
auto read_status =
847+
tsl::ReadFileToString(tsl::Env::Default(), path, &dot_file_contents);
848+
if (!read_status.ok()) {
849+
return std::string(read_status.message());
850+
}
851+
return dot_file_contents;
852+
#endif // CUDA_VERSION >= 12000
853+
return "CUDA graph debug dot print is not supported.";
854+
}
855+
834856
} // namespace stream_executor::gpu

xla/stream_executor/cuda/cuda_command_buffer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class CudaCommandBuffer final : public GpuCommandBuffer {
5353
static absl::StatusOr<std::unique_ptr<CudaCommandBuffer>> Create(
5454
Mode mode, StreamExecutor* executor, CudaContext* cuda_context);
5555

56+
std::string ToString() const override;
57+
5658
~CudaCommandBuffer() override;
5759

5860
private:

xla/stream_executor/gpu/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ cc_library(
137137
"gpu_command_buffer.h",
138138
],
139139
deps = [
140+
"//xla:debug_options_flags",
141+
"//xla/service:dump",
140142
"//xla/stream_executor:bit_pattern",
141143
"//xla/stream_executor:command_buffer",
142144
"//xla/stream_executor:device_memory",

xla/stream_executor/gpu/gpu_command_buffer.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ limitations under the License.
3333
#include "absl/strings/str_format.h"
3434
#include "absl/strings/string_view.h"
3535
#include "absl/types/span.h"
36+
#include "xla/debug_options_flags.h"
37+
#include "xla/service/dump.h"
3638
#include "xla/stream_executor/bit_pattern.h"
3739
#include "xla/stream_executor/command_buffer.h"
3840
#include "xla/stream_executor/device_memory.h"
@@ -571,14 +573,10 @@ absl::Status GpuCommandBuffer::Finalize() {
571573
// Maybe dump created GPU graph to a dot file for debugging.
572574
if (state_ == State::kCreate &&
573575
(VLOG_IS_ON(10) || (VLOG_IS_ON(9) && mode_ == Mode::kPrimary))) {
574-
std::string path = tsl::io::GetTempFilename(/*extension=*/"dot");
575-
TF_RETURN_IF_ERROR(WriteGraphToDotFile(path));
576-
if (VLOG_IS_ON(100) || (VLOG_IS_ON(90) && mode_ == Mode::kPrimary)) {
577-
std::string dot_file_contents;
578-
TF_RETURN_IF_ERROR(
579-
tsl::ReadFileToString(tsl::Env::Default(), path, &dot_file_contents));
580-
VLOG(90) << "Contents of " << path << " is:\n" << dot_file_contents;
581-
}
576+
xla::DebugOptions debug_options = xla::GetDebugOptionsFromFlags();
577+
std::string contents = ToString();
578+
std::string filename = absl::StrFormat("gpu_command_buffer_%p.dot", this);
579+
xla::DumpToFileInDir(debug_options, filename, contents);
582580
}
583581

584582
size_t num_commands = commands_.size();

xla/stream_executor/rocm/rocm_command_buffer.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,4 +463,8 @@ absl::Status RocmCommandBuffer::CheckCanBeUpdated() {
463463
return absl::OkStatus();
464464
}
465465

466+
std::string RocmCommandBuffer::ToString() const {
467+
return "ROCM graph debug dot print is not supported.";
468+
}
469+
466470
} // namespace stream_executor::gpu

xla/stream_executor/rocm/rocm_command_buffer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class RocmCommandBuffer : public GpuCommandBuffer {
4646
static absl::StatusOr<std::unique_ptr<RocmCommandBuffer>> Create(
4747
Mode mode, StreamExecutor* executor);
4848

49+
std::string ToString() const override;
50+
4951
~RocmCommandBuffer() override;
5052

5153
private:

0 commit comments

Comments
 (0)