──────────────────────────────────────────────────────────────────── 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
TraceGraphandexternal_feedsmap. - The in-progress
MPSGraph*(held asvoid*in this pure-C++ header; the.mmre-casts via__bridge). - The
BuilderContextthat emitters write into. - A scratch
std::string* error_msgpointer 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
1MpsBuilder
void MpsBuilder(const TraceGraph & graph, const int & external_feeds, int * error_msg)Destructor
1~MpsBuilder
void ~MpsBuilder()Operators
1Methods
5compile_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.
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.
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.
compile_trace
CompiledExecutable * compile_trace(bool dynamic_batch, const int & param_ids, const int & explicit_outputs)Forward-only compile. See compile_trace for semantics.
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.