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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
import unittest
from caffe2.proto import caffe2_pb2
from caffe2.python import core
import caffe2.python.hip_test_util as hiputl
import caffe2.python.hypothesis_test_util as hu
import hypothesis.strategies as st
import numpy as np
from hypothesis import given, assume, settings
class TestSpecializedSegmentOps(hu.HypothesisTestCase):
@given(
batchsize=st.integers(1, 20),
fptype=st.sampled_from([np.float16, np.float32]),
fp16asint=st.booleans(),
blocksize=st.sampled_from([8, 16, 32, 64, 85, 96, 128, 163]),
normalize_by_lengths=st.booleans(),
empty_indices=st.booleans(),
**hu.gcs
)
def test_sparse_lengths_sum_cpu(
self,
batchsize,
fptype,
fp16asint,
blocksize,
normalize_by_lengths,
empty_indices,
gc,
dc,
):
if fptype != np.float32:
assume(gc.device_type == caffe2_pb2.CPU)
assume(not hiputl.run_in_hip(gc, dc))
assume(caffe2_pb2.CUDA not in {d.device_type for d in dc})
if normalize_by_lengths:
print("<test_sparse_lengths_sum_mean_cpu>")
else:
print("<test_sparse_lengths_sum_cpu>")
tblsize = 300
if fptype == np.float32:
Tbl = np.random.rand(tblsize, blocksize).astype(np.float32)
atol = 1e-5
else:
if fp16asint:
Tbl = (
(10.0 * np.random.rand(tblsize, blocksize))
.round()
.astype(np.float16)
)
atol = 1e-3
else:
Tbl = np.random.rand(tblsize, blocksize).astype(np.float16)
atol = 1e-1
# array of each row length
if empty_indices:
Lengths = np.zeros(batchsize, dtype=np.int32)
else:
Lengths = np.random.randint(1, 30, size=batchsize, dtype=np.int32)
# flat indices
Indices = np.random.randint(0, tblsize, size=sum(Lengths), dtype=np.int64)
op = core.CreateOperator(
"SparseLengths" + ("Mean" if normalize_by_lengths else "Sum"),
["Tbl", "Indices", "Lengths"],
"out",
)
def sparse_lengths_sum_ref(Tbl, Indices, Lengths):
rptr = np.cumsum(np.insert(Lengths, [0], [0]))
out = np.zeros((len(Lengths), blocksize))
if normalize_by_lengths:
for i in range(0, len(rptr[0:-1])):
if Lengths[i] != 0:
out[i] = (
Tbl[Indices[rptr[i] : rptr[i + 1]]].sum(axis=0)
* 1.0
/ float(Lengths[i])
)
else:
for i in range(0, len(rptr[0:-1])):
out[i] = Tbl[Indices[rptr[i] : rptr[i + 1]]].sum(axis=0)
return [out.astype(np.float32)]
self.assertReferenceChecks(
gc,
op,
[Tbl, Indices, Lengths],
sparse_lengths_sum_ref,
threshold=1e-3,
atol=atol,
)
@given(
batchsize=st.integers(1, 20),
fptype=st.sampled_from([np.float16, np.float32]),
fp16asint=st.booleans(),
blocksize=st.sampled_from([8, 16, 32, 64, 85, 96, 128, 163]),
empty_indices=st.booleans(),
**hu.gcs
)
def test_sparse_lengths_weightedsum_cpu(
self, batchsize, fptype, fp16asint, blocksize, empty_indices, gc, dc
):
if fptype != np.float32:
assume(gc.device_type == caffe2_pb2.CPU)
assume(not hiputl.run_in_hip(gc, dc))
assume(caffe2_pb2.CUDA not in {d.device_type for d in dc})
print("<test_sparse_lengths_weightedsum_cpu>")
tblsize = 300
if fptype == np.float32:
Tbl = np.random.rand(tblsize, blocksize).astype(np.float32)
atol = 1e-5
else:
if fp16asint:
Tbl = (
(10.0 * np.random.rand(tblsize, blocksize))
.round()
.astype(np.float16)
)
atol = 1e-3
else:
Tbl = np.random.rand(tblsize, blocksize).astype(np.float16)
atol = 1e-1
# array of each row length
if empty_indices:
Lengths = np.zeros(batchsize, dtype=np.int32)
else:
Lengths = np.random.randint(1, 30, size=batchsize, dtype=np.int32)
# flat indices
Indices = np.random.randint(0, tblsize, size=sum(Lengths), dtype=np.int64)
Weights = np.random.rand(sum(Lengths)).astype(np.float32)
op = core.CreateOperator(
"SparseLengthsWeightedSum", ["Tbl", "Weights", "Indices", "Lengths"], "out"
)
def sparse_lengths_weightedsum_ref(Tbl, Weights, Indices, Lengths):
rptr = np.cumsum(np.insert(Lengths, [0], [0]))
out = np.zeros((len(Lengths), blocksize))
for i in range(0, len(rptr[0:-1])):
w = Weights[rptr[i] : rptr[i + 1]]
out[i] = (Tbl[Indices[rptr[i] : rptr[i + 1]]] * w[:, np.newaxis]).sum(
axis=0
)
return [out.astype(np.float32)]
self.assertReferenceChecks(
gc,
op,
[Tbl, Weights, Indices, Lengths],
sparse_lengths_weightedsum_ref,
threshold=1e-3,
atol=atol,
)
@given(
batchsize=st.integers(1, 20),
blocksize=st.sampled_from([8, 16, 17, 26, 32, 64, 85, 96, 128, 148, 163]),
normalize_by_lengths=st.booleans(),
empty_indices=st.booleans(),
**hu.gcs_cpu_only
)
def test_sparse_lengths_weightedsum_8BitsRowwiseOp_cpu(
self, batchsize, blocksize, normalize_by_lengths, empty_indices, gc, dc
):
if normalize_by_lengths:
print(
"<test_sparse_lengths_weightedsum_SparseLengthsWeightedMean8BitsRowwise_cpu>"
)
else:
print(
"<test_sparse_lengths_weightedsum_SparseLengthsWeightedSum8BitsRowwise_cpu>"
)
tblsize = 300
Tbl = np.random.randint(7, size=(tblsize, blocksize), dtype=np.uint8)
atol = 1e-5
# array of each row length
if empty_indices:
Lengths = np.zeros(batchsize, dtype=np.int32)
else:
Lengths = np.random.randint(1, 30, size=batchsize, dtype=np.int32)
# flat indices
Indices = np.random.randint(0, tblsize, size=sum(Lengths), dtype=np.int64)
Weights = np.random.rand(sum(Lengths)).astype(np.float32)
Scale_Bias = np.random.rand(tblsize, 2).astype(np.float32)
op = core.CreateOperator(
"SparseLengthsWeighted"
+ ("Mean" if normalize_by_lengths else "Sum")
+ "8BitsRowwise",
["Tbl", "Weights", "Indices", "Lengths", "Scale_Bias"],
"out",
)
def sparse_lengths_weightedsum_8BitsRowwiseOp_cpu_ref(
Tbl, Weights, Indices, Lengths, Scale_Bias
):
rptr = np.cumsum(np.insert(Lengths, [0], [0]))
out = np.zeros((len(Lengths), blocksize))
for i in range(0, len(rptr[0:-1])):
w = Weights[rptr[i] : rptr[i + 1]]
s = Scale_Bias[Indices[rptr[i] : rptr[i + 1]], 0][:, np.newaxis]
b = Scale_Bias[Indices[rptr[i] : rptr[i + 1]], 1][:, np.newaxis]
f = 1.0
if normalize_by_lengths and Lengths[i] != 0:
f = 1.0 / float(Lengths[i])
out[i] = (
w[:, np.newaxis] * (s * Tbl[Indices[rptr[i] : rptr[i + 1]]] + b)
).sum(axis=0) * f
return [out.astype(np.float32)]
self.assertReferenceChecks(
gc,
op,
[Tbl, Weights, Indices, Lengths, Scale_Bias],
sparse_lengths_weightedsum_8BitsRowwiseOp_cpu_ref,
threshold=1e-3,
atol=atol,
)
@given(
batchsize=st.integers(1, 20),
blocksize=st.sampled_from([8, 16, 17, 26, 32, 64, 85, 96, 128, 148, 163]),
normalize_by_lengths=st.booleans(),
empty_indices=st.booleans(),
**hu.gcs_cpu_only
)
def test_sparse_lengths_sum_8BitsRowwiseOp_cpu(
self, batchsize, blocksize, normalize_by_lengths, empty_indices, gc, dc
):
if normalize_by_lengths:
print("<test_sparse_lengths_sum_SparseLengthsMean8BitsRowwise_cpu>")
else:
print("<test_sparse_lengths_sum_SparseLengthsSum8BitsRowwise_cpu>")
tblsize = 300
Tbl = np.random.randint(7, size=(tblsize, blocksize), dtype=np.uint8)
atol = 1e-5
# array of each row length
if empty_indices:
Lengths = np.zeros(batchsize, dtype=np.int32)
else:
Lengths = np.random.randint(1, 30, size=batchsize, dtype=np.int32)
# flat indices
Indices = np.random.randint(0, tblsize, size=sum(Lengths), dtype=np.int64)
Scale_Bias = np.random.rand(tblsize, 2).astype(np.float32)
op = core.CreateOperator(
"SparseLengths"
+ ("Mean" if normalize_by_lengths else "Sum")
+ "8BitsRowwise",
["Tbl", "Indices", "Lengths", "Scale_Bias"],
"out",
)
def sparse_lengths_sum_8BitsRowwiseOp_cpu_reg(
Tbl, Indices, Lengths, Scale_Bias
):
rptr = np.cumsum(np.insert(Lengths, [0], [0]))
out = np.zeros((len(Lengths), blocksize))
for i in range(0, len(rptr[0:-1])):
s = Scale_Bias[Indices[rptr[i] : rptr[i + 1]], 0][:, np.newaxis]
b = Scale_Bias[Indices[rptr[i] : rptr[i + 1]], 1][:, np.newaxis]
f = 1.0
if normalize_by_lengths and Lengths[i] != 0:
f = 1.0 / float(Lengths[i])
out[i] = (s * Tbl[Indices[rptr[i] : rptr[i + 1]]] + b).sum(axis=0) * f
return [out.astype(np.float32)]
self.assertReferenceChecks(
gc,
op,
[Tbl, Indices, Lengths, Scale_Bias],
sparse_lengths_sum_8BitsRowwiseOp_cpu_reg,
threshold=1e-3,
atol=atol,
)
@given(
batchsize=st.integers(1, 20),
blocksize=st.sampled_from([8, 16, 17, 26, 32, 64, 85, 96, 128, 148, 163]),
normalize_by_lengths=st.booleans(),
**hu.gcs_cpu_only
)
@settings(deadline=10000)
def test_sparse_lengths_sum_8BitsRowwiseOp_cpu_invalid_index(
self, batchsize, blocksize, normalize_by_lengths, gc, dc
):
tblsize = 300
Tbl = np.random.randint(7, size=(tblsize, blocksize), dtype=np.uint8)
# array of each row length
Lengths = np.random.randint(1, 30, size=batchsize, dtype=np.int32)
# flat indices
Indices = np.random.randint(0, tblsize, size=sum(Lengths), dtype=np.int64)
Indices[0] += 1000
Scale_Bias = np.random.rand(tblsize, 2).astype(np.float32)
op = core.CreateOperator(
"SparseLengths"
+ ("Mean" if normalize_by_lengths else "Sum")
+ "8BitsRowwise",
["Tbl", "Indices", "Lengths", "Scale_Bias"],
"out",
)
self.ws.create_blob("Tbl").feed(Tbl)
self.ws.create_blob("Indices").feed(Indices)
self.ws.create_blob("Lengths").feed(Lengths)
self.ws.create_blob("Scale_Bias").feed(Scale_Bias)
with self.assertRaises(RuntimeError):
self.ws.run(op)
if __name__ == "__main__":
unittest.main()
|