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/service/convolution_group_converter.h"
17
18 #include <memory>
19 #include <string>
20
21 #include "tensorflow/compiler/xla/service/hlo_computation.h"
22 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
23 #include "tensorflow/compiler/xla/service/hlo_matchers.h"
24 #include "tensorflow/compiler/xla/service/hlo_opcode.h"
25 #include "tensorflow/compiler/xla/service/hlo_parser.h"
26 #include "tensorflow/compiler/xla/test.h"
27 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
28 #include "tensorflow/compiler/xla/types.h"
29
30 namespace xla {
31 namespace {
32
33 using ConvolutionGroupConverterTest = HloTestBase;
34 namespace op = testing::opcode_matchers;
35
TEST_F(ConvolutionGroupConverterTest,ConvertFeatureGroupCountEqualToInputFeatureDim)36 TEST_F(ConvolutionGroupConverterTest,
37 ConvertFeatureGroupCountEqualToInputFeatureDim) {
38 string hlo_string = R"(HloModule Convolve1D1Window_0_module
39
40 ENTRY %Convolve1D1Window_0.v3 (input: f32[1,2,2], filter: f32[1,1,2]) -> f32[1,2,2] {
41 %input = f32[1,2,2]{2,1,0} parameter(0)
42 %copy = f32[1,2,2]{2,0,1} copy(f32[1,2,2]{2,1,0} %input)
43 %filter = f32[1,1,2]{2,1,0} parameter(1)
44 ROOT %convolution = f32[1,2,2]{2,0,1} convolution(f32[1,2,2]{2,0,1} %copy, f32[1,1,2]{2,1,0} %filter), window={size=1}, dim_labels=b0f_0io->b0f, feature_group_count=2
45 })";
46 TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
47 ParseHloString(hlo_string));
48
49 auto computation = module->entry_computation();
50 HloInstruction* root = computation->root_instruction();
51 EXPECT_EQ(root->opcode(), HloOpcode::kConvolution);
52 ConvolutionGroupConverter converter(nullptr, /*convert_batch_groups_only=*/
53 false);
54 ASSERT_TRUE(converter.Run(module.get()).ValueOrDie());
55 root = computation->root_instruction();
56 // Make sure the convolution is converted to one with feature_group_count = 1.
57 EXPECT_EQ(root->opcode(), HloOpcode::kConvolution);
58 EXPECT_EQ(root->feature_group_count(), 1);
59 // Verify that the filter operand has been replaced.
60 EXPECT_THAT(root->operand(1),
61 op::Select(op::Eq(op::Broadcast(op::Constant()),
62 op::Broadcast(op::Constant())),
63 op::Broadcast(op::Reshape(op::Parameter())),
64 op::Broadcast(op::Constant())));
65 }
66
TEST_F(ConvolutionGroupConverterTest,ConvertFeatureGroupCountDivisorOfInputFeatureDim)67 TEST_F(ConvolutionGroupConverterTest,
68 ConvertFeatureGroupCountDivisorOfInputFeatureDim) {
69 string hlo_string = R"(HloModule Convolve1D1Window_0_module
70
71 ENTRY %Convolve1D1Window_0.v3 (input: f32[1,2,4], filter: f32[1,2,2]) -> f32[1,2,2] {
72 %input = f32[1,2,4]{2,1,0} parameter(0)
73 %copy = f32[1,2,4]{2,0,1} copy(f32[1,2,4]{2,1,0} %input)
74 %filter = f32[1,2,2]{2,1,0} parameter(1)
75 ROOT %convolution = f32[1,2,2]{2,0,1} convolution(f32[1,2,4]{2,0,1} %copy, f32[1,2,2]{2,1,0} %filter), window={size=1}, dim_labels=b0f_0io->b0f, feature_group_count=2
76 })";
77 TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
78 ParseHloString(hlo_string));
79
80 auto computation = module->entry_computation();
81 HloInstruction* root = computation->root_instruction();
82 EXPECT_EQ(root->opcode(), HloOpcode::kConvolution);
83 ConvolutionGroupConverter converter(nullptr, /*convert_batch_groups_only=*/
84 false);
85 ASSERT_TRUE(converter.Run(module.get()).ValueOrDie());
86 root = computation->root_instruction();
87 // Make sure the convolution is replaced with a concatenate.
88 EXPECT_EQ(root->opcode(), HloOpcode::kConcatenate);
89 // And the operands of the concatenate are convolutions, each with a feature
90 // group count = 1.
91 EXPECT_EQ(root->operand(0)->opcode(), HloOpcode::kConvolution);
92 EXPECT_EQ(root->operand(1)->opcode(), HloOpcode::kConvolution);
93 EXPECT_EQ(root->operand(0)->feature_group_count(), 1);
94 EXPECT_EQ(root->operand(1)->feature_group_count(), 1);
95 }
96
TEST_F(ConvolutionGroupConverterTest,ConvertBatchGroupCountEqualToInputBatchDim)97 TEST_F(ConvolutionGroupConverterTest,
98 ConvertBatchGroupCountEqualToInputBatchDim) {
99 string hlo_string = R"(HloModule Convolve1D1Window_0_module
100
101 ENTRY %Convolve1D1Window_0.v3 (input: f32[16,19,19,512]{3,2,1,0}, filter: f32[16,19,19,512]{3,2,1,0}) -> f32[3,3,512,1]{3,2,1,0} {
102 %input = f32[16,19,19,512]{3,2,1,0} parameter(0)
103 %filter = f32[16,19,19,512]{3,2,1,0} parameter(1)
104 ROOT %convolution = f32[3,3,512,1]{3,2,1,0} convolution(f32[16,19,19,512]{3,2,1,0} %input, f32[16,19,19,512]{3,2,1,0} %filter), window={size=19x19 pad=1_1x1_1}, dim_labels=f01b_i01o->01fb, batch_group_count=512
105 })";
106 TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
107 ParseHloString(hlo_string));
108
109 auto computation = module->entry_computation();
110 HloInstruction* root = computation->root_instruction();
111 EXPECT_EQ(root->opcode(), HloOpcode::kConvolution);
112 auto cost_model = [](HloInstruction* conv) { return false; };
113 ConvolutionGroupConverter converter(cost_model, /*convert_batch_groups_only=*/
114 true);
115 ASSERT_TRUE(converter.Run(module.get()).ValueOrDie());
116 root = computation->root_instruction();
117
118 // Verify that the convolution is replaced by a convert.
119 EXPECT_EQ(root->opcode(), HloOpcode::kConvert);
120 // Make sure the convert is being fed by a reduce window.
121 EXPECT_EQ(root->operand(0)->opcode(), HloOpcode::kReduceWindow);
122 }
123
124 } // namespace
125 } // namespace xla
126