use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum ParamKind {
Weight,
Bias,
RunningMean,
RunningVar,
Raw,
}
#[derive(Debug, Clone, Copy)]
pub struct Leaf {
pub name: &'static str,
pub kind: ParamKind,
pub unconditional: bool,
pub config_named: bool,
}
const fn leaf(name: &'static str, kind: ParamKind) -> Leaf {
Leaf {
name,
kind,
unconditional: true,
config_named: false,
}
}
const fn cond(name: &'static str, kind: ParamKind) -> Leaf {
Leaf {
name,
kind,
unconditional: false,
config_named: false,
}
}
const fn named_by_config(name: &'static str, kind: ParamKind, unconditional: bool) -> Leaf {
Leaf {
name,
kind,
unconditional,
config_named: true,
}
}
#[derive(Debug, Clone, Copy)]
pub struct Constructor {
pub func: &'static str,
pub vb_arg: usize,
pub leaves: &'static [Leaf],
pub cite: &'static str,
}
const LINEAR: &[Leaf] = &[
leaf("weight", ParamKind::Weight),
leaf("bias", ParamKind::Bias),
];
const LINEAR_NO_BIAS: &[Leaf] = &[leaf("weight", ParamKind::Weight)];
const LINEAR_B: &[Leaf] = &[
leaf("weight", ParamKind::Weight),
cond("bias", ParamKind::Bias),
];
const EMBEDDING: &[Leaf] = &[leaf("weight", ParamKind::Weight)];
const LAYER_NORM: &[Leaf] = &[
leaf("weight", ParamKind::Weight),
cond("bias", ParamKind::Bias),
];
const LAYER_NORM_NO_BIAS: &[Leaf] = &[leaf("weight", ParamKind::Weight)];
const RMS_NORM: &[Leaf] = &[leaf("weight", ParamKind::Weight)];
const GROUP_NORM: &[Leaf] = &[
leaf("weight", ParamKind::Weight),
leaf("bias", ParamKind::Bias),
];
const PRELU: &[Leaf] = &[leaf("weight", ParamKind::Weight)];
const CONV: &[Leaf] = &[
leaf("weight", ParamKind::Weight),
leaf("bias", ParamKind::Bias),
];
const CONV_NO_BIAS: &[Leaf] = &[leaf("weight", ParamKind::Weight)];
const GRU: &[Leaf] = &[
leaf("weight_ih_l0", ParamKind::Weight),
leaf("weight_hh_l0", ParamKind::Weight),
cond("bias_ih_l0", ParamKind::Bias),
cond("bias_hh_l0", ParamKind::Bias),
];
const LSTM: &[Leaf] = &[
named_by_config("weight_ih_l0", ParamKind::Weight, true),
named_by_config("weight_hh_l0", ParamKind::Weight, true),
named_by_config("bias_ih_l0", ParamKind::Bias, false),
named_by_config("bias_hh_l0", ParamKind::Bias, false),
];
const BATCH_NORM: &[Leaf] = &[
leaf("running_mean", ParamKind::RunningMean),
leaf("running_var", ParamKind::RunningVar),
cond("weight", ParamKind::Weight),
cond("bias", ParamKind::Bias),
];
pub const CONSTRUCTORS: &[Constructor] = &[
Constructor {
func: "linear",
vb_arg: 2,
leaves: LINEAR,
cite: "linear.rs:84",
},
Constructor {
func: "linear_no_bias",
vb_arg: 2,
leaves: LINEAR_NO_BIAS,
cite: "linear.rs:97",
},
Constructor {
func: "linear_b",
vb_arg: 3,
leaves: LINEAR_B,
cite: "linear.rs:103",
},
Constructor {
func: "embedding",
vb_arg: 2,
leaves: EMBEDDING,
cite: "embedding.rs:39",
},
Constructor {
func: "layer_norm",
vb_arg: 2,
leaves: LAYER_NORM,
cite: "layer_norm.rs:146",
},
Constructor {
func: "layer_norm_no_bias",
vb_arg: 2,
leaves: LAYER_NORM_NO_BIAS,
cite: "layer_norm.rs:166",
},
Constructor {
func: "rms_norm",
vb_arg: 2,
leaves: RMS_NORM,
cite: "layer_norm.rs:212",
},
Constructor {
func: "group_norm",
vb_arg: 3,
leaves: GROUP_NORM,
cite: "group_norm.rs:76",
},
Constructor {
func: "prelu",
vb_arg: 1,
leaves: PRELU,
cite: "activation.rs:104",
},
Constructor {
func: "conv1d",
vb_arg: 4,
leaves: CONV,
cite: "conv.rs:307",
},
Constructor {
func: "conv1d_no_bias",
vb_arg: 4,
leaves: CONV_NO_BIAS,
cite: "conv.rs:329",
},
Constructor {
func: "conv2d",
vb_arg: 4,
leaves: CONV,
cite: "conv.rs:386",
},
Constructor {
func: "conv2d_no_bias",
vb_arg: 4,
leaves: CONV_NO_BIAS,
cite: "conv.rs:413",
},
Constructor {
func: "conv_transpose1d",
vb_arg: 4,
leaves: CONV,
cite: "conv.rs:345",
},
Constructor {
func: "conv_transpose1d_no_bias",
vb_arg: 4,
leaves: CONV_NO_BIAS,
cite: "conv.rs:366",
},
Constructor {
func: "conv_transpose2d",
vb_arg: 4,
leaves: CONV,
cite: "conv.rs:434",
},
Constructor {
func: "conv_transpose2d_no_bias",
vb_arg: 4,
leaves: CONV_NO_BIAS,
cite: "conv.rs:455",
},
Constructor {
func: "batch_norm",
vb_arg: 2,
leaves: BATCH_NORM,
cite: "batch_norm.rs:301",
},
Constructor {
func: "gru",
vb_arg: 3,
leaves: GRU,
cite: "rnn.rs:345",
},
Constructor {
func: "lstm",
vb_arg: 3,
leaves: LSTM,
cite: "rnn.rs:189",
},
];
pub fn lookup(func: &str) -> Option<&'static Constructor> {
CONSTRUCTORS.iter().find(|c| c.func == func)
}
pub fn raw_get_name_arg(method: &str) -> Option<usize> {
match method {
"get" => Some(1),
"get_with_hints" => Some(1),
"get_with_hints_dtype" => Some(1),
"get_unchecked" => Some(0),
"get_unchecked_dtype" => Some(0),
_ => None,
}
}
pub fn prefix_method(method: &str) -> Option<PrefixOp> {
match method {
"pp" | "push_prefix" => Some(PrefixOp::Push),
"set_prefix" => Some(PrefixOp::Replace),
"root" => Some(PrefixOp::Root),
_ => None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PrefixOp {
Push,
Replace,
Root,
}