1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/compiler/xla/service/batchnorm_expander.h"
17
18#include <memory>
19#include <utility>
20
21#include "tensorflow/compiler/xla/layout_util.h"
22#include "tensorflow/compiler/xla/literal_util.h"
23#include "tensorflow/compiler/xla/ptr_util.h"
24#include "tensorflow/compiler/xla/service/hlo_computation.h"
25#include "tensorflow/compiler/xla/service/hlo_instruction.h"
26#include "tensorflow/compiler/xla/service/hlo_matchers.h"
27#include "tensorflow/compiler/xla/service/hlo_opcode.h"
28#include "tensorflow/compiler/xla/service/hlo_pass_fix.h"
29#include "tensorflow/compiler/xla/shape_util.h"
30#include "tensorflow/compiler/xla/test.h"
31#include "tensorflow/compiler/xla/tests/hlo_test_base.h"
32#include "tensorflow/compiler/xla/types.h"
33#include "tensorflow/compiler/xla/xla_data.pb.h"
34#include "tensorflow/core/lib/strings/str_util.h"
35
36namespace xla {
37namespace {
38
39using BatchNormExpanderTest = HloTestBase;
40
41// Test that we expand BatchNormTraining.
42TEST_F(BatchNormExpanderTest, BatchNormTraining) {
43  Shape input_shape = ShapeUtil::MakeShape(F32, {2, 2, 2, 2});
44  Shape scale_shape = ShapeUtil::MakeShape(F32, {2});
45  Shape offset_shape = ShapeUtil::MakeShape(F32, {2});
46
47  HloComputation::Builder builder(TestName());
48  HloInstruction* param0 = builder.AddInstruction(
49      HloInstruction::CreateParameter(0, input_shape, "activiation"));
50
51  HloInstruction* param1 = builder.AddInstruction(
52      HloInstruction::CreateParameter(1, scale_shape, "scale"));
53
54  HloInstruction* param2 = builder.AddInstruction(
55      HloInstruction::CreateParameter(2, offset_shape, "offset"));
56
57  builder.AddInstruction(HloInstruction::CreateBatchNormTraining(
58      ShapeUtil::MakeTupleShape({input_shape, scale_shape, offset_shape}),
59      param0, param1, param2,
60      /*epsilon=*/0.001, /*feature_index=*/3));
61
62  auto module = CreateNewModule();
63  auto computation = module->AddEntryComputation(builder.Build());
64  HloInstruction* root = computation->root_instruction();
65  EXPECT_EQ(root->opcode(), HloOpcode::kBatchNormTraining);
66  BatchNormExpander rewriter(/*rewrite_training_op=*/true,
67                             /*rewrite_inference_op=*/true,
68                             /*rewrite_grad_op=*/true);
69  ASSERT_TRUE(rewriter.Run(module.get()).ValueOrDie());
70  root = computation->root_instruction();
71  // Make sure this operation is expanded.
72  EXPECT_EQ(root->opcode(), HloOpcode::kTuple);
73}
74
75// Test that we expand BatchNormGrad.
76TEST_F(BatchNormExpanderTest, BatchNormGrad) {
77  Shape input_shape = ShapeUtil::MakeShape(F32, {2, 2, 2, 2});
78  Shape scale_shape = ShapeUtil::MakeShape(F32, {2});
79  Shape mean_shape = ShapeUtil::MakeShape(F32, {2});
80  Shape var_shape = ShapeUtil::MakeShape(F32, {2});
81  Shape grad_output_shape = ShapeUtil::MakeShape(F32, {2, 2, 2, 2});
82
83  HloComputation::Builder builder(TestName());
84  HloInstruction* param0 = builder.AddInstruction(
85      HloInstruction::CreateParameter(0, input_shape, "activation"));
86
87  HloInstruction* param1 = builder.AddInstruction(
88      HloInstruction::CreateParameter(1, scale_shape, "scale"));
89
90  HloInstruction* param2 = builder.AddInstruction(
91      HloInstruction::CreateParameter(2, mean_shape, "mean"));
92
93  HloInstruction* param3 = builder.AddInstruction(
94      HloInstruction::CreateParameter(3, var_shape, "var"));
95
96  HloInstruction* param4 = builder.AddInstruction(
97      HloInstruction::CreateParameter(4, grad_output_shape, "grad_output"));
98
99  builder.AddInstruction(HloInstruction::CreateBatchNormGrad(
100      ShapeUtil::MakeTupleShape({input_shape, scale_shape, mean_shape}), param0,
101      param1, param2, param3, param4,
102      /*epsilon=*/0.001, /*feature_index=*/3));
103
104  auto module = CreateNewModule();
105  auto computation = module->AddEntryComputation(builder.Build());
106  HloInstruction* root = computation->root_instruction();
107  EXPECT_EQ(root->opcode(), HloOpcode::kBatchNormGrad);
108  BatchNormExpander rewriter(/*rewrite_training_op=*/true,
109                             /*rewrite_inference_op=*/true,
110                             /*rewrite_grad_op=*/true);
111  ASSERT_TRUE(rewriter.Run(module.get()).ValueOrDie());
112  root = computation->root_instruction();
113  // Make sure this operation is expanded.
114  EXPECT_EQ(root->opcode(), HloOpcode::kTuple);
115}
116
117}  // namespace
118}  // namespace xla
119