pub struct MetalKernel { /* private fields */ }Expand description
A Metal kernel built from source, as MLX’s mx.fast.metal_kernel: a name, the names
of its inputs and outputs, and the body of a kernel function. Calling it generates the
function’s signature as MLX does:
- an input of at least one dimension is
const device T* name, a 0-d inputconst constant T& name;name_shape,name_stridesandname_ndimare added when the body uses them; - an output is
device T* name(device atomic<T>* namewith atomic outputs); - thread attributes the body uses (
thread_position_in_grid,thread_index_in_simdgroup, …) become parameters; - template arguments become
constexprvalues andtypedefs at the top of the body; MLX’s type names (bfloat16_t,float16_t) andelem_to_locare defined.
It runs on the GPU only, one thread per grid position (dispatchThreads), and has no
derivative or batching rule; wrap it in crate::custom_function to give it one.
use nuvec_core::fast::{KernelConfig, MetalKernel};
use nuvec_core::{Array, DType, Device, Result, Stream, default_stream};
let exp = MetalKernel::new(
"myexp",
&["inp"],
&["out"],
"uint elem = thread_position_in_grid.x;\nout[elem] = metal::exp(inp[elem]);",
);
let x = Array::from_slice(&[0.0f32, 1.0], &[2])?;
let config = KernelConfig { grid: [2, 1, 1], threadgroup: [2, 1, 1], ..Default::default() };
let y = exp.call(&[x], &[(&[2], DType::F32)], &config)?.remove(0);
assert!((y.to_vec::<f32>()?[1] - std::f32::consts::E).abs() < 1e-6);Implementations§
Source§impl MetalKernel
impl MetalKernel
Sourcepub fn new(name: &str, inputs: &[&str], outputs: &[&str], source: &str) -> Self
pub fn new(name: &str, inputs: &[&str], outputs: &[&str], source: &str) -> Self
A kernel named name (letters, digits and underscores) whose body source reads
inputs and writes outputs (in this order).
Sourcepub fn header(self, header: &str) -> Self
pub fn header(self, header: &str) -> Self
Code placed before the kernel function (helper functions, includes).
Sourcepub fn ensure_row_contiguous(self, yes: bool) -> Self
pub fn ensure_row_contiguous(self, yes: bool) -> Self
Whether inputs are copied to row-major order first (the default, as MLX); otherwise
the body indexes them through name_strides.
Sourcepub fn atomic_outputs(self, yes: bool) -> Self
pub fn atomic_outputs(self, yes: bool) -> Self
Whether outputs are device atomic<T>* (to be updated with atomic_* functions).
Trait Implementations§
Source§impl Clone for MetalKernel
impl Clone for MetalKernel
Auto Trait Implementations§
impl Freeze for MetalKernel
impl RefUnwindSafe for MetalKernel
impl Send for MetalKernel
impl Sync for MetalKernel
impl Unpin for MetalKernel
impl UnsafeUnpin for MetalKernel
impl UnwindSafe for MetalKernel
Blanket Implementations§
impl<T> AutoreleaseSafe for Twhere
T: ?Sized,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more