Skip to main content

ExternFn

Struct ExternFn 

Source
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

Source

pub fn from_mlir( name: impl Into<String>, module: impl Into<String>, ) -> Result<ExternFn, 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.

Source

pub fn with_vjp(self, vjp: ExternFn) -> Result<ExternFn, 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.

Source

pub fn inputs(&self) -> &[AVal]

The types of the inputs of main.

Source

pub fn outputs(&self) -> &[AVal]

The types of the outputs of main.

Source

pub fn call<I, O>(&self, input: &I, like: &O) -> <O as Tree<Native>>::On<Trace>
where I: Tree<Trace>, O: Tree<Native>,

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.

Trait Implementations§

Source§

impl Clone for ExternFn

Source§

fn clone(&self) -> ExternFn

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ExternFn

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for ExternFn

§

impl Freeze for ExternFn
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze, *const ArcInner<str> is !null: Freeze, PhantomData<ArcInner<str>>: Freeze, PhantomData<AVal>: Freeze, *const ArcInner<ExternFn> is !null: Freeze, PhantomData<ArcInner<ExternFn>>: Freeze,

§

impl Move for ExternFn

§

impl RefUnwindSafe for ExternFn

§

impl Send for ExternFn
where RawVecInner: Send, Arc<str>: Send, Arc<ExternFn>: Send,

§

impl Sync for ExternFn
where RawVecInner: Sync, Arc<str>: Sync, Arc<ExternFn>: Sync,

§

impl Unpin for ExternFn
where *const u8 is !null: Unpin, Arc<str>: Unpin, Arc<ExternFn>: Unpin,

§

impl UnsafeUnpin for ExternFn

§

impl UnwindSafe for ExternFn
where RawVecInner: UnwindSafe, Arc<str>: UnwindSafe, Arc<ExternFn>: 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
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.