| 1 |
""" Tests that check if JIT-compiled numpy operations produce reasonably |
| 2 |
good assembler |
| 3 |
""" |
| 4 |
|
| 5 |
import py |
| 6 |
from rpython.jit.metainterp.test.support import LLJitMixin |
| 7 |
from rpython.jit.backend.x86.test.test_basic import Jit386Mixin |
| 8 |
from rpython.jit.metainterp.warmspot import reset_jit, get_stats |
| 9 |
from rpython.jit.metainterp.jitprof import Profiler |
| 10 |
from rpython.jit.metainterp import counter |
| 11 |
from rpython.rlib.jit import Counters |
| 12 |
from rpython.rlib.rarithmetic import intmask |
| 13 |
from pypy.module.micronumpy import boxes |
| 14 |
from pypy.module.micronumpy.compile import FakeSpace, Parser, InterpreterState |
| 15 |
from pypy.module.micronumpy.base import W_NDimArray |
| 16 |
from rpython.jit.backend.detect_cpu import getcpuclass |
| 17 |
|
| 18 |
CPU = getcpuclass() |
| 19 |
if not CPU.vector_ext: |
| 20 |
py.test.skip("this cpu %s has no implemented vector backend" % CPU) |
| 21 |
|
| 22 |
def get_profiler(): |
| 23 |
from rpython.jit.metainterp import pyjitpl |
| 24 |
return pyjitpl._warmrunnerdesc.metainterp_sd.profiler |
| 25 |
|
| 26 |
class TestNumpyJit(LLJitMixin): |
| 27 |
enable_opts = "intbounds:rewrite:virtualize:string:earlyforce:pure:heap:unroll" |
| 28 |
graph = None |
| 29 |
interp = None |
| 30 |
|
| 31 |
def setup_method(self, method): |
| 32 |
if not self.CPUClass.vector_ext: |
| 33 |
py.test.skip("needs vector extension to run (for now)") |
| 34 |
|
| 35 |
def assert_float_equal(self, f1, f2, delta=0.0001): |
| 36 |
assert abs(f1-f2) < delta |
| 37 |
|
| 38 |
def setup_class(cls): |
| 39 |
default = """ |
| 40 |
a = [1,2,3,4] |
| 41 |
z = (1, 2) |
| 42 |
c = a + b |
| 43 |
sum(c) -> 1::1 |
| 44 |
a -> 3:1:2 |
| 45 |
""" |
| 46 |
|
| 47 |
d = {} |
| 48 |
p = Parser() |
| 49 |
allcodes = [p.parse(default)] |
| 50 |
for name, meth in cls.__dict__.iteritems(): |
| 51 |
if name.startswith("define_"): |
| 52 |
code = meth() |
| 53 |
d[name[len("define_"):]] = len(allcodes) |
| 54 |
allcodes.append(p.parse(code)) |
| 55 |
cls.code_mapping = d |
| 56 |
cls.codes = allcodes |
| 57 |
|
| 58 |
def compile_graph(self): |
| 59 |
if self.graph is not None: |
| 60 |
return |
| 61 |
space = FakeSpace() |
| 62 |
codes = self.codes |
| 63 |
|
| 64 |
def f(i): |
| 65 |
interp = InterpreterState(codes[i]) |
| 66 |
interp.run(space) |
| 67 |
if not len(interp.results): |
| 68 |
raise Exception("need results") |
| 69 |
w_res = interp.results[-1] |
| 70 |
if isinstance(w_res, W_NDimArray): |
| 71 |
i, s = w_res.create_iter() |
| 72 |
w_res = i.getitem(s) |
| 73 |
if isinstance(w_res, boxes.W_Float64Box): |
| 74 |
return w_res.value |
| 75 |
if isinstance(w_res, boxes.W_Float32Box): |
| 76 |
return float(w_res.value) |
| 77 |
elif isinstance(w_res, boxes.W_Int64Box): |
| 78 |
return float(w_res.value) |
| 79 |
elif isinstance(w_res, boxes.W_Int32Box): |
| 80 |
return float(int(w_res.value)) |
| 81 |
elif isinstance(w_res, boxes.W_Int16Box): |
| 82 |
return float(int(w_res.value)) |
| 83 |
elif isinstance(w_res, boxes.W_Int8Box): |
| 84 |
return float(int(w_res.value)) |
| 85 |
elif isinstance(w_res, boxes.W_UInt64Box): |
| 86 |
return float(intmask(w_res.value)) |
| 87 |
elif isinstance(w_res, boxes.W_UInt32Box): |
| 88 |
return float(intmask(w_res.value)) |
| 89 |
elif isinstance(w_res, boxes.W_UInt16Box): |
| 90 |
return float(intmask(w_res.value)) |
| 91 |
elif isinstance(w_res, boxes.W_UInt8Box): |
| 92 |
return float(intmask(w_res.value)) |
| 93 |
elif isinstance(w_res, boxes.W_LongBox): |
| 94 |
return float(w_res.value) |
| 95 |
elif isinstance(w_res, boxes.W_BoolBox): |
| 96 |
return float(w_res.value) |
| 97 |
print "ERROR: did not implement return type for interpreter" |
| 98 |
raise TypeError(w_res) |
| 99 |
|
| 100 |
if self.graph is None: |
| 101 |
interp, graph = self.meta_interp(f, [0], |
| 102 |
listops=True, |
| 103 |
listcomp=True, |
| 104 |
backendopt=True, |
| 105 |
graph_and_interp_only=True, |
| 106 |
ProfilerClass=Profiler, |
| 107 |
vec=True) |
| 108 |
self.__class__.interp = interp |
| 109 |
self.__class__.graph = graph |
| 110 |
|
| 111 |
def check_vectorized(self, expected_tried, expected_success): |
| 112 |
profiler = get_profiler() |
| 113 |
tried = profiler.get_counter(Counters.OPT_VECTORIZE_TRY) |
| 114 |
success = profiler.get_counter(Counters.OPT_VECTORIZED) |
| 115 |
assert tried >= success |
| 116 |
assert tried == expected_tried |
| 117 |
assert success == expected_success |
| 118 |
|
| 119 |
def run(self, name): |
| 120 |
self.compile_graph() |
| 121 |
profiler = get_profiler() |
| 122 |
profiler.start() |
| 123 |
reset_jit() |
| 124 |
i = self.code_mapping[name] |
| 125 |
retval = self.interp.eval_graph(self.graph, [i]) |
| 126 |
return retval |
| 127 |
|
| 128 |
def define_float32_copy(): |
| 129 |
return """ |
| 130 |
a = astype(|30|, float32) |
| 131 |
x1 = a -> 7 |
| 132 |
x2 = a -> 8 |
| 133 |
x3 = a -> 9 |
| 134 |
x4 = a -> 10 |
| 135 |
r = x1 + x2 + x3 + x4 |
| 136 |
r |
| 137 |
""" |
| 138 |
def test_float32_copy(self): |
| 139 |
result = self.run("float32_copy") |
| 140 |
assert int(result) == 7+8+9+10 |
| 141 |
self.check_vectorized(1, 1) |
| 142 |
|
| 143 |
def define_int32_copy(): |
| 144 |
return """ |
| 145 |
a = astype(|30|, int32) |
| 146 |
x1 = a -> 7 |
| 147 |
x2 = a -> 8 |
| 148 |
x3 = a -> 9 |
| 149 |
x4 = a -> 10 |
| 150 |
x1 + x2 + x3 + x4 |
| 151 |
""" |
| 152 |
def test_int32_copy(self): |
| 153 |
result = self.run("int32_copy") |
| 154 |
assert int(result) == 7+8+9+10 |
| 155 |
self.check_vectorized(1, 1) |
| 156 |
|
| 157 |
def define_float32_add(): |
| 158 |
return """ |
| 159 |
a = astype(|30|, float32) |
| 160 |
b = a + a |
| 161 |
b -> 15 |
| 162 |
""" |
| 163 |
def test_float32_add(self): |
| 164 |
result = self.run("float32_add") |
| 165 |
self.assert_float_equal(result, 15.0 + 15.0) |
| 166 |
self.check_vectorized(2, 2) |
| 167 |
|
| 168 |
def define_float_add(): |
| 169 |
return """ |
| 170 |
a = |30| |
| 171 |
b = a + a |
| 172 |
b -> 17 |
| 173 |
""" |
| 174 |
def test_float_add(self): |
| 175 |
result = self.run("float_add") |
| 176 |
self.assert_float_equal(result, 17.0 + 17.0) |
| 177 |
self.check_vectorized(1, 1) |
| 178 |
|
| 179 |
def define_uint_add(): |
| 180 |
return """ |
| 181 |
a = astype(|30|, uint64) |
| 182 |
b = a + a |
| 183 |
b -> 17 |
| 184 |
""" |
| 185 |
def test_uint_add(self): |
| 186 |
result = self.run("uint_add") |
| 187 |
assert int(result) == 17+17 |
| 188 |
self.check_vectorized(2, 1) |
| 189 |
|
| 190 |
def define_float32_add_const(): |
| 191 |
return """ |
| 192 |
a = astype(|30|, float32) |
| 193 |
b = a + 77.345 |
| 194 |
b -> 29 |
| 195 |
""" |
| 196 |
def test_float32_add_const(self): |
| 197 |
result = self.run("float32_add_const") |
| 198 |
self.assert_float_equal(result, 29.0 + 77.345) |
| 199 |
self.check_vectorized(2, 2) |
| 200 |
|
| 201 |
def define_float_add_const(): |
| 202 |
return """ |
| 203 |
a = |30| + 25.5 |
| 204 |
a -> 29 |
| 205 |
""" |
| 206 |
def test_float_add_const(self): |
| 207 |
result = self.run("float_add_const") |
| 208 |
self.assert_float_equal(result, 29.0 + 25.5) |
| 209 |
self.check_vectorized(1, 1) |
| 210 |
|
| 211 |
def define_int_add_const(): |
| 212 |
return """ |
| 213 |
a = astype(|30|, int) |
| 214 |
b = a + 1i |
| 215 |
d = astype(|30|, int) |
| 216 |
c = d + 2.0 |
| 217 |
x1 = b -> 7 |
| 218 |
x2 = b -> 8 |
| 219 |
x3 = c -> 11 |
| 220 |
x4 = c -> 12 |
| 221 |
x1 + x2 + x3 + x4 |
| 222 |
""" |
| 223 |
def test_int_add_const(self): |
| 224 |
result = self.run("int_add_const") |
| 225 |
assert int(result) == 7+1+8+1+11+2+12+2 |
| 226 |
self.check_vectorized(2, 2) |
| 227 |
|
| 228 |
def define_int_expand(): |
| 229 |
return """ |
| 230 |
a = astype(|30|, int) |
| 231 |
c = astype(|1|, int) |
| 232 |
c[0] = 16 |
| 233 |
b = a + c |
| 234 |
x1 = b -> 7 |
| 235 |
x2 = b -> 8 |
| 236 |
x1 + x2 |
| 237 |
""" |
| 238 |
def test_int_expand(self): |
| 239 |
result = self.run("int_expand") |
| 240 |
assert int(result) == 7+16+8+16 |
| 241 |
self.check_vectorized(2, 2) |
| 242 |
|
| 243 |
def define_int32_expand(): |
| 244 |
return """ |
| 245 |
a = astype(|30|, int32) |
| 246 |
c = astype(|1|, int32) |
| 247 |
c[0] = 16i |
| 248 |
b = a + c |
| 249 |
x1 = b -> 7 |
| 250 |
x2 = b -> 8 |
| 251 |
x1 + x2 |
| 252 |
""" |
| 253 |
def test_int32_expand(self): |
| 254 |
result = self.run("int32_expand") |
| 255 |
assert int(result) == 7+16+8+16 |
| 256 |
self.check_vectorized(2, 1) |
| 257 |
|
| 258 |
def define_int16_expand(): |
| 259 |
return """ |
| 260 |
a = astype(|30|, int16) |
| 261 |
c = astype(|1|, int16) |
| 262 |
c[0] = 16i |
| 263 |
b = a + c |
| 264 |
d = b -> 7:15 |
| 265 |
sum(d) |
| 266 |
""" |
| 267 |
def test_int16_expand(self): |
| 268 |
result = self.run("int16_expand") |
| 269 |
i = 8 |
| 270 |
assert int(result) == i*16 + sum(range(7,7+i)) |
| 271 |
|
| 272 |
self.check_vectorized(3, 0) |
| 273 |
|
| 274 |
def define_int8_expand(): |
| 275 |
return """ |
| 276 |
a = astype(|30|, int8) |
| 277 |
c = astype(|1|, int8) |
| 278 |
c[0] = 8i |
| 279 |
b = a + c |
| 280 |
d = b -> 0:17 |
| 281 |
sum(d) |
| 282 |
""" |
| 283 |
def test_int8_expand(self): |
| 284 |
result = self.run("int8_expand") |
| 285 |
assert int(result) == 17*8 + sum(range(0,17)) |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
self.check_vectorized(3, 0) |
| 291 |
|
| 292 |
def define_int32_add_const(): |
| 293 |
return """ |
| 294 |
a = astype(|30|, int32) |
| 295 |
b = a + 1i |
| 296 |
d = astype(|30|, int32) |
| 297 |
c = d + 2.0 |
| 298 |
x1 = b -> 7 |
| 299 |
x2 = b -> 8 |
| 300 |
x3 = c -> 11 |
| 301 |
x4 = c -> 12 |
| 302 |
x1 + x2 + x3 + x4 |
| 303 |
""" |
| 304 |
def test_int32_add_const(self): |
| 305 |
result = self.run("int32_add_const") |
| 306 |
assert int(result) == 7+1+8+1+11+2+12+2 |
| 307 |
self.check_vectorized(2, 2) |
| 308 |
|
| 309 |
def define_float_mul_array(): |
| 310 |
return """ |
| 311 |
a = astype(|30|, float) |
| 312 |
b = astype(|30|, float) |
| 313 |
c = a * b |
| 314 |
x1 = c -> 7 |
| 315 |
x2 = c -> 8 |
| 316 |
x3 = c -> 11 |
| 317 |
x4 = c -> 12 |
| 318 |
x1 + x2 + x3 + x4 |
| 319 |
""" |
| 320 |
def test_float_mul_array(self): |
| 321 |
result = self.run("float_mul_array") |
| 322 |
assert int(result) == 7*7+8*8+11*11+12*12 |
| 323 |
self.check_vectorized(2, 2) |
| 324 |
|
| 325 |
def define_int32_mul_array(): |
| 326 |
return """ |
| 327 |
a = astype(|30|, int32) |
| 328 |
b = astype(|30|, int32) |
| 329 |
c = a * b |
| 330 |
x1 = c -> 7 |
| 331 |
x2 = c -> 8 |
| 332 |
x3 = c -> 11 |
| 333 |
x4 = c -> 12 |
| 334 |
x1 + x2 + x3 + x4 |
| 335 |
""" |
| 336 |
def test_int32_mul_array(self): |
| 337 |
result = self.run("int32_mul_array") |
| 338 |
assert int(result) == 7*7+8*8+11*11+12*12 |
| 339 |
self.check_vectorized(2, 2) |
| 340 |
|
| 341 |
def define_float32_mul_array(): |
| 342 |
return """ |
| 343 |
a = astype(|30|, float32) |
| 344 |
b = astype(|30|, float32) |
| 345 |
c = a * b |
| 346 |
x1 = c -> 7 |
| 347 |
x2 = c -> 8 |
| 348 |
x3 = c -> 11 |
| 349 |
x4 = c -> 12 |
| 350 |
x1 + x2 + x3 + x4 |
| 351 |
""" |
| 352 |
def test_float32_mul_array(self): |
| 353 |
result = self.run("float32_mul_array") |
| 354 |
assert int(result) == 7*7+8*8+11*11+12*12 |
| 355 |
self.check_vectorized(2, 2) |
| 356 |
|
| 357 |
def define_conversion(): |
| 358 |
return """ |
| 359 |
a = astype(|30|, int8) |
| 360 |
b = astype(|30|, int) |
| 361 |
c = a + b |
| 362 |
sum(c) |
| 363 |
""" |
| 364 |
def test_conversion(self): |
| 365 |
result = self.run("conversion") |
| 366 |
assert result == sum(range(30)) + sum(range(30)) |
| 367 |
self.check_vectorized(4, 2) |
| 368 |
|
| 369 |
def define_sum(): |
| 370 |
return """ |
| 371 |
a = |30| |
| 372 |
sum(a) |
| 373 |
""" |
| 374 |
def test_sum(self): |
| 375 |
result = self.run("sum") |
| 376 |
assert result == sum(range(30)) |
| 377 |
self.check_vectorized(1, 0) |
| 378 |
|
| 379 |
def define_sum_int(): |
| 380 |
return """ |
| 381 |
a = astype(|65|,int) |
| 382 |
sum(a) |
| 383 |
""" |
| 384 |
def test_sum_int(self): |
| 385 |
result = self.run("sum_int") |
| 386 |
assert result == sum(range(65)) |
| 387 |
self.check_vectorized(2, 2) |
| 388 |
|
| 389 |
def define_sum_multi(): |
| 390 |
return """ |
| 391 |
a = |30| |
| 392 |
b = sum(a) |
| 393 |
c = |60| |
| 394 |
d = sum(c) |
| 395 |
b + d |
| 396 |
""" |
| 397 |
|
| 398 |
def test_sum_multi(self): |
| 399 |
result = self.run("sum_multi") |
| 400 |
assert result == sum(range(30)) + sum(range(60)) |
| 401 |
self.check_vectorized(1, 0) |
| 402 |
|
| 403 |
def define_sum_float_to_int16(): |
| 404 |
return """ |
| 405 |
a = |30| |
| 406 |
sum(a,int16) |
| 407 |
""" |
| 408 |
def test_sum_float_to_int16(self): |
| 409 |
result = self.run("sum_float_to_int16") |
| 410 |
assert result == sum(range(30)) |
| 411 |
|
| 412 |
def define_sum_float_to_int32(): |
| 413 |
return """ |
| 414 |
a = |30| |
| 415 |
sum(a,int32) |
| 416 |
""" |
| 417 |
def test_sum_float_to_int32(self): |
| 418 |
result = self.run("sum_float_to_int32") |
| 419 |
assert result == sum(range(30)) |
| 420 |
|
| 421 |
def define_sum_float_to_float32(): |
| 422 |
return """ |
| 423 |
a = |30| |
| 424 |
sum(a,float32) |
| 425 |
""" |
| 426 |
def test_sum_float_to_float32(self): |
| 427 |
result = self.run("sum_float_to_float32") |
| 428 |
assert result == sum(range(30)) |
| 429 |
self.check_vectorized(1, 1) |
| 430 |
|
| 431 |
def define_sum_float_to_uint64(): |
| 432 |
return """ |
| 433 |
a = |30| |
| 434 |
sum(a,uint64) |
| 435 |
""" |
| 436 |
def test_sum_float_to_uint64(self): |
| 437 |
result = self.run("sum_float_to_uint64") |
| 438 |
assert result == sum(range(30)) |
| 439 |
self.check_vectorized(1, 0) |
| 440 |
|
| 441 |
def define_cumsum(): |
| 442 |
return """ |
| 443 |
a = |30| |
| 444 |
b = cumsum(a) |
| 445 |
b -> 5 |
| 446 |
""" |
| 447 |
|
| 448 |
def test_cumsum(self): |
| 449 |
result = self.run("cumsum") |
| 450 |
assert result == 15 |
| 451 |
|
| 452 |
def define_axissum(): |
| 453 |
return """ |
| 454 |
a = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]] |
| 455 |
b = sum(a,0) |
| 456 |
b -> 1 |
| 457 |
""" |
| 458 |
|
| 459 |
def test_axissum(self): |
| 460 |
result = self.run("axissum") |
| 461 |
assert result == 30 |
| 462 |
|
| 463 |
|
| 464 |
self.check_vectorized(1, 0) |
| 465 |
|
| 466 |
def define_reduce(): |
| 467 |
return """ |
| 468 |
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 469 |
sum(a) |
| 470 |
""" |
| 471 |
|
| 472 |
def test_reduce_compile_only_once(self): |
| 473 |
self.compile_graph() |
| 474 |
reset_jit() |
| 475 |
i = self.code_mapping['reduce'] |
| 476 |
|
| 477 |
retval = self.interp.eval_graph(self.graph, [i]) |
| 478 |
assert retval == sum(range(1,11)) |
| 479 |
retval = self.interp.eval_graph(self.graph, [i]) |
| 480 |
assert retval == sum(range(1,11)) |
| 481 |
|
| 482 |
assert len(get_stats().loops) == 1 |
| 483 |
self.check_vectorized(2, 0) |
| 484 |
|
| 485 |
def test_reduce_axis_compile_only_once(self): |
| 486 |
self.compile_graph() |
| 487 |
reset_jit() |
| 488 |
i = self.code_mapping['axissum'] |
| 489 |
|
| 490 |
retval = self.interp.eval_graph(self.graph, [i]) |
| 491 |
retval = self.interp.eval_graph(self.graph, [i]) |
| 492 |
|
| 493 |
assert len(get_stats().loops) == 1 |
| 494 |
self.check_vectorized(3, 0) |
| 495 |
|
| 496 |
def define_prod(): |
| 497 |
return """ |
| 498 |
a = [1,2,3,4,1,2,3,4] |
| 499 |
prod(a) |
| 500 |
""" |
| 501 |
|
| 502 |
def define_prod_zero(): |
| 503 |
return """ |
| 504 |
a = [1,2,3,4,1,2,3,0] |
| 505 |
prod(a) |
| 506 |
""" |
| 507 |
|
| 508 |
def test_prod(self): |
| 509 |
result = self.run("prod") |
| 510 |
assert int(result) == 576 |
| 511 |
|
| 512 |
def test_prod_zero(self): |
| 513 |
result = self.run("prod_zero") |
| 514 |
assert int(result) == 0 |
| 515 |
|
| 516 |
|
| 517 |
def define_max(): |
| 518 |
return """ |
| 519 |
a = |30| |
| 520 |
a[13] = 128.0 |
| 521 |
max(a) |
| 522 |
""" |
| 523 |
|
| 524 |
def test_max(self): |
| 525 |
result = self.run("max") |
| 526 |
assert result == 128 |
| 527 |
self.check_vectorized(1, 0) |
| 528 |
|
| 529 |
def define_min(): |
| 530 |
return """ |
| 531 |
a = |30| |
| 532 |
a[13] = -128 |
| 533 |
min(a) |
| 534 |
""" |
| 535 |
|
| 536 |
def test_min(self): |
| 537 |
result = self.run("min") |
| 538 |
assert result == -128 |
| 539 |
self.check_vectorized(1, 0) |
| 540 |
|
| 541 |
def define_any(): |
| 542 |
return """ |
| 543 |
a = astype([0,0,0,0,0,0,0,1,0,0,0],int8) |
| 544 |
any(a) |
| 545 |
""" |
| 546 |
|
| 547 |
def define_any_int(): |
| 548 |
return """ |
| 549 |
a = astype([0,0,0,0,256,0,0,0,0,0,0],int16) |
| 550 |
any(a) |
| 551 |
""" |
| 552 |
|
| 553 |
def define_any_ret_0(): |
| 554 |
return """ |
| 555 |
a = astype([0,0,0,0,0,0,0,0,0,0,0],int64) |
| 556 |
any(a) |
| 557 |
""" |
| 558 |
|
| 559 |
def define_float_any(): |
| 560 |
return """ |
| 561 |
a = [0,0,0,0,0,0,0,0.1,0,0,0] |
| 562 |
any(a) |
| 563 |
""" |
| 564 |
|
| 565 |
def define_float32_any(): |
| 566 |
return """ |
| 567 |
a = astype([0,0,0,0,0,0,0,0.1,0,0,0], float32) |
| 568 |
any(a) |
| 569 |
""" |
| 570 |
|
| 571 |
def test_any_float(self): |
| 572 |
result = self.run("float_any") |
| 573 |
assert int(result) == 1 |
| 574 |
self.check_vectorized(1, 1) |
| 575 |
|
| 576 |
def test_any_float32(self): |
| 577 |
result = self.run("float32_any") |
| 578 |
assert int(result) == 1 |
| 579 |
self.check_vectorized(2, 2) |
| 580 |
|
| 581 |
def test_any(self): |
| 582 |
result = self.run("any") |
| 583 |
assert int(result) == 1 |
| 584 |
self.check_vectorized(2, 1) |
| 585 |
|
| 586 |
def test_any_int(self): |
| 587 |
result = self.run("any_int") |
| 588 |
assert int(result) == 1 |
| 589 |
self.check_vectorized(2, 1) |
| 590 |
|
| 591 |
def test_any_ret_0(self): |
| 592 |
result = self.run("any_ret_0") |
| 593 |
assert int(result) == 0 |
| 594 |
self.check_vectorized(2, 2) |
| 595 |
|
| 596 |
def define_all(): |
| 597 |
return """ |
| 598 |
a = astype([1,1,1,1,1,1,1,1],int32) |
| 599 |
all(a) |
| 600 |
""" |
| 601 |
def define_all_int(): |
| 602 |
return """ |
| 603 |
a = astype([1,100,255,1,3,1,1,1],int32) |
| 604 |
all(a) |
| 605 |
""" |
| 606 |
def define_all_ret_0(): |
| 607 |
return """ |
| 608 |
a = astype([1,1,1,1,1,0,1,1],int32) |
| 609 |
all(a) |
| 610 |
""" |
| 611 |
def define_float_all(): |
| 612 |
return """ |
| 613 |
a = [1,1,1,1,1,1,1,1] |
| 614 |
all(a) |
| 615 |
""" |
| 616 |
def define_float32_all(): |
| 617 |
return """ |
| 618 |
a = astype([1,1,1,1,1,1,1,1],float32) |
| 619 |
all(a) |
| 620 |
""" |
| 621 |
|
| 622 |
def test_all_float(self): |
| 623 |
result = self.run("float_all") |
| 624 |
assert int(result) == 1 |
| 625 |
self.check_vectorized(1, 1) |
| 626 |
|
| 627 |
def test_all_float32(self): |
| 628 |
result = self.run("float32_all") |
| 629 |
assert int(result) == 1 |
| 630 |
self.check_vectorized(2, 2) |
| 631 |
|
| 632 |
def test_all(self): |
| 633 |
result = self.run("all") |
| 634 |
assert int(result) == 1 |
| 635 |
self.check_vectorized(2, 2) |
| 636 |
|
| 637 |
def test_all_int(self): |
| 638 |
result = self.run("all_int") |
| 639 |
assert int(result) == 1 |
| 640 |
self.check_vectorized(2, 2) |
| 641 |
|
| 642 |
def test_all_ret_0(self): |
| 643 |
result = self.run("all_ret_0") |
| 644 |
assert int(result) == 0 |
| 645 |
self.check_vectorized(2, 2) |
| 646 |
|
| 647 |
def define_logical_xor_reduce(): |
| 648 |
return """ |
| 649 |
a = [1,1,1,1,1,1,1,1] |
| 650 |
logical_xor_reduce(a) |
| 651 |
""" |
| 652 |
|
| 653 |
def test_logical_xor_reduce(self): |
| 654 |
result = self.run("logical_xor_reduce") |
| 655 |
assert result == 0 |
| 656 |
self.check_vectorized(0, 0) |
| 657 |
|
| 658 |
def define_already_forced(): |
| 659 |
return """ |
| 660 |
a = |30| |
| 661 |
b = a + 4.5 |
| 662 |
b -> 5 # forces |
| 663 |
c = b * 8 |
| 664 |
c -> 5 |
| 665 |
""" |
| 666 |
|
| 667 |
def test_already_forced(self): |
| 668 |
result = self.run("already_forced") |
| 669 |
assert result == (5 + 4.5) * 8 |
| 670 |
self.check_vectorized(2, 2) |
| 671 |
|
| 672 |
def define_ufunc(): |
| 673 |
return """ |
| 674 |
a = |30| |
| 675 |
b = unegative(a) |
| 676 |
b -> 3 |
| 677 |
""" |
| 678 |
|
| 679 |
def test_ufunc(self): |
| 680 |
result = self.run("ufunc") |
| 681 |
assert result == -3 |
| 682 |
self.check_vectorized(1, 1) |
| 683 |
|
| 684 |
def define_specialization(): |
| 685 |
return """ |
| 686 |
a = |30| |
| 687 |
b = a + a |
| 688 |
c = unegative(b) |
| 689 |
c -> 3 |
| 690 |
d = a * a |
| 691 |
unegative(d) |
| 692 |
d -> 3 |
| 693 |
d = a * a |
| 694 |
unegative(d) |
| 695 |
d -> 3 |
| 696 |
d = a * a |
| 697 |
unegative(d) |
| 698 |
d -> 3 |
| 699 |
d = a * a |
| 700 |
unegative(d) |
| 701 |
d -> 3 |
| 702 |
""" |
| 703 |
|
| 704 |
def test_specialization(self): |
| 705 |
result = self.run("specialization") |
| 706 |
assert result == (3*3) |
| 707 |
self.check_vectorized(3, 3) |
| 708 |
|
| 709 |
def define_multidim(): |
| 710 |
return """ |
| 711 |
a = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]] |
| 712 |
b = a + a |
| 713 |
b -> 1 -> 1 |
| 714 |
""" |
| 715 |
|
| 716 |
def test_multidim(self): |
| 717 |
result = self.run('multidim') |
| 718 |
assert result == 8 |
| 719 |
self.check_vectorized(1, 1) |
| 720 |
|
| 721 |
def define_broadcast(): |
| 722 |
return """ |
| 723 |
a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] |
| 724 |
b = [1, 2, 3, 4] |
| 725 |
c = a + b |
| 726 |
c -> 1 -> 2 |
| 727 |
""" |
| 728 |
|
| 729 |
def test_broadcast(self): |
| 730 |
result = self.run("broadcast") |
| 731 |
assert result == 10 |
| 732 |
self.check_vectorized(1, 0) |
| 733 |
|
| 734 |
def define_setslice(): |
| 735 |
return """ |
| 736 |
a = |30| |
| 737 |
b = |10| |
| 738 |
b[1] = 5.5 |
| 739 |
a[0:30:3] = b |
| 740 |
a -> 3 |
| 741 |
""" |
| 742 |
|
| 743 |
def test_setslice(self): |
| 744 |
result = self.run("setslice") |
| 745 |
assert result == 5.5 |
| 746 |
self.check_vectorized(1, 1) |
| 747 |
|
| 748 |
def define_virtual_slice(): |
| 749 |
return """ |
| 750 |
a = |30| |
| 751 |
c = a + a |
| 752 |
d = c -> 1:20 |
| 753 |
d -> 1 |
| 754 |
""" |
| 755 |
|
| 756 |
def test_virtual_slice(self): |
| 757 |
result = self.run("virtual_slice") |
| 758 |
assert result == 4 |
| 759 |
self.check_vectorized(1, 1) |
| 760 |
|
| 761 |
def define_flat_iter(): |
| 762 |
return ''' |
| 763 |
a = |30| |
| 764 |
b = flat(a) |
| 765 |
c = b + a |
| 766 |
c -> 3 |
| 767 |
''' |
| 768 |
|
| 769 |
def test_flat_iter(self): |
| 770 |
result = self.run("flat_iter") |
| 771 |
assert result == 6 |
| 772 |
self.check_vectorized(1, 1) |
| 773 |
|
| 774 |
def define_flat_getitem(): |
| 775 |
return ''' |
| 776 |
a = |30| |
| 777 |
b = flat(a) |
| 778 |
b -> 4: -> 6 |
| 779 |
''' |
| 780 |
|
| 781 |
def test_flat_getitem(self): |
| 782 |
result = self.run("flat_getitem") |
| 783 |
assert result == 10.0 |
| 784 |
self.check_vectorized(1,1) |
| 785 |
|
| 786 |
def define_flat_setitem(): |
| 787 |
return ''' |
| 788 |
a = |30| |
| 789 |
b = flat(a) |
| 790 |
b[4:] = a->:26 |
| 791 |
a -> 5 |
| 792 |
''' |
| 793 |
|
| 794 |
def test_flat_setitem(self): |
| 795 |
result = self.run("flat_setitem") |
| 796 |
assert result == 1.0 |
| 797 |
self.check_vectorized(1,0) |
| 798 |
|
| 799 |
def define_dot(): |
| 800 |
return """ |
| 801 |
a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] |
| 802 |
b = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]] |
| 803 |
c = dot(a, b) |
| 804 |
c -> 1 -> 2 |
| 805 |
""" |
| 806 |
|
| 807 |
def test_dot(self): |
| 808 |
result = self.run("dot") |
| 809 |
assert result == 184 |
| 810 |
self.check_trace_count(4) |
| 811 |
self.check_vectorized(1,1) |
| 812 |
|
| 813 |
def define_argsort(): |
| 814 |
return """ |
| 815 |
a = |30| |
| 816 |
argsort(a) |
| 817 |
a->6 |
| 818 |
""" |
| 819 |
|
| 820 |
def test_argsort(self): |
| 821 |
result = self.run("argsort") |
| 822 |
assert result == 6 |
| 823 |
self.check_vectorized(1,1) |
| 824 |
|
| 825 |
def define_where(): |
| 826 |
return """ |
| 827 |
a = [1, 0, 1, 0] |
| 828 |
x = [1, 2, 3, 4] |
| 829 |
y = [-10, -20, -30, -40] |
| 830 |
r = where(a, x, y) |
| 831 |
r -> 3 |
| 832 |
""" |
| 833 |
|
| 834 |
def test_where(self): |
| 835 |
result = self.run("where") |
| 836 |
assert result == -40 |
| 837 |
|
| 838 |
def define_searchsorted(): |
| 839 |
return """ |
| 840 |
a = [1, 4, 5, 6, 9] |
| 841 |
b = |30| -> ::-1 |
| 842 |
c = searchsorted(a, b) |
| 843 |
c -> -1 |
| 844 |
""" |
| 845 |
|
| 846 |
def test_searchsorted(self): |
| 847 |
result = self.run("searchsorted") |
| 848 |
assert result == 0 |
| 849 |
self.check_trace_count(6) |
| 850 |
|
| 851 |
def define_int_mul_array(): |
| 852 |
return """ |
| 853 |
a = astype(|30|, int32) |
| 854 |
b = astype(|30|, int32) |
| 855 |
c = a * b |
| 856 |
x1 = c -> 7 |
| 857 |
x2 = c -> 8 |
| 858 |
x3 = c -> 11 |
| 859 |
x4 = c -> 12 |
| 860 |
x1 + x2 + x3 + x4 |
| 861 |
""" |
| 862 |
def test_int_mul_array(self): |
| 863 |
|
| 864 |
|
| 865 |
result = self.run("int_mul_array") |
| 866 |
assert int(result) == 7*7+8*8+11*11+12*12 |
| 867 |
self.check_vectorized(2, 2) |
| 868 |
|
| 869 |
def define_slice(): |
| 870 |
return """ |
| 871 |
a = |30| |
| 872 |
b = a -> ::3 |
| 873 |
c = b + b |
| 874 |
c -> 3 |
| 875 |
""" |
| 876 |
|
| 877 |
def test_slice(self): |
| 878 |
result = self.run("slice") |
| 879 |
assert result == 18 |
| 880 |
self.check_vectorized(1,1) |
| 881 |
|
| 882 |
def define_multidim_slice(): |
| 883 |
return """ |
| 884 |
a = [[1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8], [7, 8, 9, 10], [9, 10, 11, 12], [11, 12, 13, 14], [13, 14, 15, 16], [16, 17, 18, 19]] |
| 885 |
b = a -> ::2 |
| 886 |
c = b + b |
| 887 |
d = c -> 1 |
| 888 |
d -> 1 |
| 889 |
""" |
| 890 |
|
| 891 |
def test_multidim_slice(self): |
| 892 |
result = self.run('multidim_slice') |
| 893 |
assert result == 12 |
| 894 |
self.check_trace_count(3) |
| 895 |
|
| 896 |
|
| 897 |
self.check_vectorized(1,0) |
| 898 |
|
| 899 |
def define_dot_matrix(): |
| 900 |
return """ |
| 901 |
mat = |16| |
| 902 |
m = reshape(mat, [4,4]) |
| 903 |
vec = [0,1,2,3] |
| 904 |
a = dot(m, vec) |
| 905 |
a -> 3 |
| 906 |
""" |
| 907 |
|
| 908 |
def test_dot_matrix(self): |
| 909 |
result = self.run("dot_matrix") |
| 910 |
assert int(result) == 86 |
| 911 |
self.check_vectorized(1, 1) |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
def define_pow(): |
| 917 |
return """ |
| 918 |
a = |30| ** 2 |
| 919 |
a -> 29 |
| 920 |
""" |
| 921 |
|
| 922 |
def test_pow(self): |
| 923 |
result = self.run("pow") |
| 924 |
assert result == 29 ** 2 |
| 925 |
self.check_trace_count(1) |
| 926 |
|
| 927 |
def define_pow_int(): |
| 928 |
return """ |
| 929 |
a = astype(|30|, int) |
| 930 |
b = astype([2], int) |
| 931 |
c = a ** b |
| 932 |
c -> 15 |
| 933 |
""" |
| 934 |
|
| 935 |
def test_pow_int(self): |
| 936 |
result = self.run("pow_int") |
| 937 |
assert result == 15 ** 2 |
| 938 |
self.check_trace_count(4) |