Skip to content
fn

compile_trace_with_backward

→CompiledExecutable*
CompiledExecutable * compile_trace_with_backward(const TraceGraph & graph, const int & external_feeds, TensorId loss_id, const int & param_ids, int * error_msg, bool dynamic_batch, const int & extra_output_ids)

Phase 1.3: forward + backward in one executable.

Walks the trace exactly like compile_trace, then calls MPSGraph's autograd builder [graph gradientForPrimaryTensor:loss withTensors:params name:] to derive the gradient of the loss with respect to each parameter, and appends those gradient tensors to the executable's target list.

Parameters

graphconst TraceGraph&
The recorded forward op DAG.
external_feedsconst std::unordered_map<TensorId, TensorImplPtr>&
External-feed map (parameters + user inputs). Must contain an entry for every id in param_ids.
loss_idTensorId
Trace id of the scalar loss tensor. Must be produced by some op inside graph (not an external feed).
param_idsconst std::vector<TensorId>&
Trace ids of the model parameters with respect to which the gradient is taken. Must all be external feeds.
error_msgstd::string*= None
Reason on abort (unsupported op, missing param feed, …).

Returns

CompiledExecutable*

Owns the compiled executable. output_ids = {loss_id}; grad_output_ids = freshly-minted ids for each param_ids[i]'s gradient (same order as param_ids). output_shapes / output_dtypes carry the loss meta followed by per-gradient meta (matches the layout the runtime uses when binding output buffers). Returns nullptr on abort.