[XLA] Remove Literal::Swap and replace all uses with moves.

This stems from the dark ages when Literal was an unmovable proto. Swap was
supposed to be fast (it moves) but in the conversion to a standalone class Swap
wasn't implemented properly and became a 3-way copy instead of a 3-way move.

All of the users want move anyways, so just remove Swap and use moves on all
call sites.

If actual swapping is needed, std::swap will work just fine for Literal, and
the default implementation is as fast as 3 moves.

PiperOrigin-RevId: 168689138
This commit is contained in:
Benjamin Kramer 2017-09-14 08:04:42 -07:00 committed by TensorFlower Gardener
parent 6d7c72f19c
commit 0550f50bfd
6 changed files with 8 additions and 17 deletions

View File

@ -75,8 +75,7 @@ Status ExecutorTransferManager::TransferLiteralFromDevice(
executor, source, ShapeUtil::ByteSizeOf(device_shape),
literal->MutableInternalData()));
if (!ShapeUtil::Equal(literal_shape, device_shape)) {
literal->Swap(
literal->Relayout(literal_shape.layout()).get());
*literal = std::move(*literal->Relayout(literal_shape.layout()));
}
TF_RET_CHECK(ShapeUtil::Equal(literal_shape, literal->shape()));
return Status::OK();

View File

@ -126,7 +126,7 @@ Status XlaOpKernelContext::ConstantInputReshaped(
"Error evaluating ", context_->op_kernel().name(), " input ", index,
": ", computed.status().error_message());
}
constant_literal->Swap(computed.ValueOrDie().get());
*constant_literal = std::move(*computed.ValueOrDie());
return Status::OK();
}
@ -195,7 +195,7 @@ Status XlaOpKernelContext::ConstantInputAsInt64Literal(int index,
return Status::OK();
case xla::S64:
out->Swap(&literal);
*out = std::move(literal);
return Status::OK();
default:

View File

@ -662,7 +662,7 @@ string Literal::ToString() const {
std::vector<Shape> shape;
for (auto& tuple_element : elements) {
shape.push_back(tuple_element->shape());
literal->add_tuple_literals()->Swap(tuple_element.get());
*literal->add_tuple_literals() = std::move(*tuple_element);
}
*literal->mutable_shape() = ShapeUtil::MakeTupleShape(shape);
return literal;

View File

@ -166,12 +166,6 @@ class Literal {
const Shape& shape() const { return shape_; }
Shape* mutable_shape() { return &shape_; }
void Swap(Literal* other) {
Literal temp = *this;
*this = *other;
*other = temp;
}
// Creates a new literal of a given rank. To minimize ambiguity (for users
// and the compiler) these CreateR[0-2] methods should explicitly specify the
// native type. For example:

View File

@ -182,9 +182,8 @@ Status CpuTransferManager::TransferLiteralFromOutfeed(
tensorflow::gtl::ArraySlice<int64> dimensions(
tensorflow::bit_cast<const int64*>(literal_shape.dimensions().data()),
literal_shape.dimensions().size());
auto empty =
Literal::CreateFromDimensions(literal_shape.element_type(), dimensions);
literal->Swap(empty.get());
*literal = std::move(*Literal::CreateFromDimensions(
literal_shape.element_type(), dimensions));
TF_ASSIGN_OR_RETURN(Shape received_shape,
TransferArrayBufferFromOutfeed(
executor, literal->MutableInternalData(), size));
@ -235,8 +234,7 @@ Status CpuTransferManager::TransferLiteralFromOutfeed(
for (int64 i = 0; i < literal_shape.tuple_shapes_size(); ++i) {
*elements[i]->mutable_shape() = received_shape.tuple_shapes(i);
}
auto result = Literal::MakeTupleOwned(std::move(elements));
literal->Swap(result.get());
*literal = std::move(*Literal::MakeTupleOwned(std::move(elements)));
TF_RET_CHECK(ShapeUtil::Equal(literal->shape(), literal_shape));
return Status::OK();
}

View File

@ -87,7 +87,7 @@ Status GenericTransferManager::TransferLiteralFromDevice(
executor, source, /*size=*/ShapeUtil::ByteSizeOf(device_shape),
/*destination=*/literal->MutableInternalData()));
if (!ShapeUtil::Equal(literal_shape, device_shape)) {
literal->Swap(literal->Relayout(literal_shape.layout()).get());
*literal = std::move(*literal->Relayout(literal_shape.layout()));
}
TF_RET_CHECK(ShapeUtil::Equal(literal_shape, literal->shape()));
return Status::OK();