Skip to main content

coeus_nn/
softmax.rs

1// ── Softmax ──
2// Softmax along a dimension with proper autograd backward pass.
3
4use crate::module::{Module, ModuleError};
5use coeus_autograd::Var;
6use coeus_core::Float;
7
8/// Functional softmax along `dim`.
9pub fn softmax<T: Float + coeus_cpu::RealScalar, B: coeus_ops::BackendOps<T> + Default>(
10    input: &Var<T, B>,
11    dim: isize,
12) -> Result<Var<T, B>, B::Error> {
13    coeus_autograd::softmax(input, dim)
14}
15
16/// Softmax module (last dimension by default).
17#[derive(Clone, Debug, Default)]
18pub struct Softmax {
19    /// Axis along which to apply softmax (negative indices count from the end).
20    dim: isize,
21}
22
23impl Softmax {
24    /// Create a `Softmax` module that normalizes along `dim`.
25    pub fn new(dim: isize) -> Self {
26        Self { dim }
27    }
28}
29
30impl<T: Float + coeus_cpu::RealScalar, B: coeus_ops::BackendOps<T> + Default> Module<T, B>
31    for Softmax
32{
33    fn parameters(&self) -> Vec<Var<T, B>> {
34        vec![]
35    }
36
37    fn forward(&self, input: &Var<T, B>) -> Result<Var<T, B>, ModuleError<B::Error>> {
38        let rank = input.tensor.ndim();
39        let requested = self.dim.unsigned_abs();
40        let axis = if self.dim >= 0 {
41            usize::try_from(self.dim).map_err(|_| ModuleError::InvalidAxis {
42                module: "Softmax",
43                axis: requested,
44                rank,
45            })?
46        } else {
47            rank.checked_sub(requested)
48                .ok_or(ModuleError::InvalidAxis {
49                    module: "Softmax",
50                    axis: requested,
51                    rank,
52                })?
53        };
54        if axis >= rank {
55            return Err(ModuleError::InvalidAxis {
56                module: "Softmax",
57                axis: requested,
58                rank,
59            });
60        }
61        let normalized = isize::try_from(axis).map_err(|_| ModuleError::InvalidAxis {
62            module: "Softmax",
63            axis,
64            rank,
65        })?;
66
67        Ok(softmax(input, normalized)?)
68    }
69}