Browse Source

在 USER_CUDA_VISIBLE_DEVICES 为 None 时为用户设置提醒

tags/v1.0.0alpha
x54-729 2 years ago
parent
commit
32e2b57439
4 changed files with 8 additions and 5 deletions
  1. +0
    -1
      fastNLP/core/drivers/paddle_driver/fleet_launcher.py
  2. +2
    -1
      fastNLP/core/drivers/paddle_driver/initialize_paddle_driver.py
  3. +4
    -1
      fastNLP/core/drivers/paddle_driver/single_device.py
  4. +2
    -2
      fastNLP/core/utils/paddle_utils.py

+ 0
- 1
fastNLP/core/drivers/paddle_driver/fleet_launcher.py View File

@@ -10,7 +10,6 @@ from fastNLP.envs.env import (
FASTNLP_DISTRIBUTED_CHECK,
FASTNLP_LOG_LEVEL,
FASTNLP_GLOBAL_SEED,
USER_CUDA_VISIBLE_DEVICES,
)
from .utils import (
find_free_ports,


+ 2
- 1
fastNLP/core/drivers/paddle_driver/initialize_paddle_driver.py View File

@@ -42,7 +42,8 @@ def initialize_paddle_driver(driver: str, device: Optional[Union[str, int, List[

user_visible_devices = os.getenv("USER_CUDA_VISIBLE_DEVICES")
if user_visible_devices is None:
raise RuntimeError("This situation cannot happen, please report a bug to us.")
raise RuntimeError("`USER_CUDA_VISIBLE_DEVICES` cannot be None, please check if you have set "
"`FASTNLP_BACKEND` to 'paddle' before using FastNLP.")
_could_use_device_num = len(user_visible_devices.split(","))
if isinstance(device, int):
if device < 0 and device != -1:


+ 4
- 1
fastNLP/core/drivers/paddle_driver/single_device.py View File

@@ -39,6 +39,9 @@ class PaddleSingleDriver(PaddleDriver):
raise ValueError("`paddle.DataParallel` is not supported in `PaddleSingleDriver`")

cuda_visible_devices = os.environ.get(USER_CUDA_VISIBLE_DEVICES, None)
if cuda_visible_devices is None:
raise RuntimeError("`USER_CUDA_VISIBLE_DEVICES` cannot be None, please check if you have set "
"`FASTNLP_BACKEND` to 'paddle' before using FastNLP.")
if cuda_visible_devices == "":
device = "cpu"
logger.info("You have set `CUDA_VISIBLE_DEVICES` to '' in system environment variable, and we are gonna to"
@@ -54,7 +57,7 @@ class PaddleSingleDriver(PaddleDriver):
device_id = device
else:
device_id = get_paddle_device_id(device)
os.environ["CUDA_VISIBLE_DEVICES"] = os.environ[USER_CUDA_VISIBLE_DEVICES].split(",")[device_id]
os.environ["CUDA_VISIBLE_DEVICES"] = cuda_visible_devices.split(",")[device_id]
self.model_device = get_paddle_gpu_str(device)

self.local_rank = 0


+ 2
- 2
fastNLP/core/utils/paddle_utils.py View File

@@ -37,8 +37,8 @@ def get_device_from_visible(device: Union[str, int], output_type=int):
cuda_visible_devices = os.getenv("CUDA_VISIBLE_DEVICES")
user_visible_devices = os.getenv(USER_CUDA_VISIBLE_DEVICES)
if user_visible_devices is None:
raise RuntimeError("`USER_CUDA_VISIBLE_DEVICES` is None, please check if you have set "
"`FASTNLP_BACKEND` to 'paddle' before 'import fastNLP'.")
raise RuntimeError("`USER_CUDA_VISIBLE_DEVICES` cannot be None, please check if you have set "
"`FASTNLP_BACKEND` to 'paddle' before using FastNLP.")
idx = get_paddle_device_id(device)
# 利用 USER_CUDA_VISIBLDE_DEVICES 获取用户期望的设备
if user_visible_devices is None:


Loading…
Cancel
Save