Skip to content
fn

run_executable_inplace

void run_executable_inplace(CompiledExecutable * exe, const int & input_feeds, const int & output_targets)

In-place variant: instead of allocating fresh MTLBuffers per output and returning new TensorImpls, write each output directly into the corresponding pre-existing TensorImpl's GPU buffer. Used by the compiled-optimizer step path where every output is a parameter (or optimizer-state buffer) that already has an allocated MTLBuffer; skipping the alloc + copy_ round-trip cuts the wrapper's overhead by ~50%.

Parameters

exeCompiledExecutable*
The compiled forward (or forward+backward) graph.
input_feedsvector<TensorImplPtr>
Inputs in input_ids order — same as run_executable.
output_targetsvector<TensorImplPtr>
Pre-existing tensors whose elements receive the executable's outputs. Must equal output_ids.size() + grad_output_ids.size() in length, and each target must match its slot's shape and dtype exactly (no broadcasting). A target may be a Metal view of part of a tensor: its elements are written into its part of the base.

Notes

Not supported in dynamic-batch mode (output shapes vary per call, no stable target buffers). Each target's version moves after the write so autograd's mutation tracker sees the update. A target whose buffer is its elements has its MLX array reference replaced with a fresh one wrapping the output buffer, which also drops any cached MLX dependency state.