Skip to content

Commit 741b16b

Browse files
Marcello Maggionitensorflower-gardener
authored andcommitted
[XLA] Respect layout of outfeed when SPMD partioning.
PiperOrigin-RevId: 351281979 Change-Id: I56af3d07ed4a2181de31b924a40d7cef13a360d7
1 parent 4517ba2 commit 741b16b

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎tensorflow/compiler/xla/service/spmd/spmd_partitioner.cc‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3329,9 +3329,12 @@ Status SpmdPartitioningVisitor::HandleOutfeed(HloInstruction* hlo) {
33293329
auto token = GetPartitionedHlo(hlo->operand(1)).hlo();
33303330

33313331
if (EvenlyPartitions(shape, sharding)) {
3332+
Shape outfeed_shape = operand->shape();
3333+
TF_RETURN_IF_ERROR(LayoutUtil::CopyLayoutBetweenShapes(hlo->outfeed_shape(),
3334+
&outfeed_shape));
33323335
SetPartitionedHlo(hlo, [&]() {
33333336
return b_.AddInstruction(HloInstruction::CreateOutfeed(
3334-
operand->shape(), operand, token, hlo->outfeed_config()));
3337+
outfeed_shape, operand, token, hlo->outfeed_config()));
33353338
});
33363339
return Status::OK();
33373340
}
@@ -3440,6 +3443,8 @@ Status SpmdPartitioningVisitor::HandleOutfeed(HloInstruction* hlo) {
34403443
};
34413444
outfeed_data = slice_outfeed({}, outfeed_data);
34423445
}
3446+
TF_RETURN_IF_ERROR(LayoutUtil::CopyLayoutBetweenShapes(
3447+
hlo->outfeed_shape(), &per_branch_partitioned_shapes[i]));
34433448
branch_b.AddInstruction(HloInstruction::CreateOutfeed(
34443449
per_branch_partitioned_shapes[i], outfeed_data, outfeed_token,
34453450
hlo->outfeed_config()));

‎tensorflow/compiler/xla/service/spmd/spmd_partitioner_test.cc‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,8 @@ ENTRY entry {
708708
token.0 = token[] after-all()
709709
data = (f32[1024,2]{1,0}, f32[2]{0}) parameter(0), sharding={{devices=[2,1]0,1},
710710
{devices=[2]0,1}}
711-
ROOT outfeed = token[] outfeed(data, token.0), sharding={{devices=[2,1]0,1},
711+
ROOT outfeed = token[] outfeed(data, token.0),
712+
outfeed_shape=(f32[1024,2]{0,1}, f32[2]{0}), sharding={{devices=[2,1]0,1},
712713
{devices=[2]0,1}}
713714
})";
714715
TF_ASSERT_OK_AND_ASSIGN(auto module,
@@ -717,6 +718,12 @@ ENTRY entry {
717718
HloInstruction* root = module->entry_computation()->root_instruction();
718719
EXPECT_THAT(root, AllOf(op::Shape("token[]"),
719720
op::Outfeed(op::Parameter(), op::AfterAll())));
721+
auto expected_layout0 = LayoutUtil::MakeLayout({0, 1});
722+
auto expected_layout1 = LayoutUtil::MakeLayout({0});
723+
EXPECT_TRUE(LayoutUtil::Equal(root->outfeed_shape().tuple_shapes(0).layout(),
724+
expected_layout0));
725+
EXPECT_TRUE(LayoutUtil::Equal(root->outfeed_shape().tuple_shapes(1).layout(),
726+
expected_layout1));
720727
}
721728

722729
TEST_F(SpmdPartitioningTest, OutfeedReplicated) {
@@ -746,7 +753,8 @@ ENTRY entry {
746753
token.0 = token[] after-all()
747754
data = (f32[1023,2]{1,0}, f32[3]{0}) parameter(0), sharding={{devices=[2,1]0,1},
748755
{devices=[2]0,1}}
749-
outfeed = token[] outfeed(data, token.0), sharding={{devices=[2,1]0,1},
756+
outfeed = token[] outfeed(data, token.0),
757+
outfeed_shape=(f32[1023,2]{0,1}, f32[3]{0}), sharding={{devices=[2,1]0,1},
750758
{devices=[2]0,1}}
751759
})";
752760
TF_ASSERT_OK_AND_ASSIGN(auto module,
@@ -770,6 +778,24 @@ ENTRY entry {
770778
EXPECT_THAT(root->called_computations()[1]->root_instruction(),
771779
AllOf(op::Shape("token[]"),
772780
op::Outfeed(second_outfeed, op::GetTupleElement())));
781+
782+
auto expected_layout0 = LayoutUtil::MakeLayout({0, 1});
783+
auto expected_layout1 = LayoutUtil::MakeLayout({0});
784+
auto first_outfeed_instr = root->called_computations()[0]->root_instruction();
785+
auto second_outfeed_instr =
786+
root->called_computations()[1]->root_instruction();
787+
EXPECT_TRUE(LayoutUtil::Equal(
788+
first_outfeed_instr->outfeed_shape().tuple_shapes(0).layout(),
789+
expected_layout0));
790+
EXPECT_TRUE(LayoutUtil::Equal(
791+
first_outfeed_instr->outfeed_shape().tuple_shapes(1).layout(),
792+
expected_layout1));
793+
EXPECT_TRUE(LayoutUtil::Equal(
794+
second_outfeed_instr->outfeed_shape().tuple_shapes(0).layout(),
795+
expected_layout0));
796+
EXPECT_TRUE(LayoutUtil::Equal(
797+
second_outfeed_instr->outfeed_shape().tuple_shapes(1).layout(),
798+
expected_layout1));
773799
}
774800

775801
TEST_F(SpmdPartitioningTest, ReduceWindowReplicatedInput) {

0 commit comments

Comments
 (0)