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

Fix an issue in PartitionGatherTrivialSlicedOperandDimensions when handling out-of-bound indices. #20401

Merged
merged 1 commit into from
Dec 13, 2024
Merged
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
50 changes: 45 additions & 5 deletions xla/service/spmd/gather_scatter_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,44 @@ std::vector<int64_t> GatherOutputDimsByPriority(
return priority_dims_for_output;
}

PartitionedHlo ClampGatherIndices(const PartitionedHlo& indices,
const Shape& operand_base_shape,
absl::Span<const int64_t> start_index_map,
int64_t index_vector_dim, SpmdBuilder* b) {
const PrimitiveType indices_type = indices.hlo()->shape().element_type();

HloInstruction* max_indices;
if (index_vector_dim < indices.rank()) {
std::vector<int32_t> max_indices_values;
max_indices_values.reserve(start_index_map.size());
for (int64_t operand_dim : start_index_map) {
max_indices_values.push_back(operand_base_shape.dimensions(operand_dim) -
1);
}
max_indices = b->AddInstruction(HloInstruction::CreateConstant(
LiteralUtil::CreateR1<int32_t>(max_indices_values)));
max_indices = b->AddInstruction(HloInstruction::CreateBroadcast(
indices.hlo()->shape(), max_indices, {index_vector_dim}));
} else {
CHECK_EQ(start_index_map.size(), 1);
max_indices = CreateR0WithType<int32_t>(
indices_type, operand_base_shape.dimensions(start_index_map[0]) - 1, b);
max_indices = b->AddInstruction(HloInstruction::CreateBroadcast(
indices.hlo()->shape(), max_indices, {}));
}

HloInstruction* constant_zero = CreateR0WithType<int32_t>(indices_type, 0, b);
HloInstruction* min_indices =
b->AddInstruction(HloInstruction::CreateBroadcast(indices.hlo()->shape(),
constant_zero, {}));

HloInstruction* clamped_indices = b->AddInstruction(
HloInstruction::CreateTernary(indices.hlo()->shape(), HloOpcode::kClamp,
min_indices, indices.hlo(), max_indices));
clamped_indices->set_sharding(indices.sharding());
return PartitionedHlo(clamped_indices, indices.base_shape(), indices.state());
}

