Skip to content
class

IBackend

IBackend

Abstract compute backend interface.

One concrete IBackend exists per Device slot (CPU, GPU); Dispatcher holds them and routes every op based on the tensor's device tag. Implementations dispatch the actual math to their device-native library — Apple Accelerate (vDSP / vForce / BLAS / LAPACK) for CPU, MLX for GPU.

See Also

CpuBackend — Accelerate-backed concrete CPU. GpuBackend — MLX-backed concrete GPU. Dispatcher — selects which to use.

Notes

Ownership. Each concrete backend is created once and stored inside the Dispatcher singleton via std::unique_ptr; the lifetime of every IBackend therefore equals the lifetime of the process.

Thread safety. Methods are thread-compatible but not internally synchronised: the engine never calls into the same backend from multiple threads simultaneously on the same op, though distinct ops on distinct tensors may overlap. Callers that share a backend across threads must provide their own serialisation, or rely on the underlying framework (e.g. MLX's own thread-safety guarantees).

Design rationale. All methods take Storage by const-reference and return a new Storage; no in-place mutation is exposed at this layer. Concrete implementations choose the most efficient internal representation — CpuStorage (Apple aligned host buffer) for CPU, GpuStorage (mlx::core::array) for GPU.

Destructor

1

Virtual methods

11
= 0

cross_entropy_loss

ClassLossForwardResult cross_entropy_loss(const int & input, const int & target, const int * weight, const int & input_shape, const int & target_shape, Dtype dt, double eps, int ignore_index, int reduction)

Cross-entropy loss with optional per-class weights and ignore_index. saved_aux inside the result holds the log-softmax activations needed by the backward pass. weight is optional (may be nullptr for unweighted CE).

= 0const

device

→Device
Device device()

Returns the Device tag associated with this backend.

Returns

Device

Device::CPU for CpuBackend, Device::GPU for GpuBackend.

= 0

linalg_eig

StoragePair linalg_eig(const int & a, const int & shape, const int & values_shape, const int & vectors_shape, Dtype dt)

General eigendecomposition for non-symmetric matrices; returns (eigenvalues, eigenvectors). Eigenvalues may be complex; CPU backend returns packed real+imag components.

= 0

linalg_eigh

StoragePair linalg_eigh(const int & a, const int & shape, const int & values_shape, const int & vectors_shape, Dtype dt, bool upper)

Symmetric/Hermitian eigendecomposition (real eigenvalues guaranteed). Returns (eigenvalues, eigenvectors); eigenvectors are column-ordered. Reads the upper triangle when upper is true, the lower triangle otherwise.

= 0

linalg_qr

StoragePair linalg_qr(const int & a, const int & shape, const int & q_shape, const int & r_shape, Dtype dt)

Reduced QR decomposition; returns (Q, R).

= 0

nll_loss

ClassLossForwardResult nll_loss(const int & input, const int & target, const int * weight, const int & input_shape, const int & target_shape, Dtype dt, int ignore_index, int reduction)

Negative log-likelihood loss; operates on log-probabilities directly (unlike cross_entropy which applies log-softmax internally).

= 0

nonzero_forward

CpuStorage nonzero_forward(const int & input, const int & input_shape, Dtype input_dtype, int & numel_out)

Returns a CpuStorage containing the flat indices of all nonzero elements. numel_out is set to the number of nonzero elements found.

= 0

permute_cpu

CpuStorage permute_cpu(const CpuStorage & src, const int & src_shape, const int & perm, Dtype dt)

Permutes a CpuStorage buffer in-place and returns the result as a new CpuStorage. Used for data layout conversions before GPU upload.

= 0

rms_norm_backward

StoragePair rms_norm_backward(const int & x, const int & gamma, const int & saved_rstd, const int & grad, int outer, int normalized_size, const int & x_shape, const int & gamma_shape, Dtype dt)

RMSNorm backward; returns (grad_x, grad_gamma).

= 0

rms_norm_forward

StoragePair rms_norm_forward(const int & x, const int & gamma, int outer, int normalized_size, double eps, const int & x_shape, Dtype dt)

RMSNorm forward pass. outer = product of batch/sequence dims; normalized_size is the last (feature) dimension. Returns (output, saved_rstd); saved_rstd holds per-row reciprocal standard deviations needed by the backward pass.

= 0

to_cpu

→CpuStorage
CpuStorage to_cpu(const int & a, const int & shape)

Materialises the tensor's data in CPU-accessible memory.

Parameters

aconst Storage&
Backend-native storage to materialise.
shapeconst Shape&
Logical shape of a.

Returns

CpuStorage

Host-side aligned buffer containing the evaluated data.

Notes

For CpuBackend this is effectively a copy of the existing CpuStorage; for GpuBackend it calls mlx::core::array::eval() first (triggering execution of the pending lazy graph) and then copies the result into a fresh CpuStorage on the host.

Methods

239