xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/tests/reduce_hlo_test.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2017 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 <array>
17 
18 #include "absl/strings/str_cat.h"
19 #include "absl/strings/str_join.h"
20 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
21 #include "tensorflow/compiler/xla/tests/test_macros.h"
22 #include "tensorflow/core/platform/test.h"
23 
24 // Tests the Reduce HLO in ways that can't be done using the ComputationBuilder
25 // API.
26 
27 namespace xla {
28 namespace {
29 
30 struct ReduceLayout {
31   std::array<int64_t, 4> input_minor_to_major;
32   std::array<int64_t, 3> output_minor_to_major;
33 
ToStringxla::__anon6a7cba980111::ReduceLayout34   std::string ToString() const {
35     return absl::StrCat(absl::StrJoin(input_minor_to_major, "x"), "_",
36                         absl::StrJoin(output_minor_to_major, "x"));
37   }
38 };
39 
PrintReduceLayout(::testing::TestParamInfo<ReduceLayout> reduce_layout_param)40 std::string PrintReduceLayout(
41     ::testing::TestParamInfo<ReduceLayout> reduce_layout_param) {
42   return reduce_layout_param.param.ToString();
43 }
44 
PrintTo(const ReduceLayout & reduce_layout,::std::ostream * os)45 void PrintTo(const ReduceLayout& reduce_layout, ::std::ostream* os) {
46   *os << reduce_layout.ToString();
47 }
48 
49 class ReduceWithLayoutTest
50     : public HloTestBase,
51       public ::testing::WithParamInterface<ReduceLayout> {
52  public:
GetParsedModule()53   StatusOr<std::unique_ptr<HloModule>> GetParsedModule() {
54     const char* const hlo_string = R"(
55 HloModule BadReduce
56 
57 Sum {
58   x.1 = f32[] parameter(0)
59   y.1 = f32[] parameter(1)
60   ROOT add.1 = f32[] add(x.1, y.1)
61 }
62 
63 ENTRY reduce.1 {
64   parameter = f32[2,2,2,3]{3,2,1,0} parameter(0)
65   init_value = f32[] constant(0)
66   reduce = f32[2,2,3]{2,1,0} reduce(parameter, init_value), dimensions={1}, to_apply=Sum
67   ROOT copy = f32[2,2,3]{2,1,0} copy(reduce)
68 }
69 )";
70 
71     return ParseAndReturnVerifiedModule(hlo_string);
72   }
73 };
74 
XLA_TEST_P(ReduceWithLayoutTest,Reduce)75 XLA_TEST_P(ReduceWithLayoutTest, Reduce) {
76   TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module, GetParsedModule());
77   HloInstruction* reduce_instruction =
78       module->entry_computation()->root_instruction()->mutable_operand(0);
79   ASSERT_EQ(reduce_instruction->opcode(), HloOpcode::kReduce);
80 
81   const ReduceLayout& reduce_layout = GetParam();
82 
83   Shape* reduce_output_shape = reduce_instruction->mutable_shape();
84   *reduce_output_shape->mutable_layout() =
85       LayoutUtil::MakeLayout(reduce_layout.output_minor_to_major);
86 
87   Shape* reduce_input_shape =
88       reduce_instruction->mutable_operand(0)->mutable_shape();
89   *reduce_input_shape->mutable_layout() =
90       LayoutUtil::MakeLayout(reduce_layout.input_minor_to_major);
91 
92   Literal reduce_input = LiteralUtil::CreateR4<float>(
93       {{ /*i0=0*/
94         {/*i1=0*/
95          {-0.246092796, -0.179497838, -0.161181688},
96          {-0.151643038, -0.240213156, -0.198156}},
97         {/*i1=1*/
98          {-0.14222312, -0.162200093, -0.193907976},
99          {-0.239411, -0.198166847, -0.172471642}}},
100        { /*i0=1*/
101         {/*i1=0*/
102          {-0.22965157, -0.218723893, -0.129257083},
103          {-0.188762426, -0.16123569, -0.181166649}},
104         {/*i1=1*/
105          {-0.241772294, -0.245131493, -0.160247207},
106          {-0.179881215, -0.23383224, -0.121976733}}}});
107 
108   auto reduce_input_relaid =
109       reduce_input.Relayout(reduce_input_shape->layout());
110   EXPECT_TRUE(RunAndCompareNoHloPasses(
111       std::move(module), {&reduce_input_relaid}, ErrorSpec(1e-5)));
112 }
113 
114 INSTANTIATE_TEST_CASE_P(ReduceWithLayoutTest_Instantiation,
115                         ReduceWithLayoutTest,
116                         ::testing::Values(                           //
117                             ReduceLayout{{3, 2, 1, 0}, {0, 1, 2}},   //
118                             ReduceLayout{{3, 2, 1, 0}, {0, 2, 1}},   //
119                             ReduceLayout{{3, 2, 1, 0}, {1, 2, 0}},   //
120                             ReduceLayout{{3, 2, 1, 0}, {1, 0, 2}},   //
121                             ReduceLayout{{3, 2, 1, 0}, {2, 0, 1}},   //
122                             ReduceLayout{{3, 2, 1, 0}, {2, 1, 0}},   //
123                             ReduceLayout{{3, 1, 2, 0}, {1, 2, 0}},   //
124                             ReduceLayout{{1, 2, 3, 0}, {1, 0, 2}},   //
125                             ReduceLayout{{0, 2, 1, 3}, {2, 0, 1}}),  //
126                         PrintReduceLayout);
127 
128 }  // namespace
129 }  // namespace xla
130