pub struct Buffer { /* private fields */ }Expand description
A device buffer that may be a view into another buffer.
Handle-identity (id) is per-Buffer value, including views — mirroring
tinygrad, where each BUFFER_VIEW produces a distinct UOp identity.
Storage-identity (the underlying Arc<BufferData>) is shared between a
buffer and its views; use Buffer::storage_id to compare it.
Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn new(
allocator: Arc<dyn Allocator>,
dtype: DType,
shape: Vec<usize>,
options: BufferOptions,
) -> Self
pub fn new( allocator: Arc<dyn Allocator>, dtype: DType, shape: Vec<usize>, options: BufferOptions, ) -> Self
Create a new buffer with lazy allocation.
Sourcepub fn allocate(
allocator: Arc<dyn Allocator>,
dtype: DType,
shape: Vec<usize>,
options: BufferOptions,
) -> Result<Self>
pub fn allocate( allocator: Arc<dyn Allocator>, dtype: DType, shape: Vec<usize>, options: BufferOptions, ) -> Result<Self>
Create a new buffer with immediate allocation.
Sourcepub fn view(&self, offset: usize, size: usize) -> Result<Self>
pub fn view(&self, offset: usize, size: usize) -> Result<Self>
Create a view into this buffer.
The view shares storage with self (same Arc<BufferData>) but gets
a fresh BufferId so the runtime parallel-hazard model treats
disjoint views of one arena as independent — mirroring tinygrad’s
BUFFER_VIEW-as-distinct-identity semantics. Use
Buffer::storage_id to compare storage identity instead.
Sourcepub fn ensure_allocated(&self) -> Result<()>
pub fn ensure_allocated(&self) -> Result<()>
Ensure the underlying buffer is allocated.
Sourcepub fn is_allocated(&self) -> bool
pub fn is_allocated(&self) -> bool
Check if the buffer is allocated.
Sourcepub fn as_host_bytes(&self) -> Result<&[u8]>
pub fn as_host_bytes(&self) -> Result<&[u8]>
Get a byte slice of the buffer data (CPU-accessible buffers only).
Zero-copy. For realized tensors after realize(), this is safe because
the scheduler guarantees no concurrent kernel writes.
§Errors
NotAllocatedif buffer hasn’t been allocatedNotCpuAccessiblefor CUDA device buffers (usecopyoutinstead)
Sourcepub fn as_host_bytes_mut(&self) -> Result<&mut [u8]>
pub fn as_host_bytes_mut(&self) -> Result<&mut [u8]>
Sourcepub fn as_array<T: HasDType>(&self) -> Result<ArrayViewD<'_, T>>
pub fn as_array<T: HasDType>(&self) -> Result<ArrayViewD<'_, T>>
Typed immutable view over CPU-accessible buffer memory.
Returns an ndarray view shaped according to the buffer’s concrete dimensions. Only works for CPU-accessible buffers — fails for device-only CUDA memory.
§Errors
TypeMismatchifT::DTYPEdoesn’t match buffer dtypeNotCpuAccessiblefor non-CPU-accessible buffersNotAllocatedif buffer hasn’t been allocated
Sourcepub fn as_array_mut<T: HasDType>(&self) -> Result<ArrayViewMutD<'_, T>>
pub fn as_array_mut<T: HasDType>(&self) -> Result<ArrayViewMutD<'_, T>>
Typed mutable view over CPU-accessible buffer memory.
Same as Self::as_array but allows writes. Caller must ensure no
kernels are executing concurrently (safety is the caller’s
responsibility).
§Errors
Same as Self::as_array.
Sourcepub fn as_slice<T: HasDType>(&self) -> Result<&[T]>
pub fn as_slice<T: HasDType>(&self) -> Result<&[T]>
Zero-copy typed slice view (CPU-accessible only).
Sourcepub fn item<T: HasDType + Copy>(&self) -> Result<T>
pub fn item<T: HasDType + Copy>(&self) -> Result<T>
Read a single scalar value from the buffer (CPU-accessible only).
Panics if the buffer contains more than one element.
Sourcepub fn allocator_arc(&self) -> Arc<dyn Allocator> ⓘ
pub fn allocator_arc(&self) -> Arc<dyn Allocator> ⓘ
Get an Arc-cloned handle to the allocator, suitable for constructing
new buffers on the same device (used by the arena memory planner to
allocate per-lane arenas matching prototype buffers’ device).
Sourcepub fn id(&self) -> BufferId
pub fn id(&self) -> BufferId
Get the unique identifier for this buffer handle.
Each Buffer value (including each view) carries its own BufferId;
disjoint views of one arena therefore have different ids. Used by the
runtime parallel-hazard model. To compare storage identity (i.e. “do
these two buffers share the same underlying allocation”), use
Buffer::storage_id instead.
Sourcepub fn total_size(&self) -> usize
pub fn total_size(&self) -> usize
Size of the underlying allocation in bytes (shared by every view of
this buffer’s storage). Distinct from Buffer::size, which returns
the view’s size — for a non-view buffer the two are equal; for a view
into an arena, total_size reports the arena’s allocation size while
size reports just the view’s window.
Sourcepub fn storage_id(&self) -> BufferId
pub fn storage_id(&self) -> BufferId
Stable identifier for this buffer’s underlying allocation.
Equal across a base buffer and all of its views, distinct between independent allocations. Unlike a heap-pointer probe, this id is minted once at allocation time and never reused — safe to use as a hash key or alias-detection key without worrying about allocator-reuse aliasing.
Sourcepub fn copyin(&mut self, src: &[u8]) -> Result<()>
pub fn copyin(&mut self, src: &[u8]) -> Result<()>
Copy data from host memory into this buffer.
Sourcepub fn copy_from(&mut self, src: &Buffer) -> Result<()>
pub fn copy_from(&mut self, src: &Buffer) -> Result<()>
Copy data from another buffer to this buffer.
Sourcepub fn synchronize(&self) -> Result<()>
pub fn synchronize(&self) -> Result<()>
Synchronize the device (wait for all operations to complete).
Sourcepub unsafe fn as_raw_ptr(&self) -> *mut u8
pub unsafe fn as_raw_ptr(&self) -> *mut u8
Get a raw pointer to the buffer data for kernel execution.
§Safety
The returned pointer is only valid while the buffer is allocated. The caller must ensure:
- Buffer remains allocated during pointer lifetime
- No conflicting accesses occur during kernel execution
- Pointer is not used after buffer is freed
§Panics
Panics if the buffer is not yet allocated.