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

[XLA:CPU] Fuse reductions in all cases #23933

Merged
merged 1 commit into from
Mar 20, 2025
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
17 changes: 0 additions & 17 deletions xla/service/cpu/cpu_instruction_fusion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,23 +252,6 @@ FusionDecision CpuInstructionFusion::ShouldFuse(HloInstruction* consumer,
}
}

// Don't fuse reductions over the major dimensions. These have an efficient
// lowering that's only implemented for the unfused case.
if (consumer->opcode() == HloOpcode::kReduce &&
!absl::c_linear_search(
consumer->dimensions(),
LayoutUtil::Minor(consumer->operand(0)->shape().layout(), 0))) {
return FusionDecision::Forbid(
"Not fusing reductions over major dimensions");
}
if (producer->opcode() == HloOpcode::kReduce &&
!absl::c_linear_search(
producer->dimensions(),
LayoutUtil::Minor(producer->operand(0)->shape().layout(), 0))) {
return FusionDecision::Forbid(
"Not fusing reductions over major dimensions");
}

if (consumer->IsLoopFusion()) {
VLOG(2) << "Fusing: consumer is a fusion node.";
return FusionDecision::Allow();
Expand Down
7 changes: 3 additions & 4 deletions xla/service/cpu/cpu_instruction_fusion_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ INSTANTIATE_TEST_SUITE_P(GatherLoopFusionTestInstantiation,
::testing::ValuesIn(GetGatherLoopFusionTestSpecs()),
GatherLoopFusionTestSpec::Name);

TEST_F(InstructionFusionTest, NoFuseReduceMajor) {
TEST_F(InstructionFusionTest, FuseReduceMajor) {
absl::string_view module_string = R"(
HloModule module

Expand All @@ -904,9 +904,8 @@ ENTRY main {
ParseAndReturnVerifiedModule(module_string));
TF_ASSERT_OK_AND_ASSIGN(bool fused_something,
CpuInstructionFusion().Run(module.get()));
EXPECT_FALSE(fused_something);
EXPECT_THAT(module->entry_computation()->root_instruction(),
Not(op::Fusion()));
EXPECT_TRUE(fused_something);
EXPECT_THAT(module->entry_computation()->root_instruction(), op::Fusion());
}

TEST_F(InstructionFusionTest, FuseReduceMinor) {
Expand Down
20 changes: 12 additions & 8 deletions xla/service/cpu/tests/cpu_fusion_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,19 @@ TEST_F(CpuFusionTest, ElementwiseOpChainWithNonfusibleInstruction) {
HloInstruction::CreateParameter(1, r0f32, "y"))));
auto add_f32 = module->AddEmbeddedComputation(embedded_builder.Build());

Window window;
WindowDimension* window_dimension = window.add_dimensions();
window_dimension->set_size(1);
window_dimension->set_stride(1);
window_dimension->set_base_dilation(1);
window_dimension->set_window_dilation(1);
// This is a nop reduction.
auto reduce = builder.AddInstruction(HloInstruction::CreateReduce(
cshape,
builder.AddInstruction(HloInstruction::CreateReshape(
ShapeUtil::MakeShape(F32, {1, 6}), concatenate)),
auto reduce = builder.AddInstruction(HloInstruction::CreateReduceWindow(
cshape, concatenate,
/*init_value=*/
builder.AddInstruction(
HloInstruction::CreateConstant(LiteralUtil::CreateR0<float>(0))),
/*dimensions_to_reduce=*/{0}, add_f32));
window, add_f32));

auto exp = builder.AddInstruction(
HloInstruction::CreateUnary(cshape, HloOpcode::kExp, reduce));
Expand Down Expand Up @@ -208,11 +212,11 @@ TEST_F(CpuFusionTest, ElementwiseOpChainWithNonfusibleInstruction) {

auto fusion_instruction2 = reduce->operand(0);
EXPECT_EQ(HloOpcode::kFusion, fusion_instruction1->opcode());
EXPECT_EQ(HloOpcode::kReshape,
EXPECT_EQ(HloOpcode::kConcatenate,
fusion_instruction2->fused_expression_root()->opcode());
// There should be 5 fused instructions in the second fusion instruction: 1
// parameter, negate, ceil, concat, and reshape.
EXPECT_EQ(5, fusion_instruction2->fused_instruction_count())
// parameter, negate, ceil and concat.
EXPECT_EQ(4, fusion_instruction2->fused_instruction_count())
<< fusion_instruction2->fused_instructions_computation()->ToString();

// Compile and execute the computation.
Expand Down
Loading