Skip to content

Avoid torch amp cuda warning with bf16 on cpu #11161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Fixed double evaluation bug with fault-tolerance enabled where the second call was completely skipped ([#11119](https://github.com/PyTorchLightning/pytorch-lightning/pull/11119))


- Fixed an incorrect warning being produced by the model summary when using `bf16` precision on CPU ([#11161](https://github.com/PyTorchLightning/pytorch-lightning/pull/11161))


-


-


## [1.5.6] - 2021-12-15

### Fixed
Expand Down
20 changes: 11 additions & 9 deletions pytorch_lightning/utilities/model_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import contextlib
import logging
import sys
from collections import OrderedDict
from typing import Any, Dict, List, Optional, Tuple, Union

Expand All @@ -23,7 +25,6 @@
from torch.utils.hooks import RemovableHandle

import pytorch_lightning as pl
from pytorch_lightning.utilities import _AcceleratorType, AMPType
from pytorch_lightning.utilities.imports import _TORCH_GREATER_EQUAL_1_8
from pytorch_lightning.utilities.warnings import WarningCache

Expand Down Expand Up @@ -261,16 +262,17 @@ def _forward_example_input(self) -> None:
input_ = model.example_input_array
input_ = model._apply_batch_transfer_handler(input_)

if (
trainer is not None
and trainer.amp_backend == AMPType.NATIVE
and trainer._device_type != _AcceleratorType.TPU
):
model.forward = torch.cuda.amp.autocast()(model.forward)

mode = model.training
model.eval()
with torch.no_grad():

if trainer is not None:
forward_context = trainer.precision_plugin.forward_context()
elif sys.version_info >= (3, 7):
forward_context = contextlib.nullcontext()
else:
forward_context = contextlib.suppress()

with torch.no_grad(), forward_context:
# let the model hooks collect the input- and output shapes
if isinstance(input_, (list, tuple)):
model(*input_)
Expand Down