mlco2 / codecarbon

Track emissions from Compute and recommend ways to reduce their impact on the environment.
https://mlco2.github.io/codecarbon
MIT License
1.11k stars 173 forks source link

AttributeError: 'EmissionsTracker' object has no attribute 'start_task' #488

Closed Sarah7223 closed 3 months ago

Sarah7223 commented 9 months ago

I try to use codecarbon to measure the training and the inference of ml models with the functions start_task and stop_task. I get the following error : tracker.start_task("load dataset") AttributeError: 'EmissionsTracker' object has no attribute 'start_task'. Did you mean: '_start_time'?

I tried to run this script : https://github.com/mlco2/codecarbon/blob/master/examples/bert_inference.py to be sure I used the functions well but I get the same error.

Do you know why ?

inimaz commented 8 months ago

Hello!

The start_task was added in version 2.3.0, could it be that you are running an older version of the package? I might suggest to verify that the version of CodeCarbon you are running is the latest. Here you have a small script to check this

import pkg_resources

def get_package_info(package_name):
    try:
        package = pkg_resources.get_distribution(package_name)
        return {
            'version': package.version,
            'location': package.location
        }
    except pkg_resources.DistributionNotFound:
        return f"Package '{package_name}' is not installed"

package_name = 'codecarbon'
package_info = get_package_info(package_name)

if isinstance(package_info, str):
    print(package_info)
else:
    print(f"Package: {package_name}")
    print(f"Version: {package_info['version']}")
    print(f"Location: {package_info['location']}")

If the package version is indeed the latest, and the issue persist, please tell us. We can verify further.

Sarah7223 commented 8 months ago

Hello ! Thank you for your answer ! I was using the version 2.3.2 & Python version 3.10.9. I updated my Python version to Pyhton 3.12.1 and I don't have the error anymore.

However, I have a new error :

ZeroDivisionError Traceback (most recent call last) Cell In[34], line 7 5 tracker = EmissionsTracker(project_name="classification-measure") 6 #track the ml model training ----> 7 tracker.start_task("train model") 8 classifier.fit(x_train, y_train) 9 tracker.stop_task()

File ...\Python312\Lib\site-packages\codecarbon\emissions_tracker.py:450, in BaseEmissionsTracker.start_task(self, task_name) 448 for hardware in self.hardware: 449 hardware.start() --> 450 = self._prepare_emissions_data(delta=True) 452 self._tasks.update( 453 { 454 task_name: Task( (...) 457 } 458 ) 459 self._active_task = task_name

File ...\Python312\Lib\site-packages\codecarbon\emissions_tracker.py:582, in BaseEmissionsTracker._prepare_emissions_data(self, delta) 574 cloud_provider = cloud.provider 575 cloud_region = cloud.region 576 total_emissions = EmissionsData( 577 timestamp=datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), 578 project_name=self._project_name, 579 run_id=str(self.run_id), 580 duration=duration.seconds, 581 emissions=emissions, # kg --> 582 emissions_rate=emissions / duration.seconds, # kg/s 583 cpu_power=self._cpu_power.W, 584 gpu_power=self._gpu_power.W, 585 ram_power=self._ram_power.W, 586 cpu_energy=self._total_cpu_energy.kWh, 587 gpu_energy=self._total_gpu_energy.kWh, 588 ram_energy=self._total_ram_energy.kWh, 589 energy_consumed=self._total_energy.kWh, 590 country_name=country_name, 591 country_iso_code=country_iso_code, 592 region=region, 593 on_cloud=on_cloud, 594 cloud_provider=cloud_provider, 595 cloud_region=cloud_region, 596 os=self._conf.get("os"), 597 python_version=self._conf.get("python_version"), 598 codecarbon_version=self._conf.get("codecarbon_version"), 599 gpu_count=self._conf.get("gpu_count"), 600 gpu_model=self._conf.get("gpu_model"), 601 cpu_count=self._conf.get("cpu_count"), 602 cpu_model=self._conf.get("cpu_model"), 603 longitude=self._conf.get("longitude"), 604 latitude=self._conf.get("latitude"), 605 ram_total_size=self._conf.get("ram_total_size"), 606 tracking_mode=self._conf.get("tracking_mode"), 607 pue=self._pue, 608 ) 609 if delta: 610 if self._previous_emissions is None:

ZeroDivisionError: float division by zero Do you know why ?

I have another question, is it different to make the measure with the function 'start_task' like this :

tracker = EmissionsTracker(project_name="classification-measure") tracker.start_task("train model")

track the ml model training

tracker.stop_task()

tracker.start_task("model inference")

track the ml model inference

tracker.stop_task() emissions = tracker.stop()

And create 2 different trackers like this ?

tracker1 = EmissionsTracker(project_name="train model") tracker1.start()

track the ml model training

tracker1.stop()

tracker2 = EmissionsTracker(project_name="model inference") tracker2.start()

track the ml model inference

tracker2.stop()

The results are the same, right ?

LeahChercham commented 3 months ago

@Sarah7223 Hi ! This error is related to #549 I made a pull request #570 You can correct on your side by replacing all time.time() by time.perf_counter() in emission_tracker.py Cheers, Leah

SaboniAmine commented 3 months ago

Closing as the answer have been given, please re-open if there is something missing