use crate::autograd::GradFn;
use crate::autograd::var::Var;
use crate::error::Result;
use crate::ops::common::group_norm_channels_per_group;
use crate::ops::{BinaryOps, ReduceOps, ScalarOps, TensorOps, UnaryOps};
use crate::runtime::{Runtime, RuntimeClient};
use crate::tensor::{Tensor, TensorId};
use std::sync::Arc;
pub struct GroupNormBackward<R: Runtime> {
input_ids: [TensorId; 3], saved_input: Tensor<R>,
saved_weight: Tensor<R>,
num_groups: usize,
eps: f32,
input_grad_fns: [Option<Arc<dyn GradFn<R>>>; 3],
}
impl<R: Runtime> GroupNormBackward<R> {
pub fn new(
input_id: TensorId,
weight_id: TensorId,
bias_id: TensorId,
input: Tensor<R>,
weight: Tensor<R>,
num_groups: usize,
eps: f32,
input_grad_fn: Option<Arc<dyn GradFn<R>>>,
weight_grad_fn: Option<Arc<dyn GradFn<R>>>,
bias_grad_fn: Option<Arc<dyn GradFn<R>>>,
) -> Self {
Self {
input_ids: [input_id, weight_id, bias_id],
saved_input: input,
saved_weight: weight,
num_groups,
eps,
input_grad_fns: [input_grad_fn, weight_grad_fn, bias_grad_fn],
}
}
}
impl<R: Runtime> GradFn<R> for GroupNormBackward<R>
where
R::Client: TensorOps<R> + ScalarOps<R> + ReduceOps<R> + BinaryOps<R> + UnaryOps<R>,
{
fn backward(&self, grad_output: &Tensor<R>, needed: &[bool]) -> Result<Vec<Option<Tensor<R>>>> {
if !needed.iter().any(|&n| n) {
return Ok(vec![None, None, None]);
}
let client = R::default_client(grad_output.device());
let input = &self.saved_input;
let weight = &self.saved_weight;
let shape = input.shape();
let batch = shape[0];
let channels = shape[1];
let cpg = group_norm_channels_per_group(channels, self.num_groups)?;
let spatial: usize = shape[2..].iter().product();
let group_size = cpg * spatial;
if input.numel() == 0 {
let dtype = input.dtype();
let device = input.device();
let d_input = if needed[0] {
Some(Tensor::<R>::zeros_generic(shape, dtype, device)?)
} else {
None
};
let d_weight = if needed[1] {
Some(Tensor::<R>::zeros_generic(&[channels], dtype, device)?)
} else {
None
};
let d_bias = if needed[2] {
Some(Tensor::<R>::zeros_generic(&[channels], dtype, device)?)
} else {
None
};
return Ok(vec![d_input, d_weight, d_bias]);
}
let flat_shape = [batch, self.num_groups, group_size];
let input_contig = super::super::ensure_contiguous(input)?;
let grad_output_contig = super::super::ensure_contiguous(grad_output)?;
let input_flat = input_contig.reshape(&flat_shape)?;
let grad_flat = grad_output_contig.reshape(&flat_shape)?;
let mu = client.mean(&input_flat, &[2], true)?;
let x_centered = client.sub(&input_flat, &mu)?;
let x_sq = client.mul(&x_centered, &x_centered)?;
let variance = client.mean(&x_sq, &[2], true)?;
let var_eps = client.add_scalar(&variance, self.eps as f64)?;
let std = client.sqrt(&var_eps)?;
let rstd = client.recip(&std)?;
let x_norm_flat = client.mul(&x_centered, &rstd)?;
let d_input = if needed[0] {
let weight_contig = super::super::ensure_contiguous(weight)?;
let weight_4d = weight_contig.reshape(&[1, self.num_groups, cpg, 1])?;
let weight_bcast = weight_4d
.broadcast_to(&[1, self.num_groups, cpg, spatial])?
.contiguous()?;
let weight_flat = weight_bcast.reshape(&[1, self.num_groups, group_size])?;
let gw = client.mul(&grad_flat, &weight_flat)?;
let mean_gw = client.mean(&gw, &[2], true)?;
let gw_xn = client.mul(&gw, &x_norm_flat)?;
let mean_gw_xn = client.mean(&gw_xn, &[2], true)?;
let xn_correction = client.mul(&x_norm_flat, &mean_gw_xn)?;
let inner = client.sub(&gw, &mean_gw)?;
let inner = client.sub(&inner, &xn_correction)?;
let d_input_flat = client.mul(&inner, &rstd)?;
Some(d_input_flat.reshape(shape)?)
} else {
None
};
let grad_bcs = if needed[1] || needed[2] {
Some(grad_output_contig.reshape(&[batch, channels, spatial])?)
} else {
None
};
let d_weight = match (needed[1], &grad_bcs) {
(true, Some(grad_bcs)) => {
let x_norm_bcs = x_norm_flat.reshape(&[batch, channels, spatial])?;
let gxn = client.mul(grad_bcs, &x_norm_bcs)?;
Some(client.sum(&gxn, &[0, 2], false)?)
}
_ => None,
};
let d_bias = match (needed[2], &grad_bcs) {
(true, Some(grad_bcs)) => Some(client.sum(grad_bcs, &[0, 2], false)?),
_ => None,
};
Ok(vec![d_input, d_weight, d_bias])
}
fn backward_var(&self, grad_output: &Var<R>) -> Result<Vec<Option<Var<R>>>>
where
R::Client: RuntimeClient<R> + TensorOps<R> + ScalarOps<R>,
{
let grads = self.backward_all(grad_output.tensor())?;
Ok(grads
.into_iter()
.map(|g| g.map(|t| Var::new(t, false)))
.collect())
}
fn inputs(&self) -> &[TensorId] {
&self.input_ids
}
fn input_grad_fns(&self) -> Vec<Option<Arc<dyn GradFn<R>>>> {
self.input_grad_fns.to_vec()
}
fn saved_tensors(&self) -> &[Tensor<R>] {
std::slice::from_ref(&self.saved_input)
}
fn name(&self) -> &'static str {
"GroupNormBackward"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dtype::DType;
use crate::runtime::cpu::{CpuDevice, CpuRuntime};
fn non_contiguous_like(
data: &[f32],
rows: usize,
cols: usize,
device: &CpuDevice,
) -> Tensor<CpuRuntime> {
let mut transposed = vec![0.0f32; data.len()];
for r in 0..rows {
for c in 0..cols {
transposed[c * rows + r] = data[r * cols + c];
}
}
let src = Tensor::<CpuRuntime>::from_slice(&transposed, &[cols, rows], device).unwrap();
let view = src.t().unwrap();
assert!(!view.is_contiguous());
assert_eq!(view.shape(), &[rows, cols]);
view
}
#[test]
fn test_group_norm_backward_non_contiguous_grad_output() {
let device = CpuDevice::new();
let input_data = [1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0];
let grad_data = [0.1f32, 0.2, -0.3, 0.4, -0.5, 0.6];
let input = Tensor::<CpuRuntime>::from_slice(&input_data, &[2, 3], &device).unwrap();
let weight = Tensor::<CpuRuntime>::ones(&[3], DType::F32, &device).unwrap();
let backward = GroupNormBackward::<CpuRuntime>::new(
TensorId::new(),
TensorId::new(),
TensorId::new(),
input,
weight,
1,
1e-5,
None,
None,
None,
);
let grad_contig = Tensor::<CpuRuntime>::from_slice(&grad_data, &[2, 3], &device).unwrap();
let grads_contig = backward.backward_all(&grad_contig).unwrap();
let grad_noncontig = non_contiguous_like(&grad_data, 2, 3, &device);
let grads_noncontig = backward.backward_all(&grad_noncontig).unwrap();
for (a, b) in grads_contig.iter().zip(grads_noncontig.iter()) {
let a = a.as_ref().unwrap().contiguous().unwrap();
let b = b.as_ref().unwrap().contiguous().unwrap();
assert_eq!(a.shape(), b.shape());
let a_vals: Vec<f32> = a.to_vec();
let b_vals: Vec<f32> = b.to_vec();
assert_eq!(a_vals, b_vals);
}
let d_input: Vec<f32> = grads_contig[0]
.as_ref()
.unwrap()
.contiguous()
.unwrap()
.to_vec();
assert_eq!(d_input.len(), 6);
assert!(d_input.iter().any(|&v| v != 0.0));
}
#[test]
fn test_group_norm_backward_non_contiguous_saved_input() {
let device = CpuDevice::new();
let input_data = [1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0];
let grad_data = [0.1f32, 0.2, -0.3, 0.4, -0.5, 0.6];
let weight_contig = Tensor::<CpuRuntime>::ones(&[3], DType::F32, &device).unwrap();
let grad_output = Tensor::<CpuRuntime>::from_slice(&grad_data, &[2, 3], &device).unwrap();
let input_contig = Tensor::<CpuRuntime>::from_slice(&input_data, &[2, 3], &device).unwrap();
let backward_contig = GroupNormBackward::<CpuRuntime>::new(
TensorId::new(),
TensorId::new(),
TensorId::new(),
input_contig,
weight_contig.clone(),
1,
1e-5,
None,
None,
None,
);
let grads_contig = backward_contig.backward_all(&grad_output).unwrap();
let input_noncontig = non_contiguous_like(&input_data, 2, 3, &device);
let backward_noncontig = GroupNormBackward::<CpuRuntime>::new(
TensorId::new(),
TensorId::new(),
TensorId::new(),
input_noncontig,
weight_contig,
1,
1e-5,
None,
None,
None,
);
let grads_noncontig = backward_noncontig.backward_all(&grad_output).unwrap();
for (a, b) in grads_contig.iter().zip(grads_noncontig.iter()) {
let a = a.as_ref().unwrap().contiguous().unwrap();
let b = b.as_ref().unwrap().contiguous().unwrap();
assert_eq!(a.shape(), b.shape());
let a_vals: Vec<f32> = a.to_vec();
let b_vals: Vec<f32> = b.to_vec();
assert_eq!(a_vals, b_vals);
}
}
}