|
| 1 | +//! This crate handles the user facing autodiff macro. For each `#[autodiff(...)]` attribute, |
| 2 | +//! we create an [`BatchItem`] which contains the source and target function names. The source |
| 3 | +//! is the function to which the autodiff attribute is applied, and the target is the function |
| 4 | +//! getting generated by us (with a name given by the user as the first autodiff arg). |
| 5 | +
|
| 6 | +use std::fmt::{self, Display, Formatter}; |
| 7 | +use std::str::FromStr; |
| 8 | + |
| 9 | +use crate::expand::{Decodable, Encodable, HashStable_Generic}; |
| 10 | + |
| 11 | +/// Documentation for using [reverse](https://enzyme.mit.edu/rust/rev.html) and |
| 12 | +/// [forward](https://enzyme.mit.edu/rust/fwd.html) mode is available online. |
| 13 | +#[derive(Clone, Copy, Eq, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] |
| 14 | +pub enum BatchMode { |
| 15 | + /// No vectorization is applied (used during error handling). |
| 16 | + Error, |
| 17 | + /// The primal function which we will vectorize. |
| 18 | + Source, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Clone, Copy, Eq, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] |
| 22 | +pub enum BatchActivity { |
| 23 | + /// Don't batch this argument |
| 24 | + Const, |
| 25 | + /// Unsafe. |
| 26 | + Leaf, |
| 27 | + /// Just receive this argument N times. |
| 28 | + Vector, |
| 29 | +} |
| 30 | +/// We generate one of these structs for each `#[autodiff(...)]` attribute. |
| 31 | +#[derive(Clone, Eq, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] |
| 32 | +pub struct BatchItem { |
| 33 | + /// The name of the function getting differentiated |
| 34 | + pub source: String, |
| 35 | + /// The name of the function being generated |
| 36 | + pub target: String, |
| 37 | + pub attrs: BatchAttrs, |
| 38 | +} |
| 39 | +#[derive(Clone, Eq, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] |
| 40 | +pub struct BatchAttrs { |
| 41 | + /// Conceptually either forward or reverse mode AD, as described in various autodiff papers and |
| 42 | + /// e.g. in the [JAX |
| 43 | + /// Documentation](https://jax.readthedocs.io/en/latest/_tutorials/advanced-autodiff.html#how-it-s-made-two-foundational-autodiff-functions). |
| 44 | + pub mode: BatchMode, |
| 45 | + pub width: usize, |
| 46 | + pub input_activity: Vec<BatchActivity>, |
| 47 | +} |
| 48 | + |
| 49 | +impl Display for BatchMode { |
| 50 | + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 51 | + match self { |
| 52 | + BatchMode::Error => write!(f, "Error"), |
| 53 | + BatchMode::Source => write!(f, "Source"), |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl Display for BatchActivity { |
| 59 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 60 | + match self { |
| 61 | + BatchActivity::Const => write!(f, "Const"), |
| 62 | + BatchActivity::Leaf => write!(f, "Leaf"), |
| 63 | + BatchActivity::Vector => write!(f, "Vector"), |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl FromStr for BatchMode { |
| 69 | + type Err = (); |
| 70 | + |
| 71 | + fn from_str(s: &str) -> Result<BatchMode, ()> { |
| 72 | + match s { |
| 73 | + "Error" => Ok(BatchMode::Error), |
| 74 | + "Source" => Ok(BatchMode::Source), |
| 75 | + _ => Err(()), |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +impl FromStr for BatchActivity { |
| 80 | + type Err = (); |
| 81 | + |
| 82 | + fn from_str(s: &str) -> Result<BatchActivity, ()> { |
| 83 | + match s { |
| 84 | + "Const" => Ok(BatchActivity::Const), |
| 85 | + "Leaf" => Ok(BatchActivity::Leaf), |
| 86 | + "Vector" => Ok(BatchActivity::Vector), |
| 87 | + _ => Err(()), |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl BatchAttrs { |
| 93 | + |
| 94 | + pub fn error() -> Self { |
| 95 | + BatchAttrs { |
| 96 | + mode: BatchMode::Error, |
| 97 | + width: 0, |
| 98 | + input_activity: Vec::new(), |
| 99 | + } |
| 100 | + } |
| 101 | + pub fn source() -> Self { |
| 102 | + BatchAttrs { |
| 103 | + mode: BatchMode::Source, |
| 104 | + width: 0, |
| 105 | + input_activity: Vec::new(), |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + pub fn is_active(&self) -> bool { |
| 110 | + self.mode != BatchMode::Error |
| 111 | + } |
| 112 | + |
| 113 | + pub fn is_source(&self) -> bool { |
| 114 | + self.mode == BatchMode::Source |
| 115 | + } |
| 116 | + pub fn apply_batch(&self) -> bool { |
| 117 | + !matches!(self.mode, BatchMode::Error | BatchMode::Source) |
| 118 | + } |
| 119 | + |
| 120 | + pub fn into_item(self, source: String, target: String) -> BatchItem { |
| 121 | + BatchItem { source, target, attrs: self } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +impl fmt::Display for BatchItem { |
| 126 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 127 | + write!(f, "Batching {} -> {}", self.source, self.target)?; |
| 128 | + write!(f, " with attributes: {:?}", self.attrs) |
| 129 | + } |
| 130 | +} |
0 commit comments