1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
pub use super::build_module::BuildModule;
pub use super::to_device::*;

use crate::{shapes::Dtype, tensor_ops::Device};

use super::tensor_collection::*;

/// Immutable forward of `Input` that produces [Module::Output].
/// See [ModuleMut] for mutable forward.
pub trait Module<Input> {
    /// The type that this unit produces given `Input`.
    type Output;
    type Error: core::fmt::Debug;

    fn try_forward(&self, input: Input) -> Result<Self::Output, Self::Error>;

    /// Forward `Input` through the module and produce [Module::Output].
    ///
    /// **See [ModuleMut::forward_mut()] for version that can mutate `self`.**
    fn forward(&self, input: Input) -> Self::Output {
        self.try_forward(input).unwrap()
    }
}

/// Mutable forward of `Input` that produces [ModuleMut::Output].
/// See [Module] for immutable forward.
pub trait ModuleMut<Input> {
    /// The type that this unit produces given `Input`.
    type Output;
    type Error: core::fmt::Debug;

    fn try_forward_mut(&mut self, input: Input) -> Result<Self::Output, Self::Error>;

    /// Forward `Input` through the module and produce [ModuleMut::Output].
    ///
    /// **See [Module::forward()] for immutable version**
    fn forward_mut(&mut self, input: Input) -> Self::Output {
        self.try_forward_mut(input).unwrap()
    }
}

/// Something that can be built on a different device
/// than it is on.
///
/// Related to [BuildModule]
pub trait BuildOnDevice<D: Device<E>, E: Dtype> {
    type Built: BuildModule<D, E>;
    fn build_on_device(device: &D) -> Self::Built {
        Self::try_build_on_device(device).unwrap()
    }
    fn try_build_on_device(device: &D) -> Result<Self::Built, D::Err> {
        Self::Built::try_build(device)
    }
}

/// An extension trait that allows you to build a module with a device
/// method. Also allows easy specification of Dtype.
pub trait DeviceBuildExt {
    fn build_module<M: BuildOnDevice<Self, E>, E: Dtype>(&self) -> M::Built
    where
        Self: Device<E>,
    {
        M::build_on_device(self)
    }
    fn try_build_module<M: BuildOnDevice<Self, E>, E: Dtype>(&self) -> Result<M::Built, Self::Err>
    where
        Self: Device<E>,
    {
        M::try_build_on_device(self)
    }
}
impl DeviceBuildExt for crate::tensor::Cpu {}
#[cfg(feature = "cuda")]
impl DeviceBuildExt for crate::tensor::Cuda {}

/// Marker trait for modules with no updatable parameters. These have
/// blanket impls for, and [ModuleMut]
pub trait ZeroSizedModule: Default {}

impl<T: ZeroSizedModule + BuildModule<D, E>, D: Device<E>, E: Dtype> BuildOnDevice<D, E> for T {
    type Built = T;
}

impl<E: Dtype, D: Device<E>, T: ZeroSizedModule> TensorCollection<E, D> for T {
    type To<E2: Dtype, D2: Device<E2>> = T;

    fn iter_tensors<V: ModuleVisitor<Self, E, D>>(
        visitor: &mut V,
    ) -> Result<Option<Self::To<V::E2, V::D2>>, V::Err> {
        visitor.visit_fields((), |_| Default::default())
    }
}

/// Marker trait for modules that don't have different behavior between
/// mutable forwards and non-mutable forwards
pub trait NonMutableModule {}

impl<M: NonMutableModule, T> ModuleMut<T> for M
where
    Self: Module<T>,
{
    type Output = <Self as Module<T>>::Output;
    type Error = <Self as Module<T>>::Error;

    fn try_forward_mut(&mut self, input: T) -> Result<Self::Output, Self::Error> {
        self.try_forward(input)
    }
}