numr 0.14.0

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
Documentation
//! Indexing operations (gather, embedding lookup)

use super::ops::*;
use crate::autograd::Var;
use crate::dtype::DType;
use crate::error::Result;
use crate::ops::IndexingOps;
use crate::runtime::{Runtime, RuntimeClient};
use std::sync::Arc;

/// Gather along a dimension: z = gather(a, dim, index)
pub fn var_gather<R, C>(
    a: &Var<R>,
    dim: usize,
    index: &crate::tensor::Tensor<R>,
    client: &C,
) -> Result<Var<R>>
where
    R: Runtime<DType = DType>,
    C: RuntimeClient<R> + IndexingOps<R>,
    R::Client: IndexingOps<R>,
{
    let output = client.gather(a.tensor(), dim, index)?;

    if a.requires_grad() {
        let grad_fn =
            GatherBackward::<R>::new(a.id(), a.shape(), dim, index.clone(), a.grad_fn().cloned());
        Ok(Var::from_op(output, Arc::new(grad_fn)))
    } else {
        Ok(Var::new(output, false))
    }
}

/// Embedding lookup: z = embedding_lookup(weight, indices)
///
/// `indices` are integer token IDs and are not differentiable. Gradients flow
/// only to `weight` and repeated token IDs accumulate additively.
pub fn var_embedding_lookup<R, C>(
    weight: &Var<R>,
    indices: &crate::tensor::Tensor<R>,
    client: &C,
) -> Result<Var<R>>
where
    R: Runtime<DType = DType>,
    C: RuntimeClient<R> + IndexingOps<R>,
    R::Client: IndexingOps<R>,
{
    let output = client.embedding_lookup(weight.tensor(), indices)?;

    if weight.requires_grad() {
        let grad_fn = EmbeddingLookupBackward::<R>::new(
            weight.id(),
            weight.shape(),
            indices.clone(),
            weight.grad_fn().cloned(),
        );
        Ok(Var::from_op(output, Arc::new(grad_fn)))
    } else {
        Ok(Var::new(output, false))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::autograd::backward;
    use crate::autograd::test_fd::{check_grad_f64, f64_tensor, grad_var};
    use crate::runtime::cpu::{CpuDevice, CpuRuntime};
    use crate::tensor::Tensor;

    #[test]
    fn test_var_gather_backward() {
        let device = CpuDevice::new();
        let client = CpuRuntime::default_client(&device);

        // Input: 2x3 matrix
        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3], &device)
                .unwrap(),
            true,
        );

        // Gather along dim=1 with indices [[0, 2], [1, 0]]
        let index = Tensor::<CpuRuntime>::from_slice(&[0i64, 2, 1, 0], &[2, 2], &device).unwrap();
        let z = var_gather(&x, 1, &index, &client).unwrap();

        // z = [[1, 3], [5, 4]]
        let z_data: Vec<f32> = z.tensor().to_vec();
        assert_eq!(z_data, vec![1.0, 3.0, 5.0, 4.0]);

        let loss = crate::autograd::var_ops::var_sum(&z, &[0, 1], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        let grad_x: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        // Grad scatters 1s back: x[0,0] += 1, x[0,2] += 1, x[1,1] += 1, x[1,0] += 1
        // So grad = [[1, 0, 1], [1, 1, 0]]
        assert_eq!(grad_x, vec![1.0, 0.0, 1.0, 1.0, 1.0, 0.0]);
    }

    #[test]
    fn test_var_gather_repeated_indices_accumulate() {
        // index [[0, 0, 2]] reads x[0] twice, so its gradient is 2.
        let device = CpuDevice::new();
        let client = CpuRuntime::default_client(&device);
        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[1.0f64, 2.0, 3.0], &[1, 3], &device).unwrap(),
            true,
        );
        let index = Tensor::<CpuRuntime>::from_slice(&[0i64, 0, 2], &[1, 3], &device).unwrap();

        let z = var_gather(&x, 1, &index, &client).unwrap();
        let loss = crate::autograd::var_ops::var_sum(&z, &[], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        let grad_x: Vec<f64> = grads.get(x.id()).unwrap().to_vec();
        assert_eq!(grad_x, vec![2.0, 0.0, 1.0]);
    }

    fn gather_index(values: &[i64], shape: &[usize]) -> Tensor<CpuRuntime> {
        Tensor::<CpuRuntime>::from_slice(values, shape, &CpuDevice::new()).unwrap()
    }

    #[test]
    fn test_var_gather_fd_dim1_duplicate_indices() {
        let index = gather_index(&[0, 0, 2, 1, 1, 1, 3, 0, 3], &[3, 3]);
        check_grad_f64(
            "var_gather dim 1",
            move |inputs, c| var_gather(&inputs[0], 1, &index, c),
            &[f64_tensor(
                &[
                    0.3, -1.2, 0.7, 2.1, -0.4, 1.5, 0.9, -2.2, 1.1, 0.0, -0.6, 0.8,
                ],
                &[3, 4],
            )],
        );
    }

    #[test]
    fn test_var_gather_fd_dim0_duplicate_indices() {
        let index = gather_index(&[2, 0, 2, 2, 0, 0], &[2, 3]);
        check_grad_f64(
            "var_gather dim 0",
            move |inputs, c| var_gather(&inputs[0], 0, &index, c),
            &[f64_tensor(
                &[0.5, -0.25, 1.75, -1.0, 2.5, 0.125, 0.9, -1.1, 0.4],
                &[3, 3],
            )],
        );
    }

    #[test]
    fn test_var_gather_fd_double_backward() {
        // g(x) = d/dx sum(c * gather(x)^3), built by `backward_with_graph`.
        // The check differentiates g, so it covers `GatherBackward::backward_var`.
        let index = gather_index(&[0, 0, 2, 1, 2, 2], &[2, 3]);
        check_grad_f64(
            "var_gather second order",
            move |inputs, c| {
                let weight_values = [0.7, -1.3, 0.4, 1.1, -0.6, 0.9];
                let weight = Var::new(f64_tensor(&weight_values, &[2, 3]), false);
                let z = var_gather(&inputs[0], 1, &index, c)?;
                let sq = crate::autograd::var_mul(&z, &z, c)?;
                let cube = crate::autograd::var_mul(&sq, &z, c)?;
                let weighted = crate::autograd::var_mul(&cube, &weight, c)?;
                let loss = crate::autograd::var_sum(&weighted, &[], false, c)?;
                let grads = crate::autograd::backward_with_graph(&loss, c)?;
                grad_var(&grads, inputs[0].id())
            },
            &[f64_tensor(&[0.3, -0.8, 1.2, 0.6, -0.2, 0.5], &[2, 3])],
        );
    }

    #[test]
    fn test_var_embedding_lookup_backward_gradient_flows() {
        let device = CpuDevice::new();
        let client = CpuRuntime::default_client(&device);

        let weight = Var::new(
            Tensor::<CpuRuntime>::from_slice(
                &[
                    0.5f32, -1.0, 2.0, 3.5, 4.0, -2.5, 1.25, 0.75, -0.5, 2.25, 5.0, -3.0,
                ],
                &[4, 3],
                &device,
            )
            .unwrap(),
            true,
        );
        let indices = Tensor::<CpuRuntime>::from_slice(&[0i64, 2, 1], &[3], &device).unwrap();

        let out = var_embedding_lookup(&weight, &indices, &client).unwrap();
        let loss = crate::autograd::var_ops::var_sum(&out, &[0, 1], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        let grad_weight = grads.get(weight.id()).expect("weight gradient missing");
        let grad_data = grad_weight.contiguous().unwrap().to_vec::<f32>();
        assert!(grad_data.iter().any(|&g| g != 0.0));
    }

    #[test]
    fn test_var_embedding_lookup_repeated_ids_accumulate() {
        let device = CpuDevice::new();
        let client = CpuRuntime::default_client(&device);

        let single_weight = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2], &device)
                .unwrap(),
            true,
        );
        let single_indices = Tensor::<CpuRuntime>::from_slice(&[0i64], &[1], &device).unwrap();
        let single_out = var_embedding_lookup(&single_weight, &single_indices, &client).unwrap();
        let single_loss =
            crate::autograd::var_ops::var_sum(&single_out, &[0, 1], false, &client).unwrap();
        let single_grads = backward(&single_loss, &client).unwrap();
        let single_grad = single_grads
            .get(single_weight.id())
            .expect("single weight gradient missing")
            .contiguous()
            .unwrap()
            .to_vec::<f32>();

        let repeated_weight = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2], &device)
                .unwrap(),
            true,
        );
        let repeated_indices = Tensor::<CpuRuntime>::from_slice(&[0i64, 0], &[2], &device).unwrap();
        let repeated_out =
            var_embedding_lookup(&repeated_weight, &repeated_indices, &client).unwrap();
        let repeated_loss =
            crate::autograd::var_ops::var_sum(&repeated_out, &[0, 1], false, &client).unwrap();
        let repeated_grads = backward(&repeated_loss, &client).unwrap();
        let repeated_grad = repeated_grads
            .get(repeated_weight.id())
            .expect("repeated weight gradient missing")
            .contiguous()
            .unwrap()
            .to_vec::<f32>();

        assert_eq!(repeated_grad[0], single_grad[0] * 2.0);
        assert_eq!(repeated_grad[1], single_grad[1] * 2.0);
    }

    #[test]
    fn test_var_embedding_lookup_unindexed_rows_have_zero_grad() {
        let device = CpuDevice::new();
        let client = CpuRuntime::default_client(&device);

        let weight = Var::new(
            Tensor::<CpuRuntime>::from_slice(
                &[1.0f32, -2.0, 3.0, 4.0, -5.0, 6.0],
                &[3, 2],
                &device,
            )
            .unwrap(),
            true,
        );
        let indices = Tensor::<CpuRuntime>::from_slice(&[1i64], &[1], &device).unwrap();

        let out = var_embedding_lookup(&weight, &indices, &client).unwrap();
        let loss = crate::autograd::var_ops::var_sum(&out, &[0, 1], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        let grad_data = grads
            .get(weight.id())
            .expect("weight gradient missing")
            .contiguous()
            .unwrap()
            .to_vec::<f32>();
        assert_eq!(grad_data[0], 0.0);
        assert_eq!(grad_data[1], 0.0);
        assert_eq!(grad_data[4], 0.0);
        assert_eq!(grad_data[5], 0.0);
    }
}