MCPcopy Index your code
hub / github.com/modelscope/modelscope / verify_device

Function verify_device

modelscope/utils/device.py:11–36  ·  view source on GitHub ↗

Verify device is valid, device should be either cpu, cuda, gpu, cuda:X or gpu:X. Args: device (str): device str, should be either cpu, cuda, gpu, gpu:X or cuda:X where X is the ordinal for gpu device. Return: device info (tuple): device_type and device_id, if

(device_name)

Source from the content-addressed store, hash-verified

9
10
11def verify_device(device_name):
12 """ Verify device is valid, device should be either cpu, cuda, gpu, cuda:X or gpu:X.
13
14 Args:
15 device (str): device str, should be either cpu, cuda, gpu, gpu:X or cuda:X
16 where X is the ordinal for gpu device.
17
18 Return:
19 device info (tuple): device_type and device_id, if device_id is not set, will use 0 as default.
20 """
21 err_msg = 'device should be either cpu, cuda, gpu, gpu:X or cuda:X where X is the ordinal for gpu device.'
22 assert device_name is not None and device_name != '', err_msg
23 device_name = device_name.lower()
24 eles = device_name.split(':')
25 assert len(eles) <= 2, err_msg
26 assert device_name is not None
27 assert eles[0] in ['cpu', 'cuda', 'gpu'], err_msg
28 device_type = eles[0]
29 device_id = None
30 if len(eles) > 1:
31 device_id = int(eles[1])
32 if device_type == 'cuda':
33 device_type = Devices.gpu
34 if device_type == Devices.gpu and device_id is None:
35 device_id = 0
36 return device_type, device_id
37
38
39@contextmanager

Callers 7

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
test_verifyMethod · 0.90
device_placementFunction · 0.85
create_deviceFunction · 0.85

Calls

no outgoing calls

Tested by 1

test_verifyMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…