Skip to content

Commit c2cade2

Browse files
pemeliyatensorflower-gardener
authored andcommitted
PR tensorflow#43503: [ROCM] Important fixes for GpuBlasLt MatmulPlan cache
Imported from GitHub PR openxla/xla#43503 ## Summary This change prevents GPU BLASLt autotuning from blowing up the MatmulPlan cache by removing the selected algorithm from the plan cache key. MatmulPlans are now cached by the canonical GEMM key with the selected algorithm unset, while each plan caches its own algorithm list and currently selected algorithm. The original problem came with a new GEMM autotuner which, instead of calling gpublas_lt directly for each supported algorithm, profiles tiny GpuExecutables. That is, during the autotuning, we go through CublasLtMatmulThunk::ExecuteOnStream(..) which then inserts new entries to MatmulPlan cache (since canonical HLOs differ by **selected algorithm**): ``` I0000 00:00:1779469188.517485 1847675 gpublas_lt_matmul_thunk.cc:140] 0x7f4d0403d9c0: Adding new grouped MatmulPlan for stream: 0x21d619e0 instr: (f16[2400,8]{1,0}, s8[117600]{0}) custom-call(f16[2400,9]{1,0}, f16[600,9,8]{2,1,0}, s32[600]{0}), custom_call_target="__cublas$lt$groupedMatmul", backend_config={"operation_queue_id":"0","force_earliest_schedule":false,"reification_cost":[],"device_type":"DEVICE_TYPE_INVALID","grouped_gemm_backend_config":{"gemm_backend_config":{"selected_algorithm":"0","alpha_real":1,"beta":0,"dot_dimension_numbers":{"lhs_contracting_dimensions":["1"],"rhs_contracting_dimensions":["1"],"lhs_batch_dimensions":[],"rhs_batch_dimensions":[]},"alpha_imag":0,"epilogue":"DEFAULT","grad_x":false,"grad_y":false,"damax_output":false,"autotune_workspace_size":"117600","scale_mode":0},"ragged_dot_dimension_numbers":{"dot_dimension_numbers":{"lhs_contracting_dimensions":["1"],"rhs_contracting_dimensions":["1"],"lhs_batch_dimensions":[],"rhs_batch_dimensions":[]},"lhs_ragged_dimensions":["0"],"rhs_group_dimensions":["0"]}}} I0000 00:00:1779469188.957549 1847675 gpu_blas_lt.cc:314] 0x7f4f68001478: Plan created: cache size: 1 I0000 00:00:1779469190.125376 1847675 gpublas_lt_matmul_thunk.cc:140] 0x7f4d0803d9d0: Adding new grouped MatmulPlan for stream: 0x21d619e0 instr: (f16[2400,8]{1,0}, s8[117600]{0}) custom-call(f16[2400,9]{1,0}, f16[600,9,8]{2,1,0}, s32[600]{0}), custom_call_target="__cublas$lt$groupedMatmul", backend_config={"operation_queue_id":"0","force_earliest_schedule":false,"reification_cost":[],"device_type":"DEVICE_TYPE_INVALID","grouped_gemm_backend_config":{"gemm_backend_config":{"selected_algorithm":"1","alpha_real":1,"beta":0,"dot_dimension_numbers":{"lhs_contracting_dimensions":["1"],"rhs_contracting_dimensions":["1"],"lhs_batch_dimensions":[],"rhs_batch_dimensions":[]},"alpha_imag":0,"epilogue":"DEFAULT","grad_x":false,"grad_y":false,"damax_output":false,"autotune_workspace_size":"117600","scale_mode":0},"ragged_dot_dimension_numbers":{"dot_dimension_numbers":{"lhs_contracting_dimensions":["1"],"rhs_contracting_dimensions":["1"],"lhs_batch_dimensions":[],"rhs_batch_dimensions":[]},"lhs_ragged_dimensions":["0"],"rhs_group_dimensions":["0"]}}} I0000 00:00:1779469202.151521 1847675 gpu_blas_lt.cc:314] 0x7f4f68001478: Plan created: cache size: 2 ``` This might not be that bad for regular gemms, but can slow down the autotuning grouped gemm significantly. Furthermore, the MatmulPlan cache keeps a list of **128 MatmulPlans** for each gemm config after autotuning. ## Details - Introduces two-level caching for GPU BLASLt matmul execution: the outer cache reuses MatmulPlans across autotuned algorithm choices, and the inner cache keeps the retrieved algorithms and selected algorithm on the plan. - Updates thunk emission and execution to pass the selected algorithm separately from the canonical plan key. - Applies the cache flow across CUDA and ROCm BLASLt implementations, including grouped GEMM handling. - Adds coverage asserting that autotuned GEMM and grouped GEMM create a single MatmulPlan cache entry instead of one entry per selected algorithm. ## Test Plan - Added/updated `GpuBlasLtMatmulThunkTest` coverage for shared MatmulPlans and autotuned cache behavior. Copybara import of the project: -- 9a670c13c02ca01650590a0ac905f9991b4d7b5c by Pavel Emeliyanenko <pavel.emeliyanenko@amd.com>: redesigning blaslt cache Revert "redesigning blaslt cache" This reverts commit 5a96365fc83024a011c5a829926aed5cbd0d8594. moving towards cached algorithms inside matmul plan adding setcachedalgorithm added cached index improving set cached algorithm simplified set algorithm fixing cuda build cleanup fixing macros Revert "redesigning blaslt cache" This reverts commit 8a7481576e5fff2b13d39b646d3d607db8bb9628. fixing thunk emitter cache key just skip the test to check if it hangs reenabled subtests disabled all enabled 2 subtests disabled GemmWithAutotuneOneCacheEntry disabled functional test enabled autotune tests enabled only autotuneonecache enabled just single test running single test fix debugging why matmul_test hangs on CI reenabled again canonicalhlo string use workspace_size in the if statement restored ci_build -- ad8dfba320b4dc4de038570a9edc7d51a4746099 by Pavel Emeliyanenko <pavel.emeliyanenko@amd.com>: reenabled grouped gemm test -- 685309d9109ba3dfcea95976e2bb3b9b144f05be by Pavel Emeliyanenko <pavel.emeliyanenko@amd.com>: reduced the number of parallel threads Merging this change closes tensorflow#43503 PiperOrigin-RevId: 932409289
1 parent 53a6a1f commit c2cade2

