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
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum TensorInputKey {
User {
id: u64,
},
#[cfg(feature = "autodiff")]
Tangent {
of: Box<TensorInputKey>,
pass: u64,
},
}
impl TensorInputKey {
/// Returns `true` when this key names an AD tangent input.
///
/// # Examples
///
/// ```rust
/// use tenferro_ops::input_key::TensorInputKey;
///
/// let key = TensorInputKey::User { id: 0 };
/// assert!(!key.is_tangent());
/// ```
pub fn is_tangent(&self) -> bool {
match self {
TensorInputKey::User { .. } => false,
#[cfg(feature = "autodiff")]
TensorInputKey::Tangent { .. } => true,
}
}
/// Returns the user input key that owns this input's concrete primal data.
///
/// For non-AD keys this returns `self`; for tangent keys it recursively
/// follows the `of` chain to the original user input.
///
/// # Examples
///
/// ```rust
/// use tenferro_ops::input_key::TensorInputKey;
///
/// let key = TensorInputKey::User { id: 0 };
/// assert_eq!(key.primal_root(), &key);
/// ```
pub fn primal_root(&self) -> &Self {
match self {
TensorInputKey::User { .. } => self,
#[cfg(feature = "autodiff")]
TensorInputKey::Tangent { of, .. } => of.primal_root(),
}
}
#[cfg(feature = "autodiff")]
pub fn tangent_of(&self, pass: u64) -> Self {
TensorInputKey::Tangent {
of: Box::new(self.clone()),
pass,
}
}
}