use crate::err::{Error, Result};
use crate::nn::param::{Param, TrainState};
use crate::nn::{ParameterVisitor, StatePath, VisitParameters};
use crate::shapes::Shape;
use crate::tensor::backend::VariableBackend;
use crate::tensor::dtype::{ConstDType, DType};
use alloc::string::{String, ToString};
pub struct ParameterGroup<B: VariableBackend, K: ConstDType> {
params: alloc::collections::BTreeMap<String, B::Var<K>>,
}
impl<B: VariableBackend, K: ConstDType> ParameterGroup<B, K> {
pub fn from_module<M>(module: &M) -> Result<Self>
where
M: VisitParameters<B>,
{
let mut collector = ParameterCollector::<B, K> {
params: alloc::collections::BTreeMap::new(),
};
module.visit_parameters(&StatePath::root(), &mut collector)?;
Ok(Self {
params: collector.params,
})
}
#[must_use]
pub fn from_map(params: alloc::collections::BTreeMap<String, B::Var<K>>) -> Self {
Self { params }
}
#[must_use]
pub fn len(&self) -> usize {
self.params.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.params.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = (&String, &B::Var<K>)> {
self.params.iter()
}
pub(super) fn into_map(self) -> alloc::collections::BTreeMap<String, B::Var<K>> {
self.params
}
}
struct ParameterCollector<B: VariableBackend, K: ConstDType> {
params: alloc::collections::BTreeMap<String, B::Var<K>>,
}
impl<B: VariableBackend, K: ConstDType> ParameterVisitor<B> for ParameterCollector<B, K> {
fn visit_param<S, LeafK, Train>(
&mut self,
path: &StatePath,
param: &Param<S, B, LeafK, Train>,
) -> Result<()>
where
S: Shape,
LeafK: DType,
Train: TrainState,
{
if param.dtype_descriptor() != K::DESCRIPTOR {
return Ok(());
}
let variable =
param
.variable_any()
.downcast_ref::<B::Var<K>>()
.ok_or(Error::InternalInvariant {
operation: "collect parameter group",
reason: "dtype matched but backend variable type did not",
})?;
self.params.insert(path.to_string(), variable.clone());
Ok(())
}
}
pub(super) struct PreparedUpdate<S> {
pub(super) name: String,
pub(super) before: S,
pub(super) updated: S,
pub(super) first_moment: Option<S>,
pub(super) second_moment: Option<S>,
}