-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
about #6754 . ### Description show a warning if any thing may enable tf32 is detected ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [x] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Qingpeng Li <qingpeng9802@gmail.com>
- Loading branch information
1 parent
cb257d2
commit ca96867
Showing
11 changed files
with
177 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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