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