pdmlabs.energy.instrument#

Per-trial energy instrumentation for the HPO objective.

One decorator, applied at the single point in PdMExperiment._run_optimizer where the objective is handed to the adapter, covers every one of the six optimizer backends โ€“ each of them ultimately calls the same objective_fn(**params).

Hard constraints, each learned from the existing code#

  • Plain nested function, not a class or ``functools.partial``. Mango assigns attributes to the objective (mango/scheduler.py sets func.is_wrapped and func.batch_size; mango/tuner.py reads objective.batch_size to size its batches). A __slots__ instance or a partial would break Mango outright.

  • The return value passes through untouched. Adapters do float(objective_fn(**params)).

  • Exceptions propagate untouched. Every flavor does if self.debug: raise e and debug=True is the default, so swallowing here would silently change HPO behaviour.

  • Nothing in ``finally`` may raise, or a measurement failure would abort a search.

  • Picklable, since the closure ships to loky/dask workers when n_jobs>1. It carries only an EnergySink (strings only) and plain dicts โ€“ never a tracker, file handle or lambda.

Four exit paths, not three#

  1. normal return best_metrics_dict[...] -> exit_path="normal"

  2. the flavorโ€™s own handled-exception path, which returns 0 in six flavors but 1 in SA_experiment.py. From here this is indistinguishable from a normal return โ€“ the flavor catches the exception internally and returns a sentinel โ€“ so it is recorded as exit_path="normal" with that sentinel as objective_value. Identifying it after the fact means joining on the flavorโ€™s own MLflow record via mlflow_run_id. The energy is still charged either way, which is the point: a failed trial burns real energy, and E-LQO likewise charges timed-out exploratory plans up to termination.

  3. the _check_cached_run short-circuit -> exit_path="cached", so cache hits are excluded from per-trial distributions rather than dragging the mean toward zero

  4. an exception that escapes entirely (debug=True, the default) -> exit_path="error", re-raised unchanged

Functions

measured_trial(objective_fn,ย sink[,ย phase,ย dims])

Wrap a raw (**params) -> float objective with per-trial energy accounting.

pdmlabs.energy.instrument.measured_trial(objective_fn, sink, phase='SEARCH', dims=None)#

Wrap a raw (**params) -> float objective with per-trial energy accounting.

The backend is looked up per process via pdmlabs.energy.context.get_active_backend(). In a worker process there is none, so the trial is recorded with wall-clock only and attribution="phase_only" โ€“ which is the correct and safe behaviour, since concurrent trials share machine-wide counters and cannot be attributed individually.