SharedBytesAllocationController

Struct SharedBytesAllocationController 

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

Allocation controller backed by bytes::Bytes for zero-copy access.

This enables zero-copy tensor loading by referencing data directly from the source buffer without copying into heap-allocated memory.

§Safety

This implementation uses an UnsafeCell to lazily copy the content into an in-memory buffer using [NativeAllocationController] when mutable access is requested. It is safe because the controller can’t be cloned or synced between multiple threads. You can duplicate the shared bytes allocator, but every version of it will have its own buffer.

§Notes

  • Read access via memory() is zero-copy as long as no mutable access has been requested.
  • Mutable access via memory_mut() triggers a copy of the data into heap memory (copy-on-write semantics).
  • The underlying bytes::Bytes remains valid for the lifetime of this controller. This is ensured by bytes::Bytes using reference counting internally.

Implementations§

Source§

impl SharedBytesAllocationController

Source

pub fn new( bytes: Bytes, property: AllocationProperty, ) -> SharedBytesAllocationController

Create a new controller from a bytes::Bytes buffer with a specific allocation property.

The allocation property is used by GPU backends to optimize data transfers:

§Example
use cubecl_common::bytes::{SharedBytesAllocationController, AllocationProperty};

// From static data (zero-copy, no heap allocation)
let static_bytes = bytes::Bytes::from_static(&[1, 2, 3, 4]);
let controller = SharedBytesAllocationController::new(static_bytes, AllocationProperty::Other);

// Memory-mapped file data - use File property for optimized GPU transfers
let mmap_bytes = bytes::Bytes::from_static(&[1, 2, 3, 4]); // pretend this is mmap
let controller = SharedBytesAllocationController::new(mmap_bytes, AllocationProperty::File);

Trait Implementations§

Source§

impl AllocationController for SharedBytesAllocationController

Source§

fn alloc_align(&self) -> usize

The alignment this allocation was created with.
Source§

fn property(&self) -> AllocationProperty

Returns memory property for the current allocation.
Source§

fn memory(&self) -> &[MaybeUninit<u8>]

Returns a view of the memory of the whole allocation
Source§

unsafe fn memory_mut(&mut self) -> &mut [MaybeUninit<u8>]

Returns a mutable view of the memory of the whole allocation Read more
Source§

fn split( &mut self, offset: usize, ) -> Result<(Box<dyn AllocationController>, Box<dyn AllocationController>), SplitError>

Splits the current allocation in multiple separate allocations.
Source§

fn duplicate(&self) -> Option<Box<dyn AllocationController>>

Duplicates the current allocation with a clone on write strategy if the allocation controller supports it.
Source§

unsafe fn copy_into(&self, buf: &mut [u8])

Reads the data from the current allocation controller and copy its content into the provided buffer. Read more
Source§

fn grow(&mut self, _size: usize, _align: usize) -> Result<(), AllocationError>

Extends the provided [Allocation] to a new size with specified alignment. Read more
Source§

fn try_detach(&mut self) -> Option<NonNull<u8>>

Indicates whether the allocation uses the Rust alloc crate and can be safely managed by another data structure. Read more

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

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
§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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