-
Notifications
You must be signed in to change notification settings - Fork 1
/
cuda.py
46 lines (33 loc) · 896 Bytes
/
cuda.py
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
import numpy as np
import eustoma
gpu_enable = True
calculate_device = np
try:
import cupy as cp
cupy = cp
except ImportError:
gpu_enable = False
# 返回参数x的对应模块,x为Variable或ndarray类型(numpy或cupy)
def get_array_module(x):
if isinstance(x, eustoma.Variable):
x = x.data
if not gpu_enable:
return np
xp = cp.get_array_module(x)
return xp
# 将x转化为numpy
def as_numpy(x):
if isinstance(x, eustoma.Variable):
x = x.data
if np.isscalar(x):
return np.array(x)
elif isinstance(x, np.ndarray):
return x
return cp.asnumpy(x)
# 将x转换为cupy
def as_cupy(x):
if isinstance(x, eustoma.Variable):
x = x.data
if not gpu_enable:
raise Exception('Cupy cannot be loaded. Install Cupy!')
return cp.asarray(x)