Skip to main content

CpuBuffer

Struct CpuBuffer 

Source
pub struct CpuBuffer { /* private fields */ }
Expand description

A properly-aligned CPU heap buffer.

Uses Rust’s global allocator directly to guarantee alignment, which Vec<u8> cannot guarantee beyond its element alignment (1 byte).

Implementations§

Source§

impl CpuBuffer

Source

pub fn zeros(numel: usize, dtype: DType) -> Result<Self>

Allocate a zero-initialised buffer for numel elements of dtype.

Source

pub fn with_capacity(bytes: usize, align: usize) -> Result<Self>

Allocate bytes bytes with the given alignment.

Source

pub fn from_f32_slice(data: &[f32]) -> Result<Self>

Wrap existing f32 data.

Source

pub fn from_bytes_slice(data: &[u8]) -> Result<Self>

Wrap existing raw bytes (e.g., native BF16 or F16 from safetensors).

Source

pub fn as_f32_slice(&self) -> &[f32]

View as f32 slice (panics if not properly aligned/sized).

Source

pub fn as_f32_slice_mut(&mut self) -> &mut [f32]

Mutable view as f32 slice.

Source

pub fn as_ptr(&self) -> *const u8

Pointer to raw memory (useful for BLAS calls).

Source

pub fn as_mut_ptr(&mut self) -> *mut u8

Trait Implementations§

Source§

impl Buffer for CpuBuffer

Source§

fn as_bytes(&self) -> &[u8]

Returns a raw byte slice over all elements.
Source§

fn as_bytes_mut(&mut self) -> &mut [u8]

Returns a mutable raw byte slice (may be unavailable for GPU buffers).
Source§

fn len(&self) -> usize

Total capacity in bytes.
Source§

fn alignment(&self) -> usize

Alignment used when this buffer was allocated.
Source§

fn device(&self) -> &str

Textual description of where the buffer lives (e.g. “cpu”, “metal”).
Source§

fn is_empty(&self) -> bool

True if the buffer has zero capacity.
Source§

impl Debug for CpuBuffer

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for CpuBuffer

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for CpuBuffer

Source§

impl Sync for CpuBuffer

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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