Architecture¶
Process model¶
Each fitter runs in a main process and a set of worker processes. A worker is a
multiprocessing.Process that owns one MAD-NG instance. Workers do the expensive
work (tracking or closed-orbit solves, and the derivatives with respect to the knobs);
the main process sums their replies and updates the knobs.
There are two fitting engines, built the same way:
Tracking (
tracking):TrackingFitterand its subclassesArcByArcFitter,KickerFitter,ACDMarkerFitterandACDSinusoidFitterfit turn-by-turn data with mini-batch gradient descent (Adam or L-BFGS,tracking.sgd) or Levenberg-Marquardt (fitting.lm_loop), optionally with the start coordinates.POCO (
poco):ClosedOrbitFitterandCalibratedClosedOrbitFitterfit closed-orbit and closed-twiss series with Levenberg-Marquardt (fitting.lm_loop).
Both compose a MachineSetup: the MAD-NG model and its BPMs,
the knobs and their starting values and the output settings. Workers run with a
WorkerRuntime; a tracking fit adds its TrackingDataConfig, TrackingParameters and
BPM ranges (tracking.ranges).
Worker protocol¶
Messages are typed dataclasses in accelmo.fitting.protocol. Every worker runs
the same loop, AbstractWorker.run:
Receive
Start(knobs)and build the MAD-NG session.Answer each
Evaluate(knobs, batch)with oneGradReply(loss, gradient and, for POCO workers, the Hessian and normal matrix), and each command (TotalChi2,SetKnobs,UpdateInitCoords,SetUncertaintyMode) with oneAckorLossReply.On
Stop, a training tracking worker replies with itsUncertaintyPart, then exits.
A worker sends exactly one reply per request. A failure sends one ErrorReply
instead, and the worker exits without sending anything else; the main process raises
it. A NaN loss marks a worker that lost its particles or closed orbit: it is left out
of the sums and of the averaged loss
(reduce_replies()).
Run logs and monitoring¶
Every fit writes a run log (accelmo.runlog) under
OutputConfig.run_log_root, one directory per fit:
stage.jsonName, start time, knob names and
OutputConfig.run_metadata(how to rebuild the model). Written once.history.jsonlOne JSON object per step:
step,timeand the logged scalars.knobs.f64Raw float64 rows
[step, knob_0, knob_1, ...]; step -1 holds the starting knobs.result.jsonHow the fit ended. Written when it finishes.
Workers log to workers/python_worker_<id>.log in the same directory
(accelmo.fitting.worker_logging); their warnings are relayed to the main process.
The fitters feed a live terminal view, FitDashboard. The worker pool
assumes the fork start method; the warning relay is handed to workers through the forked
process state (start_relay() runs before the fork).
Optics are not stored. accelmo.monitor recomputes them from the knobs on demand,
caches them, and serves live plots of a running or finished fit
(python -m accelmo.monitor RUN_LOG_ROOT --spec module:SPEC). The machine side
supplies an OpticsSource (how to build the model and
twiss it) and any machine-specific panels, bundled in a
MonitorSpec.
Run lifecycle¶
run_with_workers() gives every fitter the same shape: run the
body; on Ctrl-C return the best result so far; always stop the workers and close the
run log. For a tracking fit the body is:
Load the data (
DataManager), split the turns into training and held-out validation batches with a seeded shuffle, and clampnum_batchesto the data.Start a
TrackingSession: build the worker payloads, start the training and validationWorkerPool.Run
SGDLoop: per epoch, evaluate every batch and take an optimiser step after each; score the held-out validation loss; keep the knobs with the lowest loss (validation when available); stop on a converged loss or gradient.Set the workers to the best knobs and stop them; their uncertainty parts give
Cov = A⁻¹ B A⁻¹(sandwich_uncertainties()).
Each fit returns a FitResult: knobs, 1-sigma
uncertainties keyed by the same knob names, and
FitDiagnostics (why and after how many iterations it stopped).
Package layout¶
Imports only point down this list; the two engines never import each other
(tests/test_package_layers.py checks it).
accelmo.dashboardThe live terminal view of a fit. Draws plain numbers; the fitters feed it.
accelmo.monitorLive plots served from run logs; optics recomputed from the logged knobs. Imports only
runlog; the machine side plugs in throughOpticsSource.accelmo.momentum_referenceMomentum reference from blank measurements; uses both engines.
accelmo.trackingThe tracking engine: fitters,
TrackingSession, BPM ranges,DataManager, the tracking worker and its uncertainty parts.tracking.dispatchbuilds the worker payloads and plans turns;tracking.sgdholds the SGD loop, learning-rate schedule and checkpointing;tracking.configthe measurement, kicker and checkpoint settings and the tracking plans.accelmo.pocoThe POCO engine: Levenberg-Marquardt fitters for closed-orbit and closed-twiss data, priors, BPM-gain and corrector calibration, and their workers (
poco.workers).accelmo.fittingWhat both engines share:
MachineSetup,SequenceConfigandOutputConfig, the worker protocol and base worker, the shared-memory reference, the worker pool, run lifecycle, reply reduction, loss weights, uncertainties andFitResult.accelmo.optimisersAdam, L-BFGS and Levenberg-Marquardt.
accelmo.machinemachine.accelerators: machine definitions (LHC,PSB,SPS,FCC), knob families, BPM pattern and tune configuration.backend.madng: the MAD-NG interfaces;MadInterfacebuilds the model,MadInterfaceadds the optimisation knobs and derivatives, andmachine_statehandles fixed machine-state inputs.accelmo.analysisDegeneracy checks.
accelmo.runlogRun-log writer (
RunLog) and reader (StageLog,find_stages()).accelmo.configOptimiserConfig,WorkerRuntime,TrackingDataConfigandTrackingParameters. Imports no otheraccelmomodule.
Knob conventions¶
Fitters work in a single optimisation space. Inputs (initial_knob_strengths,
true_strengths), internal algorithms and reported results all use the same knob
coordinates.
dp/p and pt are related by pymadng_utils.physics (or accelerator.dp2pt).
Momentum offsets are carried through MAD-NG pt.
Quadrupole tilts are fitted through <element>.tilt knobs, seeded at 1e-9 rad because
a zero-angle rotation does not carry a differentiable parameter in MAD-NG.