Skip to content

Commit

Permalink
Fix apply_to_collection(defaultdict) (#10316)
Browse files Browse the repository at this point in the history
  • Loading branch information
carmocca authored and lexierule committed Nov 9, 2021
1 parent e8ad5d0 commit 7d87986
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
12 changes: 7 additions & 5 deletions 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/).


## [1.5.1] - 2021-MM-DD

### Fixed

- Fixed `apply_to_collection(defaultdict)` ([#10316](https://github.com/PyTorchLightning/pytorch-lightning/issues/10316))


## [1.5.0] - 2021-11-02

### Added
Expand Down Expand Up @@ -132,7 +139,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added support for empty `gpus` list to run on CPU ([#10246](https://github.com/PyTorchLightning/pytorch-lightning/pull/10246))
- Added a warning if multiple batch sizes are found from ambiguous batch ([#10247](https://github.com/PyTorchLightning/pytorch-lightning/pull/10247))


### Changed

- Trainer now raises a `MisconfigurationException` when its methods are called with `ckpt_path="best"` but a checkpoint callback isn't configured ([#9841](https://github.com/PyTorchLightning/pytorch-lightning/pull/9841))
Expand Down Expand Up @@ -184,7 +190,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Enabled `on_load_checkpoint` for `LightningDataModule` for all `trainer_fn` ([#10238](https://github.com/PyTorchLightning/pytorch-lightning/pull/10238))
- Allowed separate config files for parameters with class type when LightningCLI is in `subclass_mode=False` ([#10286](https://github.com/PyTorchLightning/pytorch-lightning/pull/10286))


### Deprecated

- Deprecated Trainer argument `terminate_on_nan` in favor of `detect_anomaly`([#9175](https://github.com/PyTorchLightning/pytorch-lightning/pull/9175))
Expand Down Expand Up @@ -220,7 +225,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Deprecated `lr_sch_names` from `LearningRateMonitor` ([#10066](https://github.com/PyTorchLightning/pytorch-lightning/pull/10066))
- Deprecated `ProgressBar` callback in favor of `TQDMProgressBar` ([#10134](https://github.com/PyTorchLightning/pytorch-lightning/pull/10134))


### Removed

- Removed deprecated `metrics` ([#8586](https://github.com/PyTorchLightning/pytorch-lightning/pull/8586/))
Expand Down Expand Up @@ -264,7 +268,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Removed automatic patching of `{train,val,test,predict}_dataloader()` on the `LightningModule` ([#9764](https://github.com/PyTorchLightning/pytorch-lightning/pull/9764))
- Removed `pytorch_lightning.trainer.connectors.OptimizerConnector` ([#10120](https://github.com/PyTorchLightning/pytorch-lightning/pull/10120))


### Fixed

- Fixed ImageNet evaluation in example ([#10179](https://github.com/PyTorchLightning/pytorch-lightning/pull/10179))
Expand Down Expand Up @@ -473,7 +476,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added private `prevent_trainer_and_dataloaders_deepcopy` context manager on the `LightningModule` ([#8472](https://github.com/PyTorchLightning/pytorch-lightning/pull/8472))
- Added support for providing callables to the Lightning CLI instead of types ([#8400](https://github.com/PyTorchLightning/pytorch-lightning/pull/8400))


### Changed

- Decoupled device parsing logic from Accelerator connector to Trainer ([#8180](https://github.com/PyTorchLightning/pytorch-lightning/pull/8180))
Expand Down
4 changes: 3 additions & 1 deletion pytorch_lightning/utilities/apply_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import dataclasses
import operator
from abc import ABC
from collections import OrderedDict
from collections import defaultdict, OrderedDict
from collections.abc import Mapping, Sequence
from copy import copy
from functools import partial
Expand Down Expand Up @@ -102,6 +102,8 @@ def apply_to_collection(
)
if include_none or v is not None:
out.append((k, v))
if isinstance(data, defaultdict):
return elem_type(data.default_factory, OrderedDict(out))
return elem_type(OrderedDict(out))

is_namedtuple = _is_namedtuple(data)
Expand Down
7 changes: 6 additions & 1 deletion tests/utilities/test_apply_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
import dataclasses
import numbers
from collections import namedtuple, OrderedDict
from collections import defaultdict, namedtuple, OrderedDict
from typing import List

import numpy as np
Expand Down Expand Up @@ -153,6 +153,11 @@ def __init__(self, initial_dict):
reduced = apply_to_collection(to_reduce, int, lambda x: str(x))
assert reduced == _CustomCollection({"a": "1", "b": "2", "c": "3"})

# defaultdict
to_reduce = defaultdict(int, {"a": 1, "b": 2, "c": 3})
reduced = apply_to_collection(to_reduce, int, lambda x: str(x))
assert reduced == defaultdict(int, {"a": "1", "b": "2", "c": "3"})


def test_apply_to_collection_include_none():
to_reduce = [1, 2, 3.4, 5.6, 7, (8, 9.1, {10: 10})]
Expand Down

0 comments on commit 7d87986

Please sign in to comment.