Skip to main content

vmap

Function vmap 

Source
pub fn vmap(
    f: &Function,
    in_axes: &[Option<usize>],
    axis_size: usize,
) -> Result<(Function, Vec<Option<usize>>)>
Expand description

Batches f over a batch of axis_size examples.

in_axes[i] is the dimension of input i that indexes the examples, or None if the input is the same for every example. Returns the batched function and, for each output, its batch dimension: Some(0) if it depends on the batched inputs and None otherwise.

The batched function computes the same values as applying f to each example and stacking the results, without a loop: each primitive is replaced by its batched form, following JAX’s batching rules.

§Errors

Returns an error if in_axes does not match the inputs of f, or if a primitive cannot be batched (Error::Unsupported).

§Examples

use ruxla_ir::{AVal, Atom, DType, Eqn, Function, Params, Var};

// f(x) = sin(x) for x: f32[3]
let x = Var::new(AVal::new([3], DType::F32));
let y = Var::new(AVal::new([3], DType::F32));
let f = Function {
    invars: vec![x.clone()],
    eqns: vec![Eqn {
        prim: ruxla_prims::SIN,
        inputs: vec![Atom::Var(x)],
        params: Params::None,
        outputs: vec![y.clone()],
        loc: None,
    }],
    outvars: vec![Atom::Var(y)],
    ..Function::default()
};
let (g, out_axes) = ruxla_transforms::vmap(&f, &[Some(1)], 5).unwrap();
assert_eq!(g.in_avals()[0], AVal::new([3, 5], DType::F32));
assert_eq!(g.out_avals()[0], AVal::new([5, 3], DType::F32));
assert_eq!(out_axes, [Some(0)]);