Skip to main content

Vmap

Struct Vmap 

Source
pub struct Vmap<F> { /* private fields */ }
Expand description

A batched function; see vmap.

Implementations§

Source§

impl<F> Vmap<F>

Source

pub fn call<I, O>(&self, examples: &[I]) -> Vec<O::On<Native>>
where I: Tree<Native>, O: Tree<Trace>, F: Fn(I::On<Trace>) -> O,

Runs the batched program on examples with the interpreter and returns one output per example.

§Panics

Panics if there are no examples, if they differ in structure or types, or if a primitive cannot be batched.

Source

pub fn call_xla<I, O>( &self, examples: &[I], ) -> Result<Vec<O::On<Native>>, Error>
where I: Tree<Native>, O: Tree<Trace>, F: Fn(I::On<Trace>) -> O,

Runs the batched program on examples with XLA and returns one output per example.

§Errors

Returns an error if no PJRT plugin is configured, or if compiling or running fails.

§Panics

See Vmap::call.

Source

pub fn call_traced<I, O>(&self, examples: I) -> O
where I: Tree<Trace>, O: Tree<Trace>, F: Fn(I) -> O,

Calls the batched function inside another trace on examples, pytrees of Trace values whose arrays are stacked along a new leading dimension (the number of examples). The batched body becomes a jit equation of the outer IR. Values that f captures from the outer trace are shared by all examples (not batched).

The stacked values have the types of one example, so a stacked Scalar holds an array of rank 1: pass such values on (to another vmap, a loop’s carry, or an output) without computing with them, and return arrays of rank 0 from f for values to compute with afterwards.

§Examples
use ruxla_core::{Array, F32, Trace, interpret, vmap};

let f = |(xs, y): (Array<Trace, f32>, F32<Trace>)| {
    vmap(|x: Array<Trace, f32>| x * y).call_traced(xs)
};
let out = interpret(f, &(Array::from_vec(vec![1.0f32, 2.0, 3.0], [3]), F32::new(2.0)));
assert_eq!(out.to_vec(), [2.0, 4.0, 6.0]);
§Panics

Panics if the leaves of examples do not have the same leading dimension, or if a primitive cannot be batched.

Source

pub fn make_ir<I, O>(&self, example: &I, n: usize) -> Function
where I: Tree<Native>, O: Tree<Trace>, F: Fn(I::On<Trace>) -> O,

Returns the batched IR for n examples shaped like example.

§Panics

Panics if a primitive cannot be batched.

Trait Implementations§

Source§

impl<F> Debug for Vmap<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<F> Freeze for Vmap<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for Vmap<F>
where F: RefUnwindSafe,

§

impl<F> Send for Vmap<F>
where F: Send,

§

impl<F> Sync for Vmap<F>
where F: Sync,

§

impl<F> Unpin for Vmap<F>
where F: Unpin,

§

impl<F> UnsafeUnpin for Vmap<F>
where F: UnsafeUnpin,

§

impl<F> UnwindSafe for Vmap<F>
where F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.