pdmlabs.energy.base#

Abstract base class for all PdMLabs energy backends.

Mirrors pdmlabs.optimization.base.BaseOptimizerAdapter: backends translate a vendor API (CodeCarbon, raw RAPL/NVML) into one uniform contract so the experiment layer never imports a vendor package directly.

Nesting contract#

A backend supports exactly one open phase and, inside it, one open task. CodeCarbon’s task API is single-level, so this interface deliberately does not promise arbitrary nesting – promising it would make the CodeCarbon backend a lie rather than an implementation.

Classes

BaseEnergyBackend()

Uniform start/stop energy measurement.

class pdmlabs.energy.base.BaseEnergyBackend#

Bases: ABC

Uniform start/stop energy measurement.

Implementations must honour three rules, because this object runs inside an HPO trial and must never be able to break one:

  1. stop_* returns an EnergyReading, never raises. A measurement failure degrades the study; it must not abort an optimisation run.

  2. Provenance is always populated, even on failure, so no record can be mistaken for a hardware measurement.

  3. start_task while a task is open is an error the backend resolves itself (close-and-warn), never a silent overwrite that would attribute one task’s energy to another.

name: str = 'base'#
phase(label)#

Measure a phase, yielding a one-element list that receives the reading.

The reading is delivered through a mutable cell rather than as the as value because the value is only known at __exit__.

abstractmethod probe() → dict#

Report what this backend can actually measure, without measuring.

Returns a dict with at least power_source_cpu, power_source_gpu, ram_source and available (bool). Called by the preflight gate.

shutdown() → None#

Release any long-lived resource. Idempotent.

abstractmethod start_phase(label: str) → None#
abstractmethod start_task(label: str) → object | None#
abstractmethod stop_phase() → EnergyReading#
abstractmethod stop_task(token: object | None) → EnergyReading#