File: order_switch_op_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 (81 lines) | stat: -rw-r--r-- 2,299 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





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

from hypothesis import given, settings
from caffe2.python import core, workspace


@unittest.skipIf(not workspace.C.use_mkldnn, "No MKLDNN support.")
class OrderSwitchTest(hu.HypothesisTestCase):
    @given(n=st.integers(1, 128),
           c=st.integers(1, 64),
           h=st.integers(1, 128),
           w=st.integers(1, 128),
           **mu.gcs)
    @settings(max_examples=10, deadline=None)
    def test_nchw2nhwc(self, n, c, h, w, gc, dc):
        op = core.CreateOperator(
            "NCHW2NHWC",
            ["X"],
            ["Y"],
        )
        X = np.random.rand(n, c, h, w).astype(np.float32) - 0.5

        self.assertDeviceChecks(dc, op, [X], [0])

    @given(n=st.integers(1, 128),
           c=st.integers(1, 64),
           h=st.integers(1, 128),
           w=st.integers(1, 128),
           **mu.gcs)
    @settings(deadline=None, max_examples=50)
    def test_nhwc2nchw(self, n, c, h, w, gc, dc):
        op0 = core.CreateOperator(
            "NCHW2NHWC",
            ["X"],
            ["Y"],
        )
        op1 = core.CreateOperator(
            "NHWC2NCHW",
            ["Y"],
            ["Z"],
        )

        X = np.random.rand(n, c, h, w).astype(np.float32) - 0.5

        old_ws_name = workspace.CurrentWorkspace()
        workspace.SwitchWorkspace("_device_check_", True)
        workspace.FeedBlob('X', X, dc[0])
        op0.device_option.CopyFrom(dc[0])
        op1.device_option.CopyFrom(dc[0])
        workspace.RunOperatorOnce(op0)
        workspace.RunOperatorOnce(op1)
        Z0 = workspace.FetchBlob("Z")

        workspace.ResetWorkspace()
        workspace.FeedBlob('X', X, dc[1])
        op0.device_option.CopyFrom(dc[1])
        op1.device_option.CopyFrom(dc[1])
        workspace.RunOperatorOnce(op0)
        workspace.RunOperatorOnce(op1)
        Z1 = workspace.FetchBlob("Z")

        if not np.allclose(Z0, Z1, atol=0.01, rtol=0.01):
            print(Z1.flatten())
            print(Z0.flatten())
            print(np.max(np.abs(Z1 - Z0)))
            self.assertTrue(False)

        workspace.SwitchWorkspace(old_ws_name)


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