Skip to main content

ruxla_pjrt/
lib.rs

1//! PJRT runtime for ruxla.
2//!
3//! This crate loads PJRT plugins (shared libraries that implement the
4//! [PJRT C API]), compiles StableHLO programs with them, and runs the
5//! resulting executables on CPUs and GPUs.
6//!
7//! The entry point is [`Plugin`]. A plugin creates a [`Client`], which owns
8//! the devices of one platform. A client compiles StableHLO text into a
9//! [`LoadedExecutable`] and moves data between the host and the devices as
10//! [`Buffer`]s.
11//!
12//! # Examples
13//!
14//! ```no_run
15//! use ruxla_pjrt::{ElementType, Plugin};
16//!
17//! # fn main() -> Result<(), ruxla_pjrt::Error> {
18//! let plugin = Plugin::load(ruxla_pjrt::CPU_PLUGIN_FILE)?;
19//! let client = plugin.client()?;
20//! let exe = client.compile(
21//!     r#"
22//!     func.func public @main(%x: tensor<2xf32>) -> tensor<2xf32> {
23//!       %0 = stablehlo.add %x, %x : tensor<2xf32>
24//!       return %0 : tensor<2xf32>
25//!     }
26//!     "#,
27//! )?;
28//! let x = client.buffer_from_host(&[1.0f32, 2.0], &[2])?;
29//! let y = exe.execute(&[&x])?;
30//! assert_eq!(y[0].element_type()?, ElementType::F32);
31//! assert_eq!(y[0].to_vec::<f32>()?, [2.0, 4.0]);
32//! # Ok(())
33//! # }
34//! ```
35//!
36//! [PJRT C API]: https://openxla.org/xla/pjrt
37
38mod api;
39mod buffer;
40mod callback;
41mod client;
42mod compile_options;
43mod cuda;
44mod cusolver;
45mod element_type;
46mod error;
47mod executable;
48pub mod ffi;
49pub mod linalg;
50mod plugin;
51// The raw bindings of the C API (generated by bindgen): not part of the documented API.
52#[allow(
53    missing_docs,
54    missing_debug_implementations,
55    non_camel_case_types,
56    non_snake_case,
57    non_upper_case_globals,
58    unreachable_pub,
59    clippy::all
60)]
61#[doc(hidden)]
62pub mod sys;
63
64pub use buffer::{Buffer, HostElement};
65pub use client::{Client, ClientOption, Device};
66pub use compile_options::CompileOptions;
67pub use element_type::ElementType;
68pub use error::{Error, ErrorCode, Result};
69pub use executable::{Event, ExecuteOptions, LoadedExecutable};
70pub use plugin::Plugin;
71
72/// The file name of the PJRT CPU plugin on this platform: `libpjrt_cpu.dylib`
73/// on macOS, `libpjrt_cpu.so` elsewhere (as the builds of
74/// `zml/pjrt-artifacts` name it).
75pub const CPU_PLUGIN_FILE: &str = if cfg!(target_os = "macos") {
76    "libpjrt_cpu.dylib"
77} else {
78    "libpjrt_cpu.so"
79};
80
81/// The file name of the PJRT CUDA plugin (as JAX's `jax[cuda13]` installs
82/// it; CUDA plugins exist for Linux only).
83pub const CUDA_PLUGIN_FILE: &str = "xla_cuda_plugin.so";