use super::super::kernel::elementwise_unary;
use crate::backend_ops::{BackendOps, UnaryOp};
use coeus_core::{CpuAddressableStorage, CpuAddressableStorageMut, Float};
use coeus_tensor::Tensor;
#[inline]
pub fn log_softmax_axis<T: Float, B: BackendOps<T> + Default>(
input: &Tensor<T, B>,
axis: usize,
backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
let ndim = input.ndim();
assert!(
axis < ndim,
"log_softmax_axis: axis {axis} out of bounds for ndim {ndim}"
);
let max_vals = crate::reduction::max_axis(input, axis, backend)?;
let shifted = crate::binary::sub(input, &max_vals, backend);
let exp_shifted = elementwise_unary(&shifted, backend, UnaryOp::Exp)?;
let sum_exp = crate::reduction::sum_axis(&exp_shifted, axis, backend)?;
let log_sum_exp = elementwise_unary(&sum_exp, backend, UnaryOp::Log)?;
Ok(crate::binary::sub(&shifted, &log_sum_exp, backend))
}
#[inline]
pub fn masked_softmax<T: Float, B: BackendOps<T> + Default>(
input: &Tensor<T, B>,
mask: &Tensor<T, B>,
dim: usize,
backend: &B,
) -> Result<Tensor<T, B>, B::Error>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
assert_eq!(
input.shape(),
mask.shape(),
"masked_softmax: input and mask must have the same shape"
);
let ndim = input.ndim();
assert!(
dim < ndim,
"masked_softmax: dim {dim} out of bounds for {ndim}D input"
);
let shape = input.shape();
let axis = shape[dim];
let pre_count: usize = shape[..dim].iter().product();
let post_count: usize = shape[dim + 1..].iter().product();
let input_contiguous = input.to_contiguous_on(backend);
let mask_contiguous = mask.to_contiguous_on(backend);
let input_values = input_contiguous.as_slice();
let mask_values = mask_contiguous.as_slice();
let mut output = vec![T::zero(); input.numel()];
for pre in 0..pre_count {
for post in 0..post_count {
let base = pre * axis * post_count + post;
let mut row_max: Option<T> = None;
for lane in 0..axis {
let idx = base + lane * post_count;
if mask_values[idx] != T::zero() {
row_max = Some(match row_max {
Some(current) if current > input_values[idx] => current,
_ => input_values[idx],
});
}
}
let Some(row_max) = row_max else {
continue;
};
let mut row_sum = T::zero();
for lane in 0..axis {
let idx = base + lane * post_count;
if mask_values[idx] != T::zero() {
let value = (input_values[idx] - row_max).exp();
output[idx] = value;
row_sum += value;
}
}
if row_sum != T::zero() {
for lane in 0..axis {
let idx = base + lane * post_count;
output[idx] = output[idx] / row_sum;
}
}
}
}
Ok(Tensor::from_slice_on(shape.to_vec(), &output, backend))
}
#[inline]
pub fn causal_softmax<T: Float, B: BackendOps<T> + Default>(
input: &Tensor<T, B>,
dim: usize,
backend: &B,
) -> Result<Tensor<T, B>, B::Error>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
let ndim = input.ndim();
assert!(
dim < ndim,
"causal_softmax: dim {dim} out of bounds for {ndim}D input"
);
assert!(
dim > 0 && dim + 1 == ndim,
"causal_softmax: dim must be the final axis of a rank >= 2 tensor"
);
let shape = input.shape();
let seq_q = shape[dim - 1];
let seq_k = shape[dim];
let numel = input.numel();
let outer: usize = shape[..dim - 1].iter().product::<usize>().max(1);
let mut mask_data = vec![T::zero(); numel];
for batch in 0..outer {
for i in 0..seq_q {
for j in 0..seq_k {
if j <= i {
let flat = batch * seq_q * seq_k + i * seq_k + j;
if flat < numel {
mask_data[flat] = T::one();
}
}
}
}
}
let mask = Tensor::from_slice_on(shape.to_vec(), &mask_data, backend);
masked_softmax(input, &mask, dim, backend)
}