litert::
TensorBuffer
#include <litert_tensor_buffer.h>
A C++ wrapper for LiteRtTensorBuffer
, representing a tensor and its associated backing buffer.
Summary
Inheritance
Inherits from: internal::BaseHandle< LiteRtTensorBuffer >Constructors and Destructors
TensorBuffer
()
Public types
Public functions
BufferType
() const
Expected
< TensorBufferType >
ClearEvent
()
Expected
< void >
Duplicate
() const
Expected
< TensorBuffer
>
GetAhwb
() const
litert::Expected
< AHardwareBuffer * >
GetDmaBuf
() const
GetGlBuffer
() const
GetGlTexture
() const
GetMetalBuffer
() const
Expected
< void * >
GetOpenClMemory
() const
Expected
< LiteRtClMem >
GetVulkanMemory
() const
Expected
< HwMemoryHandle >
GetWebGpuBuffer
() const
Expected
< HwMemoryHandle >
HasEvent
() const
bool
HasType
(const RankedTensorType & type) const
bool
HasType
(const LiteRtRankedTensorType & type) const
bool
IsMetalMemory
() const
bool
true
if the tensor buffer is a Metal memory object. IsOpenClMemory
() const
bool
true
if the tensor buffer is an OpenCL memory object. IsVulkanMemory
() const
bool
true
if the tensor buffer is a Vulkan memory object. IsWebGpuMemory
() const
bool
true
if the tensor buffer is a WebGPU memory object. PackedSize
() const
Expected
< size_t >
Read
(absl::Span< T > data)
Expected
< void >
absl::Span
. TensorType
() const
Expected
< RankedTensorType >
Public static functions
CreateFromAhwb
(const Environment
& env, const RankedTensorType & tensor_type, AHardwareBuffer *ahwb, size_t ahwb_offset)
Expected
< TensorBuffer
>
CreateFromClBuffer
(const Environment
& env, const RankedTensorType & tensor_type, TensorBufferType buffer_type, LiteRtClMem cl_memory, size_t size_bytes)
Expected
< TensorBuffer
>
CreateFromGlBuffer
(const Environment
& env, const RankedTensorType & tensor_type, LiteRtGLenum target, LiteRtGLuint id, size_t size_bytes, size_t offset)
Expected
< TensorBuffer
>
CreateFromGlTexture
(const Environment
& env, const RankedTensorType & tensor_type, LiteRtGLenum target, LiteRtGLuint id, LiteRtGLenum format, size_t size_bytes, LiteRtGLint layer)
Expected
< TensorBuffer
>
CreateFromHostMemory
(const Environment
& env, const RankedTensorType & tensor_type, void *host_mem_addr, size_t buffer_size)
Expected
< TensorBuffer
>
CreateFromHostMemory
(const RankedTensorType & tensor_type, void *host_mem_addr, size_t buffer_size)
CreateManaged
(const Environment
& env, TensorBufferType buffer_type, const RankedTensorType & tensor_type, size_t buffer_size)
Expected
< TensorBuffer
>
CreateManagedHostMemory
(const RankedTensorType & tensor_type, size_t buffer_size)
ToLiteRtLockMode
(LockMode mode)
LiteRtTensorBufferLockMode
WrapCObject
(LiteRtTensorBuffer tensor_buffer, OwnHandle owned)
WrapCObject
(const internal::EnvironmentHolder
& env, LiteRtTensorBuffer tensor_buffer, OwnHandle owned)
Public types
LockMode
LockMode
Public functions
BufferType
Expected < TensorBufferType > BufferType () const
Clear
Expected < void > Clear()
Clears the tensor buffer possibly asynchronously.
It may return immediately after scheduling a clear operation though it guarantees that Read() will return data cleared, i.e. all zeros.
ClearEvent
Expected < void > ClearEvent()
Duplicate
Expected < TensorBuffer > Duplicate () const
Creates a duplicate of the current TensorBuffer
object.
The returned object is reference-counted, so the underlying LiteRtTensorBuffer
handle is not released until the last reference is removed.
GetAhwb
litert :: Expected < AHardwareBuffer * > GetAhwb () const
GetDmaBuf
litert :: Expected < DmaBuf > GetDmaBuf () const
GetMetalBuffer
Expected < void * > GetMetalBuffer () const
GetOpenClMemory
Expected < LiteRtClMem > GetOpenClMemory () const
GetVulkanMemory
Expected < HwMemoryHandle > GetVulkanMemory () const
GetWebGpuBuffer
Expected < HwMemoryHandle > GetWebGpuBuffer () const
HasEvent
bool HasEvent () const
HasType
bool HasType ( const RankedTensorType & type ) const
HasType
bool HasType ( const LiteRtRankedTensorType & type ) const
IsMetalMemory
bool IsMetalMemory () const
Returns true
if the tensor buffer is a Metal memory object.
IsOpenClMemory
bool IsOpenClMemory () const
Returns true
if the tensor buffer is an OpenCL memory object.
IsVulkanMemory
bool IsVulkanMemory () const
Returns true
if the tensor buffer is a Vulkan memory object.
IsWebGpuMemory
bool IsWebGpuMemory () const
Returns true
if the tensor buffer is a WebGPU memory object.
Lock
Expected < void * > Lock( LockMode mode )
Offset
Expected < size_t > Offset () const
PackedSize
Expected < size_t > PackedSize () const
Returns the size of the tensor buffer in packed bytes.
This size is used for reading/writing data on a locked tensor buffer.
Read
Expected < void > Read( absl::Span< T > data )
Reads data from the tensor buffer into a user-provided absl::Span
.
If the provided buffer is smaller than the tensor buffer, data will be read up to the size of the provided buffer. Returns an error if the provided buffer is larger than the tensor buffer.
Size
Expected < size_t > Size () const
Returns the size of the underlying hardware tensor buffer.
This size can differ from PackedSize()
if striding and padding exist.
TensorBuffer
TensorBuffer()
TensorType
Expected < RankedTensorType > TensorType () const
Unlock
Expected < void > Unlock()
Write
Expected < void > Write ( absl :: Span < const T > data )
Writes data from a user-provided absl::Span
to the tensor buffer.
Returns an error if the provided buffer is larger than the tensor buffer's size.
Public static functions
CreateFromAhwb
Expected < TensorBuffer > CreateFromAhwb ( const Environment & env , const RankedTensorType & tensor_type , AHardwareBuffer * ahwb , size_t ahwb_offset )
Creates a TensorBuffer
that wraps an Android Hardware Buffer.
The provided AHardwareBuffer
is not owned by the TensorBuffer
and must outlive it.
ahwb_offset
AHardwareBuffer
where the tensor data begins.CreateFromClBuffer
Expected < TensorBuffer > CreateFromClBuffer ( const Environment & env , const RankedTensorType & tensor_type , TensorBufferType buffer_type , LiteRtClMem cl_memory , size_t size_bytes )
CreateFromGlBuffer
Expected < TensorBuffer > CreateFromGlBuffer ( const Environment & env , const RankedTensorType & tensor_type , LiteRtGLenum target , LiteRtGLuint id , size_t size_bytes , size_t offset )
CreateFromGlTexture
Expected < TensorBuffer > CreateFromGlTexture ( const Environment & env , const RankedTensorType & tensor_type , LiteRtGLenum target , LiteRtGLuint id , LiteRtGLenum format , size_t size_bytes , LiteRtGLint layer )
CreateFromHostMemory
Expected < TensorBuffer > CreateFromHostMemory ( const Environment & env , const RankedTensorType & tensor_type , void * host_mem_addr , size_t buffer_size )
Creates a TensorBuffer
that wraps the provided host memory.
The provided host memory is not owned by the TensorBuffer
object and must outlive it. Callers are responsible for ensuring that the pointer is aligned to at least LITERT_HOST_MEMORY_BUFFER_ALIGNMENT
bytes and that any required padding for delegates like XNNPACK is included and initialized.
CreateFromHostMemory
Expected < TensorBuffer > CreateFromHostMemory ( const RankedTensorType & tensor_type , void * host_mem_addr , size_t buffer_size )
Deprecated. Use API with explicit Environment instead.
CreateManaged
Expected < TensorBuffer > CreateManaged ( const Environment & env , TensorBufferType buffer_type , const RankedTensorType & tensor_type , size_t buffer_size )
Creates a managed TensorBuffer
of a given buffer type and size.
The returned object is owned by the caller. For host memory, this allocator guarantees LITERT_HOST_MEMORY_BUFFER_ALIGNMENT
alignment and reserves any delegate-specific padding (e.g., XNNPACK extra bytes), so callers do not need to over-allocate manually.
CreateManagedHostMemory
Expected < TensorBuffer > CreateManagedHostMemory ( const RankedTensorType & tensor_type , size_t buffer_size )
Creates a managed host memory TensorBuffer
using the default environment (if applicable).
The returned object is owned by the caller. Deprecated. Use API with explicit Environment instead.
ToLiteRtLockMode
LiteRtTensorBufferLockMode ToLiteRtLockMode( LockMode mode )
WrapCObject
TensorBuffer WrapCObject( LiteRtTensorBuffer tensor_buffer, OwnHandle owned )
WrapCObject
TensorBuffer WrapCObject ( const internal :: EnvironmentHolder & env , LiteRtTensorBuffer tensor_buffer , OwnHandle owned )

