pub trait Primitive:
Send
+ Sync
+ 'static {
// Required methods
fn name(&self) -> &'static str;
fn abstract_eval(
&self,
inputs: &[AVal],
params: &Params,
) -> Result<Vec<AVal>>;
fn eval(
&self,
cx: &dyn EvalCx,
inputs: &[HostArray],
params: &Params,
) -> Result<Vec<HostArray>>;
// Provided methods
fn lower(
&self,
cx: &mut dyn LowerCx,
inputs: &[HloValue],
params: &Params,
outputs: &[AVal],
) -> Result<Vec<HloValue>> { ... }
fn batch(
&'static self,
cx: &mut dyn BatchCx,
inputs: &[Batched],
params: &Params,
) -> Result<Vec<Batched>> { ... }
fn jvp(
&'static self,
cx: &mut dyn AdCx,
primals: &[Atom],
tangents: &[Tangent],
params: &Params,
) -> Result<(Vec<Atom>, Vec<Tangent>)> { ... }
fn transpose(
&'static self,
cx: &mut dyn AdCx,
cotangents: &[Tangent],
inputs: &[TransposeInput],
params: &Params,
) -> Result<Vec<Tangent>> { ... }
}Expand description
A primitive: the smallest unit that carries rules for every transformation and backend.
It has the rules for shape inference, evaluation, lowering to StableHLO,
batching (vmap), and differentiation (jvp and transpose).
Required Methods§
Provided Methods§
Sourcefn lower(
&self,
cx: &mut dyn LowerCx,
inputs: &[HloValue],
params: &Params,
outputs: &[AVal],
) -> Result<Vec<HloValue>>
fn lower( &self, cx: &mut dyn LowerCx, inputs: &[HloValue], params: &Params, outputs: &[AVal], ) -> Result<Vec<HloValue>>
Emits the StableHLO operations of the primitive.
§Errors
Returns an error if the primitive cannot be lowered for these inputs.
Sourcefn batch(
&'static self,
cx: &mut dyn BatchCx,
inputs: &[Batched],
params: &Params,
) -> Result<Vec<Batched>>
fn batch( &'static self, cx: &mut dyn BatchCx, inputs: &[Batched], params: &Params, ) -> Result<Vec<Batched>>
Emits the batched version of the primitive (vmap).
inputs carry their batch dimensions, at least one of which is not
None. The rule returns the outputs with their batch dimensions. It
follows JAX’s batching rule of the primitive, so that the batched IR
has the same form as JAX’s (see crate::batching).
§Errors
Returns an error if the primitive cannot be batched for these inputs.
Sourcefn jvp(
&'static self,
cx: &mut dyn AdCx,
primals: &[Atom],
tangents: &[Tangent],
params: &Params,
) -> Result<(Vec<Atom>, Vec<Tangent>)>
fn jvp( &'static self, cx: &mut dyn AdCx, primals: &[Atom], tangents: &[Tangent], params: &Params, ) -> Result<(Vec<Atom>, Vec<Tangent>)>
The JVP rule: returns the outputs for primals and their tangents
for the input tangents (None for symbolic zeros; not all are
zero), computed with operations linear in the tangents.
§Errors
Returns an error if the primitive cannot be differentiated.
Sourcefn transpose(
&'static self,
cx: &mut dyn AdCx,
cotangents: &[Tangent],
inputs: &[TransposeInput],
params: &Params,
) -> Result<Vec<Tangent>>
fn transpose( &'static self, cx: &mut dyn AdCx, cotangents: &[Tangent], inputs: &[TransposeInput], params: &Params, ) -> Result<Vec<Tangent>>
The transpose rule of a primitive that is linear in its
TransposeInput::Linear inputs: returns the cotangent of each
input (None for constant inputs and symbolic zeros) for the output
cotangents (not all zero).
§Errors
Returns an error if the primitive is not linear in those inputs.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".