pub struct ExternFn { /* private fields */ }Expand description
A StableHLO module written by another program, called like a function
from traced programs: XLA links it with the caller and compiles them
together (extern_call). The interpreter and the native backend cannot
run it, so programs that call it run with jit.
§Examples
use ruxla_core::{Array, ExternFn, Native, Trace, make_ir};
let module = r#"
module @jit_f {
func.func public @main(%x: tensor<2xf32>) -> (tensor<2xf32>) {
%0 = stablehlo.multiply %x, %x : tensor<2xf32>
return %0 : tensor<2xf32>
}
}
"#;
let f = ExternFn::from_mlir("square", module).unwrap();
let like = Array::<Native, f32>::from_vec(vec![0.0; 2], [2]);
let ir = make_ir(|x: Array<Trace, f32>| f.call(&x, &like), &like);
assert!(ir.to_string().contains("extern_call[name=square"));Implementations§
Source§impl ExternFn
impl ExternFn
Sourcepub fn from_mlir(
name: impl Into<String>,
module: impl Into<String>,
) -> Result<Self, ExternError>
pub fn from_mlir( name: impl Into<String>, module: impl Into<String>, ) -> Result<Self, ExternError>
Reads a module in the MLIR text format (such as
jax.export.Exported.mlir_module()), whose public main is the
function; name names it in the IR and in the linked module.
§Errors
Returns an error if the module has no public main or its signature
has types that ruxla does not have.
Sourcepub fn with_vjp(self, vjp: ExternFn) -> Result<Self, ExternError>
pub fn with_vjp(self, vjp: ExternFn) -> Result<Self, ExternError>
Makes the function differentiable in reverse mode with its VJP: a
module that takes the inputs and a cotangent for each output and
returns a cotangent for each input, such as the one that
jax.export’s Exported.vjp() writes. The call is then a
custom_vjp_call whose backward function calls the VJP.
§Errors
Returns an error if the VJP does not have the signature that the function requires.
Sourcepub fn call<I: Tree<Trace>, O: Tree<Native>>(
&self,
input: &I,
like: &O,
) -> O::On<Trace>
pub fn call<I: Tree<Trace>, O: Tree<Native>>( &self, input: &I, like: &O, ) -> O::On<Trace>
Calls the module on the leaves of input and returns its outputs in
the structure of like (a pytree of native values of the output
types, whose values are not used).
§Panics
Panics if the leaves of input or like do not have the types of the
module’s inputs or outputs.