From 234e2b590fb2eaf966908044b6c9b32755838666 Mon Sep 17 00:00:00 2001 From: Nathan Hunt Date: Fri, 7 Aug 2020 02:46:50 -0500 Subject: [PATCH] Use .comet.config file for CometLogger (#1913) * Use .comet.config file or env var for API key. * Make CometLogger API key changes backwards compatible. * Fix line too long. * Add documentation about loading from ~/.comet_config. * Update required comet_ml version. * Comet logger: allow offline experiments with config file. This adds a new argument to the logger to control the online / offline mode explicitly so that if you give an API key and a save_dir (e.g. to control where checkpoints go while having ~/.comet.config) you can specify which mode you want. * Make CometLogger API key changes backwards compatible. * Comet logger: change online argument to be offline. For consistency with other loggers. * chlog Co-authored-by: Jirka Borovec --- CHANGELOG.md | 6 ++++-- environment.yml | 2 +- pytorch_lightning/loggers/comet.py | 30 +++++++++++++++++++++++------- requirements/extra.txt | 2 +- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bfe3a6eeb4..eed7f8e795 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,12 +41,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Truncated long version numbers in progress bar ([#2594](https://github.com/PyTorchLightning/pytorch-lightning/pull/2594)) -- Enable val/test loop disabling ([#2692](https://github.com/PyTorchLightning/pytorch-lightning/pull/2692)) +- Enabling val/test loop disabling ([#2692](https://github.com/PyTorchLightning/pytorch-lightning/pull/2692)) -- Refactor into `accelerator` module: +- Refactored into `accelerator` module: * GPU training ([#2704](https://github.com/PyTorchLightning/pytorch-lightning/pull/2704)) * TPU training ([#2708](https://github.com/PyTorchLightning/pytorch-lightning/pull/2708)) +- Using .comet.config file for CometLogger ([#1913](https://github.com/PyTorchLightning/pytorch-lightning/pull/1913)) + ### Deprecated - Deprecated Trainer attribute `ckpt_path`, which will now be set by `weights_save_path` ([#2681](https://github.com/PyTorchLightning/pytorch-lightning/pull/2681)) diff --git a/environment.yml b/environment.yml index 07afe80557..cabfb940c4 100644 --- a/environment.yml +++ b/environment.yml @@ -44,7 +44,7 @@ dependencies: - pip: - test-tube>=0.7.5 - mlflow>=1.0.0 - - comet_ml>=1.0.56 + - comet_ml>=3.1.12 - wandb>=0.8.21 - neptune-client>=0.4.109 - horovod>=0.19.1 diff --git a/pytorch_lightning/loggers/comet.py b/pytorch_lightning/loggers/comet.py index a328d4e3a0..895d0f7b70 100644 --- a/pytorch_lightning/loggers/comet.py +++ b/pytorch_lightning/loggers/comet.py @@ -16,8 +16,7 @@ try: except ImportError: # pragma: no-cover # For more information, see: https://www.comet.ml/docs/python-sdk/releases/#release-300 from comet_ml.papi import API # pragma: no-cover - - _COMET_AVAILABLE = True + from comet_ml.config import get_config, get_api_key except ImportError: # pragma: no-cover CometExperiment = None CometExistingExperiment = None @@ -25,6 +24,8 @@ except ImportError: # pragma: no-cover CometBaseExperiment = None API = None _COMET_AVAILABLE = False +else: + _COMET_AVAILABLE = True import torch @@ -78,8 +79,11 @@ class CometLogger(LightningLoggerBase): >>> trainer = Trainer(logger=comet_logger) Args: - api_key: Required in online mode. API key, found on Comet.ml - save_dir: Required in offline mode. The path for the directory to save local comet logs + api_key: Required in online mode. API key, found on Comet.ml. If not given, this + will be loaded from the environment variable COMET_API_KEY or ~/.comet.config + if either exists. + save_dir: Required in offline mode. The path for the directory to save local + comet logs. If given, this also sets the directory for saving checkpoints. workspace: Optional. Name of workspace for this user project_name: Optional. Send your experiment to a specific project. Otherwise will be sent to Uncategorized Experiments. @@ -88,6 +92,10 @@ class CometLogger(LightningLoggerBase): This is used to determine version number experiment_name: Optional. String representing the name for this particular experiment on Comet.ml. experiment_key: Optional. If set, restores from existing experiment. + offline: If api_key and save_dir are both given, this determines whether + the experiment will be in online or offline mode. This is useful if you use + save_dir to control the checkpoints directory and have a ~/.comet.config + file but still want to run offline experiments. """ def __init__(self, @@ -98,6 +106,7 @@ class CometLogger(LightningLoggerBase): rest_api_key: Optional[str] = None, experiment_name: Optional[str] = None, experiment_key: Optional[str] = None, + offline: bool = False, **kwargs): if not _COMET_AVAILABLE: @@ -105,10 +114,15 @@ class CometLogger(LightningLoggerBase): ' install it with `pip install comet-ml`.') super().__init__() self._experiment = None - self._save_dir = save_dir # Determine online or offline mode based on which arguments were passed to CometLogger - if api_key is not None: + api_key = api_key or get_api_key(None, get_config()) + + if api_key is not None and save_dir is not None: + self.mode = "offline" if offline else "online" + self.api_key = api_key + self._save_dir = save_dir + elif api_key is not None: self.mode = "online" self.api_key = api_key elif save_dir is not None: @@ -116,7 +130,9 @@ class CometLogger(LightningLoggerBase): self._save_dir = save_dir else: # If neither api_key nor save_dir are passed as arguments, raise an exception - raise MisconfigurationException("CometLogger requires either api_key or save_dir during initialization.") + raise MisconfigurationException( + "CometLogger requires either api_key or save_dir during initialization." + ) log.info(f"CometLogger will be initialized in {self.mode} mode") diff --git a/requirements/extra.txt b/requirements/extra.txt index 2b8854678d..c533b601c9 100644 --- a/requirements/extra.txt +++ b/requirements/extra.txt @@ -1,7 +1,7 @@ # extended list of package dependencies to reach full functionality neptune-client>=0.4.109 -comet-ml>=1.0.56 +comet-ml>=3.1.12 mlflow>=1.0.0 test_tube>=0.7.5 wandb>=0.8.21