Skip to content

Commit 253ee12

Browse files
kshitij12345lantiga
authored andcommitted
update for neptune 1.0 (#16761)
Co-authored-by: Sabine <[email protected]> Co-authored-by: Jirka Borovec <[email protected]> Co-authored-by: Carlos Mocholí <[email protected]> (cherry picked from commit 04a2f33)
1 parent 447b110 commit 253ee12

File tree

8 files changed

+128
-88
lines changed

8 files changed

+128
-88
lines changed

docs/source-pytorch/visualize/supported_exp_managers.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,24 @@ To use `Neptune.ai <https://neptune.ai/>`_ first install the neptune package:
6868

6969
.. code-block:: bash
7070
71-
pip install neptune-client
71+
pip install neptune
7272
7373
or with conda:
7474

7575
.. code-block:: bash
7676
77-
conda install -c conda-forge neptune-client
77+
conda install -c conda-forge neptune
7878
7979
Configure the logger and pass it to the :class:`~pytorch_lightning.trainer.trainer.Trainer`:
8080

8181
.. testcode::
8282
:skipif: not _NEPTUNE_AVAILABLE
8383

84+
import neptune
8485
from pytorch_lightning.loggers import NeptuneLogger
8586

8687
neptune_logger = NeptuneLogger(
87-
api_key="ANONYMOUS", # replace with your own
88+
api_key=neptune.ANONYMOUS_API_TOKEN, # replace with your own
8889
project="common/pytorch-lightning-integration", # format "<WORKSPACE/PROJECT>"
8990
)
9091
trainer = Trainer(logger=neptune_logger)
@@ -96,7 +97,7 @@ Access the neptune logger from any function (except the LightningModule *init*)
9697
class LitModel(LightningModule):
9798
def any_lightning_module_function_or_hook(self):
9899
neptune_logger = self.logger.experiment["your/metadata/structure"]
99-
neptune_logger.log(metadata)
100+
neptune_logger.append(metadata)
100101
101102
Here's the full documentation for the :class:`~pytorch_lightning.loggers.NeptuneLogger`.
102103

requirements/pytorch/loggers.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# all supported loggers. this list is here as a reference, but they are not installed in CI
2-
neptune-client
2+
neptune
33
comet-ml
44
mlflow>=1.0.0
55
wandb

src/pytorch_lightning/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
88

99
### Changed
1010

11+
- Changes to the `NeptuneLogger` ([#16761](https://github.com/Lightning-AI/lightning/pull/16761)):
12+
* It now supports neptune-client 0.16.16 and neptune >=1.0, and we have replaced the `log()` method with `append()` and `extend()`.
13+
* It now accepts a namespace `Handler` as an alternative to `Run` for the `run` argument. This means that you can call it like `NeptuneLogger(run=run["some/namespace"])` to log everything to the `some/namespace/` location of the run.
14+
15+
### Depercated
16+
1117
-
1218

1319

src/pytorch_lightning/loggers/neptune.py

Lines changed: 93 additions & 74 deletions
Large diffs are not rendered by default.

tests/tests_pytorch/loggers/test_all.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
mock.patch("pytorch_lightning.loggers.mlflow.Metric"),
4646
mock.patch("pytorch_lightning.loggers.neptune.neptune", new_callable=create_neptune_mock),
4747
mock.patch("pytorch_lightning.loggers.neptune._NEPTUNE_AVAILABLE", return_value=True),
48+
mock.patch("pytorch_lightning.loggers.neptune.Run", new=mock.Mock),
49+
mock.patch("pytorch_lightning.loggers.neptune.Handler", new=mock.Mock),
4850
mock.patch("pytorch_lightning.loggers.wandb.wandb"),
4951
mock.patch("pytorch_lightning.loggers.wandb.Run", new=mock.Mock),
5052
)
@@ -294,13 +296,13 @@ def test_logger_with_prefix_all(tmpdir, monkeypatch):
294296
# Neptune
295297
with mock.patch("pytorch_lightning.loggers.neptune.neptune"), mock.patch(
296298
"pytorch_lightning.loggers.neptune._NEPTUNE_AVAILABLE", return_value=True
297-
):
299+
), mock.patch("pytorch_lightning.loggers.neptune.Handler", new=mock.Mock):
298300
logger = _instantiate_logger(NeptuneLogger, api_key="test", project="project", save_dir=tmpdir, prefix=prefix)
299-
assert logger.experiment.__getitem__.call_count == 2
301+
assert logger.experiment.__getitem__.call_count == 0
300302
logger.log_metrics({"test": 1.0}, step=0)
301-
assert logger.experiment.__getitem__.call_count == 3
303+
assert logger.experiment.__getitem__.call_count == 1
302304
logger.experiment.__getitem__.assert_called_with("tmp/test")
303-
logger.experiment.__getitem__().log.assert_called_once_with(1.0)
305+
logger.experiment.__getitem__().append.assert_called_once_with(1.0)
304306

305307
# TensorBoard
306308
if _TENSORBOARD_AVAILABLE:

tests/tests_pytorch/loggers/test_neptune.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def create_neptune_mock():
5151
return MagicMock(init_run=MagicMock(side_effect=create_run_mock))
5252

5353

54+
# Note: For testing purpose this mock `Run` will also be used to mock `Handler`.
5455
class Run:
5556
_project_name = "test-project"
5657

@@ -77,6 +78,9 @@ def __getstate__(self):
7778
def exists(self, value):
7879
return True
7980

81+
def get_root_object(self):
82+
return self
83+
8084

8185
@pytest.fixture
8286
def tmpdir_unittest_fixture(request, tmpdir):
@@ -94,6 +98,8 @@ def run(self, *args, **kwargs):
9498
with mock.patch("pytorch_lightning.loggers.neptune._NEPTUNE_AVAILABLE", return_value=True):
9599
super().run(*args, **kwargs)
96100

101+
@patch("pytorch_lightning.loggers.neptune.Run", Run)
102+
@patch("pytorch_lightning.loggers.neptune.Handler", Run)
97103
def test_neptune_online(self, neptune):
98104
logger = NeptuneLogger(api_key="test", project="project")
99105
created_run_mock = logger.run
@@ -108,6 +114,8 @@ def test_neptune_online(self, neptune):
108114
created_run_mock.__getitem__.assert_has_calls([call("sys/id"), call("sys/name")], any_order=True)
109115
created_run_mock.__setitem__.assert_called_once_with("source_code/integrations/pytorch-lightning", __version__)
110116

117+
@patch("pytorch_lightning.loggers.neptune.Run", Run)
118+
@patch("pytorch_lightning.loggers.neptune.Handler", Run)
111119
def test_neptune_offline(self, neptune):
112120
logger = NeptuneLogger(mode="offline")
113121
created_run_mock = logger.run
@@ -118,6 +126,7 @@ def test_neptune_offline(self, neptune):
118126
self.assertEqual(logger._run_name, "offline-name")
119127

120128
@patch("pytorch_lightning.loggers.neptune.Run", Run)
129+
@patch("pytorch_lightning.loggers.neptune.Handler", Run)
121130
def test_online_with_custom_run(self, neptune):
122131
created_run = Run()
123132
logger = NeptuneLogger(run=created_run)
@@ -128,6 +137,7 @@ def test_online_with_custom_run(self, neptune):
128137
self.assertEqual(neptune.init_run.call_count, 0)
129138

130139
@patch("pytorch_lightning.loggers.neptune.Run", Run)
140+
@patch("pytorch_lightning.loggers.neptune.Handler", Run)
131141
def test_neptune_pickling(self, neptune):
132142
unpickleable_run = Run()
133143
logger = NeptuneLogger(run=unpickleable_run)
@@ -140,6 +150,7 @@ def test_neptune_pickling(self, neptune):
140150
self.assertIsNotNone(unpickled.experiment)
141151

142152
@patch("pytorch_lightning.loggers.neptune.Run", Run)
153+
@patch("pytorch_lightning.loggers.neptune.Handler", Run)
143154
def test_online_with_wrong_kwargs(self, neptune):
144155
"""Tests combinations of kwargs together with `run` kwarg which makes some of other parameters unavailable
145156
in init."""
@@ -222,8 +233,9 @@ def validation_epoch_end(self, outputs):
222233

223234
# then
224235
run_instance_mock.__getitem__.assert_any_call("training/some/key")
225-
run_instance_mock.__getitem__.return_value.log.assert_has_calls([call(42)])
236+
run_instance_mock.__getitem__.return_value.append.assert_has_calls([call(42)])
226237

238+
@patch("pytorch_lightning.loggers.neptune.stringify_unsupported", lambda x: x)
227239
def test_log_hyperparams(self, neptune):
228240
params = {"foo": "bar", "nested_foo": {"bar": 42}}
229241
test_variants = [
@@ -268,7 +280,7 @@ def test_log_metrics(self, neptune):
268280
self.assertEqual(run_instance_mock.__getitem__.call_count, 2)
269281
run_instance_mock.__getitem__.assert_any_call(metrics_foo_key)
270282
run_instance_mock.__getitem__.assert_any_call(metrics_bar_key)
271-
run_attr_mock.log.assert_has_calls([call(42), call(555)])
283+
run_attr_mock.append.assert_has_calls([call(42), call(555)])
272284

273285
def test_log_model_summary(self, neptune):
274286
model = BoringModel()

tests/tests_pytorch/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,15 +1393,15 @@ def test_comet_logger_init_args():
13931393
)
13941394

13951395

1396-
@pytest.mark.skipif(not _NEPTUNE_AVAILABLE, reason="neptune-client is required")
1396+
@pytest.mark.skipif(not _NEPTUNE_AVAILABLE, reason="neptune is required")
13971397
def test_neptune_logger_init_args():
13981398
_test_logger_init_args(
13991399
"NeptuneLogger",
14001400
{
14011401
"name": "neptune", # Resolve from NeptuneLogger.__init__
14021402
},
14031403
{
1404-
"description": "neptune", # Unsupported resolving from neptune.new.internal.init.run.init_run
1404+
"description": "neptune", # Unsupported resolving from neptune.internal.init.run.init_run
14051405
},
14061406
)
14071407

tests/tests_pytorch/utilities/test_imports.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def clean_import():
117117
("torch.distributed.is_available", _shortcut_patch(is_available, ()), "pytorch_lightning"),
118118
(
119119
"lightning_utilities.core.imports.RequirementCache.__bool__",
120-
_shortcut_patch(RequirementCache.__bool__, ("neptune-client",), ("requirement",)),
120+
_shortcut_patch(RequirementCache.__bool__, ("neptune",), ("requirement",)),
121121
"pytorch_lightning.loggers.neptune",
122122
),
123123
(

0 commit comments

Comments
 (0)