Skip to content

Commit

Permalink
Add and remove .codecarbon.config on execution
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasFehring committed Feb 20, 2024
1 parent acf09a6 commit db7e080
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions py_experimenter/experimenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ def __init__(
if not self.config.valid():
raise InvalidConfigError("Invalid configuration")

if self.use_codecarbon:
if "offline_mode" in self.config.codecarbon_configuration.config:
self.codecarbon_offline_mode = self.config.codecarbon_configuration.config["offline_mode"]
else:
self.codecarbon_offline_mode = False
self.config.codecarbon_configuration.config["offline_mode"] = False
utils.write_codecarbon_config(self.config.codecarbon_configuration.config)

self.database_credential_file_path = database_credential_file_path

if table_name is not None:
Expand Down Expand Up @@ -262,13 +254,17 @@ def execute(
if n_jobs is None:
n_jobs = self.config.n_jobs

self._write_codecarbon_config()

with Parallel(n_jobs=n_jobs) as parallel:
if max_experiments == -1:
parallel(delayed(self._worker)(experiment_function, random_order) for _ in range(n_jobs))
else:
parallel(delayed(self._execution_wrapper)(experiment_function, random_order) for _ in range(max_experiments))
self.logger.info("All configured executions finished.")

self._delete_codecarbon_config()

def unpause_experiment(self, experiment_id: int, experiment_function: Callable) -> None:
"""
Pulls the experiment with the given `experiment_id` from the database (if it is `paused`) table and executes it. In
Expand Down Expand Up @@ -373,6 +369,28 @@ def _execute_experiment(self, experiment_id, keyfield_values, experiment_functio
emission_data = tracker._prepare_emissions_data().values
result_processor._write_emissions(emission_data, self.codecarbon_offline_mode)

def _write_codecarbon_config(self) -> None:
""" "
Writes the CodeCarbon config file if CodeCarbon is used in this experiment.
"""
if self.use_codecarbon:
if "offline_mode" in self.config.codecarbon_configuration.config:
self.codecarbon_offline_mode = self.config.codecarbon_configuration.config["offline_mode"]
else:
self.codecarbon_offline_mode = False
self.config.codecarbon_configuration.config["offline_mode"] = False
utils.write_codecarbon_config(self.config.codecarbon_configuration.config)

def _delete_codecarbon_config(self) -> None:
"""
Deletes the CodeCarbon config file if CodeCarbon is used in this experiment.
"""
if self.use_codecarbon:
try:
os.remove(".codecarbon.config")
except FileNotFoundError as e:
self.logger.error(f"Could not delete CodeCarbon config file. Error: {e}")

def reset_experiments(self, *states: Tuple["str"]) -> None:
"""
Deletes the experiments from the database table that have the given `states`. Afterward, all deleted rows are added to the
Expand Down

0 comments on commit db7e080

Please sign in to comment.