#[macro_export]
macro_rules! define_l1_variants {
(
enum $Enum:ident,
feature_set $FeatureSet:ident,
l1 $L1:literal,
acc $Acc:ty,
stack $Stack:ty,
variants {
$(
($l2:literal, $l3:literal, $act:ident, $act_name:literal)
=> $Var:ident : $Ty:ty
),+ $(,)?
}
) => {
pub enum $Enum {
$(
$Var(Box<$Ty>),
)+
}
impl $Enum {
#[inline(always)]
pub fn evaluate(&self, pos: &Position, stack: &$Stack) -> Value {
let acc = stack.top();
match self {
$(Self::$Var(net) => net.evaluate(pos, acc),)+
}
}
#[inline(always)]
pub fn refresh_accumulator(&self, pos: &Position, stack: &mut $Stack) {
let acc = stack.top_mut();
match self {
$(Self::$Var(net) => net.refresh_accumulator(pos, acc),)+
}
}
#[inline(always)]
pub fn update_accumulator(
&self,
pos: &Position,
dirty: &DirtyPiece,
stack: &mut $Stack,
source_idx: usize,
) {
let (acc, prev) = stack.top_and_source(source_idx);
match self {
$(Self::$Var(net) => net.update_accumulator(pos, dirty, acc, prev),)+
}
}
#[inline(always)]
pub fn forward_update_incremental(
&self,
pos: &Position,
stack: &mut $Stack,
source_idx: usize,
) -> bool {
match self {
$(Self::$Var(net) => net.forward_update_incremental(pos, stack, source_idx),)+
}
}
pub fn read<R: std::io::Read + std::io::Seek>(
reader: &mut R,
l2: usize,
l3: usize,
activation: Activation,
) -> std::io::Result<Self> {
match (l2, l3, activation) {
$(
($l2, $l3, Activation::$act) => {
let net = <$Ty>::read(reader)?;
Ok(Self::$Var(Box::new(net)))
}
)+
_ => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!(
"Unsupported {} L1={} architecture: L2={}, L3={}, activation={:?}",
stringify!($FeatureSet), $L1, l2, l3, activation
),
)),
}
}
pub fn architecture_name(&self) -> &'static str {
match self {
$(
Self::$Var(_) => concat!(
stringify!($FeatureSet), "-",
stringify!($L1), "-",
stringify!($l2), "-",
stringify!($l3), "-",
$act_name
),
)+
}
}
pub fn architecture_spec(&self) -> ArchitectureSpec {
match self {
$(
Self::$Var(_) => ArchitectureSpec {
feature_set: FeatureSet::$FeatureSet,
l1: $L1,
l2: $l2,
l3: $l3,
activation: Activation::$act,
},
)+
}
}
pub const SUPPORTED_SPECS: &'static [ArchitectureSpec] = &[
$(
ArchitectureSpec {
feature_set: FeatureSet::$FeatureSet,
l1: $L1,
l2: $l2,
l3: $l3,
activation: Activation::$act,
},
)+
];
#[inline]
pub const fn l1_size(&self) -> usize {
$L1
}
}
};
}
pub use define_l1_variants;
#[cfg(test)]
mod tests {
}