forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse_to_dense_mask_test.py
157 lines (140 loc) · 6.44 KB
/
sparse_to_dense_mask_test.py
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
from caffe2.python import core, workspace
from caffe2.python.test_util import TestCase
import numpy as np
class TestSparseToDenseMask(TestCase):
def test_sparse_to_dense_mask_float(self):
op = core.CreateOperator(
'SparseToDenseMask',
['indices', 'values', 'default', 'lengths'],
['output'],
mask=[999999999, 2, 6])
workspace.FeedBlob(
'indices',
np.array([2, 4, 6, 1, 2, 999999999, 2], dtype=np.int32))
workspace.FeedBlob(
'values',
np.array([1, 2, 3, 4, 5, 6, 7], dtype=np.float64))
workspace.FeedBlob('default', np.array(-1, dtype=np.float64))
workspace.FeedBlob('lengths', np.array([3, 4], dtype=np.int32))
workspace.RunOperatorOnce(op)
output = workspace.FetchBlob('output')
expected = np.array([[-1, 1, 3], [6, 7, -1]], dtype=np.float64)
self.assertEqual(output.shape, expected.shape)
np.testing.assert_array_equal(output, expected)
def test_sparse_to_dense_mask_invalid_inputs(self):
op = core.CreateOperator(
'SparseToDenseMask',
['indices', 'values', 'default', 'lengths'],
['output'],
mask=[999999999, 2],
max_skipped_indices=3)
workspace.FeedBlob(
'indices',
np.array([2000000000000, 999999999, 2, 3, 4, 5], dtype=np.int32))
workspace.FeedBlob(
'values',
np.array([1, 2, 3, 4, 5, 6], dtype=np.float64))
workspace.FeedBlob('default', np.array(-1, dtype=np.float64))
workspace.FeedBlob('lengths', np.array([6], dtype=np.int32))
try:
workspace.RunOperatorOnce(op)
except RuntimeError:
self.fail("Exception raised with only one negative index")
# 3 invalid inputs should throw.
workspace.FeedBlob(
'indices',
np.array([-1, 1, 2, 3, 4, 5], dtype=np.int32))
with self.assertRaises(RuntimeError):
workspace.RunOperatorMultiple(op, 3)
def test_sparse_to_dense_mask_subtensor(self):
op = core.CreateOperator(
'SparseToDenseMask',
['indices', 'values', 'default', 'lengths'],
['output'],
mask=[999999999, 2, 888, 6])
workspace.FeedBlob(
'indices',
np.array([2, 4, 6, 999999999, 2], dtype=np.int64))
workspace.FeedBlob(
'values',
np.array([[[1, -1]], [[2, -2]], [[3, -3]], [[4, -4]], [[5, -5]]],
dtype=np.float64))
workspace.FeedBlob('default', np.array([[-1, 0]], dtype=np.float64))
workspace.FeedBlob('lengths', np.array([2, 3], dtype=np.int32))
workspace.RunOperatorOnce(op)
output = workspace.FetchBlob('output')
expected = np.array([
[[[-1, 0]], [[1, -1]], [[-1, 0]], [[-1, 0]]],
[[[4, -4]], [[5, -5]], [[-1, 0]], [[3, -3]]]], dtype=np.float64)
self.assertEqual(output.shape, expected.shape)
np.testing.assert_array_equal(output, expected)
def test_sparse_to_dense_mask_string(self):
op = core.CreateOperator(
'SparseToDenseMask',
['indices', 'values', 'default', 'lengths'],
['output'],
mask=[999999999, 2, 6])
workspace.FeedBlob(
'indices',
np.array([2, 4, 6, 1, 2, 999999999, 2], dtype=np.int32))
workspace.FeedBlob(
'values',
np.array(['1', '2', '3', '4', '5', '6', '7'], dtype='S'))
workspace.FeedBlob('default', np.array('-1', dtype='S'))
workspace.FeedBlob('lengths', np.array([3, 4], dtype=np.int32))
workspace.RunOperatorOnce(op)
output = workspace.FetchBlob('output')
expected =\
np.array([['-1', '1', '3'], ['6', '7', '-1']], dtype='S')
self.assertEqual(output.shape, expected.shape)
np.testing.assert_array_equal(output, expected)
def test_sparse_to_dense_mask_empty_lengths(self):
op = core.CreateOperator(
'SparseToDenseMask',
['indices', 'values', 'default'],
['output'],
mask=[1, 2, 6])
workspace.FeedBlob('indices', np.array([2, 4, 6], dtype=np.int32))
workspace.FeedBlob('values', np.array([1, 2, 3], dtype=np.float64))
workspace.FeedBlob('default', np.array(-1, dtype=np.float64))
workspace.RunOperatorOnce(op)
output = workspace.FetchBlob('output')
expected = np.array([-1, 1, 3], dtype=np.float64)
self.assertEqual(output.shape, expected.shape)
np.testing.assert_array_equal(output, expected)
def test_sparse_to_dense_mask_no_lengths(self):
op = core.CreateOperator(
'SparseToDenseMask',
['indices', 'values', 'default'],
['output'],
mask=[1, 2, 6])
workspace.FeedBlob('indices', np.array([2, 4, 6], dtype=np.int32))
workspace.FeedBlob('values', np.array([1, 2, 3], dtype=np.float64))
workspace.FeedBlob('default', np.array(-1, dtype=np.float64))
workspace.RunOperatorOnce(op)
output = workspace.FetchBlob('output')
expected = np.array([-1, 1, 3], dtype=np.float64)
self.assertEqual(output.shape, expected.shape)
np.testing.assert_array_equal(output, expected)
def test_sparse_to_dense_mask_presence_mask(self):
op = core.CreateOperator(
'SparseToDenseMask',
['indices', 'values', 'default', 'lengths'],
['output', 'presence_mask'],
mask=[11, 12],
return_presence_mask=True)
workspace.FeedBlob('indices', np.array([11, 12, 13], dtype=np.int32))
workspace.FeedBlob('values', np.array([11, 12, 13], dtype=np.float64))
workspace.FeedBlob('default', np.array(-1, dtype=np.float64))
workspace.FeedBlob('lengths', np.array([1, 2], dtype=np.int32))
workspace.RunOperatorOnce(op)
output = workspace.FetchBlob('output')
presence_mask = workspace.FetchBlob('presence_mask')
expected_output = np.array([[11, -1], [-1, 12]], dtype=np.float64)
expected_presence_mask = np.array(
[[True, False], [False, True]],
dtype=bool)
self.assertEqual(output.shape, expected_output.shape)
np.testing.assert_array_equal(output, expected_output)
self.assertEqual(presence_mask.shape, expected_presence_mask.shape)
np.testing.assert_array_equal(presence_mask, expected_presence_mask)