log_softmax_simd

Function log_softmax_simd 

Source
pub fn log_softmax_simd<F>(x: &ArrayView1<'_, F>) -> Array1<F>
where F: Float + SimdUnifiedOps,
Expand description

Apply Log-Softmax function using SIMD operations

The log-softmax function is defined as: log_softmax(x_i) = x_i - log(Σ_j exp(x_j))

This is more numerically stable than computing log(softmax(x)) directly, and is critical for neural network training:

  • Cross-entropy loss computation
  • Classification networks (output layer)
  • Transformer language models
  • Large language models (LLMs)

§Mathematical Properties

  • log_softmax(x) = x - logsumexp(x)
  • Σ exp(log_softmax(x)) = 1 (outputs are log-probabilities)
  • log_softmax(x + c) = log_softmax(x) (invariant to constant shift)
  • Maximum output element approaches 0 for peaked distributions

§Arguments

  • x - Input array of logits (unnormalized log-probabilities)

§Returns

  • Array with log-softmax applied elementwise (log-probabilities that sum to 0)

§Example

use scirs2_core::ndarray_ext::elementwise::log_softmax_simd;
use ndarray::{array, ArrayView1};

let logits = array![1.0_f64, 2.0, 3.0];
let log_probs = log_softmax_simd(&logits.view());

// Verify: exp(log_probs) sums to 1
let probs: f64 = log_probs.mapv(|lp| lp.exp()).sum();
assert!((probs - 1.0).abs() < 1e-10);

// log_softmax values are always <= 0
for &lp in log_probs.iter() {
    assert!(lp <= 0.0);
}

§Applications

  • Cross-Entropy Loss: -Σ target * log_softmax(logits)
  • Classification: Converting logits to log-probabilities
  • Transformers: Final output layer for token prediction
  • Language Models: Computing perplexity and token probabilities
  • Numerical Stability: Avoiding underflow in softmax computation