Skip to content
class

MpsBuilder

MpsBuilder

──────────────────────────────────────────────────────────────────── Class-form facade (Phase B of the compile OOP refactor) ────────────────────────────────────────────────────────────────────

MpsBuilder is the OOP shape behind the 5 compile_* free functions above. Each free function above is now a thin forwarder that constructs a transient MpsBuilder and dispatches to the matching method. The free-function signatures are preserved for binding stability — Python (and the cache) still import them by name.

Lifetime. One MpsBuilder per compile call. The class owns:

  • Reference to the TraceGraph and external_feeds map.
  • The in-progress MPSGraph* (held as void* in this pure-C++ header; the .mm re-casts via __bridge).
  • The BuilderContext that emitters write into.
  • A scratch std::string* error_msg pointer for failure reporting; populated on failure return.

Construction. The constructor takes the same three arguments shared by every public method: graph, external_feeds, and the optional error_msg. It does not do any MPSGraph work — the MPSGraph is created lazily inside each compile_<mode> method. This keeps construction lightweight (the free-function forwarders create an instance even when validation rejects the call) and avoids leaking an MPSGraph allocation on early-exit paths.

Why a class rather than 5 free functions. The 5 functions shared ~60% scaffolding expressed as copy-paste (device check, emitter precheck, placeholder creation, emit loop, target construction, MPSGraph compile). Grouping them as methods on a shared class lets future shared-scaffolding refactors land in one place rather than in 5 lock-stepped diffs. The 5 methods intentionally remain as distinct entry points rather than virtual dispatch — the divergence between forward-only / forward-bwd / fused-step / generic-fused-step / variables is finite-state, not hierarchical.

Constructors

1
ctor

MpsBuilder

void MpsBuilder(const TraceGraph & graph, const int & external_feeds, int * error_msg)

Destructor

1

Operators

1
op

operator=

MpsBuilder & operator=(const MpsBuilder &)

Methods

5
fn

compile_fused_training_step

CompiledExecutable * compile_fused_training_step(TensorId loss_id, const int & param_ids, const OptimizerSpec & opt_spec, const int)

Forward + bwd + hardcoded SGD/Adam. See compile_fused_training_step.

fn

compile_generic_fused_step

CompiledExecutable * compile_generic_fused_step(TensorId loss_id, const int & param_ids, const int & ghost_grad_ids, const int & output_target_ids)

Forward + bwd + ghost-grad optimizer (any kind). See compile_generic_fused_step.

fn

compile_generic_fused_step_with_vars

CompiledExecutable * compile_generic_fused_step_with_vars(TensorId loss_id, const int & param_ids, const int & ghost_grad_ids, const int & output_target_ids, const int)

As above + MPSGraph variables. See compile_generic_fused_step_with_vars.

fn

compile_trace

CompiledExecutable * compile_trace(bool dynamic_batch, const int & param_ids, const int & explicit_outputs)

Forward-only compile. See compile_trace for semantics.

fn

compile_trace_with_backward

CompiledExecutable * compile_trace_with_backward(TensorId loss_id, const int & param_ids, bool dynamic_batch, const int & extra_output_ids)

Forward + backward. See compile_trace_with_backward. extra_output_ids are trace ids of explicit non-gradient outputs (e.g. BN running-stat EMA new_rm/new_rv) that the executable must also produce + return after [loss, *grads], so the runtime can write them back.