-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support GPU model loading and refine multiprocessing strategy
- Loading branch information
Showing
18 changed files
with
306 additions
and
187 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,78 @@ | ||
__version__ = '0.1.3' | ||
|
||
import os | ||
import subprocess | ||
|
||
import multiprocess as mp | ||
from loguru import logger | ||
|
||
|
||
def _cuda_device_count(): | ||
try: | ||
nvidia_smi_output = subprocess.check_output(['nvidia-smi', '-L'], | ||
text=True) | ||
all_devices = nvidia_smi_output.strip().split('\n') | ||
|
||
cuda_visible_devices = os.getenv('CUDA_VISIBLE_DEVICES') | ||
|
||
if cuda_visible_devices: | ||
visible_devices = cuda_visible_devices.split(',') | ||
visible_devices = [int(dev.strip()) for dev in visible_devices] | ||
num_visible_devices = sum(1 for dev in visible_devices | ||
if 0 <= dev < len(all_devices)) | ||
else: | ||
num_visible_devices = len(all_devices) | ||
|
||
return num_visible_devices | ||
except Exception: | ||
# nvidia-smi not found or other error | ||
return 0 | ||
|
||
|
||
_USE_CUDA = False | ||
_CUDA_COUNT = _cuda_device_count() | ||
|
||
|
||
def use_cuda(): | ||
return _USE_CUDA | ||
|
||
|
||
def cuda_device_count(): | ||
return _CUDA_COUNT | ||
|
||
|
||
def setup_mp(): | ||
method = os.getenv('MP_START_METHOD', 'auto').lower() | ||
if method == 'auto': | ||
if _CUDA_COUNT > 0: | ||
# forkserver is more lightweight | ||
method = ('forkserver' if 'forkserver' | ||
in mp.get_all_start_methods() else 'spawn') | ||
else: | ||
method = 'fork' | ||
try: | ||
logger.info(f"Setting multiprocess start method to '{method}'.") | ||
mp.set_start_method(method, force=True) | ||
except RuntimeError as e: | ||
logger.warning(f'Error setting multiprocess start method: {e}') | ||
|
||
|
||
def setup_cuda(): | ||
global _USE_CUDA | ||
|
||
method = mp.get_start_method() | ||
if method != 'fork' and _CUDA_COUNT > 0: | ||
_USE_CUDA = True | ||
else: | ||
_USE_CUDA = False | ||
logger.debug(f'_USE_CUDA: {_USE_CUDA} | MP: {method} ' | ||
f'({mp.current_process().name})') | ||
|
||
|
||
def initialize(): | ||
if mp.current_process().name == 'MainProcess': | ||
setup_mp() | ||
setup_cuda() | ||
|
||
|
||
initialize() |
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
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
Oops, something went wrong.