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/batch_dot_simplification.h"
17
18 #include "absl/algorithm/container.h"
19 #include "tensorflow/compiler/xla/service/hlo_computation.h"
20 #include "tensorflow/compiler/xla/service/hlo_creation_utils.h"
21
22 namespace xla {
23 StatusOr<bool>
ElideDegenerateBatchDimensionFromBatchDot(HloInstruction * batch_dot)24 BatchDotSimplification::ElideDegenerateBatchDimensionFromBatchDot(
25 HloInstruction* batch_dot) {
26 const DotDimensionNumbers& dim_numbers = batch_dot->dot_dimension_numbers();
27 HloInstruction *lhs = batch_dot->mutable_operand(0),
28 *rhs = batch_dot->mutable_operand(1);
29 const Shape& lhs_shape = lhs->shape();
30
31 // A dot with no contracting dims will be rewritten into a multiply by
32 // AlgebraicSimplifier. Dots with multiple contracting dims are currently
33 // unsupported.
34 if (dim_numbers.lhs_contracting_dimensions_size() != 1) {
35 return false;
36 }
37
38 std::vector<int64> degenerate_dims;
39 for (int64 batch_dim : dim_numbers.lhs_batch_dimensions()) {
40 if (lhs_shape.dimensions(batch_dim) == 1) {
41 degenerate_dims.push_back(batch_dim);
42 }
43 }
44
45 if (degenerate_dims.empty()) {
46 return false;
47 }
48
49 TF_ASSIGN_OR_RETURN(HloInstruction * new_lhs,
50 ElideDegenerateDims(lhs, degenerate_dims));
51 TF_ASSIGN_OR_RETURN(HloInstruction * new_rhs,
52 ElideDegenerateDims(rhs, degenerate_dims));
53
54 DotDimensionNumbers new_dim_numbers = dim_numbers;
55 new_dim_numbers.clear_lhs_batch_dimensions();
56 new_dim_numbers.clear_rhs_batch_dimensions();
57
58 for (int64 i = 0, e = dim_numbers.lhs_batch_dimensions_size() -
59 degenerate_dims.size();
60 i < e; i++) {
61 new_dim_numbers.add_lhs_batch_dimensions(i);
62 new_dim_numbers.add_rhs_batch_dimensions(i);
63 }
64
65 new_dim_numbers.set_lhs_contracting_dimensions(
66 0,
67 new_dim_numbers.lhs_contracting_dimensions(0) - degenerate_dims.size());
68 new_dim_numbers.set_rhs_contracting_dimensions(
69 0,
70 new_dim_numbers.rhs_contracting_dimensions(0) - degenerate_dims.size());
71
72 TF_ASSIGN_OR_RETURN(HloInstruction * new_dot,
73 MakeDotHlo(new_lhs, new_rhs, new_dim_numbers,
74 batch_dot->precision_config()));
75
76 TF_ASSIGN_OR_RETURN(HloInstruction * new_dot_reshaped,
77 MakeReshapeHlo(batch_dot->shape(), new_dot));
78
79 VLOG(2) << "Replaced " << batch_dot->ToString() << " with "
80 << new_dot->ToString();
81
82 TF_RETURN_IF_ERROR(
83 batch_dot->parent()->ReplaceInstruction(batch_dot, new_dot_reshaped));
84
85 return true;
86 }
87
name() const88 absl::string_view BatchDotSimplification::name() const {
89 return "batch-dot-simplification";
90 }
91
Run(HloModule * module)92 StatusOr<bool> BatchDotSimplification::Run(HloModule* module) {
93 bool changed = false;
94 std::vector<HloInstruction*> dot_instrs;
95 for (HloComputation* computation : module->MakeNonfusionComputations()) {
96 absl::c_copy_if(computation->instructions(), std::back_inserter(dot_instrs),
97 [](HloInstruction* instr) {
98 return instr->opcode() == HloOpcode::kDot;
99 });
100 }
101 for (HloInstruction* dot_instr : dot_instrs) {
102 TF_ASSIGN_OR_RETURN(bool elided_batch_dim_from_one,
103 ElideDegenerateBatchDimensionFromBatchDot(dot_instr));
104 changed |= elided_batch_dim_from_one;
105 }
106 return changed;
107 }
108 } // namespace xla
109