@@ -195,6 +195,7 @@ def __init__(
195195 force_ram_power : Optional [int ] = _sentinel ,
196196 pue : Optional [float ] = _sentinel ,
197197 wue : Optional [float ] = _sentinel ,
198+ force_carbon_intensity_g_co2e_kwh : Optional [float ] = _sentinel ,
198199 force_mode_cpu_load : Optional [bool ] = _sentinel ,
199200 allow_multiple_runs : Optional [bool ] = _sentinel ,
200201 rapl_include_dram : Optional [bool ] = _sentinel ,
@@ -259,11 +260,13 @@ def __init__(
259260 then RAM power (W) = Number of RAM Slots × 5 Watts.
260261 :param pue: PUE (Power Usage Effectiveness) of the data center where the
261262 experiment is being run.
263+ :param wue: WUE (Water Usage Effectiveness) of the data center. Units of L/kWh:
264+ litres of water consumed per kilowatt-hour of electricity consumed.
265+ :param force_carbon_intensity_g_co2e_kwh: Override grid carbon intensity
266+ in gCO2e/kWh for emissions calculations.
262267 :param force_mode_cpu_load: Force the addition of a CPU in MODE_CPU_LOAD
263268 :param allow_multiple_runs: Allow multiple CodeCarbon instances on the same machine.
264269 Defaults to True since v3 (was False in v2).
265- :param wue: WUE (Water Usage Effectiveness) of the data center. Units of L/kWh:
266- litres of water consumed per kilowatt-hour of electricity consumed.
267270 :param rapl_include_dram: Include DRAM (memory) power in RAPL measurements on Linux,
268271 defaults to False. When True, measures CPU package + DRAM.
269272 Only affects systems where RAPL exposes separate DRAM domains.
@@ -276,6 +279,31 @@ def __init__(
276279
277280 # logger.info("base tracker init")
278281 self ._external_conf = get_hierarchical_config ()
282+ self ._set_from_conf (
283+ force_carbon_intensity_g_co2e_kwh ,
284+ "force_carbon_intensity_g_co2e_kwh" ,
285+ None ,
286+ float ,
287+ )
288+ parsed_intensity = None
289+ if self ._force_carbon_intensity_g_co2e_kwh is not None :
290+ try :
291+ value = float (self ._force_carbon_intensity_g_co2e_kwh )
292+ if value >= 0 :
293+ parsed_intensity = value
294+ else :
295+ logger .warning (
296+ f"Invalid value for force_carbon_intensity_g_co2e_kwh: '{ self ._force_carbon_intensity_g_co2e_kwh } '. "
297+ "It must be a non-negative number. Using default calculation methods."
298+ )
299+ except (ValueError , TypeError ):
300+ logger .warning (
301+ f"Invalid value for force_carbon_intensity_g_co2e_kwh: '{ self ._force_carbon_intensity_g_co2e_kwh } '. "
302+ "It must be a numeric value. Using default calculation methods."
303+ )
304+ self ._force_carbon_intensity_g_co2e_kwh = parsed_intensity
305+ self ._conf ["force_carbon_intensity_g_co2e_kwh" ] = parsed_intensity
306+ self .force_carbon_intensity_g_co2e_kwh = parsed_intensity
279307 self ._set_from_conf (allow_multiple_runs , "allow_multiple_runs" , True , bool )
280308 if self ._allow_multiple_runs :
281309 logger .warning (
@@ -353,6 +381,11 @@ def __init__(
353381 experiment_id , "experiment_id" , "5b0fa12a-3dd7-45bb-9766-cc326314d9f1"
354382 )
355383
384+ if self .force_carbon_intensity_g_co2e_kwh is not None :
385+ logger .info (
386+ f"Using forced carbon intensity: { self .force_carbon_intensity_g_co2e_kwh } gCO2e/kWh."
387+ )
388+
356389 assert self ._tracking_mode in ["machine" , "process" ]
357390 set_logger_level (self ._log_level )
358391 set_logger_format (self ._logger_preamble )
@@ -446,7 +479,9 @@ def __init__(
446479 self ._conf ["provider" ] = cloud .provider
447480
448481 self ._emissions : Emissions = Emissions (
449- self ._data_source , self ._electricitymaps_api_token
482+ self ._data_source ,
483+ self ._electricitymaps_api_token ,
484+ force_carbon_intensity_g_co2e_kwh = self .force_carbon_intensity_g_co2e_kwh ,
450485 )
451486 self ._init_output_methods (api_key = self ._api_key )
452487
@@ -1310,6 +1345,7 @@ def track_emissions(
13101345 force_ram_power : Optional [int ] = _sentinel ,
13111346 pue : Optional [float ] = _sentinel ,
13121347 wue : Optional [float ] = _sentinel ,
1348+ force_carbon_intensity_g_co2e_kwh : Optional [float ] = _sentinel ,
13131349 allow_multiple_runs : Optional [bool ] = _sentinel ,
13141350 rapl_include_dram : Optional [bool ] = _sentinel ,
13151351 rapl_prefer_psys : Optional [bool ] = _sentinel ,
@@ -1392,6 +1428,8 @@ def track_emissions(
13921428 :param pue: PUE (Power Usage Effectiveness) of the data center.
13931429 :param wue: WUE (Water Usage Effectiveness) of the data center. Units of L/kWh:
13941430 litres of water consumed per kilowatt-hour of electricity consumed.
1431+ :param force_carbon_intensity_g_co2e_kwh: Override grid carbon intensity
1432+ in gCO2e/kWh for emissions calculations.
13951433 :param rapl_include_dram: Include DRAM in RAPL measurements on Linux (default: False).
13961434 When True, measures CPU package + DRAM.
13971435 :param rapl_prefer_psys: Prefer psys over package domains for RAPL on Linux
@@ -1447,6 +1485,7 @@ def wrapped_fn(*args, **kwargs):
14471485 force_ram_power = force_ram_power ,
14481486 pue = pue ,
14491487 wue = wue ,
1488+ force_carbon_intensity_g_co2e_kwh = force_carbon_intensity_g_co2e_kwh ,
14501489 allow_multiple_runs = allow_multiple_runs ,
14511490 rapl_include_dram = rapl_include_dram ,
14521491 rapl_prefer_psys = rapl_prefer_psys ,
@@ -1481,6 +1520,7 @@ def wrapped_fn(*args, **kwargs):
14811520 force_ram_power = force_ram_power ,
14821521 pue = pue ,
14831522 wue = wue ,
1523+ force_carbon_intensity_g_co2e_kwh = force_carbon_intensity_g_co2e_kwh ,
14841524 allow_multiple_runs = allow_multiple_runs ,
14851525 rapl_include_dram = rapl_include_dram ,
14861526 rapl_prefer_psys = rapl_prefer_psys ,
0 commit comments