Skip to main content

AppleContext

Struct AppleContext 

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

Explicit Apple shared-allocation context for CPU and Metal backends.

Every context owns a fresh host-visible Metal client and allocation domain. Tensors created through Self::upload_tensor can be mapped by the paired CPU backend and launched by the paired Metal backend without implicit transfers. The initial mapped CPU operations are RustFFT and rank-2 Cholesky; this context does not turn other CPU operations into automatic fallbacks. Clone Self::cpu_backend or Self::metal_backend to obtain the mutable handle required by an explicitly selected operation.

§Examples

use tenferro_gpu::apple::{AppleContext, AppleTransferStats};

match AppleContext::new() {
    Ok(context) => {
        assert_eq!(
            context.cpu_backend().allocation_domain(),
            Some(context.domain_id())
        );
        assert_eq!(context.transfer_stats(), AppleTransferStats::default());
    }
    Err(error) => assert!(matches!(
        error,
        tenferro_tensor::Error::RuntimeState { .. }
            | tenferro_tensor::Error::BackendSource { .. }
    )),
}

Implementations§

Source§

impl AppleContext

Source

pub fn new() -> Result<Self>

Create an independent host-visible Metal context and paired CPU backend.

§Examples
use tenferro_gpu::apple::{AppleContext, AppleTransferStats};

match AppleContext::new() {
    Ok(context) => {
        assert_eq!(
            context.cpu_backend().allocation_domain(),
            Some(context.domain_id())
        );
        assert_eq!(context.transfer_stats(), AppleTransferStats::default());
    }
    Err(error) => assert!(matches!(
        error,
        tenferro_tensor::Error::RuntimeState { .. }
            | tenferro_tensor::Error::BackendSource { .. }
    )),
}
§Errors

Returns a typed runtime or backend error when Metal or host-visible primary buffers are unavailable.

Source

pub fn domain_id(&self) -> AllocationDomainId

Return this context’s allocation-domain identity.

§Examples
use tenferro_gpu::apple::AppleContext;

match AppleContext::new() {
    Ok(context) => assert_eq!(
        context.cpu_backend().allocation_domain(),
        Some(context.domain_id())
    ),
    Err(error) => assert!(matches!(
        error,
        tenferro_tensor::Error::RuntimeState { .. }
            | tenferro_tensor::Error::BackendSource { .. }
    )),
}
Source

pub fn cpu_backend(&self) -> &CpuBackend

Return the paired CPU backend.

§Examples
use tenferro_gpu::apple::AppleContext;

match AppleContext::new() {
    Ok(context) => assert_eq!(
        context.cpu_backend().allocation_domain(),
        Some(context.domain_id())
    ),
    Err(error) => assert!(matches!(
        error,
        tenferro_tensor::Error::RuntimeState { .. }
            | tenferro_tensor::Error::BackendSource { .. }
    )),
}
Source

pub fn metal_backend(&self) -> &WebGpuBackend

Return the paired Metal WebGPU backend.

§Examples
use tenferro_gpu::apple::AppleContext;

match AppleContext::new() {
    Ok(context) => {
        let backend = context.metal_backend();
        assert_eq!(
            backend.runtime_identity(),
            backend.runtime().runtime_identity()
        );
    }
    Err(error) => assert!(matches!(
        error,
        tenferro_tensor::Error::RuntimeState { .. }
            | tenferro_tensor::Error::BackendSource { .. }
    )),
}
Source

pub fn transfer_stats(&self) -> AppleTransferStats

Return an atomic snapshot of explicit transfer counters.

§Examples
use tenferro_gpu::apple::{AppleContext, AppleTransferStats};

match AppleContext::new() {
    Ok(context) => assert_eq!(
        context.transfer_stats(),
        AppleTransferStats::default()
    ),
    Err(error) => assert!(matches!(
        error,
        tenferro_tensor::Error::RuntimeState { .. }
            | tenferro_tensor::Error::BackendSource { .. }
    )),
}
Source

pub fn upload_tensor(&self, tensor: &Tensor) -> Result<Tensor>

Create a managed tensor by explicitly copying a host tensor once.

§Examples
use tenferro_gpu::apple::{AppleContext, AppleTransferStats};
use tenferro_tensor::{Tensor, TypedTensor};

match AppleContext::new() {
    Ok(context) => {
        let host = Tensor::from_vec_col_major([2], vec![1.0_f32, 2.0])?;
        let managed: TypedTensor<f32> = match context.upload_tensor(&host)? {
            Tensor::F32(typed) => typed,
            _ => unreachable!("expected f32 tensor"),
        };
        assert_eq!(managed.allocation_domain(), Some(context.domain_id()));
        assert_eq!(managed.with_host_read(|data| data.to_vec())?, [1.0, 2.0]);
        assert_eq!(
            context.transfer_stats(),
            AppleTransferStats {
                uploaded_bytes: 8,
                downloaded_bytes: 0,
            }
        );
    }
    Err(error) => assert!(matches!(
        error,
        tenferro_tensor::Error::RuntimeState { .. }
            | tenferro_tensor::Error::BackendSource { .. }
    )),
}
§Errors

Returns crate::Error::RuntimeState when the input is not host resident, crate::Error::Validation for invalid tensor metadata, or crate::Error::BackendSource when managed allocation or queue synchronization fails.

Source

pub fn download_tensor(&self, tensor: &Tensor) -> Result<Tensor>

Explicitly copy a matching managed tensor back to host memory.

§Examples
use tenferro_gpu::apple::{AppleContext, AppleTransferStats};
use tenferro_tensor::Tensor;

match AppleContext::new() {
    Ok(context) => {
        let host = Tensor::from_vec_col_major([2], vec![1.0_f32, 2.0])?;
        let managed = context.upload_tensor(&host)?;
        let downloaded = context.download_tensor(&managed)?;
        assert_eq!(downloaded.as_slice::<f32>()?, &[1.0, 2.0]);
        assert_eq!(
            context.transfer_stats(),
            AppleTransferStats {
                uploaded_bytes: 8,
                downloaded_bytes: 8,
            }
        );
    }
    Err(error) => assert!(matches!(
        error,
        tenferro_tensor::Error::RuntimeState { .. }
            | tenferro_tensor::Error::BackendSource { .. }
    )),
}
§Errors

Returns crate::Error::HostAccess for a foreign allocation domain or a typed backend error when readback fails.

Trait Implementations§

Source§

impl Clone for AppleContext

Source§

fn clone(&self) -> AppleContext

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 AppleContext

Source§

fn fmt(&self, formatter: &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
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<C> CloneExpand for C
where C: Clone,

§

fn __expand_clone_method(&self, _scope: &mut Scope) -> C

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
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

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.

§

impl<T> IntoComptime for T

§

fn comptime(self) -> Self

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
§

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,

§

impl<T> MaybeSync for T
where T: Sync,

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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

impl<T> TuneInputs for T
where T: Clone + Send + Sync + 'static,

§

type At<'a> = T

The concrete input type at lifetime 'a.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,