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
pub use super::build_module::BuildModule;
pub use super::to_device::*;
use crate::{shapes::Dtype, tensor::DeviceStorage, tensor_ops::Device};
use super::tensor_collection::*;
pub trait Module<Input> {
type Output;
type Error: core::fmt::Debug;
fn try_forward(&self, input: Input) -> Result<Self::Output, Self::Error>;
fn forward(&self, input: Input) -> Self::Output {
self.try_forward(input).unwrap()
}
}
pub trait ModuleMut<Input> {
type Output;
type Error: core::fmt::Debug;
fn try_forward_mut(&mut self, input: Input) -> Result<Self::Output, Self::Error>;
fn forward_mut(&mut self, input: Input) -> Self::Output {
self.try_forward_mut(input).unwrap()
}
}
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)
}
}
pub trait DeviceBuildExt: DeviceStorage {
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<D: DeviceStorage> DeviceBuildExt for D {}
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())
}
}
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)
}
}