use super::super::ops::RmsNormBackward;
use crate::autograd::Var;
use crate::error::Result;
use crate::ops::{NormalizationOps, ScalarOps, TensorOps};
use crate::runtime::{Runtime, RuntimeClient};
use std::sync::Arc;
pub fn var_rms_norm<R, C>(input: &Var<R>, weight: &Var<R>, eps: f32, client: &C) -> Result<Var<R>>
where
R: Runtime,
C: RuntimeClient<R> + NormalizationOps<R>,
R::Client: TensorOps<R> + ScalarOps<R>,
{
let output = client.rms_norm(input.tensor(), weight.tensor(), eps)?;
if input.requires_grad() || weight.requires_grad() {
let grad_fn = RmsNormBackward::<R>::new(
input.id(),
weight.id(),
input.tensor().clone(),
weight.tensor().clone(),
eps,
input.grad_fn().cloned(),
weight.grad_fn().cloned(),
);
Ok(Var::from_op(output, Arc::new(grad_fn)))
} else {
Ok(Var::new(output, false))
}
}