Skip to main content
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

Raw bytes 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.
See DeviceCapabilities below.

PreparedKernel

Returned by rt.prepare(...). Holds pre-allocated Metal buffers.

dispatch()

Dispatches the kernel. Does not return output — call read_output() afterward.

dispatch_timed()

Dispatches the kernel and returns wall-clock time in milliseconds.

read_output()

Reads the output buffer back to host memory.

Example: benchmarking loop


DeviceCapabilities

Fields

require_m3()

Raises 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
  • grid and threads used
  • The underlying Metal error code and description
Check the kernel name and geometry first — most dispatch failures are launch-shape mismatches.