accelmo.fitting.worker

Abstract base class for all worker process types.

AbstractWorker.run() owns the process lifecycle and the message loop of accelmo.fitting.protocol; subclasses build their backend engine (accelmo.backend.protocol) and answer Evaluate and Command messages.

class accelmo.fitting.worker.WorkerConfig(accelerator, magnet_range, machine_state=None, b2_errors=None, bad_bpms=None, debug=False, mad_logfile=None, python_logfile=None)[source]

Bases: object

Configuration shared by all worker processes.

The accelerator bundles machine-specific setup; the other fields are the model options and log files. Tracking workers add their geometry (TrackingWorkerConfig).

Parameters:
  • accelerator (Accelerator)

  • magnet_range (str)

  • machine_state (MachineState | None)

  • b2_errors (Path | None)

  • bad_bpms (list[str] | None)

  • debug (bool)

  • mad_logfile (Path | None)

  • python_logfile (Path | None)

accelerator: Accelerator
magnet_range: str
machine_state: Mapping[str, float] | str | Path | None = None
b2_errors: Path | None = None
bad_bpms: list[str] | None = None
debug: bool = False
mad_logfile: Path | None = None
python_logfile: Path | None = None
class accelmo.fitting.worker.AbstractWorker[WorkerDataType, ConfigType: WorkerConfig](conn, worker_id, data, config, runtime)[source]

Bases: Process, ABC, Generic[WorkerDataType, ConfigType]

Abstract base class for all worker process implementations.

Subclasses implement prepare_data(), on_start() (which builds self.engine) and evaluate(), and override on_start(), handle_command(), on_stop() and close() where they need to.

Type Parameters:

WorkerDataType: The type of data structure this worker uses

Parameters:
  • conn (Connection)

  • worker_id (int)

  • data (WorkerDataType)

  • config (ConfigType)

  • runtime (WorkerRuntime)

engine: Any
config: ConfigType
abstractmethod prepare_data(data)[source]

Process and prepare worker-specific data.

This method should extract relevant data from the input structure, compute weights, split into batches, etc.

Parameters:

data (WorkerDataType) – Worker-specific data structure

Return type:

None

Parameters:

data (WorkerDataType)

abstractmethod on_start(knobs)[source]

Build self.engine from the Start knobs.

Return type:

None

Parameters:

knobs (dict[str, float])

abstractmethod evaluate(message)[source]

Answer one Evaluate message.

Return type:

GradReply

Parameters:

message (Evaluate)

handle_command(command)[source]

Answer one Command message.

Return type:

Ack | LossReply

Parameters:

command (Command)

on_stop(failed)[source]

Called once the message loop ends, before the engine is closed.

Return type:

None

Parameters:

failed (bool)

close()[source]

Release resources other than the engine.

Return type:

None

property backend

The backend module of this worker’s accelerator.

property engine_logfile: Path | None

This worker’s backend log file.

send_reply(reply)[source]

Send one reply to the parent.

Return type:

None

Parameters:

reply (object)

run()[source]

Run the worker: Start, then evaluations and commands until Stop.

Return type:

None

send_error_payload(exc, *, phase)[source]

Best-effort send of the failure to the parent.

Return type:

None

Parameters:
  • exc (BaseException)

  • phase (str)

configure_worker_threads()[source]

Limit this worker process’s BLAS threads (WorkerRuntime.blas_threads).

Must run inside the worker process: the pool is already initialised in the forked parent, so the OPENBLAS_NUM_THREADS environment variable would be too late.

Return type:

None

configure_python_worker_logging()[source]

Log this worker to its own file; WARNING and above also reach the parent.

Return type:

None