|
| 1 | +use std::fmt::{self, Display, Formatter}; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +use crate::expand::typetree::TypeTree; |
| 5 | +use crate::expand::{Decodable, Encodable, HashStable_Generic}; |
| 6 | +use crate::ptr::P; |
| 7 | +use crate::{Ty, TyKind}; |
| 8 | + |
| 9 | +#[derive(Clone, Copy, Eq, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] |
| 10 | +pub enum DiffMode { |
| 11 | + Inactive, |
| 12 | + Source, |
| 13 | + Forward, |
| 14 | + Reverse, |
| 15 | + ForwardFirst, |
| 16 | + ReverseFirst, |
| 17 | +} |
| 18 | + |
| 19 | +pub fn is_rev(mode: DiffMode) -> bool { |
| 20 | + match mode { |
| 21 | + DiffMode::Reverse | DiffMode::ReverseFirst => true, |
| 22 | + _ => false, |
| 23 | + } |
| 24 | +} |
| 25 | +pub fn is_fwd(mode: DiffMode) -> bool { |
| 26 | + match mode { |
| 27 | + DiffMode::Forward | DiffMode::ForwardFirst => true, |
| 28 | + _ => false, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl Display for DiffMode { |
| 33 | + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 34 | + match self { |
| 35 | + DiffMode::Inactive => write!(f, "Inactive"), |
| 36 | + DiffMode::Source => write!(f, "Source"), |
| 37 | + DiffMode::Forward => write!(f, "Forward"), |
| 38 | + DiffMode::Reverse => write!(f, "Reverse"), |
| 39 | + DiffMode::ForwardFirst => write!(f, "ForwardFirst"), |
| 40 | + DiffMode::ReverseFirst => write!(f, "ReverseFirst"), |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +pub fn valid_ret_activity(mode: DiffMode, activity: DiffActivity) -> bool { |
| 46 | + if activity == DiffActivity::None { |
| 47 | + // Only valid if primal returns (), but we can't check that here. |
| 48 | + return true; |
| 49 | + } |
| 50 | + match mode { |
| 51 | + DiffMode::Inactive => false, |
| 52 | + DiffMode::Source => false, |
| 53 | + DiffMode::Forward | DiffMode::ForwardFirst => { |
| 54 | + activity == DiffActivity::Dual |
| 55 | + || activity == DiffActivity::DualOnly |
| 56 | + || activity == DiffActivity::Const |
| 57 | + } |
| 58 | + DiffMode::Reverse | DiffMode::ReverseFirst => { |
| 59 | + activity == DiffActivity::Const |
| 60 | + || activity == DiffActivity::Active |
| 61 | + || activity == DiffActivity::ActiveOnly |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +fn is_ptr_or_ref(ty: &Ty) -> bool { |
| 66 | + match ty.kind { |
| 67 | + TyKind::Ptr(_) | TyKind::Ref(_, _) => true, |
| 68 | + _ => false, |
| 69 | + } |
| 70 | +} |
| 71 | +// TODO We should make this more robust to also |
| 72 | +// accept aliases of f32 and f64 |
| 73 | +//fn is_float(ty: &Ty) -> bool { |
| 74 | +// false |
| 75 | +//} |
| 76 | +pub fn valid_ty_for_activity(ty: &P<Ty>, activity: DiffActivity) -> bool { |
| 77 | + if is_ptr_or_ref(ty) { |
| 78 | + return activity == DiffActivity::Dual |
| 79 | + || activity == DiffActivity::DualOnly |
| 80 | + || activity == DiffActivity::Duplicated |
| 81 | + || activity == DiffActivity::DuplicatedOnly |
| 82 | + || activity == DiffActivity::Const; |
| 83 | + } |
| 84 | + true |
| 85 | + //if is_scalar_ty(&ty) { |
| 86 | + // return activity == DiffActivity::Active || activity == DiffActivity::ActiveOnly || |
| 87 | + // activity == DiffActivity::Const; |
| 88 | + //} |
| 89 | +} |
| 90 | +pub fn valid_input_activity(mode: DiffMode, activity: DiffActivity) -> bool { |
| 91 | + return match mode { |
| 92 | + DiffMode::Inactive => false, |
| 93 | + DiffMode::Source => false, |
| 94 | + DiffMode::Forward | DiffMode::ForwardFirst => { |
| 95 | + // These are the only valid cases |
| 96 | + activity == DiffActivity::Dual |
| 97 | + || activity == DiffActivity::DualOnly |
| 98 | + || activity == DiffActivity::Const |
| 99 | + } |
| 100 | + DiffMode::Reverse | DiffMode::ReverseFirst => { |
| 101 | + // These are the only valid cases |
| 102 | + activity == DiffActivity::Active |
| 103 | + || activity == DiffActivity::ActiveOnly |
| 104 | + || activity == DiffActivity::Const |
| 105 | + || activity == DiffActivity::Duplicated |
| 106 | + || activity == DiffActivity::DuplicatedOnly |
| 107 | + } |
| 108 | + }; |
| 109 | +} |
| 110 | +pub fn invalid_input_activities(mode: DiffMode, activity_vec: &[DiffActivity]) -> Option<usize> { |
| 111 | + for i in 0..activity_vec.len() { |
| 112 | + if !valid_input_activity(mode, activity_vec[i]) { |
| 113 | + return Some(i); |
| 114 | + } |
| 115 | + } |
| 116 | + None |
| 117 | +} |
| 118 | + |
| 119 | +#[allow(dead_code)] |
| 120 | +#[derive(Clone, Copy, Eq, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] |
| 121 | +pub enum DiffActivity { |
| 122 | + None, |
| 123 | + Const, |
| 124 | + Active, |
| 125 | + ActiveOnly, |
| 126 | + Dual, |
| 127 | + DualOnly, |
| 128 | + Duplicated, |
| 129 | + DuplicatedOnly, |
| 130 | + FakeActivitySize, |
| 131 | +} |
| 132 | + |
| 133 | +impl Display for DiffActivity { |
| 134 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 135 | + match self { |
| 136 | + DiffActivity::None => write!(f, "None"), |
| 137 | + DiffActivity::Const => write!(f, "Const"), |
| 138 | + DiffActivity::Active => write!(f, "Active"), |
| 139 | + DiffActivity::ActiveOnly => write!(f, "ActiveOnly"), |
| 140 | + DiffActivity::Dual => write!(f, "Dual"), |
| 141 | + DiffActivity::DualOnly => write!(f, "DualOnly"), |
| 142 | + DiffActivity::Duplicated => write!(f, "Duplicated"), |
| 143 | + DiffActivity::DuplicatedOnly => write!(f, "DuplicatedOnly"), |
| 144 | + DiffActivity::FakeActivitySize => write!(f, "FakeActivitySize"), |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl FromStr for DiffMode { |
| 150 | + type Err = (); |
| 151 | + |
| 152 | + fn from_str(s: &str) -> Result<DiffMode, ()> { |
| 153 | + match s { |
| 154 | + "Inactive" => Ok(DiffMode::Inactive), |
| 155 | + "Source" => Ok(DiffMode::Source), |
| 156 | + "Forward" => Ok(DiffMode::Forward), |
| 157 | + "Reverse" => Ok(DiffMode::Reverse), |
| 158 | + "ForwardFirst" => Ok(DiffMode::ForwardFirst), |
| 159 | + "ReverseFirst" => Ok(DiffMode::ReverseFirst), |
| 160 | + _ => Err(()), |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | +impl FromStr for DiffActivity { |
| 165 | + type Err = (); |
| 166 | + |
| 167 | + fn from_str(s: &str) -> Result<DiffActivity, ()> { |
| 168 | + match s { |
| 169 | + "None" => Ok(DiffActivity::None), |
| 170 | + "Active" => Ok(DiffActivity::Active), |
| 171 | + "ActiveOnly" => Ok(DiffActivity::ActiveOnly), |
| 172 | + "Const" => Ok(DiffActivity::Const), |
| 173 | + "Dual" => Ok(DiffActivity::Dual), |
| 174 | + "DualOnly" => Ok(DiffActivity::DualOnly), |
| 175 | + "Duplicated" => Ok(DiffActivity::Duplicated), |
| 176 | + "DuplicatedOnly" => Ok(DiffActivity::DuplicatedOnly), |
| 177 | + _ => Err(()), |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +#[allow(dead_code)] |
| 183 | +#[derive(Clone, Eq, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] |
| 184 | +pub struct AutoDiffAttrs { |
| 185 | + pub mode: DiffMode, |
| 186 | + pub ret_activity: DiffActivity, |
| 187 | + pub input_activity: Vec<DiffActivity>, |
| 188 | +} |
| 189 | + |
| 190 | +impl AutoDiffAttrs { |
| 191 | + pub fn has_ret_activity(&self) -> bool { |
| 192 | + match self.ret_activity { |
| 193 | + DiffActivity::None => false, |
| 194 | + _ => true, |
| 195 | + } |
| 196 | + } |
| 197 | + pub fn has_active_only_ret(&self) -> bool { |
| 198 | + match self.ret_activity { |
| 199 | + DiffActivity::ActiveOnly => true, |
| 200 | + _ => false, |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +impl AutoDiffAttrs { |
| 206 | + pub fn inactive() -> Self { |
| 207 | + AutoDiffAttrs { |
| 208 | + mode: DiffMode::Inactive, |
| 209 | + ret_activity: DiffActivity::None, |
| 210 | + input_activity: Vec::new(), |
| 211 | + } |
| 212 | + } |
| 213 | + pub fn source() -> Self { |
| 214 | + AutoDiffAttrs { |
| 215 | + mode: DiffMode::Source, |
| 216 | + ret_activity: DiffActivity::None, |
| 217 | + input_activity: Vec::new(), |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + pub fn is_active(&self) -> bool { |
| 222 | + match self.mode { |
| 223 | + DiffMode::Inactive => false, |
| 224 | + _ => true, |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + pub fn is_source(&self) -> bool { |
| 229 | + match self.mode { |
| 230 | + DiffMode::Source => true, |
| 231 | + _ => false, |
| 232 | + } |
| 233 | + } |
| 234 | + pub fn apply_autodiff(&self) -> bool { |
| 235 | + match self.mode { |
| 236 | + DiffMode::Inactive => false, |
| 237 | + DiffMode::Source => false, |
| 238 | + _ => true, |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + pub fn into_item( |
| 243 | + self, |
| 244 | + source: String, |
| 245 | + target: String, |
| 246 | + inputs: Vec<TypeTree>, |
| 247 | + output: TypeTree, |
| 248 | + ) -> AutoDiffItem { |
| 249 | + AutoDiffItem { source, target, inputs, output, attrs: self } |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +#[derive(Clone, Eq, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] |
| 254 | +pub struct AutoDiffItem { |
| 255 | + pub source: String, |
| 256 | + pub target: String, |
| 257 | + pub attrs: AutoDiffAttrs, |
| 258 | + pub inputs: Vec<TypeTree>, |
| 259 | + pub output: TypeTree, |
| 260 | +} |
| 261 | + |
| 262 | +impl fmt::Display for AutoDiffItem { |
| 263 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 264 | + write!(f, "Differentiating {} -> {}", self.source, self.target)?; |
| 265 | + write!(f, " with attributes: {:?}", self.attrs)?; |
| 266 | + write!(f, " with inputs: {:?}", self.inputs)?; |
| 267 | + write!(f, " with output: {:?}", self.output) |
| 268 | + } |
| 269 | +} |
0 commit comments