mod compute;
#[cfg(test)]
mod tests;
use crate::error::{BarracudaError, Result};
use crate::ops::adamw::AdamConfig;
use crate::tensor::Tensor;
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct NadamParams {
pub learning_rate: f32,
pub beta1: f32,
pub beta2: f32,
pub epsilon: f32,
pub weight_decay: f32,
pub step: u32,
pub _padding: [u32; 2],
}
pub struct Nadam {
weights: Tensor,
gradients: Tensor,
m: Tensor,
v: Tensor,
config: AdamConfig,
step: u32,
}
impl Nadam {
pub fn new(
weights: Tensor,
gradients: Tensor,
m: Tensor,
v: Tensor,
config: &AdamConfig,
step: u32,
) -> Result<Self> {
if weights.shape() != gradients.shape() {
return Err(BarracudaError::shape_mismatch(
weights.shape().to_vec(),
gradients.shape().to_vec(),
));
}
if weights.shape() != m.shape() {
return Err(BarracudaError::shape_mismatch(
weights.shape().to_vec(),
m.shape().to_vec(),
));
}
if weights.shape() != v.shape() {
return Err(BarracudaError::shape_mismatch(
weights.shape().to_vec(),
v.shape().to_vec(),
));
}
config.validate("NAdam")?;
if step == 0 {
return Err(BarracudaError::invalid_op(
"NAdam",
"step must be >= 1 for bias correction",
));
}
Ok(Self {
weights,
gradients,
m,
v,
config: *config,
step,
})
}
pub(super) fn shader() -> &'static str {
{
static SHADER: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
include_str!("../../shaders/optimizer/nadam_f64.wgsl").to_string()
});
std::sync::LazyLock::force(&SHADER).as_str()
}
}
pub(super) fn weights(&self) -> &Tensor {
&self.weights
}
pub(super) fn gradients(&self) -> &Tensor {
&self.gradients
}
pub(super) fn m(&self) -> &Tensor {
&self.m
}
pub(super) fn v(&self) -> &Tensor {
&self.v
}
pub(super) fn learning_rate(&self) -> f32 {
self.config.learning_rate
}
pub(super) fn beta1(&self) -> f32 {
self.config.beta1
}
pub(super) fn beta2(&self) -> f32 {
self.config.beta2
}
pub(super) fn epsilon(&self) -> f32 {
self.config.epsilon
}
pub(super) fn weight_decay(&self) -> f32 {
self.config.weight_decay
}
pub(super) fn step(&self) -> u32 {
self.step
}
}
impl Tensor {
pub fn nadam(
self,
gradients: &Self,
m: &Self,
v: &Self,
config: &AdamConfig,
step: u32,
) -> Result<(Self, Self, Self)> {
Nadam::new(self, gradients.clone(), m.clone(), v.clone(), config, step)?.execute()
}
}