File: sparse_gradient_checker_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 (47 lines) | stat: -rw-r--r-- 1,294 bytes parent folder | download | duplicates (2)
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





import numpy as np
from scipy.sparse import coo_matrix

from hypothesis import given, settings
import hypothesis.strategies as st

from caffe2.python import core
import caffe2.python.hypothesis_test_util as hu


class TestSparseGradient(hu.HypothesisTestCase):
    @given(M=st.integers(min_value=5, max_value=20),
           N=st.integers(min_value=5, max_value=20),
           K=st.integers(min_value=5, max_value=15),
           sparsity=st.floats(min_value=0.1, max_value=1.0),
           **hu.gcs_cpu_only)
    @settings(deadline=10000)
    def test_sparse_gradient(self, M, N, K, sparsity, gc, dc):
        X = np.random.randn(M, K).astype(np.float32)
        X[X > sparsity] = 0
        X_coo = coo_matrix(X)
        val, key, seg = X_coo.data, X_coo.col, X_coo.row

        val = val.astype(np.float32)
        key = key.astype(np.int64)
        seg = seg.astype(np.int32)

        Y = np.random.randn(K, N).astype(np.float32)

        op = core.CreateOperator(
            'SparseUnsortedSegmentWeightedSum',
            ['Y', 'val', 'key', 'seg'],
            ['out'],
            num_segments=M)

        # Gradient check wrt Y
        self.assertGradientChecks(
            gc, op, [Y, val, key, seg], 0, [0])

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