Skip to main content

softmax_backward

Function softmax_backward 

Source
pub fn softmax_backward<T: Triton, D: Float, const BLOCK_SIZE: i32>(
    dy_ptr: T::Pointer<D>,
    y_ptr: T::Pointer<D>,
    dx_ptr: T::Pointer<D>,
    _n_rows: i32,
    n_cols: i32,
)
where T::I32Tensor: Tensor<i32, 1> + Comparison<i32, BoolTensor = T::BoolTensor>, T::Pointer<D>: AddOffsets<i32, 1, T::I32Tensor, Output = T::Tensor<T::Pointer<D>>>,
Expand description

Row-wise softmax backward pass.

Given the saved softmax output y = softmax(x) and the upstream gradient dy, computes the input gradient:

dx_i = y_i * (dy_i - sum_j(y_j * dy_j))

Grid: one CTA per row — pid = row index.

The dot product sum(y * dy) is a row-scalar that is broadcast back to the full row when computing dy - dot.

Constraint: BLOCK_SIZE must equal n_cols (same as the forward pass).