// Returns the min and max for the indices in a scatter/gather which has the
// operand partitioned on trivial slice dimensions (slice size 1).
std::pair<HloInstruction*, HloInstruction*>
Expand Down Expand Up @@ -451,11 +489,9 @@ absl::StatusOr<HloInstruction*> PartitionGatherTrivialSlicedOperandDimensions(

SpmdBuilder* b = visitor->builder();
const GatherDimensionNumbers& dnums = gather->gather_dimension_numbers();
std::vector<int64_t> start_index_map(dnums.start_index_map().begin(),
dnums.start_index_map().end());
if (std::optional<std::vector<int64_t>> trivial_slice_dims =
GatherScatterOperandPartitionedOnTrivialSliceDims(
operand, start_index_map, slice_sizes)) {
operand, dnums.start_index_map(), slice_sizes)) {
const HloSharding original_operand_sharding = operand.sharding();
const int64_t num_groups = operand.sharding().NumTiles(*trivial_slice_dims);
const int64_t num_tiles = operand.sharding().TotalNumTiles();
Expand Down Expand Up @@ -504,6 +540,9 @@ absl::StatusOr<HloInstruction*> PartitionGatherTrivialSlicedOperandDimensions(
// Reshard indices to its intended sharding before clamping and adjusting.
indices =
indices.Reshard(hlo_sharding_util::UngroupSharding(indices_grouped));
indices = ClampGatherIndices(indices, operand.base_shape(),
dnums.start_index_map(),
dnums.index_vector_dim(), b);
// Now the operand is partitioned in trivial slice dimensions, and the
// indices are replicated. We execute a gather on partitioned operand,
// with full number of indices, where out-of-bounds indices are clamped,
Expand All @@ -514,8 +553,9 @@ absl::StatusOr<HloInstruction*> PartitionGatherTrivialSlicedOperandDimensions(
HloInstruction* indices_max;
std::tie(indices_min, indices_max) =
IndexBoundsForGatherScatterOperandPartitionedOnTrivialSliceDims(
operand, indices, operand.state().partition_id, start_index_map,
*trivial_slice_dims, dnums.index_vector_dim(), b);
operand, indices, operand.state().partition_id,
dnums.start_index_map(), *trivial_slice_dims,
dnums.index_vector_dim(), b);
// Clamp the indices.
auto adjusted_indices = b->AddInstruction(
HloInstruction::CreateTernary(indices.hlo()->shape(), HloOpcode::kClamp,
Expand Down
26 changes: 16 additions & 10 deletions xla/service/spmd/spmd_partitioner_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7926,10 +7926,13 @@ ENTRY entry {
auto min = AllOf(op::Broadcast(offset), op::Shape("s32[2,3]"));
auto max = AllOf(op::Broadcast(op::Add(offset, op::Constant())),
op::Shape("s32[2,3]"));
auto clamp = op::Clamp(min, op::Parameter(1), max);
auto clamped_indices =
op::Clamp(op::Broadcast(op::Constant()), op::Parameter(1),
op::Broadcast(op::Constant()));
auto clamp = op::Clamp(min, clamped_indices, max);
auto gather = op::Gather(op::Parameter(0), op::Subtract(clamp, min));
auto mask =
op::Or(op::Lt(op::Parameter(1), min), op::Gt(op::Parameter(1), max));
op::Or(op::Lt(clamped_indices, min), op::Gt(clamped_indices, max));
auto masked =
op::Select(op::Broadcast(mask), op::Broadcast(op::Constant()), gather);
HloInstruction* root = module->entry_computation()->root_instruction();
Expand All @@ -7952,15 +7955,18 @@ ENTRY entry {
TF_ASSERT_OK_AND_ASSIGN(auto module,
PartitionComputation(hlo_string, /*num_devices=*/4));
VLOG(1) << module->ToString();
auto clamped_indices =
op::Clamp(op::Broadcast(op::Constant()), op::Parameter(1),
op::Broadcast(op::Constant()));
auto offset =
op::Reshape(op::DynamicSlice(op::Constant(), op::PartitionId()));
auto min = AllOf(op::Broadcast(offset), op::Shape("s32[2,3]"));
auto max = AllOf(op::Broadcast(op::Add(offset, op::Constant())),
op::Shape("s32[2,3]"));
auto clamp = op::Clamp(min, op::Parameter(1), max);
auto clamp = op::Clamp(min, clamped_indices, max);
auto gather = op::Gather(op::Parameter(0), op::Subtract(clamp, min));
auto mask =
op::Or(op::Lt(op::Parameter(1), min), op::Gt(op::Parameter(1), max));
op::Or(op::Lt(clamped_indices, min), op::Gt(clamped_indices, max));
auto masked =
op::Select(op::Broadcast(mask), op::Broadcast(op::Constant()), gather);
HloInstruction* root = module->entry_computation()->root_instruction();
Expand Down Expand Up @@ -11919,11 +11925,10 @@ ENTRY entry {
VLOG(1) << module->ToString();
HloInstruction* root = module->entry_computation()->root_instruction();
EXPECT_THAT(root, op::AllReduce(op::Select(_, _, op::Gather(_, _))));
EXPECT_THAT(root->operand(0)->operand(2)->operand(1),
op::Subtract(op::Clamp(_, op::Parameter(1), _), _));
EXPECT_THAT(
root->operand(0)->operand(2)->operand(1),
op::Subtract(op::Clamp(_, op::Clamp(_, op::Parameter(1), _), _), _));

auto clamp = FindInstruction(module.get(), HloOpcode::kClamp);
EXPECT_THAT(clamp->operand(1), op::Parameter(1));
auto dynamic_slice = FindInstruction(module.get(), HloOpcode::kDynamicSlice);
EXPECT_THAT(dynamic_slice->operand(1), op::PartitionId());
auto collective_permute =
Expand Down Expand Up @@ -11955,8 +11960,9 @@ ENTRY entry {
_, op::AllReduce(op::Select(_, _, op::Gather(op::AllReduce(_), _))),
_, _, _)));
auto gather = FindInstruction(module.get(), HloOpcode::kGather);
EXPECT_THAT(gather->operand(1),
op::Subtract(op::Clamp(_, op::Parameter(1), _), _));
EXPECT_THAT(
gather->operand(1),
op::Subtract(op::Clamp(_, op::Clamp(_, op::Parameter(1), _), _), _));
auto collective_permute =
FindInstruction(module.get(), HloOpcode::kCollectivePermute);
EXPECT_NE(collective_permute, nullptr);
Expand Down
Loading