aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/algebraic_simplifier.cc
diff options
context:
space:
mode:
Diffstat (limited to 'tensorflow/compiler/xla/service/algebraic_simplifier.cc')
-rw-r--r--tensorflow/compiler/xla/service/algebraic_simplifier.cc52
1 files changed, 52 insertions, 0 deletions
diff --git a/tensorflow/compiler/xla/service/algebraic_simplifier.cc b/tensorflow/compiler/xla/service/algebraic_simplifier.cc
index af7728da54..26a8a67601 100644
--- a/tensorflow/compiler/xla/service/algebraic_simplifier.cc
+++ b/tensorflow/compiler/xla/service/algebraic_simplifier.cc
@@ -1156,6 +1156,19 @@ Status AlgebraicSimplifierVisitor::HandleMultiply(HloInstruction* multiply) {
return Status::OK();
}
+ // 0*A => 0. Only applies for integral types for correct NaN-handling.
+ if (IsAll(lhs, 0) &&
+ primitive_util::IsIntegralType(multiply->shape().element_type()) &&
+ ReplaceInstructionIfSameShape(multiply, lhs)) {
+ return Status::OK();
+ }
+ // A*0 => 0
+ if (IsAll(rhs, 0) &&
+ primitive_util::IsIntegralType(multiply->shape().element_type()) &&
+ ReplaceInstructionIfSameShape(multiply, rhs)) {
+ return Status::OK();
+ }
+
// exp(A) * exp(B) => exp(A+B)
if (Match(multiply, m::Multiply(m::Exp(m::Op(&lhs)), m::Exp(m::Op(&rhs))))) {
auto add = computation_->AddInstruction(HloInstruction::CreateBinary(
@@ -1731,6 +1744,25 @@ Status AlgebraicSimplifierVisitor::HandleSlice(HloInstruction* slice) {
if (ReplaceInstructionIfSameShape(slice, slice->mutable_operand(0))) {
return Status::OK();
}
+
+ auto is_unstrided_slice = [](const HloInstruction* hlo) {
+ return c_all_of(hlo->slice_strides(),
+ [](int64 stride) { return stride == 1; });
+ };
+ if (slice->operand(0)->opcode() == HloOpcode::kSlice &&
+ is_unstrided_slice(slice) && is_unstrided_slice(slice->operand(0))) {
+ HloInstruction* operand_slice = slice->mutable_operand(0);
+ std::vector<int64> new_slice_starts = slice->slice_starts();
+ std::vector<int64> new_slice_limits = slice->slice_limits();
+ for (int64 i = 0; i < new_slice_starts.size(); ++i) {
+ new_slice_starts[i] += operand_slice->slice_starts(i);
+ new_slice_limits[i] += operand_slice->slice_starts(i);
+ }
+ return ReplaceWithNewInstruction(
+ slice, HloInstruction::CreateSlice(
+ slice->shape(), operand_slice->mutable_operand(0),
+ new_slice_starts, new_slice_limits, slice->slice_strides()));
+ }
return Status::OK();
}
@@ -1891,6 +1923,26 @@ Status AlgebraicSimplifierVisitor::HandleReduce(HloInstruction* reduce) {
new_reduce_dimensions, function));
}
}
+ // Convert Reduce(concat({a,b,...})) to
+ // map(reduce(a),map(reduce(b),...,))
+ //
+ // This should make fusion easier or use less memory bandwidth in the unfused
+ // case.
+ if (arg->opcode() == HloOpcode::kConcatenate &&
+ c_linear_search(reduce->dimensions(), arg->concatenate_dimension())) {
+ HloInstruction* old_reduce = nullptr;
+ for (HloInstruction* operand : arg->operands()) {
+ HloInstruction* new_reduce = computation_->AddInstruction(
+ HloInstruction::CreateReduce(reduce->shape(), operand, init_value,
+ reduce->dimensions(), function));
+ if (old_reduce != nullptr) {
+ new_reduce = computation_->AddInstruction(HloInstruction::CreateMap(
+ reduce->shape(), {old_reduce, new_reduce}, function));
+ }
+ old_reduce = new_reduce;
+ }
+ return ReplaceInstruction(reduce, old_reduce);
+ }
return Status::OK();
}