File: percentile_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 (130 lines) | stat: -rw-r--r-- 4,427 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





from caffe2.python import core, workspace
import caffe2.python.hypothesis_test_util as hu
import numpy as np


class TestPercentileOp(hu.HypothesisTestCase):
    def _test_percentile_op(
        self,
        original_inp,
        value_to_pct_map,
        dist_lengths,
        expected_values
    ):
        op = core.CreateOperator(
            'Percentile',
            ['original_values', 'value_to_pct_map', 'dist_lengths'],
            ['percentile_values']
        )
        workspace.FeedBlob('original_values', np.array(
            original_inp, dtype=np.float32))
        workspace.FeedBlob(
            'value_to_pct_map', np.array(value_to_pct_map, dtype=np.float32))
        workspace.FeedBlob('dist_lengths', np.array(
            dist_lengths, dtype=np.int32))
        workspace.RunOperatorOnce(op)
        np.testing.assert_array_almost_equal(
            workspace.FetchBlob('percentile_values'),
            np.array(expected_values),
            decimal=5
        )
        self._test_shape_inference(
            original_inp,
            value_to_pct_map,
            dist_lengths,
            expected_values
        )

    def _test_shape_inference(
        self,
        original_inp,
        value_to_pct_map,
        dist_lengths,
        expected_values
    ):
        net = core.Net('test_shape_inference')
        result = net.Percentile(
            ['original_values', 'value_to_pct_map', 'dist_lengths'],
            ['percentile_values']
        )
        workspace.FeedBlob('original_values', np.array(
            original_inp, dtype=np.float32))
        workspace.FeedBlob(
            'value_to_pct_map', np.array(value_to_pct_map, dtype=np.float32))
        workspace.FeedBlob('dist_lengths', np.array(
            dist_lengths, dtype=np.int32))
        (shapes, types) = workspace.InferShapesAndTypes([net])
        workspace.RunNetOnce(net)
        self.assertEqual(shapes[result], list(workspace.blobs[result].shape))
        self.assertEqual(shapes[result], list(workspace.blobs['original_values'].shape))
        self.assertEqual(types[result], core.DataType.FLOAT)

    def test_percentile_op_with_only_one_dist(self):
        self._test_percentile_op(
            original_inp=[[5]],
            value_to_pct_map=[[5, 0.4]],
            dist_lengths=[1],
            expected_values=[[0.4]]
        )

    def test_percentile_op_with_all_elements_in_map(self):
        self._test_percentile_op(
            original_inp=[[3, 4], [10, 4]],
            value_to_pct_map=[[3, 0.3], [4, 0.6],
                              [10, 0.8], [4, 0.5], [5, 0.6]],
            dist_lengths=[3, 2],
            expected_values=[[0.3, 0.5], [0.8, 0.5]],
        )

    def test_percentile_op_with_same_value(self):
        self._test_percentile_op(
            original_inp=[[1, 1], [1, 2]],
            value_to_pct_map=[[1, 0.1], [4, 0.4], [2, 0.5]],
            dist_lengths=[2, 1],
            expected_values=[[0.1, 0.0], [0.1, 0.5]]
        )

    def test_percentile_op_with_elements_bigger_than_map_range(self):
        self._test_percentile_op(
            original_inp=[[1, 5], [3, 4]],
            value_to_pct_map=[[1, 0.1], [4, 0.4], [2, 0.1], [3, 0.3]],
            dist_lengths=[2, 2],
            expected_values=[[0.1, 1.], [0.3, 1.0]]
        )

    def test_percentile_op_with_elements_smaller_than_map_range(self):
        self._test_percentile_op(
            original_inp=[[1], [5], [6]],
            value_to_pct_map=[[2, 0.2], [5, 0.5], [7, 0.5]],
            dist_lengths=[3],
            expected_values=[[0.0], [0.5], [0.5]]
        )

    def test_percentile_op_with_interpolation(self):
        self._test_percentile_op(
            original_inp=[[3, 2, 5], [6, 7, 8]],
            value_to_pct_map=[[1, 0.1], [4, 0.7], [4.5, 0.8],
                              [6, 0.5], [8, 0.9],
                              [8, 0.6]],
            dist_lengths=[3, 2, 1],
            expected_values=[[0.5, 0.0, 0.0], [1.0, 0.7, 0.6]]
        )

    def test_percentile_op_with_large_sample_size_per_dist(self):
        self._test_percentile_op(
            original_inp=[[3, 1], [5, 7]],
            value_to_pct_map=[[3, 0.5], [4, 0.6], [5, 0.7],
                              [1, 0.2], [2, 0.3], [5, 0.8]],
            dist_lengths=[3, 3],
            expected_values=[[0.5, 0.2], [0.7, 1.0]]
        )


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