pdmlabs.energy.context#

Per-process trial context, used to join an energy record to its MLflow run.

Why this exists#

A decorator wrapping the trial objective cannot reach the trial’s MLflow run by proximity. In all seven experiment flavors the objective’s return is dedented out of the with mlflow.start_run(...) block (for example semi_supervised_experiment.py opens the run at line 74 and returns at line 222), so by the time the decorator’s finally executes, mlflow.active_run() is already None.

The join is therefore made explicitly, in two directions:

  • energy record -> MLflow: bind_mlflow_run() is called as the first statement of PdMExperiment._finish_run, which is the per-trial teardown funnel every flavor uses, runs in the same process as the objective, and still has the run active. Binding first – ahead of log_model and friends, which can raise – means the join key survives a partial teardown.

  • MLflow -> energy record: the same call site logs energy_trial_id as an MLflow param.

Both keys are recorded; params_hash is the fallback when a trial opened no run at all (the cached short-circuit).

Same invariant as pdmlabs.optimization.trial_sink: this is a module global of an importable module, so it is never pickled by value, and it is keyed by os.getpid() so a forked child cannot overwrite its parent’s in-flight trial.

Functions

begin_trial([params_hash])

Open a trial context for this process and return its id.

bind_mlflow_run(run)

Attach the active MLflow run to this process's trial context.

current()

Read the in-flight context without consuming it.

end_trial()

Pop and return this process's trial context.

get_active_backend()

Return this process's active backend, or None in a worker process.

mark_cached()

Flag the in-flight trial as served from cache.

set_active_backend(backend)

Register backend as this process's measuring backend (None clears).

pdmlabs.energy.context.begin_trial(params_hash=None)#

Open a trial context for this process and return its id.

pdmlabs.energy.context.bind_mlflow_run(run)#

Attach the active MLflow run to this process’s trial context.

Returns the energy_trial_id so the caller can log it as a param, or None when there is no context or no run. Never raises: it sits ahead of the rest of _finish_run and must not be able to break trial teardown.

pdmlabs.energy.context.current()#

Read the in-flight context without consuming it.

pdmlabs.energy.context.end_trial()#

Pop and return this process’s trial context.

pdmlabs.energy.context.get_active_backend()#

Return this process’s active backend, or None in a worker process.

pdmlabs.energy.context.mark_cached()#

Flag the in-flight trial as served from cache.

A cached trial returns before mlflow.start_run, so it is not observable from outside the objective. It must be excluded from per-trial energy distributions, or cache hits drag the mean toward zero.

pdmlabs.energy.context.set_active_backend(backend)#

Register backend as this process’s measuring backend (None clears).