File: test_await.py

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (392 lines) | stat: -rw-r--r-- 12,058 bytes parent folder | download | duplicates (3)
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
# Owner(s): ["oncall: jit"]

import io
from typing import List, Optional, Tuple

import torch
from torch import Tensor
from torch._awaits import _Await as Await
from torch.testing._internal.jit_utils import JitTestCase, make_global


class TestAwait(JitTestCase):
    def test_await_python(self):
        def foo(x: int) -> int:
            return x + 13

        aw: Await[int] = torch.jit._awaitable(foo, 13)
        self.assertTrue(aw.fn()(*aw.args()) == torch.jit._awaitable_wait(aw))
        nw = torch.jit._awaitable_nowait(33)
        self.assertTrue(nw.is_nowait())
        self.assertTrue(nw.args() == (33,))

    def test_await_type_python(self):
        def foo() -> Tensor:
            return torch.randn()

        awaits = torch.jit.annotate(List[Await[Tensor]], [])
        awaits.append(torch.jit._awaitable(foo))

    def test_script(self):
        def delayed(z: int) -> int:
            return z + 3

        def fn(x: Tensor):
            aw: Await[int] = torch.jit._awaitable(delayed, 99)
            a = torch.eye(2)
            b = torch.jit._awaitable_wait(aw)
            return a + b + x

        inp = torch.zeros(2)

        sm = torch.jit.script(fn)
        out = fn(inp)
        script_out = sm(inp)
        self.assertTrue(torch.allclose(torch.eye(2) + 102, script_out))
        self.assertTrue(torch.allclose(script_out, out))

    def test_nowait(self):
        def fn(x: Tensor):
            aw = torch.jit._awaitable_nowait(13)
            a = torch.eye(2)
            b = torch.jit._awaitable_wait(aw)
            return a + b + x

        inp = torch.zeros(2)

        sm = torch.jit.script(fn)
        out = fn(inp)
        script_out = sm(inp)
        self.assertTrue(torch.allclose(torch.eye(2) + 13, script_out))
        self.assertTrue(torch.allclose(script_out, out))

    def test_nowait_class(self):
        class C:
            def __init__(self, a: Tensor, b: Tensor):
                self._a = a
                self._b = b

            def a(self) -> Tensor:
                return self._a

        def fn(x: Tensor):
            aw = torch.jit._awaitable_nowait(C(torch.zeros(2), torch.ones(2)))
            _a = torch.eye(2)
            c = torch.jit._awaitable_wait(aw)
            return _a + c.a() + x

        make_global(C)
        inp = torch.zeros(2)

        sm = torch.jit.script(fn)
        out = fn(inp)
        script_out = sm(inp)
        self.assertTrue(torch.allclose(torch.eye(2), script_out))
        self.assertTrue(torch.allclose(script_out, out))

    def test_await_class_arg(self):
        class C:
            def __init__(self, a: Tensor, b: Tensor):
                self.__a = a
                self.__b = b

            def a(self) -> Tensor:
                return self.__a

        make_global(C)

        def delayed(c: C) -> Tensor:
            return c.a()

        def fn(x: Tensor):
            c = C(torch.zeros(2), torch.ones(2))
            aw = torch.jit._awaitable(delayed, c)
            _a = torch.eye(2)
            c2_t = torch.jit._awaitable_wait(aw)
            return _a + c2_t + x

        inp = torch.zeros(2)

        sm = torch.jit.script(fn)
        out = fn(inp)
        script_out = sm(inp)
        self.assertTrue(torch.allclose(torch.eye(2), script_out))
        self.assertTrue(torch.allclose(script_out, out))

    def test_awaitable_to_await(self):
        class C:
            __slots__ = ["_a", "_b"]

            def __init__(self, a: Tensor, b: Tensor):
                self._a = a
                self._b = b

        make_global(C)

        # Can not stay in the class as Jit does not support Recursive annotations
        # (self in wait_impl can not be annotated as C as C is not defined by this time)
        def C_wait_impl(self: C):
            return self._a + self._b

        def fn(x: Tensor):
            aw = torch.jit._awaitable(C_wait_impl, C(torch.zeros(2), torch.ones(2)))
            _a = torch.eye(2)
            c_wait_impl_res = torch.jit._awaitable_wait(aw)
            return _a + c_wait_impl_res + x

        inp = torch.ones(2)

        sm = torch.jit.script(fn)
        out = fn(inp)
        script_out = sm(inp)
        self.assertTrue(torch.allclose(torch.eye(2) + 2 * torch.ones(2), script_out))
        self.assertTrue(torch.allclose(script_out, out))

    def test_await_class_return(self):
        class C:
            __slots__ = ["a", "b"]

            def __init__(self, a: Tensor, b: Tensor):
                self.a = a
                self.b = b

        make_global(C)

        # Can not stay in the class as Jit does not support Recursive annotations
        # (self in wait_impl can not be annotated as C as C is not defined by this time)
        def C_wait_impl(self: C) -> C:
            return C(self.a * 2, self.b * 3)

        def fn_arg_C(x: C) -> Tensor:
            return x.a + x.b

        def fn(x: Tensor):
            aw: Await[C] = torch.jit._awaitable(C_wait_impl, C(x, x))
            _a = torch.eye(2)
            y = fn_arg_C(torch.jit._awaitable_wait(aw))
            return _a + y + x

        inp = torch.ones(2)

        sm = torch.jit.script(fn)
        out = fn(inp)
        script_out = sm(inp)
        self.assertTrue(torch.allclose(torch.eye(2) + 6 * torch.ones(2), script_out))
        self.assertTrue(torch.allclose(script_out, out))
        self.assertGraphContainsExactly(
            sm.graph, kind="prim::awaitable_wait", num_kind_nodes=1
        )

    def test_await_getattr_implicit_convertion(self):
        class C:
            def __init__(self, a: Tensor, b: Tensor):
                self._a = a
                self._b = b

            def b(self):
                return self._b

        make_global(C)

        # Can not stay in the class as Jit does not support Recursive annotations
        # (self in wait_impl can not be annotated as C as C is not defined by this time)
        def C_wait_impl(self: C) -> C:
            return C(self._a * 2, self._b * 3)

        def fn_arg_C(x: C) -> Tensor:
            return x._a + x._b

        def fn(x: Tensor):
            aw: Await[C] = torch.jit._awaitable(C_wait_impl, C(x, x))
            _a = torch.eye(2)
            ai = aw._a
            awb = aw.b()
            c = C(2 * x, 2 * x)
            return _a + ai + x + c._a + c.b()

        inp = torch.ones(2)

        sm = torch.jit.script(fn)
        out = fn(inp)
        script_out = sm(inp)
        self.assertTrue(torch.allclose(torch.eye(2) + 7 * torch.ones(2), script_out))
        self.assertTrue(torch.allclose(script_out, out))
        self.assertGraphContainsExactly(
            sm.graph, kind="prim::awaitable_wait", num_kind_nodes=2
        )

    def test_await_nested(self):
        class C:
            def __init__(self, a: Tensor, b: Tensor):
                self.__a = a
                self.__b = b

            def a(self) -> Tensor:
                return self.__a

        make_global(C)

        def delayed(c: C) -> Await[Tensor]:
            return torch.jit._awaitable_nowait(3 * c.a())

        def fn(x: Tensor) -> Await[Await[Tensor]]:
            return torch.jit._awaitable(delayed, C(2 * x, x))

        def main(x: Tensor) -> Tensor:
            awaw = fn(x)
            return torch.jit._awaitable_wait(torch.jit._awaitable_wait(awaw))

        inp = torch.eye(2)

        sm = torch.jit.script(main)
        out = main(inp)
        script_out = sm(inp)
        self.assertTrue(torch.allclose(6 * torch.eye(2), script_out))
        self.assertTrue(torch.allclose(script_out, out))

    def test_eager_await_non_scriptable(self):
        # Tree type can not be compiled (Recursive type)
        class Tree:
            def __init__(self, v):
                self.parent = torch.jit.annotate(Optional[Tree], None)
                self.v = v

        make_global(Tree)

        def delayed(t: Tree):
            t.v = t.v + 1
            return t

        aw = torch.jit._awaitable(delayed, Tree(2))
        t = torch.jit._awaitable_wait(aw)
        self.assertTrue(t.v == 3)

    def test_await_isinstance(self):
        def delayed(x: Tensor) -> Tensor:
            return 2 * (x + 1)

        def main(x: Tensor) -> Tensor:
            aw = torch.jit._awaitable(delayed, x)
            if torch.jit.is_scripting():
                assert isinstance(aw, torch.jit._Await)
            return torch.jit._awaitable_wait(aw)

        inp = torch.eye(2)

        sm = torch.jit.script(main)
        out = main(inp)
        script_out = sm(inp)
        self.assertTrue(
            torch.allclose(2 * torch.eye(2) + 2 * torch.ones(2), script_out)
        )
        self.assertTrue(torch.allclose(script_out, out))

    def test_await_eager_lazy(self):
        def delayed(x: Tensor) -> Tensor:
            return 2 * (x + 1)

        t = torch.ones(2, dtype=torch.int64)
        aw = torch.jit._awaitable(delayed, t)
        self.assertTrue(isinstance(aw, torch._C._Await))
        self.assertTrue(t.dtype == aw.dtype)

    def test_await_out_of_interpreter(self):
        def delayed(x: Tensor) -> Tensor:
            return 2 * (x + 1)

        def main(x: Tensor) -> Await[Tensor]:
            aw = torch.jit._awaitable(delayed, x)
            return aw

        inp = torch.eye(2)

        sm = torch.jit.script(main)
        out_aw = main(inp)
        out = torch.jit._awaitable_wait(out_aw)

        script_out_aw = sm(inp)
        script_out = torch.jit._awaitable_wait(script_out_aw)
        self.assertTrue(
            torch.allclose(2 * torch.eye(2) + 2 * torch.ones(2), script_out)
        )
        self.assertTrue(torch.allclose(script_out, out))

    def test_jit_trace(self):
        def gap(x: Tensor):
            return torch.relu(x) + torch.sin(x)

        def delayed(x: Tensor) -> Tensor:
            return 2 * (torch.cos(x) + 1)

        def main(x: Tensor, y: Tensor) -> Tensor:
            aw = torch.jit._awaitable(delayed, x)
            z = gap(y)
            k = torch.jit._awaitable_wait(aw)
            return y + k

        inp = torch.randn(2)
        tm = torch.jit.trace(main, (inp, inp))
        inp_check = torch.ones(2)
        self.assertEqual(main(inp_check, inp_check), tm(inp_check, inp_check))

    def test_await_multiout_save(self):
        def gap(x: Tensor):
            return torch.relu(x) + torch.sin(x)

        def delayed(x: Tensor) -> Tuple[Tensor, List[Tensor]]:
            l = [x * i for i in range(5)]
            return (100 * x, l)

        def main(x: Tensor) -> Tensor:
            aw = torch.jit._awaitable(delayed, x)
            z = gap(x)
            (_, l) = torch.jit._awaitable_wait(aw)
            return l[3] + z

        inp = torch.eye(2)

        sm = torch.jit.script(main)
        out = main(inp)
        script_out = sm(inp)
        expected = 4.8415 * torch.eye(2)
        self.assertTrue(torch.allclose(expected, script_out))
        self.assertTrue(torch.allclose(script_out, out))

        iofile = io.BytesIO()
        torch.jit.save(sm, iofile)
        iofile.seek(0)
        sm = torch.jit.load(iofile)
        script_out_load = sm(inp)
        self.assertTrue(torch.allclose(expected, script_out_load))

    def test_await_func_arg(self):
        def gap(x: Tensor):
            return torch.relu(x) + torch.sin(x)

        def delayed(x: Tensor) -> Tensor:
            return -1 * x

        def fn(aw: Await[Tensor]) -> Tensor:
            return 3 * torch.jit._awaitable_wait(aw)

        def main(x: Tensor) -> Tensor:
            aw = torch.jit._awaitable(delayed, x)
            z = gap(x)
            y = fn(aw)
            return y + x

        inp = torch.eye(2)

        sm = torch.jit.script(main)
        out = main(inp)
        script_out = sm(inp)
        expected = -2 * torch.eye(2)
        self.assertTrue(torch.allclose(expected, script_out))
        self.assertTrue(torch.allclose(script_out, out))

        iofile = io.BytesIO()
        torch.jit.save(sm, iofile)
        iofile.seek(0)
        sm = torch.jit.load(iofile)
        script_out_load = sm(inp)
        self.assertTrue(torch.allclose(expected, script_out_load))