pdmlabs.energy.codecarbon_backend#

CodeCarbon energy backend โ€“ the default when tracking is enabled.

Gives CPU + GPU + RAM energy, kWh and gCO2eq, and grid carbon intensity.

Design notes, all confirmed empirically against codecarbon 3.3.1#

  • One tracker per phase, tasks inside it. CodeCarbonโ€™s task API is single-level, so phases cannot be tasks nested inside a task. Binding a trackerโ€™s lifetime to a phase gives the property the analysis depends on: within a phase, sum(task energy) <= phase energy by construction, which is what makes E_optimizer_overhead = E_search - sum(E_trial) a difference of comparable quantities rather than of two unrelated integrals. Phases are few (about five per experiment), so per-phase construction costs nothing against a search that runs for hours.

  • stop_task() does not stop the tracker โ€“ verified โ€“ so many tasks can run inside one phase.

  • Unit trap: tracker.stop() returns emissions in kgCO2eq, not energy. Phase energy is tracker.final_emissions_data.energy_consumed (kWh). Confusing them silently corrupts every phase total, so this module reads the attribute and never the return value.

  • Duration defect (measured, codecarbon 3.3.1). Once the task API is used, final_emissions_data.duration accumulates only time spent inside tasks, while energy_consumed accumulates the whole tracker lifetime. Measured: a 6.93 s phase containing 1.53 s of tasks reported duration == 1.53 but energy covering the full 6.93 s. The energy is right; the duration is not. This matters because idle subtraction multiplies by duration โ€“ taking CodeCarbonโ€™s number would have subtracted 1.5 s of idle from a 6.9 s window, inflating dynamic energy by roughly the idle draw of five seconds. This backend therefore times phases with its own perf_counter and overrides the reported duration. Task durations are unaffected and are left alone.

  • OfflineEmissionsTracker is mandatory on compute nodes: the online tracker attempts geolocation and cloud-metadata lookups that block or fail without internet.

  • gpu_ids defaults to CUDA_VISIBLE_DEVICES. In machine mode CodeCarbon otherwise sums every GPU on the node: on a two-GPU box an idle second card contributed 69% of all recorded energy during a pure-CPU workload.

Classes

CodeCarbonBackend([country_iso_code,ย ...])

class pdmlabs.energy.codecarbon_backend.CodeCarbonBackend(country_iso_code='GRC', measure_power_secs=1.0, tracking_mode='machine', gpu_ids=None, rapl_include_dram=True, pue=1.0, output_dir=None, project_name='pdmlabs', min_task_duration_s=None, **_extra)#

Bases: BaseEnergyBackend

name: str = 'codecarbon'#
probe()#

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()#

Release any long-lived resource. Idempotent.

start_phase(label)#
start_task(label)#
stop_phase()#
stop_task(token=None)#