• 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 "tensorflow/compiler/xla/service/gpu/variadic_op_splitter.h"
17 
18 #include "tensorflow/compiler/xla/literal_util.h"
19 #include "tensorflow/compiler/xla/service/gpu/ir_emission_utils.h"
20 #include "tensorflow/compiler/xla/service/hlo_computation.h"
21 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
22 #include "tensorflow/compiler/xla/service/hlo_module.h"
23 #include "tensorflow/compiler/xla/service/hlo_parser.h"
24 #include "tensorflow/compiler/xla/service/pattern_matcher.h"
25 #include "tensorflow/compiler/xla/shape_util.h"
26 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
27 #include "tensorflow/compiler/xla/util.h"
28 #include "tensorflow/compiler/xla/xla_data.pb.h"
29 
30 namespace xla {
31 namespace gpu {
32 namespace {
33 using match::Concatenate;
34 
35 class VariadicOpSplitterTest : public HloTestBase {};
36 
TEST_F(VariadicOpSplitterTest,DontSplit)37 TEST_F(VariadicOpSplitterTest, DontSplit) {
38   auto module = ParseAndReturnVerifiedModule(R"(
39   HloModule TestModule
40 
41   ENTRY TestComputation {
42     p0 = f16[30,41] parameter(0)
43     p1 = f16[30,41] parameter(1)
44     ROOT result = f16[60, 41] concatenate(p0, p1), dimensions={0}
45   })")
46                     .ValueOrDie();
47   EXPECT_FALSE(VariadicOpSplitter().Run(module.get()).ValueOrDie());
48 }
49 
TEST_F(VariadicOpSplitterTest,SplitInto2)50 TEST_F(VariadicOpSplitterTest, SplitInto2) {
51   auto builder = HloComputation::Builder(TestName());
52   auto operand = builder.AddInstruction(
53       HloInstruction::CreateConstant(LiteralUtil::CreateR1<int32_t>({42})));
54   std::vector<HloInstruction*> concat_operands(255, operand);
55   builder.AddInstruction(HloInstruction::CreateConcatenate(
56       ShapeUtil::MakeShape(S32, {255}), concat_operands, 0));
57   auto module = CreateNewVerifiedModule();
58   auto entry_computation = module->AddEntryComputation(builder.Build());
59   EXPECT_TRUE(VariadicOpSplitter().Run(module.get()).ValueOrDie());
60   EXPECT_TRUE(Match(entry_computation->root_instruction(),
61                     Concatenate().WithNumOperands(128).WithOperand(
62                         0, Concatenate().WithNumOperands(128))));
63 }
64 
TEST_F(VariadicOpSplitterTest,SplitInto3)65 TEST_F(VariadicOpSplitterTest, SplitInto3) {
66   auto builder = HloComputation::Builder(TestName());
67   auto operand = builder.AddInstruction(
68       HloInstruction::CreateConstant(LiteralUtil::CreateR1<int32_t>({42})));
69   std::vector<HloInstruction*> concat_operands(256, operand);
70   builder.AddInstruction(HloInstruction::CreateConcatenate(
71       ShapeUtil::MakeShape(S32, {256}), concat_operands, 0));
72   auto module = CreateNewVerifiedModule();
73   auto entry_computation = module->AddEntryComputation(builder.Build());
74   EXPECT_TRUE(VariadicOpSplitter().Run(module.get()).ValueOrDie());
75   EXPECT_TRUE(Match(entry_computation->root_instruction(),
76                     Concatenate(Concatenate().WithNumOperands(128),
77                                 Concatenate().WithNumOperands(128))));
78 }
79 
80 }  // namespace
81 }  // namespace gpu
82 }  // namespace xla
83