diff options
author | 2018-08-30 16:03:10 -0700 | |
---|---|---|
committer | 2018-08-30 16:07:27 -0700 | |
commit | 6f879f891abe2e267c5cf512d034d7c3641cfdb0 (patch) | |
tree | 33dfda2aa13bdec06d3aa330dd5816441d449fa7 /tensorflow/compiler/xla/literal.h | |
parent | 5d5591fbd4624ff7e50f305464667315f2d41ebb (diff) |
[XLA] Rename all (Mutable)ArraySlice to absl::Span.
PiperOrigin-RevId: 210998142
Diffstat (limited to 'tensorflow/compiler/xla/literal.h')
-rw-r--r-- | tensorflow/compiler/xla/literal.h | 166 |
1 files changed, 76 insertions, 90 deletions
diff --git a/tensorflow/compiler/xla/literal.h b/tensorflow/compiler/xla/literal.h index c6ef99db0f..d690241e4e 100644 --- a/tensorflow/compiler/xla/literal.h +++ b/tensorflow/compiler/xla/literal.h @@ -70,13 +70,12 @@ class LiteralBase { // Serialize to proto. LiteralProto ToProto() const; - // Returns an ArraySlice of the array for this literal for the given NativeT + // Returns a Span of the array for this literal for the given NativeT // (e.g., float). CHECKs if the subshape of the literal at the given // ShapeIndex is not array. See primitive_util.h for the mapping from XLA type // to native type. template <typename NativeT> - tensorflow::gtl::ArraySlice<NativeT> data( - const ShapeIndex& shape_index = {}) const; + absl::Span<const NativeT> data(const ShapeIndex& shape_index = {}) const; // Returns a const pointer to the sparse index array. Returns nullptr if the // literal is not a sparse array. @@ -100,12 +99,12 @@ class LiteralBase { // Gets an element in the literal at the given index. The multi_index is // CHECKed against the dimension sizes. template <typename NativeT> - NativeT Get(tensorflow::gtl::ArraySlice<int64> multi_index, + NativeT Get(absl::Span<const int64> multi_index, const ShapeIndex& shape_index) const; // Overloads of Get for array literals. CHECKs if the literal is not // array-shaped and dense. template <typename NativeT> - NativeT Get(tensorflow::gtl::ArraySlice<int64> multi_index) const; + NativeT Get(absl::Span<const int64> multi_index) const; // Returns the element value at index (0, ..., 0), however many zeroes are // required for that index. @@ -114,7 +113,7 @@ class LiteralBase { // As Get(), but determines the correct type and converts the value // into text. - string GetAsString(tensorflow::gtl::ArraySlice<int64> multi_index, + string GetAsString(absl::Span<const int64> multi_index, const ShapeIndex& shape_index = {}) const; // As GetSparseElement(), but determines the correct type and converts the // value into text. @@ -122,14 +121,13 @@ class LiteralBase { const ShapeIndex& shape_index = {}) const; // As Get(), but determines the correct type and converts the value into // int64. This literal must be an array. - StatusOr<int64> GetIntegralAsS64( - tensorflow::gtl::ArraySlice<int64> multi_index) const; + StatusOr<int64> GetIntegralAsS64(absl::Span<const int64> multi_index) const; // Returns the multi-index of the element in a sparse literal at the given // sparse element number. The sparse element number is the position with in // the sparse array's list of (index, value) pairs, and is checked against the // total number of (index, value) pairs in the sparse array. - tensorflow::gtl::ArraySlice<int64> GetSparseIndex( + absl::Span<const int64> GetSparseIndex( int64 sparse_element_number, const ShapeIndex& shape_index = {}) const; // Returns the value of the element in a sparse literal at the given sparse @@ -150,12 +148,12 @@ class LiteralBase { // // This literal must have a dense layout. void EachCellAsString( - const std::function<void(tensorflow::gtl::ArraySlice<int64> indices, + const std::function<void(absl::Span<const int64> indices, const string& value)>& per_cell) const; template <typename NativeT> - void EachCell(std::function<void(tensorflow::gtl::ArraySlice<int64> indices, - NativeT value)> - per_cell) const; + void EachCell( + std::function<void(absl::Span<const int64> indices, NativeT value)> + per_cell) const; // Returns whether every element in this literal is equal to value. // @@ -200,7 +198,7 @@ class LiteralBase { // Returns whether this literal is zero at the specified index. This literal // must be an array with a dense layout. - bool IsZero(tensorflow::gtl::ArraySlice<int64> indices) const; + bool IsZero(absl::Span<const int64> indices) const; // Returns the count of the elements in the array at the given shape index in // this literal. @@ -273,13 +271,12 @@ class LiteralBase { // implementation currently only supports monotonic dim0-major layouts. // This literal must be an array. StatusOr<std::unique_ptr<Literal>> Reshape( - tensorflow::gtl::ArraySlice<int64> dimensions) const; + absl::Span<const int64> dimensions) const; // Creates a new literal by broadcasting this literal with `dimensions` to // yield a literal of shape `result_shape`. StatusOr<std::unique_ptr<Literal>> Broadcast( - const Shape& result_shape, - tensorflow::gtl::ArraySlice<int64> dimensions) const; + const Shape& result_shape, absl::Span<const int64> dimensions) const; // Creates a new literal by reordering the dimensions of this literal. // The given `permutation` must be a permutation of the dimension numbers @@ -288,8 +285,7 @@ class LiteralBase { // For example, a transpose call on a literal of shape [3 x 8 x 4] and // `permutation` = {2, 0, 1} returns a new literal of shape [4 x 3 x 8]. // This literal must be an array. - std::unique_ptr<Literal> Transpose( - tensorflow::gtl::ArraySlice<int64> permutation) const; + std::unique_ptr<Literal> Transpose(absl::Span<const int64> permutation) const; // Creates a sub-array from this literal by extracting the indices // [start_index, limit_index) of each dimension. The result literal has the @@ -297,9 +293,8 @@ class LiteralBase { // start_indices and limit_indices must be the rank of the literal, and the // indices follow the order of the dimensions. // This literal must be an array. - std::unique_ptr<Literal> Slice( - tensorflow::gtl::ArraySlice<int64> start_indices, - tensorflow::gtl::ArraySlice<int64> limit_indices) const; + std::unique_ptr<Literal> Slice(absl::Span<const int64> start_indices, + absl::Span<const int64> limit_indices) const; // Creates a literal with a prepended dimension with bound "times"; e.g. a // f32[3x2] with times=4 will produce a f32[4x3x2] with the 3x2 from this @@ -328,9 +323,9 @@ class LiteralBase { // Returns the buffer holding the array data for this piece as an array // slice. This piece must be array-shaped. template <typename NativeT> - tensorflow::gtl::ArraySlice<NativeT> data() const; + absl::Span<const NativeT> data() const; template <typename NativeT> - tensorflow::gtl::MutableArraySlice<NativeT> data(); + absl::Span<NativeT> data(); // Returns the buffer holding the array data for this piece as a void*. This // piece must be array-shaped. @@ -341,9 +336,9 @@ class LiteralBase { // is CHECKed against the dimension sizes of the array. This piece must be // array-shaped. template <typename NativeT> - NativeT Get(tensorflow::gtl::ArraySlice<int64> index) const; + NativeT Get(absl::Span<const int64> index) const; template <typename NativeT> - void Set(tensorflow::gtl::ArraySlice<int64> index, NativeT value); + void Set(absl::Span<const int64> index, NativeT value); // Gets/sets the buffer holding the array data. char* buffer() const { return buffer_; } @@ -545,8 +540,7 @@ class LiteralBase { private: template <typename NativeT> std::unique_ptr<Literal> SliceInternal( - const Shape& result_shape, - tensorflow::gtl::ArraySlice<int64> start_indices) const; + const Shape& result_shape, absl::Span<const int64> start_indices) const; }; // Abstract base class representing a mutable literal in XLA. @@ -554,13 +548,12 @@ class MutableLiteralBase : public LiteralBase { public: virtual ~MutableLiteralBase() = 0; - // Returns a MutableArraySlice view of the array for this literal for the + // Returns a Span view of the array for this literal for the // given NativeT (e.g., float). CHECKs if the subshape of the literal at the // given ShapeIndex is not array. See primitive_util.h for the mapping from // XLA type to native type. template <typename NativeT> - tensorflow::gtl::MutableArraySlice<NativeT> data( - const ShapeIndex& shape_index = {}); + absl::Span<NativeT> data(const ShapeIndex& shape_index = {}); // Unhide const method from parent class. using LiteralBase::data; @@ -587,8 +580,7 @@ class MutableLiteralBase : public LiteralBase { // are populated. template <typename NativeT> void PopulateSparse(SparseIndexArray indices, - tensorflow::gtl::ArraySlice<NativeT> values, - bool sort = true); + absl::Span<const NativeT> values, bool sort = true); // Copy values from 'src_literal' rooted at 'src_shape_index' into this // literal rooted at 'dest_shape_index'. The subshape of this literal rooted @@ -609,39 +601,38 @@ class MutableLiteralBase : public LiteralBase { // corresponding base indices being 0. // This literal and 'src_literal' must be arrays. Status CopySliceFrom(const LiteralSlice& src_literal, - tensorflow::gtl::ArraySlice<int64> src_base, - tensorflow::gtl::ArraySlice<int64> dest_base, - tensorflow::gtl::ArraySlice<int64> copy_size); + absl::Span<const int64> src_base, + absl::Span<const int64> dest_base, + absl::Span<const int64> copy_size); // Copies one element from src_literal[src_index] to (*this)[dest_index]. Status CopyElementFrom(const LiteralSlice& src_literal, - tensorflow::gtl::ArraySlice<int64> src_index, - tensorflow::gtl::ArraySlice<int64> dest_index); + absl::Span<const int64> src_index, + absl::Span<const int64> dest_index); // Sets an element in the literal at the given index. The multi_index is // CHECKed against the dimension sizes. template <typename NativeT> - void Set(tensorflow::gtl::ArraySlice<int64> multi_index, - const ShapeIndex& shape_index, NativeT value); + void Set(absl::Span<const int64> multi_index, const ShapeIndex& shape_index, + NativeT value); // Overloads of Set for array literals. CHECKs if the literal is not // array-shaped and dense. template <typename NativeT> - void Set(tensorflow::gtl::ArraySlice<int64> multi_index, NativeT value); + void Set(absl::Span<const int64> multi_index, NativeT value); // Appends the given element to the literal. If the elements are not appended // in sorted order, then SortSparseElements should be called before calling // other methods. This literal must have a sparse layout. template <typename NativeT> - void AppendSparseElement(tensorflow::gtl::ArraySlice<int64> multi_index, - NativeT value, const ShapeIndex& shape_index = {}); + void AppendSparseElement(absl::Span<const int64> multi_index, NativeT value, + const ShapeIndex& shape_index = {}); // Sorts the elements in a sparse array. void SortSparseElements(const ShapeIndex& shape_index = {}); // As Set(), but truncates `value` to the literal element type before storing. // This literal must be an array. - Status SetIntegralAsS64(tensorflow::gtl::ArraySlice<int64> multi_index, - int64 value); + Status SetIntegralAsS64(absl::Span<const int64> multi_index, int64 value); // Populate this literal with the given values. Examples: // @@ -656,7 +647,7 @@ class MutableLiteralBase : public LiteralBase { // example, in the call above to literal.PopulateR2(), 'literal' must be a 2x2 // array of S32. template <typename NativeT> - void PopulateR1(tensorflow::gtl::ArraySlice<NativeT> values); + void PopulateR1(absl::Span<const NativeT> values); void PopulateR1(const tensorflow::core::Bitmap& values); template <typename NativeT> void PopulateR2(std::initializer_list<std::initializer_list<NativeT>> values); @@ -673,7 +664,7 @@ class MutableLiteralBase : public LiteralBase { // in this literal object. // // generator must be a callable of the type - // NativeT(tensorflow::gtl::ArraySlice<int64> indexes) or compatible. + // NativeT(absl::Span<int64> indexes) or compatible. // // This literal must have a dense layout. template <typename NativeT, typename FnType> @@ -693,8 +684,7 @@ class MutableLiteralBase : public LiteralBase { // moved into the tuple elements of a new tuple-shaped Literal which is // returned. Upon return, each of the Literals in 'elements' is set to a nil // shape (empty tuple). - static Literal MoveIntoTuple( - tensorflow::gtl::MutableArraySlice<Literal> elements); + static Literal MoveIntoTuple(absl::Span<Literal> elements); // Serialize from a proto. static StatusOr<std::unique_ptr<Literal>> CreateFromProto( @@ -712,20 +702,20 @@ class MutableLiteralBase : public LiteralBase { // arguments one by one. template <typename NativeT> Status CopySliceFromInternal(const LiteralBase& src_literal, - tensorflow::gtl::ArraySlice<int64> src_base, - tensorflow::gtl::ArraySlice<int64> dest_base, - tensorflow::gtl::ArraySlice<int64> copy_size); + absl::Span<const int64> src_base, + absl::Span<const int64> dest_base, + absl::Span<const int64> copy_size); // Utility structure which is used to create the optimal configuration for // a ShapeUtil::ForEachIndex() scan across two literals. struct StrideConfig { StrideConfig(const Shape& source_shape, const Shape& dest_shape, - tensorflow::gtl::ArraySlice<int64> dimensions); + absl::Span<const int64> dimensions); // The dimensions of the stride operation. Essentially every dimension // will be iterated from base[i] to base[i]+dimensions[i], in step[i] // steps. - tensorflow::gtl::ArraySlice<int64> dimensions; + absl::Span<const int64> dimensions; DimensionVector base; DimensionVector step; int64 minor_dimension = 0; @@ -854,7 +844,7 @@ class BorrowingLiteral : public LiteralBase { // This constructor is only used for array shapes. BorrowingLiteral(const char* src_buf_ptr, const Shape& shape); // Similar as above, except to be used for constructing non-nested tuples. - BorrowingLiteral(tensorflow::gtl::ArraySlice<const char*> src_buf_ptrs, + BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape); // TODO(b/79707221): adding constructors for nested tuples as well. @@ -874,7 +864,7 @@ class BorrowingLiteral : public LiteralBase { }; template <typename NativeT> -tensorflow::gtl::ArraySlice<NativeT> LiteralBase::Piece::data() const { +absl::Span<const NativeT> LiteralBase::Piece::data() const { CHECK(ShapeUtil::IsArray(subshape())) << ShapeUtil::HumanString(subshape()); CHECK_EQ(subshape().element_type(), primitive_util::NativeToPrimitiveType<NativeT>()) @@ -882,12 +872,12 @@ tensorflow::gtl::ArraySlice<NativeT> LiteralBase::Piece::data() const { << PrimitiveType_Name(primitive_util::NativeToPrimitiveType<NativeT>()) << " type, but literal element type is " << PrimitiveType_Name(subshape().element_type()); - return tensorflow::gtl::ArraySlice<NativeT>( - reinterpret_cast<const NativeT*>(buffer()), element_count()); + return absl::Span<const NativeT>(reinterpret_cast<const NativeT*>(buffer()), + element_count()); } template <typename NativeT> -tensorflow::gtl::MutableArraySlice<NativeT> LiteralBase::Piece::data() { +absl::Span<NativeT> LiteralBase::Piece::data() { CHECK(ShapeUtil::IsArray(subshape())) << ShapeUtil::HumanString(subshape()); CHECK_EQ(subshape().element_type(), primitive_util::NativeToPrimitiveType<NativeT>()) @@ -895,20 +885,19 @@ tensorflow::gtl::MutableArraySlice<NativeT> LiteralBase::Piece::data() { << PrimitiveType_Name(primitive_util::NativeToPrimitiveType<NativeT>()) << " type, but literal element type is " << PrimitiveType_Name(subshape().element_type()); - return tensorflow::gtl::MutableArraySlice<NativeT>( - reinterpret_cast<NativeT*>(buffer()), element_count()); + return absl::Span<NativeT>(reinterpret_cast<NativeT*>(buffer()), + element_count()); } template <typename NativeT> -NativeT LiteralBase::Piece::Get( - tensorflow::gtl::ArraySlice<int64> multi_index) const { +NativeT LiteralBase::Piece::Get(absl::Span<const int64> multi_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); return data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( subshape(), multi_index)]; } template <typename NativeT> -void LiteralBase::Piece::Set(tensorflow::gtl::ArraySlice<int64> multi_index, +void LiteralBase::Piece::Set(absl::Span<const int64> multi_index, NativeT value) { CHECK(LayoutUtil::IsDenseArray(subshape())); data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( @@ -916,39 +905,37 @@ void LiteralBase::Piece::Set(tensorflow::gtl::ArraySlice<int64> multi_index, } template <typename NativeT> -tensorflow::gtl::ArraySlice<NativeT> LiteralBase::data( +absl::Span<const NativeT> LiteralBase::data( const ShapeIndex& shape_index) const { return piece(shape_index).data<NativeT>(); } template <typename NativeT> -tensorflow::gtl::MutableArraySlice<NativeT> MutableLiteralBase::data( - const ShapeIndex& shape_index) { +absl::Span<NativeT> MutableLiteralBase::data(const ShapeIndex& shape_index) { return piece(shape_index).data<NativeT>(); } template <typename NativeT> -inline NativeT LiteralBase::Get(tensorflow::gtl::ArraySlice<int64> multi_index, +inline NativeT LiteralBase::Get(absl::Span<const int64> multi_index, const ShapeIndex& shape_index) const { return piece(shape_index).Get<NativeT>(multi_index); } template <typename NativeT> -inline NativeT LiteralBase::Get( - tensorflow::gtl::ArraySlice<int64> multi_index) const { +inline NativeT LiteralBase::Get(absl::Span<const int64> multi_index) const { return root_piece().Get<NativeT>(multi_index); } template <typename NativeT> -inline void MutableLiteralBase::Set( - tensorflow::gtl::ArraySlice<int64> multi_index, - const ShapeIndex& shape_index, NativeT value) { +inline void MutableLiteralBase::Set(absl::Span<const int64> multi_index, + const ShapeIndex& shape_index, + NativeT value) { return piece(shape_index).Set<NativeT>(multi_index, value); } template <typename NativeT> -inline void MutableLiteralBase::Set( - tensorflow::gtl::ArraySlice<int64> multi_index, NativeT value) { +inline void MutableLiteralBase::Set(absl::Span<const int64> multi_index, + NativeT value) { return root_piece().Set<NativeT>(multi_index, value); } @@ -967,7 +954,7 @@ NativeT LiteralBase::GetSparseElement(int64 sparse_element_number, template <typename NativeT> void MutableLiteralBase::AppendSparseElement( - tensorflow::gtl::ArraySlice<int64> multi_index, NativeT value, + absl::Span<const int64> multi_index, NativeT value, const ShapeIndex& shape_index) { Piece& p = piece(shape_index); const Shape& subshape = p.subshape(); @@ -983,8 +970,7 @@ void MutableLiteralBase::AppendSparseElement( template <typename NativeT> void LiteralBase::EachCell( - std::function<void(tensorflow::gtl::ArraySlice<int64> indices, - NativeT value)> + std::function<void(absl::Span<const int64> indices, NativeT value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; @@ -996,8 +982,7 @@ void LiteralBase::EachCell( } template <typename NativeT> -inline void MutableLiteralBase::PopulateR1( - tensorflow::gtl::ArraySlice<NativeT> values) { +inline void MutableLiteralBase::PopulateR1(absl::Span<const NativeT> values) { CHECK(ShapeUtil::IsArray(shape())); CHECK_EQ(ShapeUtil::Rank(shape()), 1); CHECK_EQ(ShapeUtil::ElementsIn(shape()), values.size()); @@ -1042,8 +1027,9 @@ void MutableLiteralBase::PopulateFromArray(const Array<NativeT>& values) { for (int dim = 0; dim < values.num_dimensions(); ++dim) { CHECK_EQ(values.dim(dim), shape().dimensions(dim)); } - values.Each([this](tensorflow::gtl::ArraySlice<int64> indices, - NativeT value) { this->Set(indices, value); }); + values.Each([this](absl::Span<const int64> indices, NativeT value) { + this->Set(indices, value); + }); } template <typename NativeT> @@ -1062,9 +1048,9 @@ void MutableLiteralBase::PopulateR4FromArray4D(const Array4D<NativeT>& values) { } template <typename NativeT> -void MutableLiteralBase::PopulateSparse( - SparseIndexArray indices, tensorflow::gtl::ArraySlice<NativeT> values, - bool sort) { +void MutableLiteralBase::PopulateSparse(SparseIndexArray indices, + absl::Span<const NativeT> values, + bool sort) { CHECK(LayoutUtil::IsSparseArray(shape())); int rank = ShapeUtil::Rank(shape()); CHECK_EQ(indices.rank(), rank); @@ -1074,7 +1060,7 @@ void MutableLiteralBase::PopulateSparse( CHECK_LE(num_elements, max_elements); CHECK_EQ(num_elements, indices.index_count()); auto root_data = root_piece().data<NativeT>(); - // Piece::data() returns an ArraySlice of size equal to the number of indices + // Piece::data() returns a Span of size equal to the number of indices // in the SparseIndexArray. So there is no need to adjust the size of the data // here. It is enough to just copy the incoming values into the data buffer. std::copy(values.begin(), values.end(), root_data.begin()); @@ -1094,14 +1080,14 @@ Status MutableLiteralBase::PopulateInternal(const FnType& generator, TF_RET_CHECK(LayoutUtil::IsDenseArray(this_shape)); TF_RET_CHECK(this_shape.element_type() == primitive_util::NativeToPrimitiveType<NativeT>()); - tensorflow::gtl::MutableArraySlice<NativeT> literal_data = data<NativeT>(); + absl::Span<NativeT> literal_data = data<NativeT>(); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, AsInt64Slice(this_shape.dimensions())); int64 minor_dimension_size = ShapeUtil::GetDimension(this_shape, stride_config.minor_dimension); - auto init_function = [&](tensorflow::gtl::ArraySlice<int64> indexes) { + auto init_function = [&](absl::Span<const int64> indexes) { DimensionVector minor_scan_indexes(rank, 0); const int64 index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); @@ -1119,7 +1105,7 @@ Status MutableLiteralBase::PopulateInternal(const FnType& generator, ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, - [&init_function](tensorflow::gtl::ArraySlice<int64> indexes) { + [&init_function](absl::Span<const int64> indexes) { init_function(indexes); return true; }); @@ -1165,7 +1151,7 @@ std::unique_ptr<Literal> LiteralBase::Replicate(int64 times) const { } DimensionVector output_indices(bounds.size(), 0); - tensorflow::gtl::ArraySlice<int64> input_indices = output_indices; + absl::Span<const int64> input_indices = output_indices; input_indices.remove_prefix(1); bool done = false; |