File: aten_test.py

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (131 lines) | stat: -rw-r--r-- 3,740 bytes parent folder | download
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
from caffe2.python import core
from hypothesis import given

import caffe2.python.hypothesis_test_util as hu
import hypothesis.strategies as st
import numpy as np


class TestATen(hu.HypothesisTestCase):

    @given(inputs=hu.tensors(n=2), **hu.gcs)
    def test_add(self, inputs, gc, dc):
        op = core.CreateOperator(
            "ATen",
            ["X", "Y"],
            ["Z"],
            operator="add")

        def ref(X, Y):
            return [X + Y]
        self.assertReferenceChecks(gc, op, inputs, ref)

    @given(inputs=hu.tensors(n=2, dtype=np.float16), **hu.gcs_gpu_only)
    def test_add_half(self, inputs, gc, dc):
        op = core.CreateOperator(
            "ATen",
            ["X", "Y"],
            ["Z"],
            operator="add")

        def ref(X, Y):
            return [X + Y]
        self.assertReferenceChecks(gc, op, inputs, ref)

    @given(inputs=hu.tensors(n=1), **hu.gcs)
    def test_pow(self, inputs, gc, dc):
        op = core.CreateOperator(
            "ATen",
            ["S"],
            ["Z"],
            operator="pow", exponent=2.0)

        def ref(X):
            return [np.square(X)]

        self.assertReferenceChecks(gc, op, inputs, ref)

    @given(x=st.integers(min_value=2, max_value=8), **hu.gcs)
    def test_sort(self, x, gc, dc):
        inputs = [np.random.permutation(x)]
        op = core.CreateOperator(
            "ATen",
            ["S"],
            ["Z", "I"],
            operator="sort")

        def ref(X):
            return [np.sort(X), np.argsort(X)]
        self.assertReferenceChecks(gc, op, inputs, ref)

    @given(inputs=hu.tensors(n=1), **hu.gcs)
    def test_sum(self, inputs, gc, dc):
        op = core.CreateOperator(
            "ATen",
            ["S"],
            ["Z"],
            operator="sum")

        def ref(X):
            return [np.sum(X)]

        self.assertReferenceChecks(gc, op, inputs, ref)

    @given(**hu.gcs)
    def test_index_uint8(self, gc, dc):
        # Indexing with uint8 is deprecated, but we need to provide backward compatibility for some old models exported through ONNX
        op = core.CreateOperator(
            "ATen",
            ['self', 'mask'],
            ["Z"],
            operator="index")

        def ref(self, mask):
            return (self[mask.astype(np.bool_)],)

        tensor = np.random.randn(2, 3, 4).astype(np.float32)
        mask = np.array([[1, 0, 0], [1, 1, 0]]).astype(np.uint8)

        self.assertReferenceChecks(gc, op, [tensor, mask], ref)

    @given(**hu.gcs)
    def test_index_put(self, gc, dc):
        op = core.CreateOperator(
            "ATen",
            ['self', 'indices', 'values'],
            ["Z"],
            operator="index_put")

        def ref(self, indices, values):
            self[indices] = values
            return (self,)

        tensor = np.random.randn(3, 3).astype(np.float32)
        mask = np.array([[True, True, True], [True, False, False], [True, True, False]])
        values = np.random.randn(6).astype(np.float32)

        self.assertReferenceChecks(gc, op, [tensor, mask, values], ref)

    @given(**hu.gcs)
    def test_unique(self, gc, dc):
        op = core.CreateOperator(
            "ATen",
            ['self'],
            ["output"],
            sorted=True,
            return_inverse=True,
            # return_counts=False,
            operator="_unique")

        def ref(self):
            index, _ = np.unique(self, return_index=False, return_inverse=True, return_counts=False)
            return (index,)

        tensor = np.array([1, 2, 6, 4, 2, 3, 2])
        print(ref(tensor))
        self.assertReferenceChecks(gc, op, [tensor], ref)


if __name__ == "__main__":
    import unittest
    unittest.main()