aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/packed_literal_reader.cc
diff options
context:
space:
mode:
authorGravatar Mark Heffernan <meheff@google.com>2018-01-05 21:46:44 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-01-05 21:50:47 -0800
commit7d64e124103c8334b7d8b127cd2eff786959d185 (patch)
treea9d5b69f8a81387d26d285eecc1f6b31745cb52b /tensorflow/compiler/xla/packed_literal_reader.cc
parent32d138db751c541e951d1958cac4918214e9644e (diff)
Remove protobuf-compatibility methods from the Literal class.
This CL primarily does two things: (1) Remove the protobuf-compatibility methods (eg, mutable_f32s()) from Literal. These were added to Literal as part of the migration of Literal from a proto to a c++ class. Now that Literal is a proper class, these protobuf methods make it difficult to enforce invariants and expose too much of the class' implementation details. (2) Make shape an immutable property of Literals, and make shape and the data members holding the Literal data coherent by construction. Previously, the shape could be set arbitrarily, and the data members such as f32_ could be arbitrarily sized irrespective of the shape of the literal. The remainder of the CL mostly deals with the fallout. Notable other changes: - Literal is no longer a recursive data structure. To avoid copies when passing a subliteral of a tuple-shaped Literal, a LiteralView class is added which provides a read-only view of an arbitrary subliteral. - Tuple-shaped Literals can no longer be built up incrementally so to avoid copying Literal values during construction, the following methods with move semantics are added: Literal::MoveFrom and Literal::MoveIntoTuple. These methods transfer ownership the underlying buffers enabling, for example, a literal to be moved into an element of a tuple-shaped literal with no data copying. - Replace the internal data structure holding the actual data from a bunch of std::vectors (eg, s32s_, f32s, etc) to a single ShapeTree<char*>. This significantly simplifies accessors and makes improved support of tuple-shaped literals much easier (eg, Literal::Get<>() can now access elements in arbitrary subliterals). Also, Literal is made movable, but not copyable. Otherwise, it is all too easy to accidentally introduce expensive copies of Literals. Literal::Clone is added to handle the case where a copy is needed (Literal::CloneToUnique already exists). PiperOrigin-RevId: 181014890
Diffstat (limited to 'tensorflow/compiler/xla/packed_literal_reader.cc')
-rw-r--r--tensorflow/compiler/xla/packed_literal_reader.cc16
1 files changed, 9 insertions, 7 deletions
diff --git a/tensorflow/compiler/xla/packed_literal_reader.cc b/tensorflow/compiler/xla/packed_literal_reader.cc
index 70e0f5a747..857aae0a79 100644
--- a/tensorflow/compiler/xla/packed_literal_reader.cc
+++ b/tensorflow/compiler/xla/packed_literal_reader.cc
@@ -44,11 +44,11 @@ StatusOr<std::unique_ptr<Literal>> PackedLiteralReader::Read(
VLOG(3) << "reading shape from file: " << ShapeUtil::HumanString(shape)
<< " layout: "
<< (layout == nullptr ? "<none>" : layout->ShortDebugString());
- auto result = MakeUnique<Literal>();
- *result->mutable_shape() = shape;
+ Shape literal_shape = shape;
if (layout != nullptr) {
- TF_RETURN_IF_ERROR(LayoutUtil::ValidateLayoutForShape(*layout, shape));
- *result->mutable_shape()->mutable_layout() = *layout;
+ TF_RETURN_IF_ERROR(
+ LayoutUtil::ValidateLayoutForShape(*layout, literal_shape));
+ *literal_shape.mutable_layout() = *layout;
}
if (shape.element_type() != F32) {
@@ -57,10 +57,12 @@ StatusOr<std::unique_ptr<Literal>> PackedLiteralReader::Read(
PrimitiveType_Name(shape.element_type()).c_str());
}
+ auto result = MakeUnique<Literal>(literal_shape);
+ result->PopulateWithValue(std::numeric_limits<float>::quiet_NaN());
+
int64 elements = ShapeUtil::ElementsIn(shape);
- result->Resize(elements, std::numeric_limits<float>::quiet_NaN());
- std::vector<float>* field = result->mutable_f32s();
- char* data = tensorflow::bit_cast<char*>(field->data());
+ tensorflow::gtl::ArraySlice<float> field = result->data<float>();
+ char* data = tensorflow::bit_cast<char*>(field.data());
uint64 bytes = elements * sizeof(float);
tensorflow::StringPiece sp;
auto s = file_->Read(offset_, bytes, &sp, data);