|
| 1 | +"""Emissions tracking functionality for monitoring energy consumption and carbon emissions.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import logging |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from autointent._callbacks import OptimizerCallback |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class EmissionsTrackerCallback(OptimizerCallback): |
| 14 | + """Class for tracking energy consumption and carbon emissions.""" |
| 15 | + |
| 16 | + name = "emissions_tracker" |
| 17 | + |
| 18 | + current_module_name: str | None = None |
| 19 | + |
| 20 | + def __init__(self) -> None: |
| 21 | + """Initialize the emission tracker.""" |
| 22 | + try: |
| 23 | + from codecarbon import EmissionsTracker |
| 24 | + except ImportError as e: |
| 25 | + msg = ( |
| 26 | + "EmissionsTrackerCallback requires the codecarbon package to be installed. " |
| 27 | + "Please install it with `pip install autointent[codecarbon]`." |
| 28 | + ) |
| 29 | + raise ImportError(msg) from e |
| 30 | + self.emission_tracker = EmissionsTracker |
| 31 | + |
| 32 | + def start_run(self, run_name: str, dirpath: Path, log_interval_time: float) -> None: # noqa: ARG002 |
| 33 | + """Start tracking emissions for the entire run. |
| 34 | +
|
| 35 | + Args: |
| 36 | + run_name: Name of the run. |
| 37 | + dirpath: Path to the directory where the logs will be saved. |
| 38 | + log_interval_time: Sampling interval for the system monitor in seconds. |
| 39 | + """ |
| 40 | + self.tracker = self.emission_tracker(project_name=run_name, measure_power_secs=log_interval_time) |
| 41 | + |
| 42 | + self.tracker.start() |
| 43 | + self.current_module_name = None |
| 44 | + |
| 45 | + def start_module(self, module_name: str, num: int, module_kwargs: dict[str, Any]) -> None: # noqa: ARG002 |
| 46 | + """Start tracking emissions for a specific task. |
| 47 | +
|
| 48 | + Args: |
| 49 | + module_name: Name of the task to track emissions for. |
| 50 | + num: Number of the module. |
| 51 | + module_kwargs: Module parameters. |
| 52 | + """ |
| 53 | + self.current_module_name = f"{module_name}_{num}" |
| 54 | + self.tracker.start_task(self.current_module_name) |
| 55 | + |
| 56 | + def update_metrics(self, metrics: dict[str, Any]) -> dict[str, float]: |
| 57 | + """Stop tracking emissions and return the emissions data. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + Dictionary containing emissions metrics. |
| 61 | + """ |
| 62 | + emissions_data = self.tracker.stop_task(self.current_module_name) |
| 63 | + emissions_data_json = json.loads(emissions_data.toJSON()) |
| 64 | + emissions_data_dict = { |
| 65 | + f"emissions/{k}": v for k, v in emissions_data_json.items() if isinstance(v, int | float) |
| 66 | + } |
| 67 | + return emissions_data_dict | metrics |
| 68 | + |
| 69 | + def update_final_metrics(self, metrics: dict[str, Any]) -> dict[str, Any]: |
| 70 | + """Update final metrics with emissions data. |
| 71 | +
|
| 72 | + Args: |
| 73 | + metrics: Final metrics to update. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + Updated metrics including emissions data. |
| 77 | + """ |
| 78 | + _ = self.tracker.stop() |
| 79 | + emissions_data_json = json.loads(self.tracker.final_emissions_data.toJSON()) |
| 80 | + emissions_data_dict = { |
| 81 | + f"emissions/{k}": v for k, v in emissions_data_json.items() if isinstance(v, int | float) |
| 82 | + } |
| 83 | + return {"emissions": emissions_data_dict} | metrics |
| 84 | + |
| 85 | + def log_value(self, **kwargs: dict[str, Any]) -> None: |
| 86 | + pass |
| 87 | + |
| 88 | + def log_metrics(self, metrics: dict[str, Any]) -> None: |
| 89 | + pass |
| 90 | + |
| 91 | + def end_module(self) -> None: |
| 92 | + pass |
| 93 | + |
| 94 | + def end_run(self) -> None: |
| 95 | + pass |
| 96 | + |
| 97 | + def log_final_metrics(self, metrics: dict[str, Any]) -> None: |
| 98 | + pass |
0 commit comments