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 ofPdMExperiment._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 oflog_modeland friends, which can raise – means the join key survives a partial teardown.MLflow -> energy record: the same call site logs
energy_trial_idas 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
|
Open a trial context for this process and return its id. |
|
Attach the active MLflow run to this process's trial context. |
|
Read the in-flight context without consuming it. |
Pop and return this process's trial context. |
|
Return this process's active backend, or |
|
Flag the in-flight trial as served from cache. |
|
|
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_idso the caller can log it as a param, orNonewhen there is no context or no run. Never raises: it sits ahead of the rest of_finish_runand 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
Nonein 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).