tenferro-gpu 0.2.0

CubeCL-backed CUDA and WebGPU provider backends for tenferro tensors.
Documentation
//! GPU backend implementations for tenferro tensors.
//!
//! # Examples
//!
//! ```rust
//! #[cfg(feature = "cuda")]
//! {
//!     use tenferro_gpu::{download_tensor, gpu_available, upload_tensor, CudaBackend};
//!     use tenferro_tensor::{Tensor, TensorElementwise};
//!
//!     if gpu_available() {
//!         let mut backend = CudaBackend::new(0).unwrap();
//!         let a = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]);
//!         let b = Tensor::from_vec_col_major(vec![2], vec![3.0_f64, 4.0]);
//!         let gpu_a = upload_tensor(backend.runtime(), &a).unwrap();
//!         let gpu_b = upload_tensor(backend.runtime(), &b).unwrap();
//!         let gpu_sum = backend.add(&gpu_a, &gpu_b).unwrap();
//!         let sum = download_tensor(backend.runtime(), &gpu_sum).unwrap();
//!         assert_eq!(sum.as_slice::<f64>().unwrap(), &[4.0, 6.0]);
//!     }
//! }
//! ```

#[cfg(feature = "cuda")]
use std::any::Any;

#[cfg(feature = "cuda")]
mod cubecl;
#[cfg(feature = "cuda")]
mod kernels;
#[cfg(feature = "webgpu")]
mod webgpu;

#[cfg(feature = "cuda")]
pub use cubecl::{
    device_ptr, download_tensor, gpu_available, upload_tensor, CudaBackend, CudaRuntime,
};
#[cfg(feature = "cuda")]
#[doc(hidden)]
pub use cubecl::{CudaExtensionCache, CudaExtensionCacheGuard};
#[cfg(feature = "webgpu")]
pub use webgpu::{
    download_webgpu_tensor, upload_webgpu_tensor, webgpu_available, WebGpuBackend, WebGpuRuntime,
};

#[cfg(feature = "cuda")]
#[doc(hidden)]
pub mod cuda_interop {
    pub use crate::cubecl::interop::*;
    pub use crate::cubecl::{CudaExtensionCache, CudaExtensionCacheGuard};
}

#[cfg(any(feature = "cuda", feature = "webgpu"))]
use tenferro_tensor::*;

#[cfg(feature = "cuda")]
pub(crate) mod backend {
    pub use tenferro_tensor::backend::*;
}

#[cfg(feature = "cuda")]
pub(crate) mod config {
    pub use tenferro_tensor::config::*;
}

#[cfg(feature = "cuda")]
pub(crate) mod types {
    pub(crate) use crate::CubeclBuffer;
    pub use tenferro_tensor::types::*;
}

/// CubeCL-managed GPU buffer stored behind tensor backend-buffer trait objects.
#[cfg(feature = "cuda")]
#[derive(Clone)]
pub(crate) struct CubeclBuffer<T> {
    handle: cubecl_runtime::server::Handle,
    len: usize,
    pub(crate) _marker: std::marker::PhantomData<T>,
}

#[cfg(feature = "cuda")]
impl<T> std::fmt::Debug for CubeclBuffer<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CubeclBuffer")
            .field("len", &self.len)
            .finish()
    }
}

#[cfg(feature = "cuda")]
impl<T> CubeclBuffer<T> {
    pub(crate) fn new(handle: cubecl_runtime::server::Handle, len: usize) -> Self {
        Self {
            handle,
            len,
            _marker: std::marker::PhantomData,
        }
    }

    pub(crate) fn handle(&self) -> &cubecl_runtime::server::Handle {
        &self.handle
    }

    pub(crate) fn element_len(&self) -> usize {
        self.len
    }
}

#[cfg(feature = "cuda")]
impl<T: Send + Sync + 'static> BackendBuffer<T> for CubeclBuffer<T> {
    fn backend_family(&self) -> &'static str {
        "cubecl"
    }

    fn len(&self) -> usize {
        self.len
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}