Skip to main content

select_with

Function select_with 

Source
pub fn select_with<B: Backend, T: Tree<B>>(
    pred: Bool<B>,
    on_true: impl FnOnce() -> T,
    on_false: impl FnOnce() -> T,
) -> T
Expand description

select_tree(pred, &on_true(), &on_false()), computing only the chosen side when the backend can branch (natively); traced, both sides are computed and selected, which gives the same IR as lax.select in JAX.

Use it where a value is computed and then discarded unless a condition holds (a reset at the end of an episode, for example); the closures must be pure, so that skipping one does not change the result.

ยงExamples

use ruxla_core::{F32, Native, lax};

let done = ruxla_core::Bool::<Native>::new(false);
let x = lax::select_with(done, || F32::new(0.0), || F32::new(1.5));
assert_eq!(x.value(), 1.5);