22from unittest import mock
33
44from codecarbon .core .slurm import warn_on_multi_rank_double_counting
5+ from codecarbon .emissions_tracker import OfflineEmissionsTracker
56
67
78class TestMultiRankWarning (unittest .TestCase ):
@@ -11,14 +12,29 @@ def _warnings(self, tracking_mode="machine", **env):
1112 warn_on_multi_rank_double_counting (tracking_mode )
1213 return mocked_logger .warning .call_count
1314
14- def test_several_ranks_per_node_in_machine_mode_warns (self ):
15- self .assertEqual (1 , self ._warnings (SLURM_NTASKS_PER_NODE = "4" ))
16- # Heterogeneous allocations are written "4(x2)".
17- self .assertEqual (1 , self ._warnings (SLURM_NTASKS_PER_NODE = "4(x2)" ))
15+ def test_non_zero_local_rank_in_machine_mode_warns (self ):
16+ self .assertEqual (
17+ 1 , self ._warnings (SLURM_LOCALID = "1" , SLURM_STEP_TASKS_PER_NODE = "4" )
18+ )
19+ # Heterogeneous allocations are written "4(x2)"; falls back to the job var.
20+ self .assertEqual (
21+ 1 , self ._warnings (SLURM_LOCALID = "3" , SLURM_TASKS_PER_NODE = "4(x2)" )
22+ )
1823
1924 def test_no_warning_without_double_counting (self ):
20- self .assertEqual (0 , self ._warnings (SLURM_NTASKS_PER_NODE = "1" ))
21- self .assertEqual (0 , self ._warnings ())
25+ # Local rank 0 is the recommended tracker.
2226 self .assertEqual (
23- 0 , self ._warnings (tracking_mode = "process " , SLURM_NTASKS_PER_NODE = "4" )
27+ 0 , self ._warnings (SLURM_LOCALID = "0 " , SLURM_STEP_TASKS_PER_NODE = "4" )
2428 )
29+ # Batch step: per-node counts are exported but SLURM_LOCALID is not.
30+ self .assertEqual (
31+ 0 , self ._warnings (SLURM_NTASKS_PER_NODE = "4" , SLURM_TASKS_PER_NODE = "4" )
32+ )
33+ self .assertEqual (0 , self ._warnings ())
34+ self .assertEqual (0 , self ._warnings (tracking_mode = "process" , SLURM_LOCALID = "1" ))
35+
36+ def test_tracker_init_warns_on_non_zero_local_rank (self ):
37+ with mock .patch .dict ("os.environ" , {"SLURM_LOCALID" : "2" }):
38+ with mock .patch ("codecarbon.core.slurm.logger" ) as mocked_logger :
39+ OfflineEmissionsTracker (country_iso_code = "FRA" , tracking_mode = "machine" )
40+ self .assertEqual (1 , mocked_logger .warning .call_count )
0 commit comments