Skip to content

[XLA:GPU] Use DeviceDescription instead of hard-coding warp size as 32 #2938

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

Open
wants to merge 1 commit into
base: r2.18-rocm-enhanced
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 11 additions & 1 deletion third_party/xla/xla/service/gpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ xla_cc_test(
srcs = ["gpu_copy_insertion_test.cc"],
deps = [
":buffer_sharing",
":gpu_device_info_for_tests",
"//xla:test",
"//xla:test_helpers",
"//xla/hlo/analysis:hlo_dataflow_analysis",
"//xla/hlo/ir:hlo",
"//xla/service:copy_insertion",
"//xla/tests:hlo_test_base",
Expand Down Expand Up @@ -263,7 +265,9 @@ xla_cc_test(

cc_library(
name = "gpu_device_info_for_tests",
testonly = 1,
# This is *not* a test library because it is used in a cc_binary which is used for testing but
# test_only libraries are not allowed in cc_binaries.
testonly = 0,
srcs = ["gpu_device_info_for_tests.cc"],
hdrs = ["gpu_device_info_for_tests.h"],
compatible_with = get_compatible_with_portable(),
Expand Down Expand Up @@ -704,6 +708,7 @@ cc_library(
"//xla/hlo/ir:hlo",
"//xla/service:hlo_module_config",
"//xla/stream_executor:semantic_version",
"//xla/stream_executor:device_description",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:inlined_vector",
Expand Down Expand Up @@ -1345,6 +1350,7 @@ cc_library(
"//xla/service/gpu/transforms:copy_fusion",
"//xla/service/gpu/transforms:horizontal_loop_fusion",
"//xla/service/gpu/transforms:sanitize_constant_names",
"//xla/stream_executor:device_description",
],
)

Expand Down Expand Up @@ -2521,6 +2527,10 @@ xla_cc_test(
"//xla/hlo/ir:hlo",
"//xla/service:hlo_parser",
"//xla/tests:hlo_test_base",
"//xla/service:hlo_runner",
"//xla/service:instruction_fusion",
"//xla/service:platform_util",
"//xla/stream_executor:device_description",
"//xla/tests:xla_internal_test_main",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
Expand Down
19 changes: 8 additions & 11 deletions third_party/xla/xla/service/gpu/autotuning/autotuner_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,8 @@ class AutotuneConfig {
debug_options.xla_gpu_experimental_autotune_cache_mode()) {}

std::string GetModelStr() const {
if (auto deviceless_config = std::get_if<DevicelessConfig>(&config_)) {
return AutotuneCacheKey::DeviceDescriptionToCacheKey(
deviceless_config->device_description);
}

const auto& device_config = std::get<DeviceConfig>(config_);
return AutotuneCacheKey::DeviceDescriptionToCacheKey(
device_config.stream_exec->GetDeviceDescription());
GetDeviceDescription());
}

se::StreamExecutor* GetExecutor() const {
Expand All @@ -175,11 +169,14 @@ class AutotuneConfig {
}

const se::GpuComputeCapability& GetGpuComputeCapability() const {
if (auto c = std::get_if<DeviceConfig>(&config_)) {
return c->stream_exec->GetDeviceDescription().gpu_compute_capability();
return GetDeviceDescription().gpu_compute_capability();
}

const se::DeviceDescription& GetDeviceDescription() const {
if (auto* device_config = std::get_if<DeviceConfig>(&config_)) {
return device_config->stream_exec->GetDeviceDescription();
}
return std::get<DevicelessConfig>(config_)
.device_description.gpu_compute_capability();
return std::get<DevicelessConfig>(config_).device_description;
}

bool IsDeviceless() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,7 @@ GpuConvAlgorithmPicker::AutotuneRuntimeArguments::FromInstruction(

// Get canonical HLO.
std::string canonical_hlo(
AutotuneCacheKey(config.GetExecutor()->GetDeviceDescription(), *instr)
.GetHlo());
AutotuneCacheKey(config.GetDeviceDescription(), *instr).GetHlo());

TF_ASSIGN_OR_RETURN(GpuConvConfig gpu_conv_config, GetGpuConvConfig(instr));

Expand Down
39 changes: 21 additions & 18 deletions third_party/xla/xla/service/gpu/autotuning/gemm_fusion_autotuner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ absl::StatusOr<std::unique_ptr<HloModule>> TritonGemmAutotuneExtractor(

// If the priority fusion pass above skipped some instructions, turn them
// into fusions.
FusionWrapper fusion_wrapper;
FusionWrapper fusion_wrapper(gpu_device_info);
TF_RETURN_IF_ERROR(fusion_wrapper.Run(new_module.get()).status());
}
return new_module;
Expand Down Expand Up @@ -528,7 +528,7 @@ absl::Status DumpAutotunedFusion(const AutotuneConfig& autotune_config,
TritonGemmConfig::FromProto(result.triton()));
}
const se::DeviceDescription& device_desc =
autotune_config.GetExecutor()->GetDeviceDescription();
autotune_config.GetDeviceDescription();
TF_ASSIGN_OR_RETURN(
std::unique_ptr<HloModule> module,
util.ExtractModule([&](const DebugOptions& debug_opts) {
Expand Down Expand Up @@ -693,12 +693,12 @@ GemmFusionAutotunerImpl::GenerateTritonConfigs(const HloDotInstruction& dot) {
// a sufficient number of thread block programs to occupy all available cores.
// Around 5 full waves completely avoid the need for split-K.
// n_tiles = split_k * (M * N) / (block_m * block_n)
const int kCoreCount =
!config_.IsDeviceless()
? config_.GetExecutor()->GetDeviceDescription().core_count()
: 100; // some sensible default
const int kCoreCount = config_.GetDeviceDescription().core_count();
CHECK_GE(kCoreCount, 1);
const int64_t kSufficientNumberOfTiles = kMaxWavesForSplitK * kCoreCount;
const int64_t result_size = ShapeUtil::ElementsIn(dot.shape());
const int64_t threads_per_warp =
config_.GetDeviceDescription().threads_per_warp();

// Triton configurations are adjusted and deduplicated.
absl::flat_hash_set<TritonGemmConfig> added;
Expand Down Expand Up @@ -735,7 +735,7 @@ GemmFusionAutotunerImpl::GenerateTritonConfigs(const HloDotInstruction& dot) {
2 * std::max(kMinTileSize, kLdmatrixGranularity / minBitWidth));
int meta_elements = config.block_m * config.block_k / 16;
config.num_warps =
std::min<int>(config.num_warps, meta_elements / WarpSize());
std::min<int>(config.num_warps, meta_elements / threads_per_warp);
}

if (added.insert(config).second) {
Expand Down Expand Up @@ -783,13 +783,13 @@ GemmFusionAutotunerImpl::CompileAll(AutotunerCompileUtil& compile_util,
-> absl::StatusOr<bool> {
std::unique_ptr<Executable> executable;
if (std::holds_alternative<TritonGemmConfig>(config)) {
TF_ASSIGN_OR_RETURN(
executable, compile_util.Compile([&](const DebugOptions& opts) {
return TritonGemmAutotuneExtractor(
std::get<TritonGemmConfig>(config),
config_.GetExecutor()->GetDeviceDescription(), fusion, opts,
allow_filtering_kernels_spilling_registers);
}));
TF_ASSIGN_OR_RETURN(executable,
compile_util.Compile([&](const DebugOptions& opts) {
return TritonGemmAutotuneExtractor(
std::get<TritonGemmConfig>(config),
config_.GetDeviceDescription(), fusion, opts,
allow_filtering_kernels_spilling_registers);
}));
} else if (std::holds_alternative<CuDnnConfig>(config)) {
executable =
compile_util
Expand All @@ -801,9 +801,9 @@ GemmFusionAutotunerImpl::CompileAll(AutotunerCompileUtil& compile_util,
} else if (std::holds_alternative<CuBlasConfig>(config)) {
TF_ASSIGN_OR_RETURN(
executable, compile_util.Compile([&](const DebugOptions& opts) {
return CublasGemmAutotuneExtractor(
config_, config_.GetExecutor()->GetDeviceDescription(),
toolkit_version_, fusion, opts);
return CublasGemmAutotuneExtractor(config_,
config_.GetDeviceDescription(),
toolkit_version_, fusion, opts);
}));
} else {
LOG(FATAL) << "Unsupported config type: " << config.index();
Expand Down Expand Up @@ -1005,6 +1005,9 @@ GemmFusionAutotunerImpl::GetExhaustiveTritonConfigs() const {
bool tune_ctas =
debug_options_.xla_gpu_enable_triton_hopper() && cc.IsAtLeastHopper();

const int64_t threads_per_warp =
config_.GetDeviceDescription().threads_per_warp();

for (int num_stages : kNumStages) {
// Volta doesn't support num_stages > 2.
if (!cc.IsAtLeastAmpere() && num_stages > 2) {
Expand All @@ -1017,7 +1020,7 @@ GemmFusionAutotunerImpl::GetExhaustiveTritonConfigs() const {
const int tile_rhs = tile_k * tile_n;
for (int num_warps : kNumWarps) {
// Each thread should read at least one input element.
if (num_warps * WarpSize() > std::min(tile_lhs, tile_rhs)) {
if (num_warps * threads_per_warp > std::min(tile_lhs, tile_rhs)) {
break;
}
for (int split_k : kSplitK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ absl::StatusOr<std::vector<TritonGemmConfig>> GetPossibleMatmulAutotuneConfigs(
auto ccc = deviceless_proto.mutable_cuda_compute_capability();
ccc->set_major(compute_capability.major);
ccc->set_minor(compute_capability.minor);
deviceless_proto.set_core_count(100);
deviceless_proto.set_threads_per_warp(32);
DevicelessConfig test_config{se::DeviceDescription{deviceless_proto}};
AutotuneConfig autotune_config{test_config, debug_options};
GemmFusionAutotunerImpl autotuner(autotune_config, toolkit_version,
Expand Down Expand Up @@ -941,7 +943,11 @@ ENTRY wais {
compute_capability, GetToolkitVersion(), debug_options));
for (const auto& config : configs) {
int metadata_size = config.block_m * config.block_k / 16;
EXPECT_LE(config.num_warps * WarpSize(), metadata_size);
EXPECT_LE(
config.num_warps *
WarpSize(
backend().default_stream_executor()->GetDeviceDescription()),
metadata_size);
EXPECT_GT(config.block_k, 16); // kMinTileSize
}
}
Expand Down
19 changes: 10 additions & 9 deletions third_party/xla/xla/service/gpu/buffer_sharing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ limitations under the License.
namespace xla {
namespace gpu {

std::optional<bool> FusionCanShareBufferHint(const HloInstruction* user,
const HloInstruction* operand,
const ShapeIndex& user_index) {
std::optional<bool> FusionCanShareBufferHint(
const HloInstruction* user, const HloInstruction* operand,
const ShapeIndex& user_index,
const se::DeviceDescription& device_description) {
const HloFusionInstruction* fusion = DynCast<HloFusionInstruction>(user);
if (fusion == nullptr) {
return std::nullopt;
Expand Down Expand Up @@ -77,8 +78,6 @@ std::optional<bool> FusionCanShareBufferHint(const HloInstruction* user,
// Allow multiple output users, if they end in reductions.
// This only works for the reduction emitter, as it calculates the reduction
// first, i.e. before processing other outputs (that may overwrite the input).
stream_executor::GpuDeviceInfoProto device_info;
stream_executor::DeviceDescription device_description(device_info);
auto analysis = HloFusionAnalysis::Create(*user, device_description);
bool is_reduction_emitter = analysis.GetEmitterFusionKind() ==
HloFusionAnalysis::EmitterFusionKind::kReduction;
Expand Down Expand Up @@ -219,9 +218,10 @@ std::optional<bool> FusionCanShareBufferHint(const HloInstruction* user,
return found_path_to_output;
}

std::optional<bool> CanShareBufferHint(const HloInstruction* user,
const HloInstruction* operand,
const ShapeIndex& user_index) {
std::optional<bool> CanShareBufferHint(
const HloInstruction* user, const HloInstruction* operand,
const ShapeIndex& user_index,
const se::DeviceDescription& device_description) {
switch (user->opcode()) {
case HloOpcode::kAllReduce:
case HloOpcode::kCollectiveBroadcast:
Expand All @@ -243,7 +243,8 @@ std::optional<bool> CanShareBufferHint(const HloInstruction* user,
}
return false;
case HloOpcode::kFusion:
return FusionCanShareBufferHint(user, operand, user_index);
return FusionCanShareBufferHint(user, operand, user_index,
device_description);
default:
return std::nullopt;
}
Expand Down
17 changes: 10 additions & 7 deletions third_party/xla/xla/service/gpu/buffer_sharing.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ limitations under the License.

#include "xla/hlo/ir/hlo_instruction.h"
#include "xla/shape_util.h"
#include "xla/stream_executor/device_description.h"

namespace xla {
namespace gpu {
std::optional<bool> FusionCanShareBufferHint(const HloInstruction* user,
const HloInstruction* operand,
const ShapeIndex& user_index);

std::optional<bool> CanShareBufferHint(const HloInstruction* user,
const HloInstruction* operand,
const ShapeIndex& user_index);
std::optional<bool> FusionCanShareBufferHint(
const HloInstruction* user, const HloInstruction* operand,
const ShapeIndex& user_index,
const se::DeviceDescription& device_description);

std::optional<bool> CanShareBufferHint(
const HloInstruction* user, const HloInstruction* operand,
const ShapeIndex& user_index,
const se::DeviceDescription& device_description);
} // namespace gpu
} // namespace xla

Expand Down
2 changes: 1 addition & 1 deletion third_party/xla/xla/service/gpu/fusion_pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ HloPassPipeline FusionPipeline(
HloPassPipeline HorizontalFusionPipeline(
const se::DeviceDescription& gpu_device_info) {
HloPassFix<HloPassPipeline> horizontal_fusion("horizontal fusion");
horizontal_fusion.AddPass<HorizontalLoopFusion>();
horizontal_fusion.AddPass<HorizontalLoopFusion>(gpu_device_info);
horizontal_fusion.AddPass<HorizontalInputFusion>(gpu_device_info);
horizontal_fusion.AddPass<HloCSE>(/*is_layout_sensitive=*/true,
/*only_fusion_computations=*/true);
Expand Down
2 changes: 2 additions & 0 deletions third_party/xla/xla/service/gpu/fusions/legacy/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ cc_library(
"//xla/service/llvm_ir:kernel_support_library",
"//xla/service/llvm_ir:llvm_loop",
"//xla/service/llvm_ir:llvm_util",
"//xla/stream_executor:device_description",
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
Expand Down Expand Up @@ -322,6 +323,7 @@ cc_library(
"//xla/service/llvm_ir:ir_array",
"//xla/service/llvm_ir:llvm_util",
"//xla/service/llvm_ir:loop_emitter",
"//xla/stream_executor:device_description",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/log:check",
Expand Down
Loading