use crate::algorithm::special::SpecialFunctions;
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::ops::activation::normalize_softmax_dim;
use crate::ops::activation_common::validate_prelu;
use crate::ops::traits::{
ActivationOps, BinaryOps, CompareOps, ConditionalOps, CumulativeOps, RandomOps, ScalarOps,
UnaryOps,
};
use crate::runtime::{Runtime, RuntimeClient};
use crate::tensor::Tensor;
pub fn softmax_with_bias_impl<R, C>(
client: &C,
a: &Tensor<R>,
bias: &Tensor<R>,
dim: isize,
) -> Result<Tensor<R>>
where
R: Runtime,
C: ActivationOps<R> + BinaryOps<R>,
{
let biased = client.add(a, bias)?;
client.softmax(&biased, dim)
}
pub fn softplus_impl<R, C>(client: &C, a: &Tensor<R>) -> Result<Tensor<R>>
where
R: Runtime,
C: ActivationOps<R> + UnaryOps<R> + ScalarOps<R> + BinaryOps<R>,
{
let relu_x = client.relu(a)?;
let abs_x = client.abs(a)?;
let neg_abs = client.neg(&abs_x)?;
let exp_neg_abs = client.exp(&neg_abs)?;
let one_plus = client.add_scalar(&exp_neg_abs, 1.0)?;
let log_term = client.log(&one_plus)?;
client.add(&relu_x, &log_term)
}
pub(crate) fn standard_normal_cdf<R, C>(client: &C, a: &Tensor<R>) -> Result<Tensor<R>>
where
R: Runtime,
C: SpecialFunctions<R> + ScalarOps<R>,
{
const FRAC_1_SQRT_2: f64 = std::f64::consts::FRAC_1_SQRT_2;
let scaled = client.mul_scalar(a, FRAC_1_SQRT_2)?;
let erf_val = client.erf(&scaled)?;
let one_plus_erf = client.add_scalar(&erf_val, 1.0)?;
client.mul_scalar(&one_plus_erf, 0.5)
}
pub fn gelu_erf_impl<R, C>(client: &C, a: &Tensor<R>) -> Result<Tensor<R>>
where
R: Runtime,
C: SpecialFunctions<R> + ScalarOps<R> + BinaryOps<R>,
{
let cdf = standard_normal_cdf(client, a)?;
client.mul(a, &cdf)
}
pub fn gelu_erf_mul_impl<R, C>(client: &C, a: &Tensor<R>, b: &Tensor<R>) -> Result<Tensor<R>>
where
R: Runtime,
C: SpecialFunctions<R> + ScalarOps<R> + BinaryOps<R>,
{
let cdf = standard_normal_cdf(client, a)?;
let gelu_erf_a = client.mul(a, &cdf)?;
client.mul(&gelu_erf_a, b)
}
pub fn gelu_erf_mul_bwd_impl<R, C>(
client: &C,
grad: &Tensor<R>,
a: &Tensor<R>,
b: &Tensor<R>,
) -> Result<(Tensor<R>, Tensor<R>)>
where
R: Runtime,
C: SpecialFunctions<R> + UnaryOps<R> + ScalarOps<R> + BinaryOps<R>,
{
const INV_SQRT_2PI: f64 = 0.3989422804014327;
let cdf = standard_normal_cdf(client, a)?;
let gelu_erf_a = client.mul(a, &cdf)?;
let neg_half_x_sq = client.mul_scalar(&client.mul(a, a)?, -0.5)?;
let pdf = client.mul_scalar(&client.exp(&neg_half_x_sq)?, INV_SQRT_2PI)?;
let x_pdf = client.mul(a, &pdf)?;
let deriv = client.add(&cdf, &x_pdf)?;
let d_b = client.mul(grad, &gelu_erf_a)?;
let grad_times_b = client.mul(grad, b)?;
let d_a = client.mul(&grad_times_b, &deriv)?;
Ok((d_a, d_b))
}
pub fn log_softmax_impl<R, C>(client: &C, a: &Tensor<R>, dim: isize) -> Result<Tensor<R>>
where
R: Runtime,
C: BinaryOps<R> + CumulativeOps<R>,
{
let ndim = a.ndim();
let dim_idx = normalize_softmax_dim(ndim, dim).ok_or(Error::InvalidDimension { dim, ndim })?;
let lse = client.logsumexp(a, &[dim_idx], true)?;
client.sub(a, &lse)
}
pub fn dropout_impl<R, C>(client: &C, a: &Tensor<R>, p: f64, training: bool) -> Result<Tensor<R>>
where
R: Runtime<DType = DType>,
C: RandomOps<R> + CompareOps<R> + ConditionalOps<R> + ScalarOps<R> + RuntimeClient<R>,
{
if !training || p == 0.0 {
return Ok(a.clone());
}
if p >= 1.0 {
return Ok(Tensor::<R>::zeros(a.shape(), a.dtype(), client.device())?);
}
let rand_tensor = client.rand(a.shape(), a.dtype())?;
let threshold = Tensor::<R>::full_scalar(a.shape(), a.dtype(), p, client.device())?;
let mask = client.gt(&rand_tensor, &threshold)?;
let scale = 1.0 / (1.0 - p);
let scaled = client.mul_scalar(a, scale)?;
let zeros = Tensor::<R>::zeros(a.shape(), a.dtype(), client.device())?;
client.where_cond(&mask, &scaled, &zeros)
}
pub fn prelu_impl<R, C>(client: &C, a: &Tensor<R>, weight: &Tensor<R>) -> Result<Tensor<R>>
where
R: Runtime<DType = DType>,
C: CompareOps<R> + ConditionalOps<R> + BinaryOps<R> + RuntimeClient<R>,
{
validate_prelu(a.dtype(), a.shape(), weight.dtype(), weight.shape())?;
let zero = Tensor::<R>::zeros(&[], a.dtype(), client.device())?;
let positive = client.gt(a, &zero)?;
let scaled = client.mul(a, weight)?;
client.where_cond(&positive, a, &scaled)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::algorithm::special::scalar::erf_scalar;
use crate::ops::ActivationOps;
use crate::runtime::cpu::{CpuDevice, CpuRuntime};
#[test]
fn test_gelu_erf_matches_exact_gelu_f64() {
let device = CpuDevice::new();
let client = CpuRuntime::default_client(&device);
let xs: [f64; 10] = [-8.0, -3.0, -1.0, -1e-3, 0.0, 1e-3, 0.5, 1.0, 3.0, 8.0];
let expected: [f64; 10] = [
-4.884_981_308_350_689e-15,
-0.004_049_694_094_890_31,
-0.158_655_253_931_457_07,
-0.000_499_601_057_786_089,
0.0,
0.000_500_398_942_213_911,
0.345_731_230_637_006_56,
0.841_344_746_068_542_9,
2.995_950_305_905_11,
7.999_999_999_999_995,
];
let a = Tensor::<CpuRuntime>::from_slice(&xs, &[xs.len()], &device).unwrap();
let out = client.gelu_erf(&a).unwrap();
let result: Vec<f64> = out.to_vec();
for i in 0..xs.len() {
let diff = (result[i] - expected[i]).abs();
assert!(
diff < 1e-12,
"gelu_erf mismatch at x={}: got {}, want {}",
xs[i],
result[i],
expected[i]
);
}
}
#[test]
fn test_gelu_erf_large_batch_simd_lanes_and_tail() {
let device = CpuDevice::new();
let client = CpuRuntime::default_client(&device);
let len = 137;
let xs: Vec<f32> = (0..len).map(|i| (i as f32 - 68.0) * 0.25).collect();
let a = Tensor::<CpuRuntime>::from_slice(&xs, &[len], &device).unwrap();
let out = client.gelu_erf(&a).unwrap();
let result: Vec<f32> = out.to_vec();
for (i, &x) in xs.iter().enumerate() {
let scaled = (x as f64) / std::f64::consts::SQRT_2;
let want = 0.5 * (x as f64) * (1.0 + erf_scalar(scaled));
let diff = (result[i] as f64 - want).abs();
assert!(
diff < 5e-6,
"gelu_erf batch mismatch at i={i}, x={x}: got {}, want {want}",
result[i]
);
}
}
fn cpu() -> (crate::runtime::cpu::CpuClient, CpuDevice) {
let device = CpuDevice::new();
(CpuRuntime::default_client(&device), device)
}
fn prelu_f64(x: &[f64], x_shape: &[usize], w: &[f64], w_shape: &[usize]) -> Vec<f64> {
let (client, device) = cpu();
let a = Tensor::<CpuRuntime>::from_slice(x, x_shape, &device).unwrap();
let weight = Tensor::<CpuRuntime>::from_slice(w, w_shape, &device).unwrap();
let out = client.prelu(&a, &weight).unwrap();
assert_eq!(out.shape(), x_shape);
out.to_vec()
}
#[test]
fn test_prelu_positive_negative_zero() {
let out = prelu_f64(&[2.0, -2.0, 0.0, 0.5, -0.5], &[5], &[0.25], &[1]);
assert_eq!(out, vec![2.0, -0.5, 0.0, 0.5, -0.125]);
}
#[test]
fn test_prelu_per_channel_nchw() {
let out = prelu_f64(
&[-1.0, 1.0, -2.0, 2.0],
&[1, 2, 1, 2],
&[0.5, 0.1],
&[2, 1, 1],
);
assert_eq!(out, vec![-0.5, 1.0, -0.2, 2.0]);
}
#[test]
fn test_prelu_per_channel_nhwc() {
let out = prelu_f64(&[-1.0, -2.0, 3.0, -4.0], &[1, 1, 2, 2], &[0.5, 0.25], &[2]);
assert_eq!(out, vec![-0.5, -0.5, 3.0, -1.0]);
}
#[test]
fn test_prelu_scalar_weight() {
let device = CpuDevice::new();
let client = CpuRuntime::default_client(&device);
let a = Tensor::<CpuRuntime>::from_slice(&[-4.0f64, 4.0], &[2], &device).unwrap();
let weight = Tensor::<CpuRuntime>::from_slice(&[0.5f64], &[], &device).unwrap();
let out = client.prelu(&a, &weight).unwrap();
assert_eq!(out.to_vec::<f64>(), vec![-2.0, 4.0]);
}
#[test]
fn test_prelu_rejects_non_broadcasting_weight() {
let (client, device) = cpu();
let a = Tensor::<CpuRuntime>::from_slice(&[0.0f64; 6], &[2, 3], &device).unwrap();
let weight = Tensor::<CpuRuntime>::from_slice(&[0.0f64; 4], &[4], &device).unwrap();
let err = client.prelu(&a, &weight).unwrap_err();
assert!(matches!(err, Error::BroadcastError { .. }));
let msg = err.to_string();
assert!(msg.contains("[2, 3]") && msg.contains("[4]"), "{msg}");
}
#[test]
fn test_prelu_rejects_weight_that_grows_input() {
let (client, device) = cpu();
let a = Tensor::<CpuRuntime>::from_slice(&[0.0f64; 2], &[2], &device).unwrap();
let weight = Tensor::<CpuRuntime>::from_slice(&[0.0f64; 3], &[3, 1], &device).unwrap();
let err = client.prelu(&a, &weight).unwrap_err();
assert!(matches!(err, Error::BroadcastError { .. }));
}
#[test]
fn test_prelu_rejects_dtype_mismatch() {
let (client, device) = cpu();
let a = Tensor::<CpuRuntime>::from_slice(&[1.0f64, -1.0], &[2], &device).unwrap();
let weight = Tensor::<CpuRuntime>::from_slice(&[0.5f32], &[1], &device).unwrap();
let err = client.prelu(&a, &weight).unwrap_err();
assert!(matches!(err, Error::DTypeMismatch { .. }));
}
}