Skip to content

Commit

Permalink
[hlo_evaluator] Don't create blank, zero-populated Literals for tuples
Browse files Browse the repository at this point in the history
When evaluating Tuple(), create a blank Literal for the destination that is populated with
kUndetermined.

We already copy in all known leaves, so all this does is avoid us creating blank leaf Pieces,
which is incredibly important when dealing with large programs pre-sharding. Also note that
we'd instantaneously throw the blank pieces all away...

PiperOrigin-RevId: 571532410
  • Loading branch information
tensorflower-gardener committed Oct 7, 2023
1 parent 78de18d commit 333145d
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions third_party/xla/xla/hlo/evaluator/hlo_evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1582,15 +1582,28 @@ Status HloEvaluator::HandleTuple(const HloInstruction* tuple) {
}
}

// Inline part of LiteralUtil::MakeTuple() that avoids creating the leaf
// buffers; these buffers can be extremely large.
std::vector<const Shape*> element_shapes;
element_shapes.reserve(operand_literals.size());
for (const auto* element : operand_literals) {
element_shapes.push_back(&element->shape());
}
Literal new_result = Literal::CreateFromShapeWithUndeterminedLeafArrays(
ShapeUtil::MakeTupleShapeWithPtrs(element_shapes));
for (int i = 0, end = operand_literals.size(); i < end; ++i) {
TF_RETURN_IF_ERROR(
new_result.CopyFrom(*operand_literals[i], /*dest_shape_index=*/{i}));
}

if (evaluated_.contains(tuple)) {
Literal new_result = LiteralUtil::MakeTuple(operand_literals);
CHECK(new_result.IsDetermined(visitor_shape_index_));
TF_RETURN_IF_ERROR(
evaluated_[tuple].CopyFrom(new_result,
evaluated_[tuple].CopyFrom(std::move(new_result),
/*dest_shape_index=*/visitor_shape_index_,
/*src_shape_index=*/visitor_shape_index_));
} else {
evaluated_[tuple] = LiteralUtil::MakeTuple(operand_literals);
evaluated_[tuple] = std::move(new_result);
}
return OkStatus();
}
Expand Down

0 comments on commit 333145d

Please sign in to comment.