MetalRuntime manages the Metal device, command queue, and buffer lifecycle. It dispatches compiled kernels and returns output data.
Constructor
dylib_path overrides the default path to the Swift runtime dylib. Leave it None in almost all cases.
execute()
One-shot dispatch. Allocates buffers, runs the kernel, reads output, and returns raw bytes.
Parameters
Returns
Rawbytes of the output buffer. Convert with np.frombuffer:
prepare()
Pre-allocate Metal buffers for repeated dispatch. Use this in hot loops to avoid per-call allocation overhead.
device_capabilities()
Query hardware features of the active Metal device.
DeviceCapabilities below.
PreparedKernel
Returned by rt.prepare(...). Holds pre-allocated Metal buffers.
dispatch()
read_output() afterward.
dispatch_timed()
read_output()
Example: benchmarking loop
DeviceCapabilities
Fields
require_m3()
RuntimeError if the device is older than M3. Pass a description of what you’re gating:
Example
Dispatch errors
When dispatch fails,rt.execute() raises a RuntimeError containing:
- Kernel name
gridandthreadsused- The underlying Metal error code and description
