Skip to main content

Buffer

Struct Buffer 

Source
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

Source

pub fn new( allocator: Arc<dyn Allocator>, dtype: DType, shape: Vec<usize>, options: BufferOptions, ) -> Self

Create a new buffer with lazy allocation.

Source

pub fn allocate( allocator: Arc<dyn Allocator>, dtype: DType, shape: Vec<usize>, options: BufferOptions, ) -> Result<Self>

Create a new buffer with immediate allocation.

Source

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.

Source

pub fn ensure_allocated(&self) -> Result<()>

Ensure the underlying buffer is allocated.

Source

pub fn is_allocated(&self) -> bool

Check if the buffer is allocated.

Source

pub fn size(&self) -> usize

Get the size of this buffer view in bytes.

Source

pub fn offset(&self) -> usize

Get the offset of this view in bytes.

Source

pub fn dtype(&self) -> DType

Get the data type.

Source

pub fn shape(&self) -> &[usize]

Get the shape of this buffer.

Source

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
  • NotAllocated if buffer hasn’t been allocated
  • NotCpuAccessible for CUDA device buffers (use copyout instead)
Source

pub fn as_host_bytes_mut(&self) -> Result<&mut [u8]>

Get a mutable byte slice of the buffer data (CPU-accessible buffers only).

§Safety contract (same as as_host_bytes)

Caller must ensure no kernels are executing concurrently.

§Errors
  • NotAllocated if buffer hasn’t been allocated
  • NotCpuAccessible for CUDA device buffers
Source

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
  • TypeMismatch if T::DTYPE doesn’t match buffer dtype
  • NotCpuAccessible for non-CPU-accessible buffers
  • NotAllocated if buffer hasn’t been allocated
Source

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.

Source

pub fn as_slice<T: HasDType>(&self) -> Result<&[T]>

Zero-copy typed slice view (CPU-accessible only).

Source

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.

Source

pub fn allocator(&self) -> &dyn Allocator

Get the allocator used by this buffer.

Source

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).

Source

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.

Source

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.

Source

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.

Source

pub fn copyin(&mut self, src: &[u8]) -> Result<()>

Copy data from host memory into this buffer.

Source

pub fn copyout(&self, dst: &mut [u8]) -> Result<()>

Copy data from this buffer to host memory.

Source

pub fn copy_from(&mut self, src: &Buffer) -> Result<()>

Copy data from another buffer to this buffer.

Source

pub fn synchronize(&self) -> Result<()>

Synchronize the device (wait for all operations to complete).

Source

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.

Trait Implementations§

Source§

impl Clone for Buffer

Source§

fn clone(&self) -> Buffer

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Buffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more