Repository navigation
Tf32 warnings #6816
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
Tf32 warnings #6816
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5f23835
rename precision doc
qingpeng9802 6014216
add `version_geq`
qingpeng9802 e271ae2
detect default tf32 settings
qingpeng9802 5d286aa
refactor `is_tf32_env()`
qingpeng9802 3b2345a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 35438b8
fix style E402
qingpeng9802 51f3270
Merge branch 'tf32-warnings' of https://github.com/qingpeng9802/MONAI…
qingpeng9802 5697a31
fix style E722
qingpeng9802 948ccc3
[MONAI] code formatting
monai-bot 96ca146
refactor the usage of `detect_default_tf32()`
qingpeng9802 d8a65bb
improve `is_tf32_env()`
qingpeng9802 93ff777
[MONAI] code formatting
monai-bot 0ad3a73
Merge branch 'dev' into tf32-warnings
wyli 348f089
resolve `torch.cuda` initialization order issue
qingpeng9802 04b71c3
Merge branch 'tf32-warnings' of https://github.com/qingpeng9802/MONAI…
qingpeng9802 9a4310f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a783158
[MONAI] code formatting
monai-bot 13cd277
use `pynvml` to avoid `torch.cuda` call
qingpeng9802 fddc87f
minor fix
qingpeng9802 2a6ff88
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e1b2075
[MONAI] code formatting
monai-bot 73cb104
Merge branch 'dev' into tf32-warnings
wyli 72c8a72
fix import `pynvml`
qingpeng9802 d1ed95b
Merge branch 'tf32-warnings' of https://github.com/qingpeng9802/MONAI…
qingpeng9802 055ed46
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f4571f1
[MONAI] code formatting
monai-bot 147224c
Merge branch 'dev' into tf32-warnings
wyli 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # 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 __future__ import annotations | ||
|
|
||
| import functools | ||
| import os | ||
| import warnings | ||
|
|
||
| __all__ = ["has_ampere_or_later", "detect_default_tf32"] | ||
|
|
||
|
|
||
| @functools.lru_cache(None) | ||
| def has_ampere_or_later() -> bool: | ||
| """ | ||
| Check if there is any Ampere and later GPU. | ||
| """ | ||
| import torch | ||
|
|
||
| from monai.utils.module import optional_import, version_geq | ||
|
|
||
| if not (torch.version.cuda and version_geq(f"{torch.version.cuda}", "11.0")): | ||
| return False | ||
|
|
||
| pynvml, has_pynvml = optional_import("pynvml") | ||
| if not has_pynvml: # assuming that the user has Ampere and later GPU | ||
| return True | ||
|
|
||
| try: | ||
| pynvml.nvmlInit() | ||
| for i in range(pynvml.nvmlDeviceGetCount()): | ||
| handle = pynvml.nvmlDeviceGetHandleByIndex(i) | ||
| major, _ = pynvml.nvmlDeviceGetCudaComputeCapability(handle) | ||
| if major >= 8: | ||
| return True | ||
| except BaseException: | ||
| pass | ||
| finally: | ||
| pynvml.nvmlShutdown() | ||
|
|
||
| return False | ||
|
|
||
|
|
||
| @functools.lru_cache(None) | ||
| def detect_default_tf32() -> bool: | ||
| """ | ||
| Dectect if there is anything that may enable TF32 mode by default. | ||
| If any, show a warning message. | ||
| """ | ||
| may_enable_tf32 = False | ||
| try: | ||
| if not has_ampere_or_later(): | ||
| return False | ||
|
|
||
| from monai.utils.module import pytorch_after | ||
|
|
||
| if pytorch_after(1, 7, 0) and not pytorch_after(1, 12, 0): | ||
| warnings.warn( | ||
| "torch.backends.cuda.matmul.allow_tf32 = True by default.\n" | ||
| " This value defaults to True when PyTorch version in [1.7, 1.11] and may affect precision.\n" | ||
| " See https://docs.monai.io/en/latest/precision_accelerating.html#precision-and-accelerating" | ||
| ) | ||
| may_enable_tf32 = True | ||
|
|
||
| override_tf32_env_vars = {"NVIDIA_TF32_OVERRIDE": "1", "TORCH_ALLOW_TF32_CUBLAS_OVERRIDE": "1"} | ||
| for name, override_val in override_tf32_env_vars.items(): | ||
| if os.environ.get(name) == override_val: | ||
| warnings.warn( | ||
| f"Environment variable `{name} = {override_val}` is set.\n" | ||
| f" This environment variable may enable TF32 mode accidentally and affect precision.\n" | ||
| f" See https://docs.monai.io/en/latest/precision_accelerating.html#precision-and-accelerating" | ||
| ) | ||
| may_enable_tf32 = True | ||
|
|
||
| return may_enable_tf32 | ||
| except BaseException: | ||
| from monai.utils.misc import MONAIEnvVars | ||
|
|
||
| if MONAIEnvVars.debug(): | ||
| raise | ||
| return False | ||
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
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.