use std::marker::PhantomData;
use std::rc::Rc;
use cubecl::client::ComputeClient;
use cubecl::prelude::{ArrayArg, CubeCount, CubeDim, CubeElement, CubePrimitive, TensorBinding};
use cubecl_cuda::CudaRuntime as CubeclCudaRuntime;
use tenferro_tensor::{TensorRank, TensorScalar, TensorWrite, TypedTensor};
use super::runtime::CudaRuntime;
pub struct Session<'s> {
runtime: CudaRuntime,
_scope: PhantomData<&'s ()>,
_not_send_sync: PhantomData<Rc<()>>,
}
impl<'s> Session<'s> {
pub(crate) unsafe fn new(runtime: CudaRuntime) -> Self {
Self {
runtime,
_scope: PhantomData,
_not_send_sync: PhantomData,
}
}
pub fn client(&self) -> &ComputeClient<CubeclCudaRuntime> {
self.runtime.client()
}
pub fn tensor_binding<T>(
&self,
tensor: &TypedTensor<T, impl TensorRank>,
op: &'static str,
) -> crate::Result<TensorBinding<CubeclCudaRuntime>>
where
T: CubeElement + TensorScalar + Clone,
{
super::dispatch::ensure_resident_on_runtime(&self.runtime, tensor, op)?;
super::interop::typed_tensor_binding(tensor, op)
}
pub fn array_arg<T>(
&self,
tensor: &TypedTensor<T, impl TensorRank>,
op: &'static str,
) -> crate::Result<ArrayArg<CubeclCudaRuntime>>
where
T: CubeElement + TensorScalar + Clone,
{
super::dispatch::ensure_resident_on_runtime(&self.runtime, tensor, op)?;
super::interop::typed_tensor_array_arg(tensor, op)
}
pub fn alloc_output<T>(&self, shape: &[usize]) -> crate::Result<TypedTensor<T>>
where
T: CubeElement + TensorScalar + Clone + Send + Sync + 'static,
{
super::interop::alloc_output(&self.runtime, shape)
}
pub fn alloc_zero_output<T>(&self, shape: &[usize]) -> crate::Result<TypedTensor<T>>
where
T: CubeElement + CubePrimitive + TensorScalar + Clone + Send + Sync + 'static,
{
super::interop::alloc_zero_output(&self.runtime, shape)
}
pub fn scale_tensor_write(&self, output: TensorWrite<'_>, factor: f64) -> crate::Result<()> {
super::interop::scale_tensor_write(&self.runtime, output, factor)
}
pub fn cube_count_1d(&self, len: usize) -> crate::Result<CubeCount> {
super::interop::cube_count_for_len(len)
}
pub fn cube_dim_1d(&self) -> CubeDim {
super::interop::cube_dim_1d()
}
}