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
impl AppleContext
Sourcepub fn new() -> Result<Self>
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.
Sourcepub fn domain_id(&self) -> AllocationDomainId
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 { .. }
)),
}Sourcepub fn cpu_backend(&self) -> &CpuBackend
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 { .. }
)),
}Sourcepub fn metal_backend(&self) -> &WebGpuBackend
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 { .. }
)),
}Sourcepub fn transfer_stats(&self) -> AppleTransferStats
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 { .. }
)),
}Sourcepub fn upload_tensor(&self, tensor: &Tensor) -> Result<Tensor>
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.
Sourcepub fn download_tensor(&self, tensor: &Tensor) -> Result<Tensor>
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
impl Clone for AppleContext
Source§fn clone(&self) -> AppleContext
fn clone(&self) -> AppleContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for AppleContext
impl !UnwindSafe for AppleContext
impl Freeze for AppleContext
impl Send for AppleContext
impl Sync for AppleContext
impl Unpin for AppleContext
impl UnsafeUnpin for AppleContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<C> CloneExpand for Cwhere
C: Clone,
impl<C> CloneExpand for Cwhere
C: Clone,
fn __expand_clone_method(&self, _scope: &mut Scope) -> C
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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