This repository was archived by the owner on Mar 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutil.py
More file actions
174 lines (127 loc) · 3.53 KB
/
Copy pathutil.py
File metadata and controls
174 lines (127 loc) · 3.53 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
import ctypes
import sys
def jax_not_installed():
try:
from jaxlib import mlir
# don't skip
return False
except ImportError:
# skip
return True
def mlir_bindings_not_installed():
try:
import mlir.extras
# don't skip
return False
except ImportError:
# skip
return True
def llvm_bindings_not_installed():
try:
import llvm
# don't skip
return False
except ImportError:
# skip
return True
def hip_check(call_result):
from hip import hip
err = call_result[0]
result = call_result[1:]
if len(result) == 1:
result = result[0]
if isinstance(err, hip.hipError_t) and err != hip.hipError_t.hipSuccess:
raise RuntimeError(str(err))
return result
def hip_synchronize():
from hip import hip
hip.hipDeviceSynchronize()
def hip_bindings_not_installed():
try:
from hip import hip
props = hip.hipDeviceProp_t()
hip_check(hip.hipGetDeviceProperties(props, 0))
# don't skip
return False
except ImportError:
return True
except Exception as e:
print(e, file=sys.stderr)
# skip
return True
def cuda_bindings_not_installed():
try:
import cupy as cp
import numpy as np
A = np.random.randint(0, 10, (10, 10))
dA = cp.asarray(A)
# don't skip
return False
except ImportError:
return True
except Exception as e:
print(e, file=sys.stderr)
# skip
return True
def chip_check(status):
import chip
if status != 0:
raise RuntimeError(
f"HIP Error {status}, {ctypes.string_at(chip.hipGetErrorString(status)).decode()}"
)
def launch_kernel(
function,
blocks_per_grid_x,
blocks_per_grid_y,
blocks_per_grid_z,
threads_per_block_x,
threads_per_block_y,
threads_per_block_z,
stream,
shared_memory,
*args,
):
import chip
import hip
from hip._util.types import DeviceArray
params = [None] * len(args)
addresses = [None] * len(args)
for i, p in enumerate(args):
if isinstance(p, DeviceArray):
addresses[i] = params[i] = p.createRef().as_c_void_p()
elif isinstance(p, int):
params[i] = ctypes.c_int32(p)
addresses[i] = ctypes.addressof(params[i])
else:
raise NotImplementedError(f"{p=} not supported with {p=}")
c_args = (ctypes.c_void_p * len(addresses))(*addresses)
function = ctypes.cast(function, chip.hipFunction_t)
stream = ctypes.cast(stream, chip.hipStream_t)
tstart = hip_check(hip.hip.hipEventCreate())
tstop = hip_check(hip.hip.hipEventCreate())
hip_check(hip.hip.hipEventRecord(tstart, None))
r = chip.hipModuleLaunchKernel(
function,
blocks_per_grid_x,
blocks_per_grid_y,
blocks_per_grid_z,
threads_per_block_x,
threads_per_block_y,
threads_per_block_z,
shared_memory,
stream,
c_args,
None,
)
hip_check(hip.hip.hipEventRecord(tstop, None))
hip_check(hip.hip.hipEventSynchronize(tstop))
time_compute = hip_check(hip.hip.hipEventElapsedTime(tstart, tstop))
chip_check(r)
return time_compute
def get_hip_arch():
if hip_bindings_not_installed():
return "gfx1100"
from hip import hip
props = hip.hipDeviceProp_t()
hip_check(hip.hipGetDeviceProperties(props, 0))
return props.gcnArchName.decode()