Repository navigation
Expand file tree
/
Copy pathdevice.py
More file actions
393 lines (294 loc) · 12.9 KB
/
Copy pathdevice.py
File metadata and controls
393 lines (294 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# Copyright 2025 Bytedance Ltd. and/or its affiliates
#
# This code is inspired by the torchtune.
# https://github.com/pytorch/torchtune/blob/main/torchtune/utils/_device.py
#
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license in https://github.com/pytorch/torchtune/blob/main/LICENSE
"""Backward-compatible device utilities.
All public names in this module are preserved for existing callers (80+ import
sites). Internally every function now delegates to the platform abstraction
layer in :mod:`verl.plugin.platform`.
"""
import logging
import os
import platform
import subprocess
from enum import IntEnum, unique
import torch
from packaging import version
from verl.plugin.platform import get_platform
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Module-level availability flags (kept for backward compatibility)
# ---------------------------------------------------------------------------
def is_torch_npu_available(check_device=True) -> bool:
"""Check if Ascend NPU is available for PyTorch operations.
Attempts to detect NPU availability by checking for the torch.npu module
and its is_available() function.
Args:
check_device : only check torch_npu package or strictly check if NPU device is available
Returns:
bool: True if NPU is available, False otherwise.
"""
try:
if not hasattr(torch, "npu"):
return False
if check_device:
return torch.npu.is_available()
else:
return True
except ImportError:
return False
is_cuda_available = torch.cuda.is_available()
is_npu_available = is_torch_npu_available()
def get_resource_name() -> str:
"""Function that return ray resource name based on the device type.
Returns:
ray resource name string, e.g. "GPU", "NPU".
"""
return get_platform().ray_resource_name()
def get_vendor() -> str:
"""Return the hardware vendor name for the current platform.
Returns:
Vendor name string, e.g. "nvidia", "metax", "huawei", "intel".
"""
return get_platform().vendor_name
# ---------------------------------------------------------------------------
# Device info helpers
# ---------------------------------------------------------------------------
def get_visible_devices_keyword() -> str:
"""Get the environment variable name for visible device selection.
Returns:
str: e.g. 'CUDA_VISIBLE_DEVICES', 'ASCEND_RT_VISIBLE_DEVICES'.
"""
return get_platform().visible_devices_envvar()
def get_device_name() -> str:
"""Get the device type string based on available accelerators.
Returns:
str: Device type string ('cuda', 'npu', 'cpu', …).
"""
return get_platform().device_name
def get_torch_device():
"""Get the PyTorch device module for the current accelerator.
Returns:
module: The PyTorch device module (torch.cuda, torch.npu, etc.).
"""
return get_platform().device_module
def get_device_id() -> int:
"""Get the index of the current accelerator device.
Returns:
int: The current device index (e.g., 0 for 'cuda:0').
"""
return get_platform().current_device()
def get_nccl_backend() -> str:
"""Get the distributed communication backend based on device type.
Returns:
str: Backend name ('nccl', 'hccl', 'gloo', …).
"""
return get_platform().communication_backend_name()
# ---------------------------------------------------------------------------
# Memory / allocator
# ---------------------------------------------------------------------------
def set_expandable_segments(enable: bool) -> None:
"""Configure memory allocator expandable segments setting.
Args:
enable: If True, enable expandable segments. If False, disable them.
"""
get_platform().set_allocator_settings(f"expandable_segments:{enable}")
# ---------------------------------------------------------------------------
# Device auto-configuration
# ---------------------------------------------------------------------------
def auto_set_device(config) -> None:
"""Automatically configure device name for different accelerators.
Args:
config: Configuration object with trainer.device attribute.
"""
if config and hasattr(config, "trainer") and hasattr(config.trainer, "device"):
detected = get_platform().device_name
# Only override when the config value doesn't match the detected platform
if detected != "cpu" and config.trainer.device != detected:
if config.trainer.device != "cpu":
logger.warning(
f"Detect setting config.trainer.device to {config.trainer.device} for "
f"{detected}, automatically set to `{detected}` instead."
)
config.trainer.device = detected
# ---------------------------------------------------------------------------
# Device properties
# ---------------------------------------------------------------------------
def get_device_capability(device_id: int = 0) -> tuple[int | None, int | None]:
"""Get the compute capability of the current accelerator device.
Args:
device_id: The device index to query. Defaults to 0.
Returns:
tuple: (major, minor) or (None, None) if not applicable.
"""
return get_platform().get_device_capability(device_id)
@unique
class AscendHardwareVersion(IntEnum):
NONE = 0
A2 = 2
A3 = 3
A5 = 5
MAX_VERSION = 999
def get_npu_versions() -> tuple[AscendHardwareVersion, str, str]:
"""Get the NPU chip generation, software version and CANN toolkit version.
Returns:
tuple[AscendHardwareVersion, str, str]: A tuple of
(hardware_version, software_version, cann_version)
Raises:
RuntimeError: If unable to retrieve version information
"""
try:
import torch_npu
device_name = torch_npu.npu.get_device_name()
except Exception:
hardware_version = AscendHardwareVersion.NONE
else:
if "Ascend910_95" in device_name or "Ascend950" in device_name:
hardware_version = AscendHardwareVersion.A5
elif "Ascend910_93" in device_name:
hardware_version = AscendHardwareVersion.A3
elif "Ascend910B" in device_name or "A2G" in device_name:
hardware_version = AscendHardwareVersion.A2
else:
hardware_version = AscendHardwareVersion.MAX_VERSION
# Check npu-smi software version
try:
result = subprocess.run(
["npu-smi", "info", "-t", "board", "-i", "1"], capture_output=True, text=True, check=True
)
except subprocess.CalledProcessError:
# Card 1 not found (common in K8s with non-consecutive device IDs)
# Try first device from ASCEND_VISIBLE_DEVICES env var
visible_devices = os.environ.get("ASCEND_VISIBLE_DEVICES")
if not visible_devices:
raise # Re-raise original error if env var not set
try:
npu_id = int(visible_devices.split(",")[0])
except (ValueError, IndexError):
raise # Re-raise original error if env var format invalid
# Retry with the first available device from K8s
try:
result = subprocess.run(
["npu-smi", "info", "-t", "board", "-i", str(npu_id)], capture_output=True, text=True, check=True
)
except subprocess.CalledProcessError:
# On A3 machines with one-card-two-die, the device ID is a die index.
# Try using the physical card index (npu_id // 2) instead.
physical_card_id = npu_id // 2
result = subprocess.run(
["npu-smi", "info", "-t", "board", "-i", str(physical_card_id)],
capture_output=True,
text=True,
check=True,
)
# Parse software version from output
software_version = None
for line in result.stdout.split("\n"):
if "Software Version" in line:
# Extract version from line like: "Software Version : 25.3.rc1.2"
parts = line.split(":")
if len(parts) > 1:
software_version = parts[1].strip().lower()
break
if not software_version:
raise RuntimeError("Could not find Software Version in npu-smi output")
# Check CANN toolkit version
arch = platform.machine()
if arch not in ["arm64", "aarch64", "x86_64"]:
raise RuntimeError(f"Unsupported architecture: {arch}")
ascend_home = os.environ.get("ASCEND_HOME_PATH", "/usr/local/Ascend/ascend-toolkit/latest")
cann_path = os.path.join(ascend_home, f"{arch}-linux")
if not os.path.exists(cann_path):
raise RuntimeError(f"CANN toolkit path does not exist: {cann_path}")
info_file = os.path.join(cann_path, "ascend_toolkit_install.info")
if not os.path.exists(info_file):
raise RuntimeError(f"CANN toolkit info file does not exist: {info_file}")
# Parse version from info file
cann_version = None
with open(info_file) as f:
for line in f:
if line.startswith("version="):
cann_version = line.split("=", 1)[1].strip().lower()
break
if not cann_version:
raise RuntimeError("Could not find version in CANN toolkit info file")
return hardware_version, software_version, cann_version
def check_ipc_version_support(software_version: str, cann_version: str) -> bool:
"""Check if the given software and CANN versions support IPC.
Compares the software version and CANN toolkit version against minimum
required versions for IPC support:
- Software Version should be >= 25.3.rc1
- CANN version should be >= 8.3.rc1
Args:
software_version: The software version string (e.g., "25.5.0", "25.3.rc1.2", "25.5.t3.b001")
cann_version: The CANN toolkit version string (e.g., "8.3.0", "8.3.rc1")
Returns:
bool: True if IPC is supported, False otherwise.
Raises:
RuntimeError: If version format is invalid
"""
# For software_version like "25.3.rc1.2", "25.5.0", or "25.5.t3.b001",
# we need to extract the base version
# Use regex to extract version with the following rules:
# - Standard version: 25.5.0 -> 25.5.0
# - RC version: 25.3.rc1.2 -> 25.3.rc1
# - t suffix version: 25.5.t3.b001 -> 25.5 (only first 2 parts if third part is lowercase t)
# - RC version: 25.3.rc1 -> 25.3.rc1
# For versions with more than 3 parts (e.g., 25.3.rc1.2), only match the first 3 parts
import re
# Match version with optional rc part or lowercase t suffix:
# - If version has lowercase t (e.g., 25.5.t3.b001), only match first 2 parts
# - Otherwise, match up to 3 parts (e.g., 25.5.0, 25.3.rc1.2)
ascend_version_pattern = r"(\d+\.\d+(?=\.t))|(\d+\.\d+(?:\.(?:rc\d+|\d+))?)"
software_match = re.match(ascend_version_pattern, software_version)
if not software_match:
raise RuntimeError(f"Invalid software version format: {software_version}")
# Select the matched group (either first 2 parts or up to 3 parts)
software_base = software_match.group(1) if software_match.group(1) else software_match.group(2)
cann_match = re.match(ascend_version_pattern, cann_version)
if not cann_match:
raise RuntimeError(f"Invalid CANN version format: {cann_version}")
else:
# Select the matched group (either first 2 parts or up to 3 parts)
cann_base = cann_match.group(1) if cann_match.group(1) else cann_match.group(2)
if version.parse(software_base) >= version.parse("25.3.rc1"):
if version.parse(cann_base) >= version.parse("8.3.rc1"):
return True
else:
logger.info(f"CANN version {cann_version} is below 8.3.RC1")
else:
logger.info(f"Software version {software_version} is below 25.3.rc1")
return False
def is_support_ipc() -> bool:
"""Check if the device supports IPC (Inter-Process Communication).
Delegates to the platform abstraction layer.
Returns:
bool: True if IPC is supported, False otherwise.
"""
return get_platform().is_ipc_supported()
def is_device_available() -> bool:
"""Check if any accelerator device is available.
Returns:
bool: True if any accelerator is available.
"""
return get_platform().is_available()
# ---------------------------------------------------------------------------
# RNG helpers
# ---------------------------------------------------------------------------
def manual_seed(seed: int) -> None:
"""Set the seed for the current accelerator device.
Args:
seed: The desired seed.
"""
get_platform().manual_seed(seed)
def manual_seed_all(seed: int) -> None:
"""Set the seed for all accelerator devices.
Args:
seed: The desired seed.
"""
get_platform().manual_seed_all(seed)