test_graph.py 11.6 KB
Newer Older
root's avatar
root committed
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
import pytest

import cupy
from cupy import cuda
from cupy import testing
import cupyx


@pytest.mark.skipif(cuda.runtime.is_hip,
                    reason='HIP does not support this')
@pytest.mark.skipif(cuda.driver.get_build_version() < 10010,
                    reason='Only CUDA 10.1+ supports this')
class TestGraph:

    def _helper1(self, a):
        # this tests ufuncs involving simple arithmetic
        a = a + 3
        a = a * 7.6
        return a**2

    def _helper2(self, a):
        # this tests ufuncs involving math API calls
        a = 3 * cupy.sin(a)
        return cupy.sqrt(a)

    def _helper3(self, a):
        # this tests CUDA library calls
        a = a * cupy.fft.fft(a)
        return cupy.fft.ifft(a)

    def _helper4(self, a):
        # this tests a common pattern in CuPy internal in which the host
        # operation depends on intermediate outcome on GPU (and thus requires
        # synchronization)
        result = cupy.zeros((1,), dtype=cupy.int32)
        if a.sum() > 0:  # synchronize!
            result += 1
        if a[-1] >= 0:  # synchronize!
            result += 2
        return result

    @pytest.mark.parametrize('upload', (True, False))
    def test_capture_run_on_same_stream(self, upload):
        s = cupy.cuda.Stream(non_blocking=True)

        for n in range(3):
            func = getattr(self, '_helper{}'.format(n+1))
            a = cupy.random.random((100,))

            with s:
                s.begin_capture()
                out1 = func(a)
                g = s.end_capture()
                if upload and cuda.runtime.runtimeGetVersion() >= 11010:
                    g.upload()
                g.launch()
            s.synchronize()

            out2 = func(a)
            testing.assert_array_equal(out1, out2)

    @pytest.mark.parametrize('upload', (True, False))
    def test_capture_run_on_different_streams(self, upload):
        s1 = cupy.cuda.Stream(non_blocking=True)
        s2 = cupy.cuda.Stream(non_blocking=True)

        for n in range(3):
            func = getattr(self, '_helper{}'.format(n+1))
            a = cupy.random.random((100,))

            with s1:
                s1.begin_capture()
                out1 = func(a)
                g = s1.end_capture()
            with s2:
                if upload and cuda.runtime.runtimeGetVersion() >= 11010:
                    g.upload()
                g.launch()
            s2.synchronize()

            out2 = func(a)
            testing.assert_array_equal(out1, out2)

    @pytest.mark.parametrize('upload', (True, False))
    def test_stream_is_capturing(self, upload):
        s = cupy.cuda.Stream(non_blocking=True)
        a = cupy.random.random((100,))

        with s:
            s.begin_capture()
            assert s.is_capturing()
            assert not cuda.Stream.null.is_capturing()
            b = a * 3
            g = s.end_capture()
        assert not s.is_capturing()
        assert not cuda.Stream.null.is_capturing()

        # check the graph integrity
        if upload and cuda.runtime.runtimeGetVersion() >= 11010:
            g.upload()
        g.launch()
        s.synchronize()
        testing.assert_array_equal(b, 3 * a)

    @pytest.mark.parametrize('upload', (True, False))
    def test_stream_fork_join(self, upload):
        s1 = cupy.cuda.Stream(non_blocking=True)
        s2 = cupy.cuda.Stream(non_blocking=True)
        e1 = cupy.cuda.Event()
        e2 = cupy.cuda.Event()
        a = cupy.random.random((100,))

        def func(x):
            return 3 * x + 1

        with s1:
            s1.begin_capture()
            out1 = a * 100
            e1.record(s1)
            s2.wait_event(e1)
            with s2:
                out2 = func(out1)
                e2.record(s2)
            s1.wait_event(e2)
            g = s1.end_capture()

        # check integrity
        assert not s1.is_capturing()
        assert not s2.is_capturing()
        if upload and cuda.runtime.runtimeGetVersion() >= 11010:
            g.upload()
        g.launch()
        s1.synchronize()
        testing.assert_array_equal(out2, func(a * 100))

    @pytest.mark.parametrize('upload', (True, False))
    def test_null_stream_cannot_capture(self, upload):
        s = cupy.cuda.Stream(non_blocking=False)
        a = cupy.random.random((100,))

        with s:
            s.begin_capture()
            b = a + 4
            assert s.is_capturing()
            # cudaStreamLegacy is unhappy when a blocking stream is capturing
            with pytest.raises(cuda.runtime.CUDARuntimeError) as e:
                cuda.Stream.null.is_capturing()
            assert 'cudaErrorStreamCaptureImplicit' in str(e.value)
            g = s.end_capture()
        assert not s.is_capturing()
        assert not cuda.Stream.null.is_capturing()

        # check the graph integrity
        if upload and cuda.runtime.runtimeGetVersion() >= 11010:
            g.upload()
        g.launch()
        s.synchronize()
        testing.assert_array_equal(b, a + 4)

    def test_stream_capture_failure1(self):
        s = cupy.cuda.Stream(non_blocking=True)

        with s:
            s.begin_capture()
            with pytest.raises(cuda.runtime.CUDARuntimeError) as e:
                s.synchronize()
            assert 'cudaErrorStreamCaptureUnsupported' in str(e.value)
            # invalid operation causes the capture sequence to be invalidated
            with pytest.raises(cuda.runtime.CUDARuntimeError) as e:
                g = s.end_capture()  # noqa
            assert 'cudaErrorStreamCaptureInvalidated' in str(e.value)

        # check s left the capture mode and permits normal usage
        assert not s.is_capturing()
        s.synchronize()

    def test_stream_capture_failure2(self):
        s1 = cupy.cuda.Stream(non_blocking=True)
        s2 = cupy.cuda.Stream(non_blocking=True)
        e2 = cupy.cuda.Event()
        a = cupy.random.random((100,))

        with s1:
            s1.begin_capture()
            with pytest.raises(cuda.runtime.CUDARuntimeError) as e:
                g = s2.end_capture()
            assert 'cudaErrorIllegalState' in str(e.value)
            e2.record(s1)
            s2.wait_event(e2)
            with s2:
                b = a**3  # noqa
            with pytest.raises(cuda.runtime.CUDARuntimeError) as e:
                g = s2.end_capture()
            assert 'cudaErrorStreamCaptureUnmatched' in str(e.value)
            # invalid operation causes the capture sequence to be invalidated
            with pytest.raises(cuda.runtime.CUDARuntimeError) as e:
                g = s1.end_capture()  # noqa
            assert 'cudaErrorStreamCaptureInvalidated' in str(e.value)

        # check both s1 and s2 left the capture mode and permit normal usage
        assert not s1.is_capturing()
        assert not s2.is_capturing()
        s1.synchronize()
        s2.synchronize()

    def test_stream_capture_failure3(self):
        s1 = cupy.cuda.Stream(non_blocking=True)
        s2 = cupy.cuda.Stream(non_blocking=True)
        e2 = cupy.cuda.Event()
        a = cupy.random.random((100,))

        with s1:
            s1.begin_capture()
            e2.record(s1)
            s2.wait_event(e2)
            with s2:
                # internally the function requires synchronization, which is
                # incompatible with stream capturing and so we raise
                with pytest.raises(RuntimeError) as e:
                    b = cupy.where(a > 0.5)  # noqa
                assert 'is capturing' in str(e.value)
            # invalid operation causes the capture sequence to be invalidated
            with pytest.raises(cuda.runtime.CUDARuntimeError) as e:
                g = s1.end_capture()  # noqa
            assert 'cudaErrorStreamCaptureUnjoined' in str(e.value)

        # check both s1 and s2 left the capture mode and permit normal usage
        assert not s1.is_capturing()
        assert not s2.is_capturing()
        s1.synchronize()
        s2.synchronize()

    def test_stream_capture_failure4(self):
        s = cupy.cuda.Stream(non_blocking=True)

        with s:
            s.begin_capture()
            # query the stream status is illegal during capturing
            s.done
            with pytest.raises(cuda.runtime.CUDARuntimeError) as e:
                s.end_capture()
            assert 'cudaErrorStreamCaptureInvalidated' in str(e.value)

        # check s left the capture mode and permits normal usage
        assert not s.is_capturing()
        s.synchronize()

    def test_stream_capture_failure5(self):
        s = cupy.cuda.Stream(non_blocking=True)
        func = self._helper4
        a = cupy.random.random((100,))

        with s:
            s.begin_capture()
            # internally the function requires synchronization, which is
            # incompatible with stream capturing and so we raise
            with pytest.raises(RuntimeError) as e:
                func(a)
            assert 'is capturing' in str(e.value)
            s.end_capture()

        # check s left the capture mode and permits normal usage
        assert not s.is_capturing()
        s.synchronize()

    def test_stream_capture_failure6(self):
        s = cupy.cuda.Stream(non_blocking=True)

        with s:
            s.begin_capture()
            # synchronize the stream is illegal during capturing
            with pytest.raises(cuda.runtime.CUDARuntimeError) as e:
                s.synchronize()
            assert 'cudaErrorStreamCaptureUnsupported' in str(e.value)
            with pytest.raises(cuda.runtime.CUDARuntimeError) as e:
                s.end_capture()
            assert 'cudaErrorStreamCaptureInvalidated' in str(e.value)

        # check s left the capture mode and permits normal usage
        assert not s.is_capturing()
        s.synchronize()

    def test_stream_capture_failure_cublas(self):
        s = cupy.cuda.Stream(non_blocking=True)
        a = cupy.random.random((3, 4))
        b = cupy.random.random((4, 5))

        with s:
            s.begin_capture()
            with pytest.raises(NotImplementedError) as e:
                cupy.matmul(a, b)
            assert 'cuBLAS' in str(e.value)
            s.end_capture()

        # check s left the capture mode and permits normal usage
        assert not s.is_capturing()
        s.synchronize()

    def test_stream_capture_failure_cusolver(self):
        s = cupy.cuda.Stream(non_blocking=True)
        a = cupy.random.random((8, 8))
        a += a.T

        with s:
            s.begin_capture()
            with pytest.raises(NotImplementedError) as e:
                cupy.linalg.svd(a)
            assert 'cuSOLVER' in str(e.value)
            s.end_capture()

        # check s left the capture mode and permits normal usage
        assert not s.is_capturing()
        s.synchronize()

    def test_stream_capture_failure_curand(self):
        s = cupy.cuda.Stream(non_blocking=True)

        with s:
            s.begin_capture()
            with pytest.raises(NotImplementedError) as e:
                cupy.random.random(100)
            assert 'cuRAND' in str(e.value)
            s.end_capture()

        # check s left the capture mode and permits normal usage
        assert not s.is_capturing()
        s.synchronize()

    def test_stream_capture_failure_cusparse(self):
        s = cupy.cuda.Stream(non_blocking=True)
        a = cupy.zeros((3, 4))
        a[0] = 1
        a = cupyx.scipy.sparse.csr_matrix(a)
        a.has_canonical_format  # avoid launching custom kernels during capture

        with s:
            s.begin_capture()
            with pytest.raises(NotImplementedError) as e:
                a * a.T
            assert 'cuSPARSE' in str(e.value)
            s.end_capture()

        # check s left the capture mode and permits normal usage
        assert not s.is_capturing()
        s.synchronize()