Skip to content

Commit

Permalink
Patch Release 2.2.3 (#19800)
Browse files Browse the repository at this point in the history
Co-authored-by: thomas chaton <[email protected]>
  • Loading branch information
awaelchli and tchaton authored Apr 23, 2024
1 parent fa4b3f7 commit f4cd9df
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/source-pytorch/ecosystem/community_examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Community Examples
==================


- `Lightning Bolts: Deep Learning components for extending PyTorch Lightning <https://lightning.ai/docs/pytorch/latest/ecosystem/bolts.html>`_.
- `Lightning Bolts: Deep Learning components for extending PyTorch Lightning <https://lightning.ai/docs/pytorch/stable/ecosystem/bolts.html>`_.
- `Lightning Flash: Your PyTorch AI Factory - Flash enables you to easily configure and run complex AI recipes <https://github.com/Lightning-AI/lightning-flash>`_.
- `Contextual Emotion Detection (DoubleDistilBert) <https://github.com/juliusberner/emotion_transformer>`_
- `Cotatron: Transcription-Guided Speech Encoder <https://github.com/mindslab-ai/cotatron>`_
Expand Down
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Examples

Our most robust examples showing all sorts of implementations
can be found in our sister library [Lightning Bolts](https://lightning.ai/docs/pytorch/latest/ecosystem/bolts.html).
can be found in our sister library [Lightning Bolts](https://lightning.ai/docs/pytorch/stable/ecosystem/bolts.html).

______________________________________________________________________

Expand Down Expand Up @@ -33,5 +33,5 @@ ______________________________________________________________________
## Domain Examples

This folder contains older examples. You should instead use the examples
in [Lightning Bolts](https://lightning.ai/docs/pytorch/latest/ecosystem/bolts.html)
in [Lightning Bolts](https://lightning.ai/docs/pytorch/stable/ecosystem/bolts.html)
for advanced use cases.
2 changes: 1 addition & 1 deletion requirements/app/app.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lightning-cloud == 0.5.65 # Must be pinned to ensure compatibility
lightning-cloud == 0.5.67 # Must be pinned to ensure compatibility
packaging
typing-extensions >=4.4.0, <4.10.0
deepdiff >=5.7.0, <6.6.0
Expand Down
19 changes: 18 additions & 1 deletion src/lightning/fabric/utilities/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
from argparse import Namespace
from dataclasses import asdict, is_dataclass
from typing import Any, Dict, Mapping, MutableMapping, Optional, Union
Expand Down Expand Up @@ -132,6 +132,23 @@ def _sanitize_params(params: Dict[str, Any]) -> Dict[str, Any]:
return params


def _convert_json_serializable(params: Dict[str, Any]) -> Dict[str, Any]:
"""Convert non-serializable objects in params to string."""
return {k: str(v) if not _is_json_serializable(v) else v for k, v in params.items()}


def _is_json_serializable(value: Any) -> bool:
"""Test whether a variable can be encoded as json."""
if value is None or isinstance(value, (bool, int, float, str, list, dict)): # fast path
return True
try:
json.dumps(value)
return True
except (TypeError, OverflowError):
# OverflowError is raised if number is too large to encode
return False


def _add_prefix(
metrics: Mapping[str, Union[Tensor, float]], prefix: str, separator: str
) -> Mapping[str, Union[Tensor, float]]:
Expand Down
7 changes: 7 additions & 0 deletions src/lightning/pytorch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).


## [2.2.3] - 2024-04-23

### Fixed

- Fixed `WandbLogger.log_hyperparameters()` raising an error if hyperparameters are not JSON serializable ([#19769](https://github.com/Lightning-AI/pytorch-lightning/pull/19769))


## [2.2.2] - 2024-04-11

### Fixed
Expand Down
8 changes: 7 additions & 1 deletion src/lightning/pytorch/loggers/wandb.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
from torch import Tensor
from typing_extensions import override

from lightning.fabric.utilities.logger import _add_prefix, _convert_params, _sanitize_callable_params
from lightning.fabric.utilities.logger import (
_add_prefix,
_convert_json_serializable,
_convert_params,
_sanitize_callable_params,
)
from lightning.fabric.utilities.types import _PATH
from lightning.pytorch.callbacks.model_checkpoint import ModelCheckpoint
from lightning.pytorch.loggers.logger import Logger, rank_zero_experiment
Expand Down Expand Up @@ -419,6 +424,7 @@ def watch(
def log_hyperparams(self, params: Union[Dict[str, Any], Namespace]) -> None: # type: ignore[override]
params = _convert_params(params)
params = _sanitize_callable_params(params)
params = _convert_json_serializable(params)
self.experiment.config.update(params, allow_val_change=True)

@override
Expand Down
2 changes: 1 addition & 1 deletion src/version.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.2
2.2.3
3 changes: 3 additions & 0 deletions tests/tests_app/cli/test_cmd_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from unittest import mock
from unittest.mock import ANY, MagicMock, Mock

import pytest
from click.testing import CliRunner
from lightning.app.cli.lightning_cli_launch import run_flow, run_flow_and_servers, run_frontend, run_server
from lightning.app.core.queues import QueuingSystem
Expand Down Expand Up @@ -189,6 +190,7 @@ def start_processes(**functions):


@_RunIf(skip_windows=True)
@pytest.mark.xfail(strict=False, reason="Flaky test")
def test_manage_server_processes_one_process_gets_killed(capfd):
functions = {"p1": run_forever_process, "p2": run_for_2_seconds_and_raise}
p = Process(target=start_processes, kwargs=functions)
Expand All @@ -207,6 +209,7 @@ def test_manage_server_processes_one_process_gets_killed(capfd):


@_RunIf(skip_windows=True)
@pytest.mark.xfail(strict=False, reason="Flaky test")
def test_manage_server_processes_all_processes_exits_with_zero_exitcode(capfd):
functions = {
"p1": exit_successfully_immediately,
Expand Down
29 changes: 28 additions & 1 deletion tests/tests_fabric/utilities/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from argparse import Namespace
from dataclasses import dataclass
from pathlib import Path

import numpy as np
import torch
from lightning.fabric.utilities.logger import (
_add_prefix,
_convert_json_serializable,
_convert_params,
_flatten_dict,
_sanitize_callable_params,
Expand Down Expand Up @@ -167,3 +168,29 @@ def test_add_prefix():
assert "prefix-metric2" not in metrics
assert metrics["prefix2_prefix-metric1"] == 1
assert metrics["prefix2_prefix-metric2"] == 2


def test_convert_json_serializable():
data = {
# JSON-serializable
"none": None,
"int": 1,
"float": 1.1,
"bool": True,
"dict": {"a": 1},
"list": [2, 3, 4],
# not JSON-serializable
"path": Path("path"),
"tensor": torch.tensor(1),
}
expected = {
"none": None,
"int": 1,
"float": 1.1,
"bool": True,
"dict": {"a": 1},
"list": [2, 3, 4],
"path": "path",
"tensor": "tensor(1)",
}
assert _convert_json_serializable(data) == expected
6 changes: 4 additions & 2 deletions tests/tests_pytorch/loggers/test_wandb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import os
import pickle
from pathlib import Path
from unittest import mock

import pytest
Expand Down Expand Up @@ -113,9 +114,10 @@ def test_wandb_logger_init(wandb_mock):
wandb_mock.init().log.assert_called_with({"acc": 1.0, "trainer/global_step": 6})

# log hyper parameters
hparams = {"test": None, "nested": {"a": 1}, "b": [2, 3, 4]}
hparams = {"none": None, "dict": {"a": 1}, "b": [2, 3, 4], "path": Path("path")}
expected = {"none": None, "dict": {"a": 1}, "b": [2, 3, 4], "path": "path"}
logger.log_hyperparams(hparams)
wandb_mock.init().config.update.assert_called_once_with(hparams, allow_val_change=True)
wandb_mock.init().config.update.assert_called_once_with(expected, allow_val_change=True)

# watch a model
logger.watch("model", "log", 10, False)
Expand Down

0 comments on commit f4cd9df

Please sign in to comment.