Skip to content

Commit 234e2b5

Browse files
neighthanBorda
andauthored
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 <[email protected]>
1 parent 4307dd9 commit 234e2b5

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

CHANGELOG.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
4141

4242
- Truncated long version numbers in progress bar ([#2594](https://github.com/PyTorchLightning/pytorch-lightning/pull/2594))
4343

44-
- Enable val/test loop disabling ([#2692](https://github.com/PyTorchLightning/pytorch-lightning/pull/2692))
44+
- Enabling val/test loop disabling ([#2692](https://github.com/PyTorchLightning/pytorch-lightning/pull/2692))
4545

46-
- Refactor into `accelerator` module:
46+
- Refactored into `accelerator` module:
4747
* GPU training ([#2704](https://github.com/PyTorchLightning/pytorch-lightning/pull/2704))
4848
* TPU training ([#2708](https://github.com/PyTorchLightning/pytorch-lightning/pull/2708))
4949

50+
- Using .comet.config file for CometLogger ([#1913](https://github.com/PyTorchLightning/pytorch-lightning/pull/1913))
51+
5052
### Deprecated
5153

5254
- Deprecated Trainer attribute `ckpt_path`, which will now be set by `weights_save_path` ([#2681](https://github.com/PyTorchLightning/pytorch-lightning/pull/2681))

environment.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ dependencies:
4444
- pip:
4545
- test-tube>=0.7.5
4646
- mlflow>=1.0.0
47-
- comet_ml>=1.0.56
47+
- comet_ml>=3.1.12
4848
- wandb>=0.8.21
4949
- neptune-client>=0.4.109
5050
- horovod>=0.19.1

pytorch_lightning/loggers/comet.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
except ImportError: # pragma: no-cover
1717
# For more information, see: https://www.comet.ml/docs/python-sdk/releases/#release-300
1818
from comet_ml.papi import API # pragma: no-cover
19-
20-
_COMET_AVAILABLE = True
19+
from comet_ml.config import get_config, get_api_key
2120
except ImportError: # pragma: no-cover
2221
CometExperiment = None
2322
CometExistingExperiment = None
2423
CometOfflineExperiment = None
2524
CometBaseExperiment = None
2625
API = None
2726
_COMET_AVAILABLE = False
27+
else:
28+
_COMET_AVAILABLE = True
2829

2930

3031
import torch
@@ -78,8 +79,11 @@ class CometLogger(LightningLoggerBase):
7879
>>> trainer = Trainer(logger=comet_logger)
7980
8081
Args:
81-
api_key: Required in online mode. API key, found on Comet.ml
82-
save_dir: Required in offline mode. The path for the directory to save local comet logs
82+
api_key: Required in online mode. API key, found on Comet.ml. If not given, this
83+
will be loaded from the environment variable COMET_API_KEY or ~/.comet.config
84+
if either exists.
85+
save_dir: Required in offline mode. The path for the directory to save local
86+
comet logs. If given, this also sets the directory for saving checkpoints.
8387
workspace: Optional. Name of workspace for this user
8488
project_name: Optional. Send your experiment to a specific project.
8589
Otherwise will be sent to Uncategorized Experiments.
@@ -88,6 +92,10 @@ class CometLogger(LightningLoggerBase):
8892
This is used to determine version number
8993
experiment_name: Optional. String representing the name for this particular experiment on Comet.ml.
9094
experiment_key: Optional. If set, restores from existing experiment.
95+
offline: If api_key and save_dir are both given, this determines whether
96+
the experiment will be in online or offline mode. This is useful if you use
97+
save_dir to control the checkpoints directory and have a ~/.comet.config
98+
file but still want to run offline experiments.
9199
"""
92100

93101
def __init__(self,
@@ -98,25 +106,33 @@ def __init__(self,
98106
rest_api_key: Optional[str] = None,
99107
experiment_name: Optional[str] = None,
100108
experiment_key: Optional[str] = None,
109+
offline: bool = False,
101110
**kwargs):
102111

103112
if not _COMET_AVAILABLE:
104113
raise ImportError('You want to use `comet_ml` logger which is not installed yet,'
105114
' install it with `pip install comet-ml`.')
106115
super().__init__()
107116
self._experiment = None
108-
self._save_dir = save_dir
109117

110118
# Determine online or offline mode based on which arguments were passed to CometLogger
111-
if api_key is not None:
119+
api_key = api_key or get_api_key(None, get_config())
120+
121+
if api_key is not None and save_dir is not None:
122+
self.mode = "offline" if offline else "online"
123+
self.api_key = api_key
124+
self._save_dir = save_dir
125+
elif api_key is not None:
112126
self.mode = "online"
113127
self.api_key = api_key
114128
elif save_dir is not None:
115129
self.mode = "offline"
116130
self._save_dir = save_dir
117131
else:
118132
# If neither api_key nor save_dir are passed as arguments, raise an exception
119-
raise MisconfigurationException("CometLogger requires either api_key or save_dir during initialization.")
133+
raise MisconfigurationException(
134+
"CometLogger requires either api_key or save_dir during initialization."
135+
)
120136

121137
log.info(f"CometLogger will be initialized in {self.mode} mode")
122138

requirements/extra.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# extended list of package dependencies to reach full functionality
22

33
neptune-client>=0.4.109
4-
comet-ml>=1.0.56
4+
comet-ml>=3.1.12
55
mlflow>=1.0.0
66
test_tube>=0.7.5
77
wandb>=0.8.21

0 commit comments

Comments
 (0)