11 files changed

Lines changed: 232 additions & 92 deletions

File tree

third_party/xla/xla/backends/gpu/runtime/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,7 @@ xla_test(
14751475
"//xla/runtime:buffer_use",
14761476
"//xla/service:buffer_assignment",
14771477
"//xla/service:executable",
1478+
"//xla/service:hlo_module_config",
14781479
"//xla/service:platform_util",
14791480
"//xla/service:shaped_slice",
14801481
"//xla/service/gpu:backend_configs_cc",

third_party/xla/xla/backends/gpu/runtime/gpublas_lt_matmul_thunk.cc

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ absl::Status CublasLtMatmulThunk::ExecuteOnStream(const ExecuteParams& params) {
129129
}
130130
return plan->ExecuteOnStream(params.stream, args, /*profile_result*/ nullptr);
131131
}
132+
132133
absl::StatusOr<se::gpu::BlasLt::MatmulPlan*>
133134
CublasLtMatmulThunk::GetCachedMatmulPlan(const ExecuteParams& params) {
134135
ASSIGN_OR_RETURN(auto* blas_lt,
@@ -139,32 +140,19 @@ CublasLtMatmulThunk::GetCachedMatmulPlan(const ExecuteParams& params) {
139140
<< " instr: " << canonical_hlo_;
140141

141142
ASSIGN_OR_RETURN(auto plan, std::visit(
142-
[&](const auto& gemm_config) {
143+
[&](auto&& gemm_config) {
143144
return blas_lt->GetMatmulPlan(gemm_config,
144145
epilogue_);
145146
},
146147
gemm_config_));
147-
148-
// Set the workspace size to the size that was used for autotuning, so
149-
// algorithm index will be the same as returned by GetAlgorithms called
150-
// during autotuning.
151-
int64_t max_workspace = autotune_workspace_size_;
152-
153-
// If autotuning is disabled, there is no point on retrieving all
154-
// algorithms, it's enough to get the default one only.
155-
int64_t num_algorithms =
156-
algorithm_idx_ == 0 ? 1 : GemmConfig::kNumAlgorithms;
157-
ASSIGN_OR_RETURN(auto algorithms,
158-
plan->GetAlgorithms(num_algorithms, max_workspace));
159-
160-
if (algorithms.empty()) {
161-
return absl::InternalError(
162-
"Failed to get a MatmulPlan: no valid algorithm found.");
163-
}
164-
RETURN_IF_ERROR(plan->SetAlgorithm(algorithms[algorithm_idx_]));
165148
return std::move(plan);
166149
};
167-
return blas_lt->GetOrCreateMatmulPlan(canonical_hlo_, create);
150+
// If autotuning is disabled, there is no point on retrieving all
151+
// algorithms, it's enough to get the default one only.
152+
size_t num_algorithms = algorithm_idx_ == 0 ? 1 : GemmConfig::kNumAlgorithms;
153+
return blas_lt->GetOrCreateMatmulPlanWithAlgorithm(
154+
canonical_hlo_, create, algorithm_idx_, num_algorithms,
155+
autotune_workspace_size_);
168156
}
169157

170158
absl::Status CublasLtMatmulThunk::Initialize(const InitializeParams& params) {
@@ -216,7 +204,7 @@ absl::StatusOr<ThunkProto> CublasLtMatmulThunk::ToProto() const {
216204
proto.mutable_cublas_lt_matmul_thunk();
217205

218206
RETURN_IF_ERROR(std::visit(
219-
[&](const auto& gemm_config) {
207+
[&](auto&& gemm_config) {
220208
using T = std::decay_t<decltype(gemm_config)>;
221209
if constexpr (std::is_same_v<T, se::gpu::GroupedGemmConfig>) {
222210
*cublas_lt_matmul_thunk->mutable_grouped_gemm_config() =

third_party/xla/xla/backends/gpu/runtime/gpublas_lt_matmul_thunk_test.cc

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ limitations under the License.
4545
#include "xla/error_spec.h"
4646
#include "xla/executable_run_options.h"
4747
#include "xla/hlo/ir/hlo_instruction.h"
48+
#include "xla/hlo/ir/hlo_module.h"
4849
#include "xla/hlo/ir/hlo_print_options.h"
4950
#include "xla/runtime/buffer_use.h"
5051
#include "xla/service/buffer_assignment.h"
5152
#include "xla/service/gpu/backend_configs.pb.h"
5253
#include "xla/service/gpu/buffer_allocations.h"
5354
#include "xla/service/gpu/cublas_cudnn.h"
5455
#include "xla/service/gpu/matmul_utils.h"
56+
#include "xla/service/hlo_module_config.h"
5557
#include "xla/service/platform_util.h"
5658
#include "xla/service/service_executable_run_options.h"
5759
#include "xla/service/shaped_slice.h"
@@ -199,7 +201,6 @@ void GpuBlasLtMatmulThunkTest::CreateExecuteThunksFromHLO(
199201
se::StreamExecutor* executor, absl::string_view hlo_string) {
200202
TF_ASSERT_OK_AND_ASSIGN(auto module,
201203
this->ParseAndReturnVerifiedModule(hlo_string));
202-
203204
GemmRewriterOptions options;
204205
TF_ASSERT_OK_AND_ASSIGN(
205206
bool changed,
@@ -211,7 +212,6 @@ void GpuBlasLtMatmulThunkTest::CreateExecuteThunksFromHLO(
211212

212213
GpuBlasLtThunkBuilder builder(executor, gpu_comp(executor));
213214
std::vector<std::unique_ptr<CublasLtMatmulThunk>> gemm_thunks;
214-
215215
for (auto* instr : module->entry_computation()->instructions()) {
216216
if (IsCublasLtMatmul(*instr)) {
217217
TF_ASSERT_OK_AND_ASSIGN(auto thunk, builder.CreateThunk(instr));
@@ -329,6 +329,71 @@ TEST_F(GpuBlasLtMatmulThunkTest, SharedMatmulPlansFunctional) {
329329
EXPECT_EQ(blas_lt->GetMatmulPlanCacheSize(), 2);
330330
}
331331

332+
TEST_F(GpuBlasLtMatmulThunkTest, GemmWithAutotuneOneCacheEntry) {
333+
auto* exec = default_exec();
334+
auto* blas_lt = exec->AsBlas()->GetBlasLt();
335+
EXPECT_NE(blas_lt, nullptr);
336+
blas_lt->ClearMatmulPlanCache();
337+
338+
constexpr absl::string_view simple_gemm_hlo = R"(
339+
HloModule SimpleGemm
340+
341+
ENTRY AddDotsFunc {
342+
x = f32[128,128] parameter(0)
343+
y = f32[128,128] parameter(1)
344+
ROOT dot_a = f32[128,128] dot(x, y), lhs_contracting_dims={1}, rhs_contracting_dims={0}
345+
})";
346+
347+
HloModuleConfig config = GetModuleConfigForTest();
348+
auto& debug_opts = config.mutable_debug_options();
349+
debug_opts.set_xla_gpu_enable_cublaslt(true);
350+
debug_opts.set_xla_gpu_autotune_level(1);
351+
debug_opts.set_xla_gpu_enable_triton_gemm(false);
352+
353+
TF_ASSERT_OK_AND_ASSIGN(
354+
std::unique_ptr<HloModule> module,
355+
ParseAndReturnVerifiedModule(simple_gemm_hlo, config));
356+
EXPECT_TRUE(RunAndCompare(std::move(module), ErrorSpec{1e-3, 1e-3}));
357+
EXPECT_EQ(blas_lt->GetMatmulPlanCacheSize(), 1);
358+
}
359+
360+
TEST_F(GpuBlasLtMatmulThunkTest, GroupedGemmWithAutotuneOneCacheEntry) {
361+
auto* rocm = gpu_comp().rocm_compute_capability();
362+
if (rocm == nullptr || !rocm->gfx9_mi300_series()) {
363+
GTEST_SKIP() << "Grouped GEMM is only supported on ROCm gfx942 or gfx950";
364+
}
365+
366+
auto* exec = default_exec();
367+
auto* blas_lt = exec->AsBlas()->GetBlasLt();
368+
EXPECT_NE(blas_lt, nullptr);
369+
blas_lt->ClearMatmulPlanCache();
370+
371+
constexpr absl::string_view grouped_gemm_hlo = R"(
372+
HloModule GroupedGemm
373+
374+
ENTRY AddRaggedDotsFunc {
375+
p0 = f16[64,9]{1,0} parameter(0)
376+
p1 = f16[2,9,8]{2,1,0} parameter(1)
377+
p2 = s32[2] constant({16, 48})
378+
ROOT ragged-dot = f16[64,8]{1,0} ragged-dot(p0, p1, p2),
379+
lhs_contracting_dims={1}, rhs_contracting_dims={1},
380+
lhs_ragged_dims={0}, rhs_group_dims={0}
381+
})";
382+
383+
HloModuleConfig config = GetModuleConfigForTest();
384+
auto& debug_opts = config.mutable_debug_options();
385+
debug_opts.set_xla_gpu_autotune_level(1);
386+
debug_opts.set_xla_gpu_enable_cublaslt(true);
387+
388+
debug_opts.set_xla_gpu_enable_triton_gemm(false);
389+
debug_opts.set_xla_gpu_experimental_use_ragged_dot_grouped_gemm(true);
390+
TF_ASSERT_OK_AND_ASSIGN(
391+
std::unique_ptr<HloModule> grouped_module,
392+
ParseAndReturnVerifiedModule(grouped_gemm_hlo, config));
393+
EXPECT_TRUE(RunAndCompare(std::move(grouped_module), ErrorSpec{1e-4, 1e-5}));
394+
EXPECT_EQ(blas_lt->GetMatmulPlanCacheSize(), 1);
395+
}
396+
332397
// Mock BlasLt interface to test only the cache function
333398
struct MockBlasLt : public se::gpu::BlasLt {
334399
absl::Status Init() override { return absl::OkStatus(); }
@@ -346,19 +411,40 @@ struct MockBlasLt : public se::gpu::BlasLt {
346411
~MockBlasLt() override = default;
347412
};
348413

414+
struct MockMatmulPlan : public se::gpu::BlasLt::MatmulPlan {
415+
absl::StatusOr<std::vector<se::gpu::BlasLt::MatmulAlgorithm>> GetAlgorithms(
416+
size_t max_algorithm_count, size_t max_workspace_size) const override {
417+
se::gpu::BlasLt::MatmulAlgorithm algo;
418+
return std::vector<se::gpu::BlasLt::MatmulAlgorithm>{algo};
419+
}
420+
421+
absl::Status SetAlgorithm(const se::gpu::BlasLt::MatmulAlgorithm&) override {
422+
return absl::OkStatus();
423+
}
424+
425+
absl::Status ExecuteOnStream(
426+
se::Stream* stream, const se::gpu::BlasLt::MemoryArgs& args,
427+
se::blas::ProfileResult* profile_result) const override {
428+
return absl::OkStatus();
429+
}
430+
~MockMatmulPlan() override = default;
431+
};
432+
349433
TEST_F(GpuBlasLtMatmulThunkTest, CacheUnitTest) {
350434
auto thread_func = [&](MockBlasLt* blas_lt, const std::string& key,
351435
int sleep_ms) -> absl::Status {
352436
auto create_func = [&]() -> absl::StatusOr<se::gpu::BlasLt::MatmulPlanPtr> {
353437
// We don't care about creation of matmul plans -> emulate it with a sleep
354438
absl::SleepFor(absl::Milliseconds(sleep_ms));
355-
return se::gpu::BlasLt::MatmulPlanPtr{};
439+
return std::make_unique<MockMatmulPlan>();
356440
};
357441

358-
return blas_lt->GetOrCreateMatmulPlan(key, create_func).status();
442+
return blas_lt
443+
->GetOrCreateMatmulPlanWithAlgorithm(key, create_func, 0, 1, 0)
444+
.status();
359445
}; // thread_func
360446

361-
const int num_blas_lts = 30, num_streams = 30,
447+
const int num_blas_lts = 8, num_streams = 8,
362448
total = num_blas_lts * num_streams, mod = 11;
363449

364450
std::vector<absl::Status> results(total);
@@ -573,9 +659,13 @@ static se::StreamExecutor* GpuExecutor() {
573659
}
574660

575661
// Returns true if the GPU supports CUDA graph tracing (requires CUDA 12.3+).
576-
static bool SupportsCudaGraphTracing(const se::StreamExecutor* executor) {
662+
static bool SupportsGpuGraphTracing(const se::StreamExecutor* executor) {
577663
const auto& desc = executor->GetDeviceDescription();
578-
const auto* cuda_cc = desc.gpu_compute_capability().cuda_compute_capability();
664+
const auto& gpu_cc = desc.gpu_compute_capability();
665+
if (gpu_cc.IsRocm()) {
666+
return true;
667+
}
668+
const auto* cuda_cc = gpu_cc.cuda_compute_capability();
579669
if (cuda_cc == nullptr) {
580670
return false;
581671
}
@@ -598,8 +688,8 @@ class CublasLtMatmulThunkCmdBufTest : public ::testing::Test {
598688

599689
void SetUp() override {
600690
executor_ = GpuExecutor();
601-
if (!SupportsCudaGraphTracing(executor_)) {
602-
GTEST_SKIP() << "CUDA graph tracing is not supported";
691+
if (!SupportsGpuGraphTracing(executor_)) {
692+
GTEST_SKIP() << "GPU graph tracing is not supported";
603693
}
604694

605695
TF_ASSERT_OK_AND_ASSIGN(stream_, executor_->CreateStream());

third_party/xla/xla/service/gpu/backend_configs.proto

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,10 @@ message GemmBackendConfig {
118118
int32 scale_mode = 21;
119119
}
120120

121-
// Backend config for the GEMM operation running through cuBLASLt.
121+
// Backend config for the grouped (ragged-dot) GEMM operation running through
122+
// cuBLASLt.
122123
message GroupedGemmBackendConfig {
123-
oneof gemm_config {
124-
GemmBackendConfig gemm_backend_config = 1;
125-
}
124+
GemmBackendConfig gemm_backend_config = 1;
126125

127126
xla.RaggedDotDimensionNumbers ragged_dot_dimension_numbers = 2;
128127
}

third_party/xla/xla/service/gpu/thunk_emitter.cc

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,22 @@ ThunkSequence FlattenThunkSequence(std::vector<ThunkSequence>&& sequences) {
321321
return result;
322322
}
323323

324+
absl::StatusOr<std::string> CanonicalGemmHlo(
325+
const HloCustomCallInstruction* instr) {
326+
ASSIGN_OR_RETURN(auto gpu_config, instr->backend_config<GpuBackendConfig>());
327+
328+
auto* gemm_config = gpu_config.has_grouped_gemm_backend_config()
329+
? gpu_config.mutable_grouped_gemm_backend_config()
330+
->mutable_gemm_backend_config()
331+
: gpu_config.mutable_gemm_backend_config();
332+
333+
// Clear algorithm-specific fields from the cache key
334+
gemm_config->clear_selected_algorithm();
335+
gemm_config->set_autotune_workspace_size(0);
336+
return instr->ToString(HloPrintOptions::Fingerprint()) +
337+
BackendConfigWrapper(gpu_config).GetRawString();
338+
}
339+
324340
} // namespace
325341

326342
ThunkEmitter::ThunkEmitter(
@@ -568,8 +584,8 @@ absl::StatusOr<ThunkSequence> ThunkEmitter::EmitCublasLtMatmulThunk(
568584
gpublas_lt::AsBlasLtEpilogue(epilogue));
569585
Thunk::ThunkInfo thunk_info = Thunk::ThunkInfo::WithProfileAnnotation(
570586
instr, ir_emitter_context_->GetNextThunkId());
571-
std::string canonical_hlo = instr->ToString(
572-
HloPrintOptions::Fingerprint().set_print_backend_config(true));
587+
588+
ASSIGN_OR_RETURN(std::string canonical_hlo, CanonicalGemmHlo(instr));
573589
auto thunk = std::make_unique<CublasLtMatmulThunk>(
574590
std::move(thunk_info), std::move(canonical_hlo), std::move(gemm_config),
575591
blas_lt_epilogue, algorithm, config.autotune_workspace_size(), a, b, c, d,
@@ -658,8 +674,7 @@ absl::StatusOr<ThunkSequence> ThunkEmitter::EmitCublasLtMatmulThunkF8(
658674
gpublas_lt::AsBlasLtEpilogue(epilogue));
659675
Thunk::ThunkInfo thunk_info = Thunk::ThunkInfo::WithProfileAnnotation(
660676
instr, ir_emitter_context_->GetNextThunkId());
661-
std::string canonical_hlo = instr->ToString(
662-
HloPrintOptions::Fingerprint().set_print_backend_config(true));
677+
ASSIGN_OR_RETURN(std::string canonical_hlo, CanonicalGemmHlo(instr));
663678
auto thunk = std::make_unique<CublasLtMatmulThunk>(
664679
std::move(thunk_info), std::move(canonical_hlo), std::move(gemm_config),
665680
blas_lt_epilogue, algorithm, config.autotune_workspace_size(), a, b, c, d,
@@ -734,8 +749,7 @@ absl::StatusOr<ThunkSequence> ThunkEmitter::EmitCublasLtGroupedMatmulThunk(
734749

735750
Thunk::ThunkInfo thunk_info = Thunk::ThunkInfo::WithProfileAnnotation(
736751
instr, ir_emitter_context_->GetNextThunkId());
737-
std::string canonical_hlo = instr->ToString(
738-
HloPrintOptions::Fingerprint().set_print_backend_config(true));
752+
ASSIGN_OR_RETURN(std::string canonical_hlo, CanonicalGemmHlo(instr));
739753

740754
auto thunk = std::make_unique<CublasLtMatmulThunk>(
741755
std::move(thunk_info), std::move(canonical_hlo), std::move(gemm_config),
@@ -788,8 +802,7 @@ absl::StatusOr<ThunkSequence> ThunkEmitter::EmitCublasLtMatmulThunkMx(
788802
gpublas_lt::AsBlasLtEpilogue(epilogue));
789803
Thunk::ThunkInfo thunk_info = Thunk::ThunkInfo::WithProfileAnnotation(
790804
instr, ir_emitter_context_->GetNextThunkId());
791-
std::string canonical_hlo = instr->ToString(
792-
HloPrintOptions::Fingerprint().set_print_backend_config(true));
805+
ASSIGN_OR_RETURN(std::string canonical_hlo, CanonicalGemmHlo(instr));
793806
auto thunk = std::make_unique<CublasLtMatmulThunk>(
794807
std::move(thunk_info), std::move(canonical_hlo), std::move(gemm_config),
795808
blas_lt_epilogue, algorithm, config.autotune_workspace_size(), a, b, c, d,

third_party/xla/xla/stream_executor/cuda/cuda_blas_lt.cc

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ absl::Status BlasLt::MatmulPlan::ExecuteOnStream(
458458
blas::ProfileResult* profile_result) const {
459459
if (!algorithm_.has_value()) {
460460
return absl::InternalError(
461-
"Algorithm must be set before calling DoMatMul!");
461+
"Algorithm must be set before calling ExecuteOnStream!");
462462
}
463463
DeviceAddressBase a = args.a, b = args.b;
464464
DeviceAddressBase a_scale = args.a_scale, b_scale = args.b_scale;
@@ -474,21 +474,20 @@ absl::Status BlasLt::MatmulPlan::ExecuteOnStream(
474474
}
475475

476476
void* workspace_addr = nullptr;
477-
uint64_t workspace_size = algorithm_->workspace_size;
478-
if (workspace_size > 0) {
477+
uint64_t workspace_size = workspace_size_;
478+
if (workspace_size_ > 0) {
479479
if (args.scratch_allocator != nullptr) {
480480
ASSIGN_OR_RETURN(DeviceAddress<uint8_t> alloc,
481-
args.scratch_allocator->AllocateBytes(workspace_size));
481+
args.scratch_allocator->AllocateBytes(workspace_size_));
482482
workspace_addr = gpu::GpuMemoryMutable(&alloc);
483483
} else {
484484
workspace_addr = args.workspace.opaque();
485485
size_t new_size = args.workspace.size();
486-
TF_RET_CHECK(workspace_addr != nullptr && new_size >= workspace_size);
486+
TF_RET_CHECK(workspace_addr != nullptr && new_size >= workspace_size_);
487487
workspace_size = new_size;
488488
}
489489
}
490490

491-
auto palgo = std::any_cast<cublasLtMatmulAlgo_t>(&algorithm_->opaque_algo);
492491
{
493492
absl::MutexLock lock(blas_lt_.mu_);
494493
TF_RET_CHECK(blas_lt_.handle_.get() != nullptr);
@@ -557,22 +556,19 @@ absl::Status BlasLt::MatmulPlan::ExecuteOnStream(
557556
blas_lt_.executor_->Activate();
558557

559558
void* c_ptr = zero_beta_ ? nullptr : args.c.opaque();
560-
if (palgo != nullptr) {
561-
SE_CUBLAS_RETURN_IF_ERROR(cublasLtMatmul(
562-
blas_lt_.handle_.get(), op_desc_.get(), &alpha_[0], a.opaque(),
563-
a_desc_.get(), b.opaque(), b_desc_.get(), &beta_[0], c_ptr,
564-
c_desc_.get(), args.d.opaque(), d_desc_.get(), palgo, workspace_addr,
565-
workspace_size,
566-
absl::bit_cast<CUstream>(stream->platform_specific_handle().stream)));
567-
} else {
568-
return absl::InternalError("cublaslt: Invalid algorithm type");
569-
}
559+
SE_CUBLAS_RETURN_IF_ERROR(cublasLtMatmul(
560+
blas_lt_.handle_.get(), op_desc_.get(), &alpha_[0], a.opaque(),
561+
a_desc_.get(), b.opaque(), b_desc_.get(), &beta_[0], c_ptr,
562+
c_desc_.get(), args.d.opaque(), d_desc_.get(), &algorithm_.value(),
563+
workspace_addr, workspace_size,
564+
absl::bit_cast<CUstream>(stream->platform_specific_handle().stream)));
570565
}
571566

572567
if (profile_result != nullptr) {
573568
ASSIGN_OR_RETURN(absl::Duration elapsed, timer->GetElapsedDuration());
574569
// set algorithm ID to be unique (otherwise it gets kDefaultAlgorithm ID)
575-
profile_result->set_algorithm(reinterpret_cast<blas::AlgorithmType>(palgo));
570+
profile_result->set_algorithm(
571+
reinterpret_cast<blas::AlgorithmType>(&algorithm_.value()));
576572
profile_result->set_is_valid(true);
577573
profile_result->set_elapsed_time_in_ms(absl::ToDoubleMilliseconds(elapsed));
578574
}

0 commit comments

Comments
 (0)