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/execution_options_util.h"
17 #include "tensorflow/compiler/xla/service/hlo_parser.h"
18 #include "tensorflow/compiler/xla/status_macros.h"
19 #include "tensorflow/compiler/xla/test.h"
20 #include "tensorflow/compiler/xla/tests/client_library_test_base.h"
21 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
22 #include "tensorflow/compiler/xla/tests/test_macros.h"
23 
24 namespace xla {
25 namespace {
26 
27 using absl::nullopt;
28 
29 class ElementalIrEmitterExecutionTest : public HloTestBase {
30  protected:
RunTest(const string & hlo_text,absl::Span<Literal * const> args)31   void RunTest(const string& hlo_text, absl::Span<Literal* const> args) {
32     HloModuleConfig config;
33     config.set_debug_options(GetDebugOptionsForTest());
34     TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
35                             ParseAndReturnVerifiedModule(hlo_text, config));
36     EXPECT_TRUE(RunAndCompareNoHloPasses(std::move(module), args, nullopt));
37   }
38 };
39 
XLA_TEST_F(ElementalIrEmitterExecutionTest,DotFusion)40 XLA_TEST_F(ElementalIrEmitterExecutionTest, DotFusion) {
41   const string hlo_text = R"(
42 HloModule FusedDot
43 
44 fused_computation {
45   arg0 = s32[1,2,1]{2,1,0} parameter(0)
46   reshape.lhs = s32[2,1]{1,0} reshape(arg0)
47   arg1 = s32[1,2,1]{2,1,0} parameter(1)
48   reshape.rhs = s32[2,1]{1,0} reshape(arg1)
49   ROOT dot = s32[1,1]{1,0} dot(reshape.lhs, reshape.rhs), lhs_contracting_dims={0}, rhs_contracting_dims={0}
50 }
51 
52 ENTRY main {
53   entry_arg0 = s32[1,2,1]{2,1,0} parameter(0)
54   entry_arg1 = s32[1,2,1]{2,1,0} parameter(1)
55   ROOT fusion = s32[1,1]{1,0} fusion(entry_arg0, entry_arg1), kind=kLoop, calls=fused_computation
56 }
57 )";
58 
59   Literal lhs = LiteralUtil::CreateR3<int32>({{{1}, {2}}});
60   Literal rhs = LiteralUtil::CreateR3<int32>({{{3}, {4}}});
61   RunTest(hlo_text, {&lhs, &rhs});
62 }
63 
XLA_TEST_F(ElementalIrEmitterExecutionTest,BatchDot)64 XLA_TEST_F(ElementalIrEmitterExecutionTest, BatchDot) {
65   const char* hlo_text = R"(
66 HloModule BatchDot
67 
68 fused_computation.1 {
69   param_0 = f64[1,1,8]{2,1,0} parameter(0)
70   r.1 = f64[2,4]{1,0} reshape(param_0)
71   param_1 = f64[1,2,2,2,1]{4,3,2,1,0} parameter(1)
72   r.2 = f64[2,4,1]{2,1,0} reshape(param_1)
73   ROOT dot = f64[2,1]{1,0} dot(r.1, r.2), lhs_batch_dims={0},
74                                           lhs_contracting_dims={1},
75                                           rhs_batch_dims={0},
76                                           rhs_contracting_dims={1}
77 }
78 
79 ENTRY resampler_Resampler.49 {
80   p0 = f64[1,1,8]{2,1,0} parameter(0)
81   p1 = f64[1,2,2,2,1]{4,3,2,1,0} parameter(1)
82   ROOT f = f64[2,1]{1,0} fusion(p0, p1), kind=kLoop, calls=fused_computation.1
83 }
84 )";
85 
86   HloModuleConfig config;
87   auto debug_options = GetDebugOptionsForTest();
88   // Disable the layout assignment pass because it would throw away the layouts
89   // in the fusion computation, but not recreate them.
90   debug_options.add_xla_disable_hlo_passes("layout-assignment");
91   config.set_debug_options(debug_options);
92   TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
93                           ParseAndReturnVerifiedModule(hlo_text, config));
94   EXPECT_TRUE(RunAndCompare(std::move(module), ErrorSpec{4e-3, 4e-3}));
95 }
96 }  // namespace
97 }  // namespace xla
98