1 /* Copyright 2019 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/reduction_dimension_grouper.h"
17
18 #include <optional>
19 #include <utility>
20
21 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
22 #include "tensorflow/compiler/xla/service/hlo_parser.h"
23 #include "tensorflow/compiler/xla/tests/filecheck.h"
24 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
25 #include "tensorflow/core/lib/core/status_test_util.h"
26 #include "tensorflow/core/platform/test.h"
27
28 namespace xla {
29
30 namespace {
31
32 class ReductionDimensionGrouperTest : public HloTestBase {
33 public:
CheckDimensionGrouper(absl::string_view hlo,std::optional<absl::string_view> expected)34 void CheckDimensionGrouper(absl::string_view hlo,
35 std::optional<absl::string_view> expected) {
36 RunAndFilecheckHloRewrite(hlo, gpu::ReductionDimensionGrouper{}, expected);
37 }
38 };
39
TEST_F(ReductionDimensionGrouperTest,ReductionWithGrouping)40 TEST_F(ReductionDimensionGrouperTest, ReductionWithGrouping) {
41 const char* hlo = R"(
42 HloModule ReductionWithGrouping
43
44 add {
45 accum = f32[] parameter(0)
46 op = f32[] parameter(1)
47 ROOT out = f32[] add(accum, op)
48 }
49
50 ENTRY main {
51 input = f32[100,10,32,3]{3,2,1,0} parameter(0)
52 zero = f32[] constant(0)
53
54 ROOT out = f32[100,10]{0,1} reduce(input, zero), dimensions={2,3}, to_apply=add
55 }
56 )";
57
58 CheckDimensionGrouper(hlo,
59 R"(
60 // CHECK: [[input_0:%[^ ]+]] = f32[100,10,32,3]{3,2,1,0} parameter(0)
61 // CHECK: [[bitcast_1:%[^ ]+]] = f32[100,10,96]{2,1,0} bitcast([[input_0]])
62 // CHECK: ROOT [[out_1_2:%[^ ]+]] = f32[100,10]{0,1} reduce([[bitcast_1]], [[zero_3:%[^ ]+]]), dimensions={2}, to_apply=[[add_4:%[^ ]+]]
63 )");
64 }
65
TEST_F(ReductionDimensionGrouperTest,ReductionWithGroupingVariadic)66 TEST_F(ReductionDimensionGrouperTest, ReductionWithGroupingVariadic) {
67 const char* hlo = R"(
68 HloModule ReductionWithGrouping
69
70 argmax {
71 running_max = f32[] parameter(0)
72 running_max_idx = u32[] parameter(1)
73 current_value = f32[] parameter(2)
74 current_value_idx = u32[] parameter(3)
75
76 current = (f32[], u32[]) tuple(running_max, running_max_idx)
77 potential = (f32[], u32[]) tuple(current_value, current_value_idx)
78
79 cmp_code = pred[] compare(current_value, running_max), direction=GT
80
81 new_max = f32[] select(cmp_code, current_value, running_max)
82 new_idx = u32[] select(cmp_code, current_value_idx, running_max_idx)
83
84 ROOT out = (f32[], u32[]) tuple(new_max, new_idx)
85 }
86
87 ENTRY main {
88 input = f32[100,10,32,3]{3,2,1,0} parameter(0)
89 idxs = u32[100,10,32,3]{3,2,1,0} parameter(1)
90 zero = f32[] constant(0)
91 zero_idx = u32[] constant(0)
92
93 ROOT out = (f32[100,10]{1,0}, u32[100,10]{1,0}) reduce(input, idxs, zero, zero_idx), dimensions={2,3}, to_apply=argmax
94 }
95 )";
96
97 CheckDimensionGrouper(hlo, R"(
98 // CHECK: [[input_0:%[^ ]+]] = f32[100,10,32,3]{3,2,1,0} parameter(0)
99 // CHECK: [[bitcast_1:%[^ ]+]] = f32[100,10,96]{2,1,0} bitcast([[input_0]])
100 // CHECK: [[idxs_2:%[^ ]+]] = u32[100,10,32,3]{3,2,1,0} parameter(1)
101 // CHECK: [[bitcast_1_3:%[^ ]+]] = u32[100,10,96]{2,1,0} bitcast([[idxs_2]])
102 // CHECK: ROOT [[out_1_4:%[^ ]+]] = (f32[100,10]{1,0}, u32[100,10]{1,0}) reduce([[bitcast_1]], [[bitcast_1_3]], [[zero_5:%[^ ]+]], [[zero_idx_6:%[^ ]+]]), dimensions={2}, to_apply=[[argmax_7:%[^ ]+]]
103 )");
104 }
105
106 } // namespace
107 } // namespace xla
108