@@ -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,33 +279,31 @@ def __init__(
276279
277280 # logger.info("base tracker init")
278281 self ._external_conf = get_hierarchical_config ()
279- custom_intensity_str = self ._external_conf .get (
280- "custom_carbon_intensity_g_co2e_kwh"
282+ self ._set_from_conf (
283+ force_carbon_intensity_g_co2e_kwh ,
284+ "force_carbon_intensity_g_co2e_kwh" ,
285+ None ,
286+ float ,
281287 )
282288 parsed_intensity = None
283- if custom_intensity_str is not None :
284- custom_intensity_str_stripped = custom_intensity_str .strip ()
285- if custom_intensity_str_stripped == "" :
286- logger .warning (
287- f"CODECARBON : Invalid value for custom_carbon_intensity_g_co2e_kwh: '{ custom_intensity_str } '. "
288- "It cannot be empty or whitespace. Using default calculation methods."
289- )
290- else :
291- try :
292- value = float (custom_intensity_str_stripped )
293- if value > 0 :
294- parsed_intensity = value
295- else :
296- logger .warning (
297- f"CODECARBON : Invalid value for custom_carbon_intensity_g_co2e_kwh: '{ custom_intensity_str_stripped } '. "
298- "It must be a positive number. Using default calculation methods."
299- )
300- except ValueError :
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 :
301295 logger .warning (
302- f"CODECARBON : Invalid value for custom_carbon_intensity_g_co2e_kwh : '{ custom_intensity_str_stripped } '. "
303- "It must be a numeric value . Using default calculation methods."
296+ f"CODECARBON : 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."
304298 )
305- self .custom_carbon_intensity_g_co2e_kwh = parsed_intensity
299+ except (ValueError , TypeError ):
300+ logger .warning (
301+ f"CODECARBON : 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
306307 self ._set_from_conf (allow_multiple_runs , "allow_multiple_runs" , True , bool )
307308 if self ._allow_multiple_runs :
308309 logger .warning (
@@ -380,9 +381,9 @@ def __init__(
380381 experiment_id , "experiment_id" , "5b0fa12a-3dd7-45bb-9766-cc326314d9f1"
381382 )
382383
383- if self .custom_carbon_intensity_g_co2e_kwh is not None :
384+ if self .force_carbon_intensity_g_co2e_kwh is not None :
384385 logger .info (
385- f"CODECARBON : Using custom carbon intensity: { self .custom_carbon_intensity_g_co2e_kwh } gCO2e/kWh."
386+ f"CODECARBON : Using forced carbon intensity: { self .force_carbon_intensity_g_co2e_kwh } gCO2e/kWh."
386387 )
387388
388389 assert self ._tracking_mode in ["machine" , "process" ]
@@ -480,7 +481,7 @@ def __init__(
480481 self ._emissions : Emissions = Emissions (
481482 self ._data_source ,
482483 self ._electricitymaps_api_token ,
483- custom_carbon_intensity_g_co2e_kwh = self .custom_carbon_intensity_g_co2e_kwh ,
484+ force_carbon_intensity_g_co2e_kwh = self .force_carbon_intensity_g_co2e_kwh ,
484485 )
485486 self ._init_output_methods (api_key = self ._api_key )
486487
@@ -1344,6 +1345,7 @@ def track_emissions(
13441345 force_ram_power : Optional [int ] = _sentinel ,
13451346 pue : Optional [float ] = _sentinel ,
13461347 wue : Optional [float ] = _sentinel ,
1348+ force_carbon_intensity_g_co2e_kwh : Optional [float ] = _sentinel ,
13471349 allow_multiple_runs : Optional [bool ] = _sentinel ,
13481350 rapl_include_dram : Optional [bool ] = _sentinel ,
13491351 rapl_prefer_psys : Optional [bool ] = _sentinel ,
@@ -1426,6 +1428,8 @@ def track_emissions(
14261428 :param pue: PUE (Power Usage Effectiveness) of the data center.
14271429 :param wue: WUE (Water Usage Effectiveness) of the data center. Units of L/kWh:
14281430 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.
14291433 :param rapl_include_dram: Include DRAM in RAPL measurements on Linux (default: False).
14301434 When True, measures CPU package + DRAM.
14311435 :param rapl_prefer_psys: Prefer psys over package domains for RAPL on Linux
@@ -1481,6 +1485,7 @@ def wrapped_fn(*args, **kwargs):
14811485 force_ram_power = force_ram_power ,
14821486 pue = pue ,
14831487 wue = wue ,
1488+ force_carbon_intensity_g_co2e_kwh = force_carbon_intensity_g_co2e_kwh ,
14841489 allow_multiple_runs = allow_multiple_runs ,
14851490 rapl_include_dram = rapl_include_dram ,
14861491 rapl_prefer_psys = rapl_prefer_psys ,
@@ -1515,6 +1520,7 @@ def wrapped_fn(*args, **kwargs):
15151520 force_ram_power = force_ram_power ,
15161521 pue = pue ,
15171522 wue = wue ,
1523+ force_carbon_intensity_g_co2e_kwh = force_carbon_intensity_g_co2e_kwh ,
15181524 allow_multiple_runs = allow_multiple_runs ,
15191525 rapl_include_dram = rapl_include_dram ,
15201526 rapl_prefer_psys = rapl_prefer_psys ,
0 commit comments