Class which encapsulates a buffer or set of buffers containing data of a particular XLA shape.
| 35 | // Class which encapsulates a buffer or set of buffers containing data of a |
| 36 | // particular XLA shape. |
| 37 | class ShapedBuffer { |
| 38 | public: |
| 39 | // Construct a ShapedBuffer with null DeviceMemoryBases at each index. The |
| 40 | // shape of the data on the host and the device may differ because the device |
| 41 | // may have a different representation for different data types. Therefore, |
| 42 | // both the on-host and on-device shape are required. The on-device shape |
| 43 | // determines the number of device allocations (DeviceMemoryBase) held by the |
| 44 | // ShapedBuffer. |
| 45 | ShapedBuffer(Shape on_host_shape, Shape on_device_shape, |
| 46 | const se::Platform* platform, int device_ordinal); |
| 47 | |
| 48 | // Movable, but not copyable. |
| 49 | ShapedBuffer(ShapedBuffer&& s); |
| 50 | ShapedBuffer& operator=(ShapedBuffer&&); |
| 51 | ShapedBuffer(const ShapedBuffer&) = delete; |
| 52 | ShapedBuffer& operator=(const ShapedBuffer&) = delete; |
| 53 | |
| 54 | // Prevent (some forms of) accidental object slicing. |
| 55 | ShapedBuffer(const ScopedShapedBuffer&) = delete; |
| 56 | ShapedBuffer& operator=(const ScopedShapedBuffer&) = delete; |
| 57 | |
| 58 | virtual ~ShapedBuffer(); |
| 59 | |
| 60 | // Returns the shape of the on-host representation of the data held by this |
| 61 | // ShapedBuffer. |
| 62 | const Shape& on_host_shape() const { return on_host_shape_; } |
| 63 | |
| 64 | // Returns the shape of the on-device representation of the data held by this |
| 65 | // ShapedBuffer. |
| 66 | const Shape& on_device_shape() const { return on_device_shape_; } |
| 67 | |
| 68 | const se::Platform* platform() const { return platform_; } |
| 69 | int device_ordinal() const { return device_ordinal_; } |
| 70 | |
| 71 | // Return the root buffer of the shape (shape index {}). |
| 72 | const se::DeviceMemoryBase& root_buffer() const { |
| 73 | return buffer(/*index=*/{}); |
| 74 | } |
| 75 | |
| 76 | // Returns the buffer at the given shape index where index is defined as in |
| 77 | // ShapeUtil::GetSubshape. |
| 78 | const se::DeviceMemoryBase& buffer(const ShapeIndex& index) const { |
| 79 | return buffers_.element(index); |
| 80 | } |
| 81 | |
| 82 | // Sets the device memory buffer at the given index. |
| 83 | void set_buffer(const se::DeviceMemoryBase& buffer, const ShapeIndex& index) { |
| 84 | *buffers_.mutable_element(index) = buffer; |
| 85 | } |
| 86 | |
| 87 | // Sets all buffers. |
| 88 | // |
| 89 | // Precondition: buffers.shape == on_device_shape_ |
| 90 | void set_buffers(ShapeTree<se::DeviceMemoryBase> buffers) { |
| 91 | CHECK(ShapeUtil::Equal(buffers.shape(), on_device_shape_)); |
| 92 | buffers_ = std::move(buffers); |
| 93 | buffers_.replace_shape_ptr(&on_device_shape_); |
| 94 | } |
no test coverage detected