Skip to content
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

Set fp16 as default behavior if user has supported Nvidia GPU (or arm mac) #2203

Merged
merged 6 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 33 additions & 1 deletion backend/src/gpu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import List, Tuple
from typing import List, Tuple, Union

import pynvml as nv
from sanic.log import logger
Expand All @@ -22,6 +22,31 @@ class _GPU:
uuid: str
index: int
handle: int
arch: int


FP16_ARCH_ABILITY_MAP = {
nv.NVML_DEVICE_ARCH_KEPLER: False,
nv.NVML_DEVICE_ARCH_MAXWELL: False,
nv.NVML_DEVICE_ARCH_PASCAL: False,
nv.NVML_DEVICE_ARCH_VOLTA: True,
nv.NVML_DEVICE_ARCH_TURING: True,
nv.NVML_DEVICE_ARCH_AMPERE: True,
nv.NVML_DEVICE_ARCH_ADA: True,
nv.NVML_DEVICE_ARCH_HOPPER: True,
nv.NVML_DEVICE_ARCH_UNKNOWN: False,
}


def can_gpu_fp16(gpu: _GPU):
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
# This generation also contains the GTX 1600 cards, which do not support FP16.
if gpu.arch == nv.NVML_DEVICE_ARCH_TURING:
# There may be a more robust way to check this, but for now I think this will do.
return "RTX" in gpu.name
if gpu.arch not in FP16_ARCH_ABILITY_MAP and gpu.arch > nv.NVML_DEVICE_ARCH_HOPPER:
# Future proofing. We can be reasonably sure that future architectures will support FP16.
return True
return FP16_ARCH_ABILITY_MAP[gpu.arch]
joeyballentine marked this conversation as resolved.
Show resolved Hide resolved


class NvidiaHelper:
Expand All @@ -39,6 +64,7 @@ def __init__(self):
uuid=nv.nvmlDeviceGetUUID(handle),
index=i,
handle=handle,
arch=nv.nvmlDeviceGetArchitecture(handle),
)
)

Expand All @@ -57,6 +83,12 @@ def get_current_vram_usage(self, gpu_index=0) -> Tuple[int, int, int]:

return info.total, info.used, info.free

def get_can_fp16(self, gpu_index: Union[int, None] = None) -> bool:
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
if gpu_index is None:
return all(can_gpu_fp16(gpu) for gpu in self.__gpus)
gpu = self.__gpus[gpu_index]
return can_gpu_fp16(gpu)


_cachedNvidiaHelper = None

Expand Down
9 changes: 7 additions & 2 deletions backend/src/packages/chaiNNer_onnx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

from . import package

nv = get_nvidia_helper()

if not is_arm_mac:
nv = get_nvidia_helper()
gpu_list = nv.list_gpus() if nv is not None else []

package.add_setting(
Expand Down Expand Up @@ -66,12 +67,16 @@ def get_providers():
)
)

should_fp16 = False
if nv is not None:
should_fp16 = nv.get_can_fp16()
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved

package.add_setting(
ToggleSetting(
label="Use TensorRT FP16 Mode",
key="tensorrt_fp16_mode",
description="Runs TensorRT in half-precision (FP16) mode for less VRAM usage. RTX GPUs also get a speedup.",
default=False,
default=should_fp16,
disabled="TensorrtExecutionProvider" not in execution_providers,
)
)
Expand Down
9 changes: 8 additions & 1 deletion backend/src/packages/chaiNNer_pytorch/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import torch

from api import DropdownSetting, ToggleSetting
from gpu import get_nvidia_helper
from system import is_arm_mac

from . import package

nv = get_nvidia_helper()

if not is_arm_mac:
gpu_list = []
for i in range(torch.cuda.device_count()):
Expand All @@ -32,6 +35,10 @@
),
)

should_fp16 = False
if nv is not None:
should_fp16 = nv.get_can_fp16()

package.add_setting(
ToggleSetting(
label="Use FP16 Mode",
Expand All @@ -41,7 +48,7 @@
if is_arm_mac
else "Runs PyTorch in half-precision (FP16) mode for less VRAM usage. RTX GPUs also get a speedup."
),
default=False,
default=should_fp16 or is_arm_mac,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
),
)

Expand Down