• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include <utility>
17 
18 #include "tensorflow/compiler/xla/service/gpu/gpu_fusible.h"
19 #include "tensorflow/compiler/xla/service/gpu/instruction_fusion.h"
20 #include "tensorflow/compiler/xla/service/gpu/tests/gpu_codegen_test.h"
21 #include "tensorflow/compiler/xla/service/hlo_module_config.h"
22 #include "tensorflow/compiler/xla/service/hlo_parser.h"
23 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
24 #include "tensorflow/core/platform/test.h"
25 
26 namespace xla {
27 namespace gpu {
28 namespace {
29 
30 class GpuFusionTest : public GpuCodegenTest {};
31 
TEST_F(GpuFusionTest,FusedReshape)32 TEST_F(GpuFusionTest, FusedReshape) {
33   const char* hlo_text = R"(
34     HloModule test_module
35 
36     fused_computation {
37       p0.param_0 = f32[4,1,1]{2,1,0} parameter(0)
38       p1.param_1 = f32[4,1]{1,0} parameter(1)
39       reshape = f32[4,1]{1,0} reshape(p0.param_0)
40       ROOT add = f32[4,1] add(reshape, p1.param_1)
41     }
42 
43     ENTRY BroadcastIntoAdd {
44       p0 = f32[4,1,1]{2,1,0} parameter(0)
45       p1 = f32[4,1]{1,0} parameter(1)
46       ROOT fusion = f32[4,1]{1,0} fusion(p0, p1), kind=kLoop,
47                                                   calls=fused_computation
48     }
49 )";
50 
51   CompileAndVerifyIr(hlo_text,
52                      R"(
53 ; CHECK-LABEL: @fusion
54 ; CHECK: fadd
55 ; CHECK: }
56       )");
57 }
58 
59 // Check that we limit the number of operands to fusions we create.
TEST_F(GpuFusionTest,FusedBiggerThenThresholdButDoNotChangeTheFusionl)60 TEST_F(GpuFusionTest, FusedBiggerThenThresholdButDoNotChangeTheFusionl) {
61   constexpr int64_t kNumParams = MaxOperandsAndOutputsPerFusion() + 1;
62 
63   // Compute
64   //   p0 + p1 + p2 + ... + pn,
65   // Use so many parameters that they do not fit into one fusion.
66   auto module = CreateNewVerifiedModule();
67   HloComputation::Builder b(TestName());
68   Shape input_shape = ShapeUtil::MakeShape(F32, {10, 100});
69   Shape slice_shape = ShapeUtil::MakeShape(F32, {10, 2});
70   Shape concat_shape = ShapeUtil::MakeShape(F32, {10, 2 * kNumParams});
71   HloInstruction* input =
72       b.AddInstruction(HloInstruction::CreateParameter(0, input_shape, "p"));
73 
74   std::vector<HloInstruction*> slice_params;
75   for (int64_t i = 0; i < kNumParams; ++i) {
76     slice_params.push_back(b.AddInstruction(HloInstruction::CreateSlice(
77         slice_shape, input, {0, 0}, {10, 2}, {1, 1})));
78   }
79   b.AddInstruction(
80       HloInstruction::CreateConcatenate(concat_shape, slice_params, 1));
81   module->AddEntryComputation(b.Build());
82   EXPECT_TRUE(GpuInstructionFusion(false).Run(module.get()).ValueOrDie());
83   EXPECT_TRUE(module->entry_computation()->root_instruction()->opcode() ==
84               HloOpcode::kFusion);
85   for (HloInstruction* instr : module->entry_computation()->instructions()) {
86     EXPECT_TRUE(instr->opcode() != HloOpcode::kSlice);
87   }
88 }
89 
90 }  // namespace
91 }  // namespace gpu
92 }  // namespace xla
93