1 /* Copyright 2020 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/operand_upcaster.h"
17
18 #include "tensorflow/compiler/xla/service/shape_inference.h"
19
20 namespace xla {
21 namespace {
22
MaybeInferShape(const HloInstruction * instruction)23 StatusOr<std::optional<Shape>> MaybeInferShape(
24 const HloInstruction* instruction) {
25 switch (instruction->opcode()) {
26 case HloOpcode::kDot:
27 return ShapeInference::InferDotOpShape(
28 instruction->operand(0)->shape(), instruction->operand(1)->shape(),
29 instruction->dot_dimension_numbers(),
30 /*preferred_element_type=*/std::nullopt);
31 case HloOpcode::kConvolution:
32 return ShapeInference::InferConvolveShape(
33 instruction->operand(0)->shape(), instruction->operand(1)->shape(),
34 instruction->feature_group_count(), instruction->batch_group_count(),
35 instruction->window(), instruction->convolution_dimension_numbers(),
36 /*preferred_element_type=*/std::nullopt);
37 default:
38 return std::optional<Shape>(std::nullopt);
39 }
40 }
41
42 } // namespace
43
InstructionMatchesPattern(HloInstruction * instruction)44 bool OperandUpcaster::InstructionMatchesPattern(HloInstruction* instruction) {
45 auto status_or_inferred_shape = MaybeInferShape(instruction);
46 if (!status_or_inferred_shape.ok() ||
47 !status_or_inferred_shape->has_value()) {
48 return false;
49 }
50 const Shape& inferred_shape = status_or_inferred_shape.ValueOrDie().value();
51 if (inferred_shape.element_type() == instruction->shape().element_type() &&
52 absl::c_all_of(instruction->operands(),
53 [&](const HloInstruction* operand) {
54 return operand->shape().element_type() ==
55 inferred_shape.element_type();
56 })) {
57 return false;
58 }
59 return ShapeUtil::ElementCanUpcast(inferred_shape, instruction->shape());
60 }
61
ExpandInstruction(HloInstruction * instruction)62 StatusOr<HloInstruction*> OperandUpcaster::ExpandInstruction(
63 HloInstruction* instruction) {
64 auto* computation = instruction->parent();
65 auto type = instruction->shape().element_type();
66 for (int i = 0; i < instruction->operand_count(); ++i) {
67 auto* operand = instruction->mutable_operand(i);
68 if (operand->shape().element_type() == type) {
69 continue;
70 }
71 auto upcast_shape = operand->shape();
72 upcast_shape.set_element_type(type);
73 auto* convert_inst = computation->AddInstruction(
74 HloInstruction::CreateConvert(upcast_shape, operand));
75 TF_RETURN_IF_ERROR(
76 instruction->ReplaceOperandWithDifferentShape(i, convert_inst));
77 }
78 return nullptr;
79 }
80
81 } // namespace xla
82