Skip to content
fn

run_metal_kernel

→Storage
int run_metal_kernel(const MetalKernel & kernel, const int & inputs, const int & output_shape, Dtype output_dtype, const KernelLaunchConfig & config, const int & constants)

Encode + dispatch a compiled kernel; block until it completes.

Allocates an output SharedStorage of output_shape / output_dtype, binds inputs at indices 0..n-1, constants at n..n+k-1, and the output buffer at n+k, then dispatches the threadgroup grid given by config and waits synchronously for the GPU to finish.

See Also

compile_metal_kernel — produces kernel. make_metal_shared — backs the returned storage.

Parameters

kernelconst MetalKernel&
Compiled pipeline produced by compile_metal_kernel. Must satisfy kernel.is_valid() == true.
inputsconst std::vector<Storage>&
Input tensors. SharedStorage is bridged in place; GpuStorage is materialised (eval()) and copied into a fresh shared buffer; CpuStorage is copied verbatim.
output_shapeconst Shape&
Logical shape of the output tensor.
output_dtypeDtype
Element dtype of the output tensor.
configconst KernelLaunchConfig&
Threadgroup grid + per-threadgroup thread geometry.
constantsconst std::vector<KernelConstant>&= None
Scalar constants bound after the input buffers. Defaults to an empty list.

Returns

Storage

SharedStorage backed by a unified-memory buffer; the result is immediately CPU-readable on return (the call waits for completion).

Raises

:runtime_error
If the kernel is invalid, an input type cannot be resolved, or the GPU dispatch fails.

Notes

Returning SharedStorage (rather than GpuStorage) trades some bandwidth for caller flexibility — the result can flow into either CPU or GPU consumers without an extra copy.