Unverified Commit 037e466b authored by Kevin Canwen Xu's avatar Kevin Canwen Xu Committed by GitHub
Browse files

Add CodeCarbon Integration (#12304)

* Add optional dependency

* Add CodeCarbon integration

* Add CodeCarbon integration

* Add CodeCarbon integration

* typo
parent bfd5da8e
...@@ -87,6 +87,7 @@ if stale_egg_info.exists(): ...@@ -87,6 +87,7 @@ if stale_egg_info.exists():
_deps = [ _deps = [
"Pillow", "Pillow",
"black==21.4b0", "black==21.4b0",
"codecarbon==1.2.0",
"cookiecutter==1.7.2", "cookiecutter==1.7.2",
"dataclasses", "dataclasses",
"datasets", "datasets",
...@@ -252,6 +253,7 @@ extras["serving"] = deps_list("pydantic", "uvicorn", "fastapi", "starlette") ...@@ -252,6 +253,7 @@ extras["serving"] = deps_list("pydantic", "uvicorn", "fastapi", "starlette")
extras["speech"] = deps_list("soundfile", "torchaudio") extras["speech"] = deps_list("soundfile", "torchaudio")
extras["vision"] = deps_list("Pillow") extras["vision"] = deps_list("Pillow")
extras["timm"] = deps_list("timm") extras["timm"] = deps_list("timm")
extras["codecarbon"] = deps_list("codecarbon")
extras["sentencepiece"] = deps_list("sentencepiece", "protobuf") extras["sentencepiece"] = deps_list("sentencepiece", "protobuf")
extras["testing"] = ( extras["testing"] = (
...@@ -274,6 +276,7 @@ extras["all"] = ( ...@@ -274,6 +276,7 @@ extras["all"] = (
+ extras["vision"] + extras["vision"]
+ extras["integrations"] + extras["integrations"]
+ extras["timm"] + extras["timm"]
+ extras["codecarbon"]
) )
extras["docs_specific"] = deps_list( extras["docs_specific"] = deps_list(
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
deps = { deps = {
"Pillow": "Pillow", "Pillow": "Pillow",
"black": "black==21.4b0", "black": "black==21.4b0",
"codecarbon": "codecarbon==1.2.0",
"cookiecutter": "cookiecutter==1.7.2", "cookiecutter": "cookiecutter==1.7.2",
"dataclasses": "dataclasses", "dataclasses": "dataclasses",
"datasets": "datasets", "datasets": "datasets",
......
...@@ -100,6 +100,10 @@ def is_neptune_available(): ...@@ -100,6 +100,10 @@ def is_neptune_available():
return importlib.util.find_spec("neptune") is not None return importlib.util.find_spec("neptune") is not None
def is_codecarbon_available():
return importlib.util.find_spec("codecarbon") is not None
def hp_params(trial): def hp_params(trial):
if is_optuna_available(): if is_optuna_available():
import optuna import optuna
...@@ -259,6 +263,8 @@ def get_available_reporting_integrations(): ...@@ -259,6 +263,8 @@ def get_available_reporting_integrations():
integrations.append("tensorboard") integrations.append("tensorboard")
if is_wandb_available(): if is_wandb_available():
integrations.append("wandb") integrations.append("wandb")
if is_codecarbon_available():
integrations.append("codecarbon")
return integrations return integrations
...@@ -718,6 +724,34 @@ class NeptuneCallback(TrainerCallback): ...@@ -718,6 +724,34 @@ class NeptuneCallback(TrainerCallback):
pass pass
class CodeCarbonCallback(TrainerCallback):
"""
A :class:`~transformers.TrainerCallback` that tracks the CO2 emission of training.
"""
def __init__(self):
assert (
is_codecarbon_available()
), "CodeCarbonCallback requires `codecarbon` to be installed. Run `pip install codecarbon`."
import codecarbon
self._codecarbon = codecarbon
self.tracker = None
def on_init_end(self, args, state, control, **kwargs):
if self.tracker is None and state.is_local_process_zero:
# CodeCarbon will automatically handle environment variables for configuration
self.tracker = self._codecarbon.EmissionsTracker(output_dir=args.output_dir)
def on_train_begin(self, args, state, control, model=None, **kwargs):
if self.tracker and state.is_local_process_zero:
self.tracker.start()
def on_train_end(self, args, state, control, **kwargs):
if self.tracker and state.is_local_process_zero:
self.tracker.stop()
INTEGRATION_TO_CALLBACK = { INTEGRATION_TO_CALLBACK = {
"azure_ml": AzureMLCallback, "azure_ml": AzureMLCallback,
"comet_ml": CometCallback, "comet_ml": CometCallback,
...@@ -725,6 +759,7 @@ INTEGRATION_TO_CALLBACK = { ...@@ -725,6 +759,7 @@ INTEGRATION_TO_CALLBACK = {
"neptune": NeptuneCallback, "neptune": NeptuneCallback,
"tensorboard": TensorBoardCallback, "tensorboard": TensorBoardCallback,
"wandb": WandbCallback, "wandb": WandbCallback,
"codecarbon": CodeCarbonCallback,
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment