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
from hypothesis import given, settings
import caffe2.python.hypothesis_test_util as hu
import caffe2.python.serialized_test.serialized_test_util as serial
import hypothesis.strategies as st
import numpy as np
class TestLengthSplitOperator(serial.SerializedTestCase):
def _length_split_op_ref(self, input_lengths, n_split_array):
output = []
n_split = n_split_array[0]
for x in input_lengths:
mod = x % n_split
val = x // n_split + 1
for _ in range(n_split):
if mod > 0:
output.append(val)
mod -= 1
else:
output.append(val - 1)
return [np.array(output).astype(np.int32)]
@given(**hu.gcs_cpu_only)
@settings(deadline=10000)
def test_length_split_edge(self, gc, dc):
input_lengths = np.array([3, 4, 5]).astype(np.int32)
n_split_ = np.array([5]).astype(np.int32)
# Expected output:
# [1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1]
op = core.CreateOperator(
'LengthsSplit',
['input_lengths',
'n_split'],
['Y'],
)
# Check against numpy reference
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[input_lengths,
n_split_],
reference=self._length_split_op_ref,
)
# Check over multiple devices
self.assertDeviceChecks(dc, op, [input_lengths, n_split_], [0])
@given(**hu.gcs_cpu_only)
@settings(deadline=10000)
def test_length_split_arg(self, gc, dc):
input_lengths = np.array([9, 4, 5]).astype(np.int32)
n_split = 3
# Expected output:
# [3, 3, 3, 2, 1, 1, 2, 2, 1]
op = core.CreateOperator(
'LengthsSplit',
['input_lengths'],
['Y'], n_split=n_split
)
# Check against numpy reference
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[input_lengths],
reference=lambda x : self._length_split_op_ref(x, [n_split]),
)
# Check over multiple devices
self.assertDeviceChecks(dc, op, [input_lengths], [0])
@given(**hu.gcs_cpu_only)
@settings(deadline=10000)
def test_length_split_override_arg(self, gc, dc):
input_lengths = np.array([9, 4, 5]).astype(np.int32)
n_split_ignored = 2
n_split_used = np.array([3]).astype(np.int32)
op = core.CreateOperator(
'LengthsSplit',
['input_lengths',
'n_split'],
['Y'], n_split=n_split_ignored
)
# Check against numpy reference
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[input_lengths,
n_split_used],
reference=self._length_split_op_ref,
)
# Check over multiple devices
self.assertDeviceChecks(dc, op, [input_lengths, n_split_used], [0])
@given(m=st.integers(1, 100), n_split=st.integers(1, 20),
**hu.gcs_cpu_only)
@settings(deadline=10000)
def test_length_split_even_divide(self, m, n_split, gc, dc):
# multiples of n_split
input_lengths = np.random.randint(100, size=m).astype(np.int32) * n_split
n_split_ = np.array([n_split]).astype(np.int32)
op = core.CreateOperator(
'LengthsSplit',
['input_lengths',
'n_split'],
['Y'],
)
# Check against numpy reference
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[input_lengths,
n_split_],
reference=self._length_split_op_ref,
)
# Check over multiple devices
self.assertDeviceChecks(dc, op, [input_lengths, n_split_], [0])
@given(m=st.integers(1, 100), n_split=st.integers(1, 20),
**hu.gcs_cpu_only)
@settings(deadline=10000)
def test_length_split_random(self, m, n_split, gc, dc):
input_lengths = np.random.randint(100, size=m).astype(np.int32)
n_split_ = np.array([n_split]).astype(np.int32)
op = core.CreateOperator(
'LengthsSplit',
['input_lengths',
'n_split'],
['Y'],
)
# Check against numpy reference
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[input_lengths,
n_split_],
reference=self._length_split_op_ref,
)
# Check over multiple devices
self.assertDeviceChecks(dc, op, [input_lengths, n_split_], [0])
if __name__ == "__main__":
import unittest
unittest.main()
|