use crate::autograd::Var;
use crate::autograd::var_ops::{var_mul, var_where};
use crate::dtype::DType;
use crate::error::Result;
use crate::ops::activation_common::validate_prelu;
use crate::ops::{CompareOps, ConditionalOps, TensorOps};
use crate::runtime::{Runtime, RuntimeClient};
use crate::tensor::Tensor;
pub fn var_prelu<R, C>(a: &Var<R>, weight: &Var<R>, client: &C) -> Result<Var<R>>
where
R: Runtime<DType = DType>,
C: RuntimeClient<R> + CompareOps<R> + ConditionalOps<R> + TensorOps<R>,
R::Client: TensorOps<R>,
{
validate_prelu(
a.tensor().dtype(),
a.shape(),
weight.tensor().dtype(),
weight.shape(),
)?;
let zero = Tensor::<R>::zeros(&[], a.tensor().dtype(), a.tensor().device())?;
let positive = client.gt(a.tensor(), &zero)?;
let scaled = var_mul(a, weight, client)?;
var_where(&positive, a, &scaled, client)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::autograd::test_fd::{check_grad_f64, f64_tensor};
use crate::autograd::{backward, var_sum};
use crate::runtime::cpu::{CpuDevice, CpuRuntime};
const X: [f64; 8] = [-2.3, -0.7, 0.2, 0.9, 1.6, -3.1, 0.4, -1.2];
#[test]
fn fd_prelu_scalar_weight() {
check_grad_f64(
"var_prelu scalar weight",
|inputs, c| var_prelu(&inputs[0], &inputs[1], c),
&[f64_tensor(&X, &[2, 4]), f64_tensor(&[0.3], &[1])],
);
}
#[test]
fn fd_prelu_per_channel_weight() {
check_grad_f64(
"var_prelu per channel",
|inputs, c| var_prelu(&inputs[0], &inputs[1], c),
&[
f64_tensor(&X, &[1, 2, 4]),
f64_tensor(&[0.3, 0.15], &[2, 1]),
],
);
}
#[test]
fn gradients_follow_the_positive_mask_and_reduce_to_weight_shape() {
let client = CpuRuntime::default_client(&CpuDevice::new());
let x = Var::new(f64_tensor(&[-1.0, 2.0, 0.0, -4.0], &[2, 2]), true);
let w = Var::new(f64_tensor(&[0.5, 0.25], &[2]), true);
let y = var_prelu(&x, &w, &client).unwrap();
assert_eq!(y.tensor().to_vec::<f64>(), vec![-0.5, 2.0, 0.0, -1.0]);
let loss = var_sum(&y, &[], false, &client).unwrap();
let grads = backward(&loss, &client).unwrap();
assert_eq!(
grads.get(x.id()).unwrap().to_vec::<f64>(),
vec![0.5, 1.0, 0.5, 0.25]
);
let dw = grads.get(w.id()).unwrap();
assert_eq!(dw.shape(), &[2]);
assert_eq!(dw.to_vec::<f64>(), vec![-1.0, -4.0]);
}
#[test]
fn rejects_non_broadcasting_weight() {
let client = CpuRuntime::default_client(&CpuDevice::new());
let x = Var::new(f64_tensor(&X, &[2, 4]), true);
let w = Var::new(f64_tensor(&[0.1, 0.2, 0.3], &[3]), true);
assert!(var_prelu(&x, &w, &client).is_err());
}
#[test]
fn no_grad_inputs_give_detached_var() {
let client = CpuRuntime::default_client(&CpuDevice::new());
let x = Var::new(f64_tensor(&X, &[8]), false);
let w = Var::new(f64_tensor(&[0.1], &[1]), false);
let y = var_prelu(&x, &w, &client).unwrap();
assert!(!y.requires_grad());
assert!(y.grad_fn().is_none());
}
}