Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mlco2_codecarbon /tests /test_emissions_tracker_constant.py
| import os | |
| import tempfile | |
| import time | |
| import unittest | |
| from unittest import mock | |
| import pandas as pd | |
| from codecarbon.core import cpu | |
| from codecarbon.emissions_tracker import ( | |
| EmissionsTracker, | |
| OfflineEmissionsTracker, | |
| track_emissions, | |
| ) | |
| def heavy_computation(run_time_secs: int = 3): | |
| end_time: float = ( | |
| time.perf_counter() + run_time_secs | |
| ) # Run for `run_time_secs` seconds | |
| while time.perf_counter() < end_time: | |
| pass | |
| class TestCarbonTrackerConstant(unittest.TestCase): | |
| def setUp(self) -> None: | |
| self.project_name = "project_TestCarbonTrackerConstant" | |
| self.emissions_file = "emissions-test-TestCarbonTrackerConstant.csv" | |
| self.emissions_path = tempfile.gettempdir() | |
| self.emissions_file_path = os.path.join( | |
| self.emissions_path, self.emissions_file | |
| ) | |
| if os.path.isfile(self.emissions_file_path): | |
| os.remove(self.emissions_file_path) | |
| def tearDown(self) -> None: | |
| if os.path.isfile(self.emissions_file_path): | |
| os.remove(self.emissions_file_path) | |
| def test_carbon_tracker_online_constant(self): | |
| tracker = EmissionsTracker( | |
| output_dir=self.emissions_path, output_file=self.emissions_file | |
| ) | |
| tracker.start() | |
| heavy_computation(run_time_secs=1) | |
| emissions = tracker.stop() | |
| assert isinstance(emissions, float) | |
| self.assertNotEqual(emissions, 0.0) | |
| self.assertAlmostEqual(emissions, 6.262572537957655e-05, places=2) | |
| self.verify_output_file(self.emissions_file_path) | |
| def test_carbon_tracker_offline_constant(self): | |
| tracker = OfflineEmissionsTracker( | |
| country_iso_code="USA", | |
| output_dir=self.emissions_path, | |
| output_file=self.emissions_file, | |
| ) | |
| tracker.start() | |
| heavy_computation(run_time_secs=1) | |
| emissions = tracker.stop() | |
| assert isinstance(emissions, float) | |
| self.assertNotEqual(emissions, 0.0) | |
| self.assertAlmostEqual(emissions, 6.262572537957655e-05, places=2) | |
| self.verify_output_file(self.emissions_file_path) | |
| def test_carbon_tracker_offline_constant_force_cpu_power( | |
| self, mock_tdp, mock_psutil | |
| ): | |
| # Same as test_carbon_tracker_offline_constant test but this time forcing the default cpu power | |
| USER_INPUT_CPU_POWER = 1_000 | |
| # Mock the output of tdp | |
| mock_tdp.return_value = None | |
| mock_psutil.return_value = False | |
| tracker = OfflineEmissionsTracker( | |
| country_iso_code="USA", | |
| output_dir=self.emissions_path, | |
| output_file=self.emissions_file, | |
| force_cpu_power=USER_INPUT_CPU_POWER, | |
| ) | |
| tracker.start() | |
| heavy_computation(run_time_secs=1) | |
| emissions = tracker.stop() | |
| assert isinstance(emissions, float) | |
| self.assertNotEqual(emissions, 0.0) | |
| # Assert the content stored. cpu_power should be 50% of input TDP | |
| assertdf = pd.read_csv(self.emissions_file_path) | |
| self.assertEqual(USER_INPUT_CPU_POWER / 2, assertdf["cpu_power"][0]) | |
| def test_carbon_tracker_offline_constant_pue(self, mock_tdp, mock_psutil): | |
| # The PUE must be applied to the reported power as well as to the energy, | |
| # so that energy_consumed stays consistent with the power columns. | |
| USER_INPUT_CPU_POWER = 1_000 | |
| PUE = 2.0 | |
| mock_tdp.return_value = None | |
| mock_psutil.return_value = False | |
| tracker = OfflineEmissionsTracker( | |
| country_iso_code="USA", | |
| output_dir=self.emissions_path, | |
| output_file=self.emissions_file, | |
| force_cpu_power=USER_INPUT_CPU_POWER, | |
| pue=PUE, | |
| ) | |
| tracker.start() | |
| heavy_computation(run_time_secs=1) | |
| emissions = tracker.stop() | |
| assert isinstance(emissions, float) | |
| assertdf = pd.read_csv(self.emissions_file_path) | |
| self.assertEqual(USER_INPUT_CPU_POWER / 2 * PUE, assertdf["cpu_power"][0]) | |
| # energy_consumed must be reconstructible from the reported power | |
| total_power = ( | |
| assertdf["cpu_power"][0] | |
| + assertdf["gpu_power"][0] | |
| + assertdf["ram_power"][0] | |
| ) | |
| expected_energy = total_power * assertdf["duration"][0] / 3600 / 1000 | |
| self.assertAlmostEqual( | |
| expected_energy, assertdf["energy_consumed"][0], delta=expected_energy * 0.1 | |
| ) | |
| def test_carbon_tracker_offline_load_force_cpu_power( | |
| self, mock_psutil_available, mock_tdp, mock_cpu_percent | |
| ): | |
| USER_INPUT_CPU_POWER = 1_000 | |
| mock_tdp.return_value = 500 | |
| mock_psutil_available.return_value = True | |
| tracker = OfflineEmissionsTracker( | |
| country_iso_code="USA", | |
| output_dir=self.emissions_path, | |
| output_file=self.emissions_file, | |
| force_cpu_power=USER_INPUT_CPU_POWER, | |
| ) | |
| tracker.start() | |
| heavy_computation(run_time_secs=1) | |
| emissions = tracker.stop() | |
| assert isinstance(emissions, float) | |
| self.assertNotEqual(emissions, 0.0) | |
| cpu_load = 0.5 | |
| assertdf = pd.read_csv(self.emissions_file_path) | |
| load_factor = 0.1 + 0.9 * (cpu_load**3) | |
| expected_power = USER_INPUT_CPU_POWER * load_factor | |
| self.assertAlmostEqual(assertdf["cpu_power"][0], expected_power, delta=50) | |
| def test_decorator_constant(self): | |
| def dummy_train_model(): | |
| return 42 | |
| dummy_train_model() | |
| self.verify_output_file(self.emissions_file_path) | |
| def verify_output_file(self, file_path: str) -> None: | |
| with open(file_path, "r") as f: | |
| lines = [line.rstrip() for line in f] | |
| assert len(lines) == 2 | |
| def test_carbon_tracker_offline_region_error(self): | |
| # Test errors when unknown information is given to the offline tracker | |
| from codecarbon.external.geography import CloudMetadata | |
| tracker = OfflineEmissionsTracker( | |
| country_iso_code="USA", | |
| cloud_provider="aws", | |
| cloud_region="us-east-1", | |
| output_dir=self.emissions_path, | |
| output_file=self.emissions_file, | |
| ) | |
| tracker.start() | |
| tracker._measure_power_and_energy() | |
| tracker._ensure_emissions_engine() | |
| cloud: CloudMetadata = tracker._get_cloud_metadata() | |
| try: | |
| with self.assertRaises(ValueError) as context: | |
| tracker._emissions.get_cloud_country_iso_code(cloud) | |
| self.assertTrue( | |
| "Unable to find country ISO Code" in context.exception.args[0] | |
| ) | |
| with self.assertRaises(ValueError) as context: | |
| tracker._emissions.get_cloud_geo_region(cloud) | |
| self.assertTrue( | |
| "Unable to find State/City name for " in context.exception.args[0] | |
| ) | |
| with self.assertRaises(ValueError) as context: | |
| tracker._emissions.get_cloud_country_name(cloud) | |
| self.assertTrue("Unable to find country name" in context.exception.args[0]) | |
| finally: | |
| tracker.stop() | |
Xet Storage Details
- Size:
- 7.76 kB
- Xet hash:
- a83fe413607369623fba21f92949bf86a285dc25a6c213255b711b008662a2cf
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.