use super::super::kernels;
use super::super::{CpuClient, CpuRuntime};
use crate::dispatch_dtype;
use crate::error::Result;
use crate::runtime::{ensure_contiguous, row_layout};
use crate::tensor::Tensor;
#[derive(Copy, Clone)]
pub enum ActivationOp {
Relu,
Sigmoid,
Silu,
Gelu,
}
#[derive(Copy, Clone)]
pub enum ParametricActivationOp {
LeakyRelu,
Elu,
}
pub fn activation_op_impl(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
op: ActivationOp,
op_name: &'static str,
) -> Result<Tensor<CpuRuntime>> {
let dtype = a.dtype();
let a_contig = ensure_contiguous(a)?;
let out = Tensor::<CpuRuntime>::empty(a.shape(), dtype, &client.device)?;
let total = a.numel();
let (rows, row_len) = row_layout(a.shape(), total);
let a_ptr = a_contig.ptr();
let out_ptr = out.ptr();
dispatch_dtype!(dtype, T => {
let a_ptr = a_ptr as *const T;
let out_ptr = out_ptr as *mut T;
unsafe {
for row in 0..rows {
let off = row * row_len;
match op {
ActivationOp::Relu => kernels::relu_kernel::<T>(
a_ptr.add(off),
out_ptr.add(off),
row_len,
),
ActivationOp::Sigmoid => kernels::sigmoid_kernel::<T>(
a_ptr.add(off),
out_ptr.add(off),
row_len,
),
ActivationOp::Silu => kernels::silu_kernel::<T>(
a_ptr.add(off),
out_ptr.add(off),
row_len,
),
ActivationOp::Gelu => kernels::gelu_kernel::<T>(
a_ptr.add(off),
out_ptr.add(off),
row_len,
),
}
}
}
}, op_name);
Ok(out)
}
#[derive(Copy, Clone)]
#[allow(clippy::enum_variant_names)]
pub enum FusedActivationMulOp {
SiluMul,
GeluMul,
ReluMul,
SigmoidMul,
}
pub fn fused_activation_mul_impl(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
b: &Tensor<CpuRuntime>,
op: FusedActivationMulOp,
op_name: &'static str,
) -> Result<Tensor<CpuRuntime>> {
let dtype = a.dtype();
if b.dtype() != dtype {
return Err(crate::error::Error::DTypeMismatch {
lhs: dtype,
rhs: b.dtype(),
});
}
if a.shape() != b.shape() {
return Err(crate::error::Error::ShapeMismatch {
expected: a.shape().to_vec(),
got: b.shape().to_vec(),
});
}
let a_contig = ensure_contiguous(a)?;
let b_contig = ensure_contiguous(b)?;
let out = Tensor::<CpuRuntime>::empty(a.shape(), dtype, &client.device)?;
let total = a.numel();
let (rows, row_len) = row_layout(a.shape(), total);
let a_ptr = a_contig.ptr();
let b_ptr = b_contig.ptr();
let out_ptr = out.ptr();
dispatch_dtype!(dtype, T => {
let a_ptr = a_ptr as *const T;
let b_ptr = b_ptr as *const T;
let out_ptr = out_ptr as *mut T;
unsafe {
for row in 0..rows {
let off = row * row_len;
match op {
FusedActivationMulOp::SiluMul => kernels::silu_mul_kernel::<T>(
a_ptr.add(off), b_ptr.add(off), out_ptr.add(off), row_len,
),
FusedActivationMulOp::GeluMul => kernels::gelu_mul_kernel::<T>(
a_ptr.add(off), b_ptr.add(off), out_ptr.add(off), row_len,
),
FusedActivationMulOp::ReluMul => kernels::relu_mul_kernel::<T>(
a_ptr.add(off), b_ptr.add(off), out_ptr.add(off), row_len,
),
FusedActivationMulOp::SigmoidMul => kernels::sigmoid_mul_kernel::<T>(
a_ptr.add(off), b_ptr.add(off), out_ptr.add(off), row_len,
),
}
}
}
}, op_name);
Ok(out)
}
pub fn parametric_activation_impl(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
op: ParametricActivationOp,
param: f64,
op_name: &'static str,
) -> Result<Tensor<CpuRuntime>> {
let dtype = a.dtype();
let a_contig = ensure_contiguous(a)?;
let out = Tensor::<CpuRuntime>::empty(a.shape(), dtype, &client.device)?;
let total = a.numel();
let (rows, row_len) = row_layout(a.shape(), total);
let a_ptr = a_contig.ptr();
let out_ptr = out.ptr();
dispatch_dtype!(dtype, T => {
let a_ptr = a_ptr as *const T;
let out_ptr = out_ptr as *mut T;
unsafe {
for row in 0..rows {
let off = row * row_len;
match op {
ParametricActivationOp::LeakyRelu => kernels::leaky_relu_kernel::<T>(
a_ptr.add(off),
out_ptr.add(off),
row_len,
param,
),
ParametricActivationOp::Elu => kernels::elu_kernel::<T>(
a_ptr.add(off),
out_ptr.add(off),
row_len,
param,
),
}
}
}
}, op_name);
Ok(out)
}
#[inline]
pub fn leaky_relu_impl(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
negative_slope: f64,
) -> Result<Tensor<CpuRuntime>> {
parametric_activation_impl(
client,
a,
ParametricActivationOp::LeakyRelu,
negative_slope,
"leaky_relu",
)
}
#[inline]
pub fn elu_impl(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
alpha: f64,
) -> Result<Tensor<CpuRuntime>> {
parametric_activation_impl(client, a, ParametricActivationOp::Elu, alpha, "elu")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ops::ActivationOps;
use crate::runtime::Runtime;
use crate::runtime::cpu::CpuDevice;
fn row_data(w: usize, seed: i32) -> Vec<f32> {
(0..w)
.map(|i| ((i as i32 + seed) % 13 - 6) as f32 * 0.37)
.collect()
}
fn assert_row_invariant<F>(op: F)
where
F: Fn(&CpuClient, &Tensor<CpuRuntime>) -> Result<Tensor<CpuRuntime>>,
{
let device = CpuDevice::new();
let client = CpuRuntime::default_client(&device);
for &w in &[8usize, 16, 17] {
for &b in &[1usize, 2, 3, 5] {
let mut data = Vec::with_capacity(b * w);
for r in 0..b {
data.extend(row_data(w, r as i32));
}
let batched = Tensor::<CpuRuntime>::from_slice(&data, &[b, 1, w], &device).unwrap();
let batched_out: Vec<f32> = op(&client, &batched).unwrap().to_vec();
for r in 0..b {
let row = &data[r * w..(r + 1) * w];
let solo = Tensor::<CpuRuntime>::from_slice(row, &[1, 1, w], &device).unwrap();
let solo_out: Vec<f32> = op(&client, &solo).unwrap().to_vec();
let batched_row = &batched_out[r * w..(r + 1) * w];
for i in 0..w {
assert_eq!(
batched_row[i].to_bits(),
solo_out[i].to_bits(),
"w={w} b={b} row={r} idx={i}: batched={} solo={}",
batched_row[i],
solo_out[i],
);
}
}
}
}
}
#[test]
fn test_silu_row_invariant() {
assert_row_invariant(|client, x| client.silu(x));
}
#[test]
fn test_sigmoid_row_invariant() {
assert_row_invariant(|client, x| client.sigmoid(x));
}
#[test]
fn test_elu_row_invariant() {
assert_row_invariant(|client, x| client.elu(x, 1.0));
}
#[test]
fn test_silu_mul_row_invariant() {
let device = CpuDevice::new();
let client = CpuRuntime::default_client(&device);
for &w in &[8usize, 16, 17] {
for &b in &[1usize, 2, 3, 5] {
let mut a_data = Vec::with_capacity(b * w);
let mut b_data = Vec::with_capacity(b * w);
for r in 0..b {
a_data.extend(row_data(w, r as i32));
b_data.extend(row_data(w, r as i32 + 100));
}
let a = Tensor::<CpuRuntime>::from_slice(&a_data, &[b, 1, w], &device).unwrap();
let bt = Tensor::<CpuRuntime>::from_slice(&b_data, &[b, 1, w], &device).unwrap();
let batched_out: Vec<f32> = client.silu_mul(&a, &bt).unwrap().to_vec();
for r in 0..b {
let a_row = &a_data[r * w..(r + 1) * w];
let b_row = &b_data[r * w..(r + 1) * w];
let a_solo =
Tensor::<CpuRuntime>::from_slice(a_row, &[1, 1, w], &device).unwrap();
let b_solo =
Tensor::<CpuRuntime>::from_slice(b_row, &[1, 1, w], &device).unwrap();
let solo_out: Vec<f32> = client.silu_mul(&a_solo, &b_solo).unwrap().to_vec();
let batched_row = &batched_out[r * w..(r + 1) * w];
for i in 0..w {
assert_eq!(
batched_row[i].to_bits(),
solo_out[i].to_bits(),
"w={w} b={b} row={r} idx={i}: batched={} solo={}",
batched_row[i],
solo_out[i],
);
}
}
}
}
}
}