-
Notifications
You must be signed in to change notification settings - Fork 3.6k
True half-precision support in Trainer #18193
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
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
290101e
True half-precision support in Trainer
awaelchli 5b027b9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ec1f397
add tests
awaelchli 6417261
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 853046a
tests
awaelchli 49209c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c20c501
fix typo
awaelchli 33881bb
convert
awaelchli a29359a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dc35ef4
reset
awaelchli 44bef1b
docs
awaelchli 8882e28
docs
awaelchli b58b111
docs
awaelchli 1512556
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1ff3c1f
changelog
awaelchli 049ed20
Merge remote-tracking branch 'origin/feature/16-true-trainer' into fe…
awaelchli 4a6b9aa
Merge branch 'master' into feature/16-true-trainer
awaelchli e47608e
reset
awaelchli e515195
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4e289d1
Merge branch 'master' into feature/16-true-trainer
Borda 718bbec
Update docs/source-pytorch/common/trainer.rst
awaelchli 3e58327
Merge branch 'master' into feature/16-true-trainer
awaelchli 2ce56f1
bad merge
awaelchli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright The Lightning AI team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# 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 contextlib import contextmanager | ||
from typing import Any, Generator, Literal | ||
|
||
import torch | ||
from lightning_utilities import apply_to_collection | ||
from torch import Tensor | ||
from torch.nn import Module | ||
|
||
from lightning.fabric.plugins.precision.utils import _convert_fp_tensor | ||
from lightning.pytorch.plugins.precision.precision_plugin import PrecisionPlugin | ||
|
||
|
||
class HalfPrecisionPlugin(PrecisionPlugin): | ||
"""Plugin for training with half precision. | ||
|
||
Args: | ||
precision: Whether to use ``torch.float16`` (``'16-true'``) or ``torch.bfloat16`` (``'bf16-true'``). | ||
""" | ||
|
||
precision: Literal["bf16-true", "16-true"] = "16-true" | ||
|
||
def __init__(self, precision: Literal["bf16-true", "16-true"] = "16-true") -> None: | ||
self.precision = precision | ||
self._desired_input_dtype = torch.bfloat16 if precision == "bf16-true" else torch.float16 | ||
|
||
def convert_module(self, module: Module) -> Module: | ||
return module.to(dtype=self._desired_input_dtype) | ||
|
||
@contextmanager | ||
def init_context(self) -> Generator[None, None, None]: | ||
"""A context manager to change the default tensor type when initializing module parameters or tensors. | ||
|
||
See: :meth:`torch.set_default_dtype` | ||
""" | ||
default_dtype = torch.get_default_dtype() | ||
torch.set_default_dtype(self._desired_input_dtype) | ||
yield | ||
torch.set_default_dtype(default_dtype) | ||
|
||
@contextmanager | ||
def forward_context(self) -> Generator[None, None, None]: | ||
"""A context manager to change the default tensor type when tensors get created during the module's | ||
forward. | ||
|
||
See: :meth:`torch.set_default_tensor_type` | ||
""" | ||
default_dtype = torch.get_default_dtype() | ||
torch.set_default_dtype(self._desired_input_dtype) | ||
yield | ||
torch.set_default_dtype(default_dtype) | ||
|
||
def convert_input(self, data: Any) -> Any: | ||
return apply_to_collection(data, function=_convert_fp_tensor, dtype=Tensor, dst_type=self._desired_input_dtype) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.