diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2017-12-15 23:38:01 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2017-12-18 10:10:43 -0800 |
commit | fc2526a8c1cf0bc2a93c8cc819ff7209eb4628c9 (patch) | |
tree | eb4a3ea9ba49b7cbeabe1195c98267856b6d62ae /tensorflow/compiler/xla/service/shaped_buffer.h | |
parent | f2996ec7a1e0e9f4d07637889475681a7432375c (diff) |
Merged commit includes the following changes:
179277894 by gunan:
Run buildifier on build file.
--
179275101 by meheff:
Replace DeviceMemoryBase with ShapedBuffer in XLA interfaces.
Executable, TransferManager, and AllocationTracker now use ShapedBuffer to hold device memory addresses holding XLA data. Most of the change is straight-forward with the exception of AllocationTracker which was mostly rewritten (and simplified) and some refactoring in the CPU executable.
Also, have ShapedBuffer hold on-host and on-device Shapes which are the shapes of the representation of the data on the host and device, respectively. This is necessary because with cl/178624364 the on-host and on-device shape may no longer be equal.
--
179265385 by A. Unique TensorFlower:
Return error rather than CHECK fail in Executable::ExecuteOnStreamWrapper
--
179264551 by dandelion:
Internal fixes.
--
PiperOrigin-RevId: 179277894
Diffstat (limited to 'tensorflow/compiler/xla/service/shaped_buffer.h')
-rw-r--r-- | tensorflow/compiler/xla/service/shaped_buffer.h | 100 |
1 files changed, 45 insertions, 55 deletions
diff --git a/tensorflow/compiler/xla/service/shaped_buffer.h b/tensorflow/compiler/xla/service/shaped_buffer.h index ca8bfff674..f570ebb9cb 100644 --- a/tensorflow/compiler/xla/service/shaped_buffer.h +++ b/tensorflow/compiler/xla/service/shaped_buffer.h @@ -31,69 +31,68 @@ limitations under the License. namespace xla { // Class which encapsulates a buffer or set of buffers containing data of a -// particular XLA shape. Used for zero-copy execution interface for a -// XLA client running in the same process as the service (LocalClient), +// particular XLA shape. class ShapedBuffer { public: - // Convenience method which creates a ShapedBuffer of array shape (not a - // tuple). Its single buffer pointer is set to the given value "buffer". The - // given buffer must be large enough to store the given shape as given by - // ShapeUtil::ByteSizeOf. - static StatusOr<std::unique_ptr<ShapedBuffer>> MakeArrayShapedBuffer( - const Shape& shape, const perftools::gputools::Platform* platform, - int device_ordinal, const perftools::gputools::DeviceMemoryBase& buffer); - - // Return a newly allocated ShapedBuffer of an arbitrary shape. Array buffers - // (leaves in the shape) are allocated and uninitialized. Tuple buffers (if - // any) are allocated and initialized to the backend-specific representation - // of an array of pointers to the tuple elements. - static StatusOr<std::unique_ptr<ShapedBuffer>> Allocate( - const Shape& shape, DeviceMemoryAllocator* allocator, int device_ordinal, - const std::function<int64(const Shape&)>& shape_size_fn); - - ShapedBuffer(const Shape& shape, + // Construct a ScopedShapedBuffer with null DeviceMemoryBases at each + // index. The shape of the data on the host and the device may differ because + // the device may have a different representation for different data + // types. Therefore, both the on-host and on-device shape are required. The + // on-device shape determines the number of device allocations + // (DeviceMemoryBase) held by the ShapedBuffer. + ShapedBuffer(const Shape& on_host_shape, const Shape& on_device_shape, const perftools::gputools::Platform* platform, int device_ordinal); - const Shape& shape() const { return shape_; } + // Returns the shape of the on-host representation of the data held by this + // ShapedBuffer. + const Shape& on_host_shape() const { return on_host_shape_; } + + // Returns the shape of the on-device representation of the data held by this + // ShapedBuffer. + const Shape& on_device_shape() const { return on_device_shape_; } + const perftools::gputools::Platform* platform() const { return platform_; } int device_ordinal() const { return device_ordinal_; } + // Return the root buffer of the shape (shape index {}). + const perftools::gputools::DeviceMemoryBase& root_buffer() const { + return buffer(/*index=*/{}); + } + // Returns the buffer at the given shape index where index is defined as in // ShapeUtil::GetSubshape. const perftools::gputools::DeviceMemoryBase& buffer( - const ShapeIndex& index) const; - perftools::gputools::DeviceMemoryBase* mutable_buffer( - const ShapeIndex& index); - - // Returns the underlying structure which stores the buffer pointers. - const std::vector<perftools::gputools::DeviceMemoryBase>& buffers() const { - return buffers_; + const ShapeIndex& index) const { + return buffers_.element(index); } - std::vector<perftools::gputools::DeviceMemoryBase>* mutable_buffers() { - return &buffers_; + + // Sets the device memory buffer at the given index. + void set_buffer(const perftools::gputools::DeviceMemoryBase& buffer, + const ShapeIndex& index) { + *buffers_.mutable_element(index) = buffer; } - // Returns the tree of indices which map to buffer pointers. - const ShapeTree<size_t>& shape_index_to_buffer_entry() const { - return shape_index_to_buffer_entry_; + // Returns the underlying ShapeTree containing all the device addresses in the + // ShapedBuffer. + const ShapeTree<perftools::gputools::DeviceMemoryBase>& buffers() const { + return buffers_; } - ShapeTree<size_t>* mutable_shape_index_to_buffer_entry() { - return &shape_index_to_buffer_entry_; + ShapeTree<perftools::gputools::DeviceMemoryBase>& buffers() { + return buffers_; } // Set all device memory pointers in the object to null. void clear(); - // Adds a new buffer at the given shape index. - void AddBufferAtIndex(const perftools::gputools::DeviceMemoryBase& buffer, - const ShapeIndex& shape_index); - string ToString() const; protected: - // The shape of the device buffer with layout. - const Shape shape_; + // The shape of the data when represented on the host. + const Shape on_host_shape_; + + // The shape of the data on the device. + const Shape on_device_shape_; // The platform the memory is allocated on. const perftools::gputools::Platform* platform_; @@ -101,14 +100,8 @@ class ShapedBuffer { // The device the memory is allocated on. const int device_ordinal_; - // The list of DeviceMemoryBase pointers representing this shape. - // Note that there can be a many to one relationship between tuple elements - // and buffers. To account for this, shape_index_to_buffer_entry_ allows us - // to make from a position in a shape to an index into this list. - std::vector<perftools::gputools::DeviceMemoryBase> buffers_; - - // The tree of indices into buffers_. - ShapeTree<size_t> shape_index_to_buffer_entry_; + // The tree of device buffers. Its shape is on_device_shape(). + ShapeTree<perftools::gputools::DeviceMemoryBase> buffers_; }; std::ostream& operator<<(std::ostream& out, const ShapedBuffer& buffer); @@ -118,17 +111,16 @@ std::ostream& operator<<(std::ostream& out, const ShapedBuffer& buffer); // destructed. class ScopedShapedBuffer : public ShapedBuffer { public: - // Identical to ShapedBuffer::Allocate. - static StatusOr<std::unique_ptr<ScopedShapedBuffer>> Allocate( - const Shape& shape, DeviceMemoryAllocator* allocator, int device_ordinal, - const std::function<int64(const Shape&)>& shape_size_fn); - // Takes a ShapedBuffer and returns a ScopedShapedBuffer which manages the // deallocation of the device memory held in the shaped buffer. All device // memory pointers in the given ShapedBuffer are set to null. static StatusOr<std::unique_ptr<ScopedShapedBuffer>> MakeScoped( ShapedBuffer* shaped_buffer, DeviceMemoryAllocator* allocator); + // Create a ScopedShapedBuffer with null DeviceMemoryBases at each index. + ScopedShapedBuffer(const Shape& on_host_shape, const Shape& on_device_shape, + DeviceMemoryAllocator* allocator, int device_ordinal); + // Return the allocator used to allocate the device memory held in this // ScopedShapedBuffer. DeviceMemoryAllocator* memory_allocator() const { return allocator_; } @@ -143,8 +135,6 @@ class ScopedShapedBuffer : public ShapedBuffer { virtual ~ScopedShapedBuffer(); protected: - ScopedShapedBuffer(const Shape& shape, DeviceMemoryAllocator* allocator, - int device_ordinal); ScopedShapedBuffer(const ScopedShapedBuffer&) = delete; void operator=(const ScopedShapedBuffer&) = delete; |