Skip to main content

TensorWasmLinearMemory

Struct TensorWasmLinearMemory 

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

A Wasm linear memory backed by UnifiedBuffer.

The buffer is allocated at construction time with the requested maximum (or DEFAULT_MAX_BYTES) so grow_to becomes a size check. This avoids the cost of cudaMemcpy on every memory.grow and keeps the kernel-side pointer stable across growth events.

Implementations§

Source§

impl TensorWasmLinearMemory

Source

pub fn new( minimum_bytes: usize, maximum_bytes: Option<usize>, ) -> Result<Self, UnifiedError>

Create a new linear memory.

  • minimum_bytes: initial visible size (Wasm pages × 65 536).
  • maximum_bytes: cap on growth. If None, DEFAULT_MAX_BYTES is used.
Source

pub fn new_on( minimum_bytes: usize, maximum_bytes: Option<usize>, device_id: DeviceId, ) -> Result<Self, UnifiedError>

Same as new but on a specific device.

Fails with UnifiedError::TooLarge when maximum_bytes (or the minimum_bytes floor) would exceed HARD_MAX_LINEAR_MEMORY_BYTES. This closes mem-H5 / exec-S-2 / exec-S-10: Wasmtime’s wasmtime::ResourceLimiter::memory_growing only fires on memory.grow, so a guest declaring (memory 1 65536) would otherwise force a 4 GiB allocation at instantiation. The hard cap is enforced before the backing allocator is invoked.

Source

pub fn new_on_with_tenant_context( minimum_bytes: usize, maximum_bytes: Option<usize>, device_id: DeviceId, tenant_ctx: Arc<TenantContext>, ) -> Result<Self, TensorWasmError>

Tenant-aware variant of Self::new_on.

Routes the underlying UnifiedBuffer allocation through UnifiedBuffer::new_with_visible_window_on_with_tenant_context so the tenant’s GPU memory cap is consulted before allocation and release_gpu_bytes(cap) is called on Drop. The same HARD_MAX_LINEAR_MEMORY_BYTES ceiling and min > max checks run first so a cap violation never races against a guest-bug rejection.

Roadmap feature #8 path: invoked by TensorWasmMemoryCreator::with_tenant_context / TensorWasmMemoryCreator::with_pool_and_tenant_context whenever they fall through to the no-pool fresh-allocation branch.

Source

pub fn current_size(&self) -> usize

Current logical size in bytes.

Source

pub fn capacity(&self) -> usize

Pre-allocated capacity (the hard cap).

Source

pub fn is_uvm_backed(&self) -> bool

Whether the underlying linear-memory backing is CUDA Unified Memory.

Returns true when the crate was compiled with EITHER --features unified-memory (cust path) OR --features cudarc-backend (the W1.2 spike, used as the Backing::Cudarc variant when unified-memory is off — see the precedence table in crate::unified). This is the compile-time probe that closes the v0.3.2 audit’s “wasm linear memory not UVM-backed” gap: a guest pointer resolved through the W1.1 wasi-cuda kernel-args pipeline doubles as a device pointer iff this returns true. The pool-backed PooledLinearMemory path also goes through UnifiedBuffer under the hood, so it shares this property regardless of which CUDA backing feature is active.

§Memory-growth semantics

cuMemAllocManaged returns a fixed-size allocation that cannot be resized in place. We therefore pre-allocate the requested maximum_bytes (or DEFAULT_MAX_BYTES) at construction time and treat LinearMemory::grow_to as a logical-size bump up to that cap (option (a) from the v0.3.3 design tracker). This matches Wasmtime’s static memory model, keeps the kernel-side pointer stable across growth events, and keeps the hot path zero-copy at the cost of reserving the worst-case footprint up front. Growing the physical allocation (option (b): allocate-copy-free) is a v0.4 follow-up tracked in docs/RISKS.md.

Trait Implementations§

Source§

impl Debug for TensorWasmLinearMemory

Source§

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

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

impl LinearMemory for TensorWasmLinearMemory

Source§

fn byte_size(&self) -> usize

Returns the number of allocated bytes which are accessible at this time.
Source§

fn byte_capacity(&self) -> usize

Returns byte capacity of this linear memory’s current allocation. Read more
Source§

fn grow_to(&mut self, new_size: usize) -> Result<()>

Grows this memory to have the new_size, in bytes, specified. Read more
Source§

fn as_ptr(&self) -> *mut u8

Return the allocated memory as a mutable pointer to u8.

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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<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