use core::marker::PhantomData;
#[cfg(feature = "cuda")]
use crate::tensor::device::{Cuda, CudaN};
#[cfg(feature = "wgpu")]
use crate::tensor::device::{Wgpu, WgpuN};
use crate::shapes::{Dim, ShapeBuf};
use crate::tensor::device::{Cpu, DeviceId};
use crate::tensor::dtype::{DTypeDescriptor, DTypeId};
use crate::tensor::grad::{Grad, NoGrad};
use typenum::{Bit, UInt, UTerm, Unsigned};
use alloc::vec::Vec;
pub trait ArgInto<Target> {
fn into_arg(self) -> Target;
}
pub trait LayerArgInto<Target> {
fn into_layer_arg(self) -> Target;
}
incin_macros::impl_layer_args!(9);
#[derive(Debug, Clone)]
pub struct TensorArgsData<S, T, D, G> {
pub(crate) shape: S,
pub(crate) dtype: T,
pub(crate) device: D,
pub(crate) grad: G,
}
pub trait NotUnit {}
macro_rules! impl_self_arginto {
($($t:ty)*) => {
$(
impl ArgInto<$t> for $t {
#[inline(always)]
fn into_arg(self) -> $t {
self
}
}
)*
};
}
impl_self_arginto! {
f32
f64
usize
bool
DTypeId
DTypeDescriptor
DeviceId
Cpu
Grad
NoGrad
ShapeBuf
}
pub trait UnitTree: Default + Copy + Clone + core::fmt::Debug {}
impl UnitTree for () {}
impl<T: UnitTree> UnitTree for ((), T) {}
impl UnitTree for ((),) {}
impl UnitTree for ((), (), ()) {}
impl UnitTree for ((), (), (), ()) {}
impl UnitTree for ((), (), (), (), ()) {}
impl UnitTree for ((), (), (), (), (), ()) {}
impl UnitTree for ((), (), (), (), (), (), ()) {}
impl UnitTree for ((), (), (), (), (), (), (), ()) {}
impl<T: UnitTree> ArgInto<T> for () {
#[inline(always)]
fn into_arg(self) -> T {
T::default()
}
}
impl ArgInto<DTypeDescriptor> for DTypeId {
#[inline(always)]
fn into_arg(self) -> DTypeDescriptor {
self.descriptor()
}
}
impl ArgInto<UTerm> for UTerm {
#[inline(always)]
fn into_arg(self) -> UTerm {
self
}
}
impl<U, B> ArgInto<UInt<U, B>> for UInt<U, B>
where
U: Unsigned + Dim,
B: Bit + Default + Copy + Clone + core::fmt::Debug + Send + Sync + Eq + PartialEq + 'static,
UInt<U, B>: Unsigned
+ Default
+ Copy
+ Clone
+ core::fmt::Debug
+ Send
+ Sync
+ Eq
+ PartialEq
+ 'static,
{
#[inline(always)]
fn into_arg(self) -> UInt<U, B> {
self
}
}
#[cfg(feature = "cuda")]
impl ArgInto<Cuda> for Cuda {
#[inline(always)]
fn into_arg(self) -> Cuda {
self
}
}
#[cfg(feature = "wgpu")]
impl ArgInto<Wgpu> for Wgpu {
#[inline(always)]
fn into_arg(self) -> Wgpu {
self
}
}
#[cfg(feature = "cuda")]
impl<N: Unsigned> ArgInto<CudaN<N>> for CudaN<N> {
#[inline(always)]
fn into_arg(self) -> CudaN<N> {
self
}
}
#[cfg(feature = "wgpu")]
impl<N: Unsigned> ArgInto<WgpuN<N>> for WgpuN<N> {
#[inline(always)]
fn into_arg(self) -> WgpuN<N> {
self
}
}
impl<D: Dim> ArgInto<Vec<D>> for Vec<D> {
#[inline(always)]
fn into_arg(self) -> Vec<D> {
self
}
}
impl<const N: usize> ArgInto<Vec<usize>> for [usize; N] {
#[inline(always)]
fn into_arg(self) -> Vec<usize> {
self.to_vec()
}
}
impl ArgInto<Vec<usize>> for &[usize] {
#[inline(always)]
fn into_arg(self) -> Vec<usize> {
self.to_vec()
}
}
impl<const N: usize> ArgInto<[usize; N]> for [usize; N] {
#[inline(always)]
fn into_arg(self) -> [usize; N] {
self
}
}
impl ArgInto<(usize, ())> for [usize; 1] {
#[inline(always)]
fn into_arg(self) -> (usize, ()) {
(self[0], ())
}
}
impl ArgInto<(usize, (usize, ()))> for [usize; 2] {
#[inline(always)]
fn into_arg(self) -> (usize, (usize, ())) {
(self[0], (self[1], ()))
}
}
impl ArgInto<(usize, (usize, (usize, ())))> for [usize; 3] {
#[inline(always)]
fn into_arg(self) -> (usize, (usize, (usize, ()))) {
(self[0], (self[1], (self[2], ())))
}
}
type Dim4Tuple = (usize, (usize, (usize, (usize, ()))));
impl ArgInto<Dim4Tuple> for [usize; 4] {
#[inline(always)]
fn into_arg(self) -> (usize, (usize, (usize, (usize, ())))) {
(self[0], (self[1], (self[2], (self[3], ()))))
}
}
impl<A, TA> ArgInto<(TA, ())> for (A,)
where
A: ArgInto<TA>,
{
#[inline(always)]
fn into_arg(self) -> (TA, ()) {
(self.0.into_arg(), ())
}
}
impl<A, B, TA, TB> ArgInto<(TA, (TB, ()))> for (A, B)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
{
#[inline(always)]
fn into_arg(self) -> (TA, (TB, ())) {
(self.0.into_arg(), (self.1.into_arg(), ()))
}
}
impl<A, B, C, TA, TB, TC> ArgInto<(TA, (TB, (TC, ())))> for (A, B, C)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
C: ArgInto<TC>,
{
#[inline(always)]
fn into_arg(self) -> (TA, (TB, (TC, ()))) {
(
self.0.into_arg(),
(self.1.into_arg(), (self.2.into_arg(), ())),
)
}
}
impl<A, B, C, D, TA, TB, TC, TD> ArgInto<(TA, (TB, (TC, (TD, ()))))> for (A, B, C, D)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
C: ArgInto<TC>,
D: ArgInto<TD>,
{
#[inline(always)]
fn into_arg(self) -> (TA, (TB, (TC, (TD, ())))) {
(
self.0.into_arg(),
(
self.1.into_arg(),
(self.2.into_arg(), (self.3.into_arg(), ())),
),
)
}
}
type Dim5Arg = ((), ((), ((), (usize, ((), ())))));
impl ArgInto<Dim5Arg> for Dim5Arg {
#[inline(always)]
fn into_arg(self) -> Dim5Arg {
self
}
}
macro_rules! impl_not_unit {
($($t:ty)*) => {
$(
impl NotUnit for $t {}
)*
};
}
impl_not_unit! {
f32
f64
usize
bool
&usize
Cpu
Grad
NoGrad
DTypeId
DTypeDescriptor
DeviceId
ShapeBuf
}
impl<T1> NotUnit for (T1,) {}
impl<T1, T2> NotUnit for (T1, T2) {}
impl<T1, T2, T3> NotUnit for (T1, T2, T3) {}
impl<T1, T2, T3, T4> NotUnit for (T1, T2, T3, T4) {}
impl<T1, T2, T3, T4, T5> NotUnit for (T1, T2, T3, T4, T5) {}
impl<T1, T2, T3, T4, T5, T6> NotUnit for (T1, T2, T3, T4, T5, T6) {}
impl<T1, T2, T3, T4, T5, T6, T7> NotUnit for (T1, T2, T3, T4, T5, T6, T7) {}
impl<T1, T2, T3, T4, T5, T6, T7, T8> NotUnit for (T1, T2, T3, T4, T5, T6, T7, T8) {}
impl<T, const N: usize> NotUnit for [T; N] {}
impl<T, const N: usize> NotUnit for &[T; N] {}
impl NotUnit for UTerm {}
impl<U, B> NotUnit for UInt<U, B>
where
U: Unsigned + Dim,
B: Bit + Default + Copy + Clone + core::fmt::Debug + Send + Sync + Eq + PartialEq + 'static,
UInt<U, B>: Unsigned
+ Default
+ Copy
+ Clone
+ core::fmt::Debug
+ Send
+ Sync
+ Eq
+ PartialEq
+ 'static,
{
}
#[cfg(feature = "cuda")]
impl NotUnit for Cuda {}
#[cfg(feature = "wgpu")]
impl NotUnit for Wgpu {}
#[cfg(feature = "cuda")]
impl<N: Unsigned> NotUnit for CudaN<N> {}
#[cfg(feature = "wgpu")]
impl<N: Unsigned> NotUnit for WgpuN<N> {}
impl<D: Dim> NotUnit for Vec<D> {}
macro_rules! impl_dim_tuple_arg_into {
($($name:ident),+ $(,)?) => {
impl<$($name,)*> ArgInto<($($name,)*)> for ($($name,)*) {
#[inline(always)]
fn into_arg(self) -> ($($name,)*) {
self
}
}
};
}
impl_dim_tuple_arg_into!(D0, D1, D2);
impl_dim_tuple_arg_into!(D0, D1, D2, D3, D4);
impl_dim_tuple_arg_into!(D0, D1, D2, D3, D4, D5);
impl_dim_tuple_arg_into!(D0, D1, D2, D3, D4, D5, D6);
impl_dim_tuple_arg_into!(D0, D1, D2, D3, D4, D5, D6, D7);
impl ArgInto<TensorArgsData<(), (), (), ()>> for () {
#[inline(always)]
fn into_arg(self) -> TensorArgsData<(), (), (), ()> {
TensorArgsData {
shape: (),
dtype: (),
device: (),
grad: (),
}
}
}
impl<A: ArgInto<B>, B: NotUnit> ArgInto<TensorArgsData<B, (), (), ()>> for A {
#[inline(always)]
fn into_arg(self) -> TensorArgsData<B, (), (), ()> {
TensorArgsData {
shape: self.into_arg(),
dtype: (),
device: (),
grad: (),
}
}
}
impl<A: ArgInto<B>, B: NotUnit> ArgInto<TensorArgsData<(), B, (), ()>> for A {
#[inline(always)]
fn into_arg(self) -> TensorArgsData<(), B, (), ()> {
TensorArgsData {
shape: (),
dtype: self.into_arg(),
device: (),
grad: (),
}
}
}
impl<A: ArgInto<B>, B: NotUnit> ArgInto<TensorArgsData<(), (), B, ()>> for A {
#[inline(always)]
fn into_arg(self) -> TensorArgsData<(), (), B, ()> {
TensorArgsData {
shape: (),
dtype: (),
device: self.into_arg(),
grad: (),
}
}
}
impl<A: ArgInto<B>, B: NotUnit> ArgInto<TensorArgsData<(), (), (), B>> for A {
#[inline(always)]
fn into_arg(self) -> TensorArgsData<(), (), (), B> {
TensorArgsData {
shape: (),
dtype: (),
device: (),
grad: self.into_arg(),
}
}
}
impl<A, B, TA, TB> ArgInto<TensorArgsData<TA, TB, (), ()>> for (A, B)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
TA: NotUnit,
TB: NotUnit,
{
fn into_arg(self) -> TensorArgsData<TA, TB, (), ()> {
TensorArgsData {
shape: self.0.into_arg(),
dtype: self.1.into_arg(),
device: (),
grad: (),
}
}
}
impl<A, B, TA, TB> ArgInto<TensorArgsData<TA, (), TB, ()>> for (A, B)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
TA: NotUnit,
TB: NotUnit,
{
fn into_arg(self) -> TensorArgsData<TA, (), TB, ()> {
TensorArgsData {
shape: self.0.into_arg(),
dtype: (),
device: self.1.into_arg(),
grad: (),
}
}
}
impl<A, B, TA, TB> ArgInto<TensorArgsData<TA, (), (), TB>> for (A, B)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
TA: NotUnit,
TB: NotUnit,
{
fn into_arg(self) -> TensorArgsData<TA, (), (), TB> {
TensorArgsData {
shape: self.0.into_arg(),
dtype: (),
device: (),
grad: self.1.into_arg(),
}
}
}
impl<A, B, TA, TB> ArgInto<TensorArgsData<(), TA, TB, ()>> for (A, B)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
TA: NotUnit,
TB: NotUnit,
{
fn into_arg(self) -> TensorArgsData<(), TA, TB, ()> {
TensorArgsData {
shape: (),
dtype: self.0.into_arg(),
device: self.1.into_arg(),
grad: (),
}
}
}
impl<A, B, TA, TB> ArgInto<TensorArgsData<(), TA, (), TB>> for (A, B)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
TA: NotUnit,
TB: NotUnit,
{
fn into_arg(self) -> TensorArgsData<(), TA, (), TB> {
TensorArgsData {
shape: (),
dtype: self.0.into_arg(),
device: (),
grad: self.1.into_arg(),
}
}
}
impl<A, B, TA, TB> ArgInto<TensorArgsData<(), (), TA, TB>> for (A, B)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
TA: NotUnit,
TB: NotUnit,
{
fn into_arg(self) -> TensorArgsData<(), (), TA, TB> {
TensorArgsData {
shape: (),
dtype: (),
device: self.0.into_arg(),
grad: self.1.into_arg(),
}
}
}
impl<A, B, C, TA, TB, TC> ArgInto<TensorArgsData<(), TA, TB, TC>> for (A, B, C)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
C: ArgInto<TC>,
TA: NotUnit,
TB: NotUnit,
TC: NotUnit,
{
fn into_arg(self) -> TensorArgsData<(), TA, TB, TC> {
TensorArgsData {
shape: (),
dtype: self.0.into_arg(),
device: self.1.into_arg(),
grad: self.2.into_arg(),
}
}
}
impl<A, B, C, TA, TB, TC> ArgInto<TensorArgsData<TA, (), TB, TC>> for (A, B, C)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
C: ArgInto<TC>,
TA: NotUnit,
TB: NotUnit,
TC: NotUnit,
{
fn into_arg(self) -> TensorArgsData<TA, (), TB, TC> {
TensorArgsData {
shape: self.0.into_arg(),
dtype: (),
device: self.1.into_arg(),
grad: self.2.into_arg(),
}
}
}
impl<A, B, C, TA, TB, TC> ArgInto<TensorArgsData<TA, TB, (), TC>> for (A, B, C)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
C: ArgInto<TC>,
TA: NotUnit,
TB: NotUnit,
TC: NotUnit,
{
fn into_arg(self) -> TensorArgsData<TA, TB, (), TC> {
TensorArgsData {
shape: self.0.into_arg(),
dtype: self.1.into_arg(),
device: (),
grad: self.2.into_arg(),
}
}
}
impl<A, B, C, TA, TB, TC> ArgInto<TensorArgsData<TA, TB, TC, ()>> for (A, B, C)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
C: ArgInto<TC>,
TA: NotUnit,
TB: NotUnit,
TC: NotUnit,
{
fn into_arg(self) -> TensorArgsData<TA, TB, TC, ()> {
TensorArgsData {
shape: self.0.into_arg(),
dtype: self.1.into_arg(),
device: self.2.into_arg(),
grad: (),
}
}
}
impl<A, B, C, D, TA, TB, TC, TD> ArgInto<TensorArgsData<TA, TB, TC, TD>> for (A, B, C, D)
where
A: ArgInto<TA>,
B: ArgInto<TB>,
C: ArgInto<TC>,
D: ArgInto<TD>,
TA: NotUnit,
TB: NotUnit,
TC: NotUnit,
TD: NotUnit,
{
fn into_arg(self) -> TensorArgsData<TA, TB, TC, TD> {
TensorArgsData {
shape: self.0.into_arg(),
dtype: self.1.into_arg(),
device: self.2.into_arg(),
grad: self.3.into_arg(),
}
}
}
impl<T> ArgInto<PhantomData<T>> for PhantomData<T> {
#[inline(always)]
fn into_arg(self) -> PhantomData<T> {
self
}
}