Skip to main content

WhereRule

Struct WhereRule 

Source
pub struct WhereRule;
Expand description

Backward rule for Op::Where (ternary select).

Forward: out[i] = if cond[i] != 0 { a[i] } else { b[i] }. Inputs: (cond, a, b) where cond is U8, a and b share dtype T.

Gradients:

  • cond: None (U8 mask is non-differentiable).
  • a: upstream * cast(cond, T) — gradient flows only at the slots where a was picked.
  • b: upstream * cast(1 - cond, T) — gradient flows only at the slots where b was picked.

The 1 - cond mask is built via Op::AddScalar(-1.0) followed by Op::Neg: m_b = -(m_a - 1) = 1 - m_a. This avoids needing a Const(1) factory inside the rule (which would require synthesizing a slot-populated leaf via the device handle).

Trait Implementations§

Source§

impl GradientRule for WhereRule

Source§

fn backward( &self, graph: &SharedGraph, _op: &Op, inputs: &[NodeId], _output: NodeId, upstream: NodeId, ) -> GradientList

Build gradient nodes for this op. Read more

Auto Trait Implementations§

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
Source§

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

Source§

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.

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V