Debian Codegraph @

tests/test_main.py

raw
1 from typing import Callable, Dict, Any, Tuple, Union, Optional, cast
2 import pytest
3 import functools
4 import itertools
5 import numpy as np
6 import eagerpy as ep
7 from eagerpy import Tensor
8 from eagerpy.types import Shape, AxisAxes
9
10 # make sure there are no undecorated tests in the "special tests" section below
11 # -> /\n\ndef test_
12 # make sure the undecorated tests in the "normal tests" section all contain
13 # assertions and do not return something
14 # -> /\n return
15
16
17 ###############################################################################
18 # normal tests
19 # - no decorator
20 # - assertions
21 ###############################################################################
22
23
24 def test_astensor_raw(t: Tensor) -> None:
25 assert (ep.astensor(t.raw) == t).all()
26
27
28 def test_astensor_tensor(t: Tensor) -> None:
29 assert (ep.astensor(t) == t).all()
30
31
32 def test_astensor_restore_raw(t: Tensor) -> None:
33 r = t.raw
34 y, restore_type = ep.astensor_(r)
35 assert (y == t).all()
36 assert type(restore_type(y)) == type(r)
37 y = y + 1
38 assert type(restore_type(y)) == type(r)
39
40
41 def test_astensor_restore_tensor(t: Tensor) -> None:
42 r = t
43 y, restore_type = ep.astensor_(r)
44 assert (y == t).all()
45 assert type(restore_type(y)) == type(r)
46 y = y + 1
47 assert type(restore_type(y)) == type(r)
48
49
50 def test_astensors_raw(t: Tensor) -> None:
51 ts = (t, t + 1, t + 2)
52 rs = tuple(t.raw for t in ts)
53 ys = ep.astensors(*rs)
54 assert isinstance(ys, tuple)
55 assert len(ts) == len(ys)
56 for ti, yi in zip(ts, ys):
57 assert (ti == yi).all()
58
59
60 def test_astensors_tensor(t: Tensor) -> None:
61 ts = (t, t + 1, t + 2)
62 ys = ep.astensors(*ts)
63 assert isinstance(ys, tuple)
64 assert len(ts) == len(ys)
65 for ti, yi in zip(ts, ys):
66 assert (ti == yi).all()
67
68
69 def test_astensors_raw_restore(t: Tensor) -> None:
70 ts = (t, t + 1, t + 2)
71 rs = tuple(t.raw for t in ts)
72 ys, restore_type = ep.astensors_(*rs)
73 assert isinstance(ys, tuple)
74 assert len(ts) == len(ys)
75 for ti, yi in zip(ts, ys):
76 assert (ti == yi).all()
77
78 ys = tuple(y + 1 for y in ys)
79 xs = restore_type(*ys)
80 assert isinstance(xs, tuple)
81 assert len(xs) == len(ys)
82 for xi, ri in zip(xs, rs):
83 assert type(xi) == type(ri)
84
85 x0 = restore_type(ys[0])
86 assert not isinstance(x0, tuple)
87
88
89 def test_astensors_tensors_restore(t: Tensor) -> None:
90 ts = (t, t + 1, t + 2)
91 rs = ts
92 ys, restore_type = ep.astensors_(*rs)
93 assert isinstance(ys, tuple)
94 assert len(ts) == len(ys)
95 for ti, yi in zip(ts, ys):
96 assert (ti == yi).all()
97
98 ys = tuple(y + 1 for y in ys)
99 xs = restore_type(*ys)
100 assert isinstance(xs, tuple)
101 assert len(xs) == len(ys)
102 for xi, ri in zip(xs, rs):
103 assert type(xi) == type(ri)
104
105 x0 = restore_type(ys[0])
106 assert not isinstance(x0, tuple) # type: ignore
107
108
109 def test_module() -> None:
110 assert ep.istensor(ep.numpy.tanh([3, 5]))
111 assert not ep.istensor(ep.numpy.tanh(3))
112
113
114 def test_module_dir() -> None:
115 assert "zeros" in dir(ep.numpy)
116
117
118 def test_repr(t: Tensor) -> None:
119 assert not repr(t).startswith("<")
120 t = ep.zeros(t, (10, 10))
121 assert not repr(t).startswith("<")
122 assert len(repr(t).split("\n")) > 1
123
124
125 def test_logical_or_manual(t: Tensor) -> None:
126 assert (ep.logical_or(t < 3, ep.zeros_like(t).bool()) == (t < 3)).all()
127
128
129 def test_logical_not_manual(t: Tensor) -> None:
130 assert (ep.logical_not(t > 3) == (t <= 3)).all()
131
132
133 def test_softmax_manual(t: Tensor) -> None:
134 s = ep.softmax(t)
135 assert (s >= 0).all()
136 assert (s <= 1).all()
137 np.testing.assert_allclose(s.sum().numpy(), 1.0, rtol=1e-6)
138
139
140 def test_log_softmax_manual(t: Tensor) -> None:
141 np.testing.assert_allclose(
142 ep.log_softmax(t).exp().numpy(), ep.softmax(t).numpy(), rtol=1e-6
143 )
144
145
146 def test_value_and_grad_fn(dummy: Tensor) -> None:
147 if isinstance(dummy, ep.NumPyTensor):
148 pytest.skip()
149
150 def f(x: ep.Tensor) -> ep.Tensor:
151 return x.square().sum()
152
153 vgf = ep.value_and_grad_fn(dummy, f)
154 t = ep.arange(dummy, 8).float32().reshape((2, 4))
155 v, g = vgf(t)
156 assert v.item() == 140
157 assert (g == 2 * t).all()
158
159
160 def test_value_and_grad_fn_with_aux(dummy: Tensor) -> None:
161 if isinstance(dummy, ep.NumPyTensor):
162 pytest.skip()
163
164 def f(x: Tensor) -> Tuple[Tensor, Tensor]:
165 x = x.square()
166 return x.sum(), x
167
168 vgf = ep.value_and_grad_fn(dummy, f, has_aux=True)
169 t = ep.arange(dummy, 8).float32().reshape((2, 4))
170 v, aux, g = vgf(t)
171 assert v.item() == 140
172 assert (aux == t.square()).all()
173 assert (g == 2 * t).all()
174
175
176 def test_value_and_grad(dummy: Tensor) -> None:
177 if isinstance(dummy, ep.NumPyTensor):
178 pytest.skip()
179
180 def f(x: Tensor) -> Tensor:
181 return x.square().sum()
182
183 t = ep.arange(dummy, 8).float32().reshape((2, 4))
184 v, g = ep.value_and_grad(f, t)
185 assert v.item() == 140
186 assert (g == 2 * t).all()
187
188
189 def test_value_aux_and_grad(dummy: Tensor) -> None:
190 if isinstance(dummy, ep.NumPyTensor):
191 pytest.skip()
192
193 def f(x: Tensor) -> Tuple[Tensor, Tensor]:
194 x = x.square()
195 return x.sum(), x
196
197 t = ep.arange(dummy, 8).float32().reshape((2, 4))
198 v, aux, g = ep.value_aux_and_grad(f, t)
199 assert v.item() == 140
200 assert (aux == t.square()).all()
201 assert (g == 2 * t).all()
202
203
204 def test_value_aux_and_grad_multiple_aux(dummy: Tensor) -> None:
205 if isinstance(dummy, ep.NumPyTensor):
206 pytest.skip()
207
208 def f(x: Tensor) -> Tuple[Tensor, Tuple[Tensor, Tensor]]:
209 x = x.square()
210 return x.sum(), (x, x + 1)
211
212 t = ep.arange(dummy, 8).float32().reshape((2, 4))
213 v, (aux0, aux1), g = ep.value_aux_and_grad(f, t)
214 assert v.item() == 140
215 assert (aux0 == t.square()).all()
216 assert (aux1 == t.square() + 1).all()
217 assert (g == 2 * t).all()
218
219
220 def test_value_and_grad_multiple_args(dummy: Tensor) -> None:
221 if isinstance(dummy, ep.NumPyTensor):
222 pytest.skip()
223
224 def f(x: Tensor, y: Tensor) -> Tensor:
225 return (x * y).sum()
226
227 t = ep.arange(dummy, 8).float32().reshape((2, 4))
228 v, g = ep.value_and_grad(f, t, t)
229 assert v.item() == 140
230 assert (g == t).all()
231
232
233 def test_logical_and_manual(t: Tensor) -> None:
234 assert (ep.logical_and(t < 3, ep.ones_like(t).bool()) == (t < 3)).all()
235
236
237 def test_transpose_1d(dummy: Tensor) -> None:
238 t = ep.arange(dummy, 8).float32()
239 assert (ep.transpose(t) == t).all()
240
241
242 def test_onehot_like_raises(dummy: Tensor) -> None:
243 t = ep.arange(dummy, 18).float32().reshape((6, 3))
244 indices = ep.arange(t, 6) // 2
245 ep.onehot_like(t, indices)
246
247 t = ep.arange(dummy, 90).float32().reshape((6, 3, 5))
248 indices = ep.arange(t, 6) // 2
249 with pytest.raises(ValueError):
250 ep.onehot_like(t, indices)
251
252 t = ep.arange(dummy, 18).float32().reshape((6, 3))
253 indices = ep.arange(t, 6).reshape((6, 1)) // 2
254 with pytest.raises(ValueError):
255 ep.onehot_like(t, indices)
256
257 t = ep.arange(dummy, 18).float32().reshape((6, 3))
258 indices = ep.arange(t, 5) // 2
259 with pytest.raises(ValueError):
260 ep.onehot_like(t, indices)
261
262
263 def test_tile_raises(t: Tensor) -> None:
264 ep.tile(t, (3,) * t.ndim)
265 with pytest.raises(ValueError):
266 ep.tile(t, (3,) * (t.ndim - 1))
267
268
269 def test_pad_raises(dummy: Tensor) -> None:
270 t = ep.arange(dummy, 120).reshape((2, 3, 4, 5)).float32()
271 ep.pad(t, ((0, 0), (0, 0), (2, 3), (1, 2)), mode="constant")
272 with pytest.raises(ValueError):
273 ep.pad(t, ((0, 0), (2, 3), (1, 2)), mode="constant")
274 with pytest.raises(ValueError):
275 ep.pad(
276 t, ((0, 0), (0, 0, 1, 2), (2, 3), (1, 2)), mode="constant" # type: ignore
277 )
278 with pytest.raises(ValueError):
279 ep.pad(t, ((0, 0), (0, 0), (2, 3), (1, 2)), mode="foo")
280
281
282 def test_mean_bool(t: Tensor) -> None:
283 with pytest.raises(ValueError):
284 ep.mean(t != 0)
285
286
287 def test_mean_int(t: Tensor) -> None:
288 with pytest.raises(ValueError):
289 ep.mean(ep.arange(t, 5))
290
291
292 @pytest.mark.parametrize("f", [ep.logical_and, ep.logical_or])
293 def test_logical_and_nonboolean(
294 t: Tensor, f: Callable[[Tensor, Tensor], Tensor]
295 ) -> None:
296 t = t.float32()
297 f(t > 1, t > 1)
298 with pytest.raises(ValueError):
299 f(t, t > 1)
300 with pytest.raises(ValueError):
301 f(t > 1, t)
302 with pytest.raises(ValueError):
303 f(t, t)
304
305
306 def test_crossentropy_raises(dummy: Tensor) -> None:
307 t = ep.arange(dummy, 50).reshape((10, 5)).float32()
308 t = t / t.max()
309 ep.crossentropy(t, t.argmax(axis=-1))
310
311 t = ep.arange(dummy, 150).reshape((10, 5, 3)).float32()
312 t = t / t.max()
313 with pytest.raises(ValueError):
314 ep.crossentropy(t, t.argmax(axis=-1))
315
316 t = ep.arange(dummy, 50).reshape((10, 5)).float32()
317 t = t / t.max()
318 with pytest.raises(ValueError):
319 ep.crossentropy(t, t.argmax(axis=-1)[:8])
320
321
322 def test_matmul_raise(dummy: Tensor) -> None:
323 t = ep.arange(dummy, 8).float32().reshape((2, 4))
324 ep.matmul(t, t.T)
325 with pytest.raises(ValueError):
326 ep.matmul(t, t[0])
327 with pytest.raises(ValueError):
328 ep.matmul(t[0], t)
329 with pytest.raises(ValueError):
330 ep.matmul(t[0], t[0])
331
332
333 def test_take_along_axis_2d_first_raises(dummy: Tensor) -> None:
334 t = ep.arange(dummy, 32).float32().reshape((8, 4))
335 indices = ep.arange(t, t.shape[-1]) % t.shape[0]
336 with pytest.raises(NotImplementedError):
337 ep.take_along_axis(t, indices[ep.newaxis], axis=0)
338
339
340 def test_norms_class() -> None:
341 assert ep.Tensor.norms is not None
342
343
344 def test_numpy_readonly(t: Tensor) -> None:
345 a = t.numpy()
346 assert a.flags.writeable is False
347 with pytest.raises(ValueError, match="read-only"):
348 a[:] += 1
349
350
351 def test_numpy_inplace(t: Tensor) -> None:
352 copy = t + 0
353 a = t.numpy().copy()
354 a[:] += 1
355 assert (t == copy).all()
356
357
358 def test_iter_list_stack(t: Tensor) -> None:
359 t2 = ep.stack(list(iter(t)))
360 assert t.shape == t2.shape
361 assert (t == t2).all()
362
363
364 def test_list_stack(t: Tensor) -> None:
365 t2 = ep.stack(list(t))
366 assert t.shape == t2.shape
367 assert (t == t2).all()
368
369
370 def test_iter_next(t: Tensor) -> None:
371 assert isinstance(next(iter(t)), Tensor)
372
373
374 def test_flatten(dummy: Tensor) -> None:
375 t = ep.ones(dummy, (16, 3, 32, 32))
376 assert ep.flatten(t).shape == (16 * 3 * 32 * 32,)
377 assert ep.flatten(t, start=1).shape == (16, 3 * 32 * 32)
378 assert ep.flatten(t, start=2).shape == (16, 3, 32 * 32)
379 assert ep.flatten(t, start=3).shape == (16, 3, 32, 32)
380 assert ep.flatten(t, end=-2).shape == (16 * 3 * 32, 32)
381 assert ep.flatten(t, end=-3).shape == (16 * 3, 32, 32)
382 assert ep.flatten(t, end=-4).shape == (16, 3, 32, 32)
383 assert ep.flatten(t, start=1, end=-2).shape == (16, 3 * 32, 32)
384
385
386 @pytest.mark.parametrize("axis", [None, 0, 1, (0, 1)])
387 def test_squeeze_not_one(dummy: Tensor, axis: Optional[AxisAxes]) -> None:
388 t = ep.zeros(dummy, (3, 4, 5))
389 if axis is None:
390 t.squeeze(axis=axis)
391 else:
392 with pytest.raises(Exception):
393 # squeezing specifc axis should fail if they are not 1
394 t.squeeze(axis=axis)
395
396
397 ###############################################################################
398 # special tests
399 # - decorated with compare_*
400 # - return values
401 ###############################################################################
402
403
404 def get_numpy_kwargs(kwargs: Any) -> Dict:
405 return {
406 k: ep.astensor(t.numpy()) if ep.istensor(t) else t for k, t in kwargs.items()
407 }
408
409
410 def compare_all(f: Callable[..., Tensor]) -> Callable[..., None]:
411 """A decorator to simplify writing test functions"""
412
413 @functools.wraps(f)
414 def test_fn(*args: Any, **kwargs: Any) -> None:
415 assert len(args) == 0
416 nkwargs = get_numpy_kwargs(kwargs)
417 t = f(*args, **kwargs)
418 n = f(*args, **nkwargs)
419 t = t.numpy()
420 n = n.numpy()
421 assert t.shape == n.shape
422 assert (t == n).all()
423
424 return test_fn
425
426
427 def compare_allclose(*args: Any, rtol: float = 1e-07, atol: float = 0) -> Callable:
428 """A decorator to simplify writing test functions"""
429
430 def compare_allclose_inner(f: Callable[..., Tensor]) -> Callable[..., None]:
431 @functools.wraps(f)
432 def test_fn(*args: Any, **kwargs: Any) -> None:
433 assert len(args) == 0
434 nkwargs = get_numpy_kwargs(kwargs)
435 t = f(*args, **kwargs)
436 n = f(*args, **nkwargs)
437 t = t.numpy()
438 n = n.numpy()
439 assert t.shape == n.shape
440 np.testing.assert_allclose(t, n, rtol=rtol, atol=atol)
441
442 return test_fn
443
444 if len(args) == 1 and callable(args[0]):
445 # decorator applied without parenthesis
446 return compare_allclose_inner(args[0])
447 return compare_allclose_inner
448
449
450 def compare_equal(
451 f: Callable[..., Union[Tensor, int, float, bool, Shape]]
452 ) -> Callable[..., None]:
453 """A decorator to simplify writing test functions"""
454
455 @functools.wraps(f)
456 def test_fn(*args: Any, **kwargs: Any) -> None:
457 assert len(args) == 0
458 nkwargs = get_numpy_kwargs(kwargs)
459 t = f(*args, **kwargs)
460 n = f(*args, **nkwargs)
461 assert isinstance(t, type(n))
462 assert t == n
463
464 return test_fn
465
466
467 @compare_equal
468 def test_format(dummy: Tensor) -> bool:
469 t = ep.arange(dummy, 5).sum()
470 return f"{t:.1f}" == "10.0"
471
472
473 @compare_equal
474 def test_item(t: Tensor) -> float:
475 t = t.sum()
476 return t.item()
477
478
479 @compare_equal
480 def test_len(t: Tensor) -> int:
481 return len(t)
482
483
484 @compare_equal
485 def test_scalar_bool(t: Tensor) -> bool:
486 return bool(ep.sum(t) == 0)
487
488
489 @compare_all
490 def test_neg(t: Tensor) -> Tensor:
491 return -t
492
493
494 @compare_all
495 def test_square(t: Tensor) -> Tensor:
496 return ep.square(t)
497
498
499 @compare_allclose
500 def test_pow(t: Tensor) -> Tensor:
501 return ep.pow(t, 3)
502
503
504 @compare_allclose
505 def test_pow_float(t: Tensor) -> Tensor:
506 return ep.pow(t, 2.5)
507
508
509 @compare_allclose
510 def test_pow_op(t: Tensor) -> Tensor:
511 return t ** 3
512
513
514 @compare_allclose
515 def test_pow_tensor(t: Tensor) -> Tensor:
516 return ep.pow(t, (t + 0.5))
517
518
519 @compare_allclose
520 def test_pow_op_tensor(t: Tensor) -> Tensor:
521 return t ** (t + 0.5)
522
523
524 @compare_all
525 def test_add(t1: Tensor, t2: Tensor) -> Tensor:
526 return t1 + t2
527
528
529 @compare_all
530 def test_add_scalar(t: Tensor) -> Tensor:
531 return t + 3
532
533
534 @compare_all
535 def test_radd_scalar(t: Tensor) -> Tensor:
536 return 3 + t
537
538
539 @compare_all
540 def test_sub(t1: Tensor, t2: Tensor) -> Tensor:
541 return t1 - t2
542
543
544 @compare_all
545 def test_sub_scalar(t: Tensor) -> Tensor:
546 return t - 3
547
548
549 @compare_all
550 def test_rsub_scalar(t: Tensor) -> Tensor:
551 return 3 - t
552
553
554 @compare_all
555 def test_mul(t1: Tensor, t2: Tensor) -> Tensor:
556 return t1 * t2
557
558
559 @compare_all
560 def test_mul_scalar(t: Tensor) -> Tensor:
561 return t * 3
562
563
564 @compare_all
565 def test_rmul_scalar(t: Tensor) -> Tensor:
566 return 3 * t
567
568
569 @compare_allclose
570 def test_truediv(t1: Tensor, t2: Tensor) -> Tensor:
571 return t1 / t2
572
573
574 @compare_allclose(rtol=1e-6)
575 def test_truediv_scalar(t: Tensor) -> Tensor:
576 return t / 3
577
578
579 @compare_allclose
580 def test_rtruediv_scalar(t: Tensor) -> Tensor:
581 return 3 / (abs(t) + 3e-8)
582
583
584 @compare_allclose
585 def test_floordiv(t1: Tensor, t2: Tensor) -> Tensor:
586 return t1 // t2
587
588
589 @compare_allclose(rtol=1e-6)
590 def test_floordiv_scalar(t: Tensor) -> Tensor:
591 return t // 3
592
593
594 @compare_allclose
595 def test_rfloordiv_scalar(t: Tensor) -> Tensor:
596 return 3 // (abs(t) + 1e-8)
597
598
599 @compare_all
600 def test_mod(t1: Tensor, t2: Tensor) -> Tensor:
601 return t1 % (abs(t2) + 1)
602
603
604 @compare_all
605 def test_mod_scalar(t: Tensor) -> Tensor:
606 return t % 3
607
608
609 @compare_all
610 def test_getitem(t: Tensor) -> Tensor:
611 return t[2]
612
613
614 @compare_all
615 def test_getitem_tuple(dummy: Tensor) -> Tensor:
616 t = ep.arange(dummy, 8).float32().reshape((2, 4))
617 return t[1, 3]
618
619
620 @compare_all
621 def test_getitem_newaxis(dummy: Tensor) -> Tensor:
622 t = ep.arange(dummy, 8).float32()
623 return t[ep.newaxis]
624
625
626 @compare_all
627 def test_getitem_ellipsis_newaxis(dummy: Tensor) -> Tensor:
628 t = ep.arange(dummy, 8).float32()
629 return t[..., ep.newaxis]
630
631
632 @compare_all
633 def test_getitem_tensor(dummy: Tensor) -> Tensor:
634 t = ep.arange(dummy, 32).float32()
635 indices = ep.arange(t, 3, 10, 2)
636 return t[indices]
637
638
639 @compare_all
640 def test_getitem_range(dummy: Tensor) -> Tensor:
641 t = ep.arange(dummy, 32).float32()
642 indices = range(3, 10, 2)
643 return t[indices]
644
645
646 @compare_all
647 def test_getitem_list(dummy: Tensor) -> Tensor:
648 t = ep.arange(dummy, 32).float32()
649 indices = list(range(3, 10, 2))
650 return t[indices]
651
652
653 @compare_all
654 def test_getitem_ndarray(dummy: Tensor) -> Tensor:
655 t = ep.arange(dummy, 32).float32()
656 indices = np.arange(3, 10, 2)
657 return t[indices]
658
659
660 @compare_all
661 def test_getitem_tuple_tensors(dummy: Tensor) -> Tensor:
662 t = ep.arange(dummy, 32).float32().reshape((8, 4))
663 rows = ep.arange(t, len(t))
664 indices = ep.arange(t, len(t)) % t.shape[1]
665 return t[rows, indices]
666
667
668 @compare_all
669 def test_getitem_tuple_tensors_full(dummy: Tensor) -> Tensor:
670 t = ep.arange(dummy, 32).float32().reshape((8, 4))
671 rows = ep.arange(t, len(t))[:, np.newaxis].tile((1, t.shape[-1]))
672 cols = t.argsort(axis=-1)
673 return t[rows, cols]
674
675
676 @compare_all
677 def test_getitem_tuple_tensors_full_broadcast(dummy: Tensor) -> Tensor:
678 t = ep.arange(dummy, 32).float32().reshape((8, 4))
679 rows = ep.arange(t, len(t))[:, np.newaxis]
680 cols = t.argsort(axis=-1)
681 return t[rows, cols]
682
683
684 @compare_all
685 def test_getitem_tuple_range_tensor(dummy: Tensor) -> Tensor:
686 t = ep.arange(dummy, 32).float32().reshape((8, 4))
687 rows = range(len(t))
688 indices = ep.arange(t, len(t)) % t.shape[1]
689 return t[rows, indices]
690
691
692 @compare_all
693 def test_getitem_tuple_range_range(dummy: Tensor) -> Tensor:
694 t = ep.arange(dummy, 36).float32().reshape((6, 6))
695 rows = cols = range(len(t))
696 return t[rows, cols]
697
698
699 @compare_all
700 def test_getitem_tuple_list_tensor(dummy: Tensor) -> Tensor:
701 t = ep.arange(dummy, 32).float32().reshape((8, 4))
702 rows = list(range(len(t)))
703 indices = ep.arange(t, len(t)) % t.shape[1]
704 return t[rows, indices]
705
706
707 @compare_all
708 def test_getitem_slice(t: Tensor) -> Tensor:
709 return t[1:3]
710
711
712 @compare_all
713 def test_getitem_slice_slice(dummy: Tensor) -> Tensor:
714 t = ep.arange(dummy, 32).float32().reshape((4, 8))
715 return t[:, :3]
716
717
718 @compare_all
719 def test_getitem_boolean_tensor(dummy: Tensor) -> Tensor:
720 t = ep.arange(dummy, 32).float32().reshape((4, 8))
721 indices = ep.arange(t, 4) <= 2
722 return t[indices]
723
724
725 @compare_all
726 def test_take_along_axis_2d(dummy: Tensor) -> Tensor:
727 t = ep.arange(dummy, 32).float32().reshape((8, 4))
728 indices = ep.arange(t, len(t)) % t.shape[-1]
729 return ep.take_along_axis(t, indices[..., ep.newaxis], axis=-1)
730
731
732 @compare_all
733 def test_take_along_axis_3d(dummy: Tensor) -> Tensor:
734 t = ep.arange(dummy, 64).float32().reshape((2, 8, 4))
735 indices = ep.arange(t, 2 * 8).reshape((2, 8, 1)) % t.shape[-1]
736 return ep.take_along_axis(t, indices, axis=-1)
737
738
739 @compare_all
740 def test_sqrt(t: Tensor) -> Tensor:
741 return ep.sqrt(t)
742
743
744 @compare_equal
745 def test_shape(t: Tensor) -> Shape:
746 return t.shape
747
748
749 @compare_all
750 def test_reshape(t: Tensor) -> Tensor:
751 shape = (1,) + t.shape + (1,)
752 return ep.reshape(t, shape)
753
754
755 @compare_all
756 def test_reshape_minus_1(t: Tensor) -> Tensor:
757 return ep.reshape(t, -1)
758
759
760 @compare_all
761 def test_reshape_int(t: Tensor) -> Tensor:
762 n = 1
763 for k in t.shape:
764 n *= k
765 return ep.reshape(t, n)
766
767
768 @compare_all
769 def test_clip(t: Tensor) -> Tensor:
770 return ep.clip(t, 2, 3.5)
771
772
773 @compare_all
774 def test_sign(t: Tensor) -> Tensor:
775 return ep.sign(t)
776
777
778 @compare_all
779 def test_sum(t: Tensor) -> Tensor:
780 return ep.sum(t)
781
782
783 @compare_all
784 def test_sum_axis(t: Tensor) -> Tensor:
785 return ep.sum(t, axis=0)
786
787
788 @compare_all
789 def test_sum_axes(dummy: Tensor) -> Tensor:
790 t = ep.ones(dummy, 30).float32().reshape((3, 5, 2))
791 return ep.sum(t, axis=(0, 1))
792
793
794 @compare_all
795 def test_sum_keepdims(t: Tensor) -> Tensor:
796 return ep.sum(t, axis=0, keepdims=True)
797
798
799 @compare_all
800 def test_sum_none_keepdims(t: Tensor) -> Tensor:
801 return ep.sum(t, axis=None, keepdims=True)
802
803
804 @compare_all
805 def test_sum_bool(t: Tensor) -> Tensor:
806 return ep.sum(t != 0)
807
808
809 @compare_all
810 def test_sum_int(t: Tensor) -> Tensor:
811 return ep.sum(ep.arange(t, 5))
812
813
814 @compare_all
815 def test_prod(t: Tensor) -> Tensor:
816 return ep.prod(t)
817
818
819 @compare_all
820 def test_prod_axis(t: Tensor) -> Tensor:
821 return ep.prod(t, axis=0)
822
823
824 @compare_all
825 def test_prod_axes(dummy: Tensor) -> Tensor:
826 t = ep.ones(dummy, 30).float32().reshape((3, 5, 2))
827 return ep.prod(t, axis=(0, 1))
828
829
830 @compare_all
831 def test_prod_keepdims(t: Tensor) -> Tensor:
832 return ep.prod(t, axis=0, keepdims=True)
833
834
835 @compare_all
836 def test_prod_none_keepdims(t: Tensor) -> Tensor:
837 return ep.prod(t, axis=None, keepdims=True)
838
839
840 @compare_all
841 def test_prod_bool(t: Tensor) -> Tensor:
842 return ep.prod(t != 0)
843
844
845 @compare_all
846 def test_prod_int(t: Tensor) -> Tensor:
847 return ep.prod(ep.arange(t, 5))
848
849
850 @compare_all
851 def test_mean(t: Tensor) -> Tensor:
852 return ep.mean(t)
853
854
855 @compare_all
856 def test_mean_axis(t: Tensor) -> Tensor:
857 return ep.mean(t, axis=0)
858
859
860 @compare_all
861 def test_mean_axes(dummy: Tensor) -> Tensor:
862 t = ep.ones(dummy, 30).float32().reshape((3, 5, 2))
863 return ep.mean(t, axis=(0, 1))
864
865
866 @compare_all
867 def test_mean_keepdims(t: Tensor) -> Tensor:
868 return ep.mean(t, axis=0, keepdims=True)
869
870
871 @compare_all
872 def test_mean_none_keepdims(t: Tensor) -> Tensor:
873 return ep.mean(t, axis=None, keepdims=True)
874
875
876 @compare_all
877 def test_all(t: Tensor) -> Tensor:
878 return ep.all(t > 3)
879
880
881 @compare_all
882 def test_all_axis(t: Tensor) -> Tensor:
883 return ep.all(t > 3, axis=0)
884
885
886 @compare_all
887 def test_all_axes(dummy: Tensor) -> Tensor:
888 t = ep.arange(dummy, 30).float32().reshape((3, 5, 2))
889 return ep.all(t > 3, axis=(0, 1))
890
891
892 @compare_all
893 def test_all_keepdims(t: Tensor) -> Tensor:
894 return ep.all(t > 3, axis=0, keepdims=True)
895
896
897 @compare_all
898 def test_all_none_keepdims(t: Tensor) -> Tensor:
899 return ep.all(t > 3, axis=None, keepdims=True)
900
901
902 @compare_all
903 def test_any(t: Tensor) -> Tensor:
904 return ep.any(t > 3)
905
906
907 @compare_all
908 def test_any_axis(t: Tensor) -> Tensor:
909 return ep.any(t > 3, axis=0)
910
911
912 @compare_all
913 def test_any_axes(dummy: Tensor) -> Tensor:
914 t = ep.arange(dummy, 30).float32().reshape((3, 5, 2))
915 return ep.any(t > 3, axis=(0, 1))
916
917
918 @compare_all
919 def test_any_keepdims(t: Tensor) -> Tensor:
920 return ep.any(t > 3, axis=0, keepdims=True)
921
922
923 @compare_all
924 def test_any_none_keepdims(t: Tensor) -> Tensor:
925 return ep.any(t > 3, axis=None, keepdims=True)
926
927
928 @compare_all
929 def test_min(t: Tensor) -> Tensor:
930 return ep.min(t)
931
932
933 @compare_all
934 def test_min_axis(t: Tensor) -> Tensor:
935 return ep.min(t, axis=0)
936
937
938 @compare_all
939 def test_min_axes(dummy: Tensor) -> Tensor:
940 t = ep.ones(dummy, 30).float32().reshape((3, 5, 2))
941 return ep.min(t, axis=(0, 1))
942
943
944 @compare_all
945 def test_min_keepdims(t: Tensor) -> Tensor:
946 return ep.min(t, axis=0, keepdims=True)
947
948
949 @compare_all
950 def test_min_none_keepdims(t: Tensor) -> Tensor:
951 return ep.min(t, axis=None, keepdims=True)
952
953
954 @compare_all
955 def test_max(t: Tensor) -> Tensor:
956 return ep.max(t)
957
958
959 @compare_all
960 def test_max_axis(t: Tensor) -> Tensor:
961 return ep.max(t, axis=0)
962
963
964 @compare_all
965 def test_max_axes(dummy: Tensor) -> Tensor:
966 t = ep.ones(dummy, 30).float32().reshape((3, 5, 2))
967 return ep.max(t, axis=(0, 1))
968
969
970 @compare_all
971 def test_max_keepdims(t: Tensor) -> Tensor:
972 return ep.max(t, axis=0, keepdims=True)
973
974
975 @compare_all
976 def test_max_none_keepdims(t: Tensor) -> Tensor:
977 return ep.max(t, axis=None, keepdims=True)
978
979
980 @compare_allclose(rtol=1e-6)
981 def test_exp(t: Tensor) -> Tensor:
982 return ep.exp(t)
983
984
985 @compare_allclose
986 def test_log(t: Tensor) -> Tensor:
987 return ep.log(t.maximum(1e-8))
988
989
990 @compare_allclose
991 def test_log2(t: Tensor) -> Tensor:
992 return ep.log2(t.maximum(1e-8))
993
994
995 @compare_allclose
996 def test_log10(t: Tensor) -> Tensor:
997 return ep.log10(t.maximum(1e-8))
998
999
1000 @compare_allclose
1001 def test_log1p(t: Tensor) -> Tensor:
1002 return ep.log1p(t)
1003
1004
1005 @compare_allclose(rtol=1e-6)
1006 def test_tanh(t: Tensor) -> Tensor:
1007 return ep.tanh(t)
1008
1009
1010 @compare_allclose(rtol=1e-6)
1011 def test_arctanh(t: Tensor) -> Tensor:
1012 return ep.arctanh((t - t.mean()) / t.max())
1013
1014
1015 @compare_all
1016 def test_abs_op(t: Tensor) -> Tensor:
1017 return abs(t)
1018
1019
1020 @compare_all
1021 def test_abs(t: Tensor) -> Tensor:
1022 return ep.abs(t)
1023
1024
1025 @compare_all
1026 def test_minimum(t1: Tensor, t2: Tensor) -> Tensor:
1027 return ep.minimum(t1, t2)
1028
1029
1030 @compare_all
1031 def test_minimum_scalar(t: Tensor) -> Tensor:
1032 return ep.minimum(t, 3)
1033
1034
1035 @compare_all
1036 def test_rminimum_scalar(t: Tensor) -> Tensor:
1037 return ep.minimum(3, t)
1038
1039
1040 @compare_all
1041 def test_maximum(t1: Tensor, t2: Tensor) -> Tensor:
1042 return ep.maximum(t1, t2)
1043
1044
1045 @compare_all
1046 def test_maximum_scalar(t: Tensor) -> Tensor:
1047 return ep.maximum(t, 3)
1048
1049
1050 @compare_all
1051 def test_rmaximum_scalar(t: Tensor) -> Tensor:
1052 return ep.maximum(3, t)
1053
1054
1055 @compare_all
1056 def test_argmin(t: Tensor) -> Tensor:
1057 return ep.argmin(t)
1058
1059
1060 @compare_all
1061 def test_argmin_axis(t: Tensor) -> Tensor:
1062 return ep.argmin(t, axis=0)
1063
1064
1065 @compare_all
1066 def test_argmax(t: Tensor) -> Tensor:
1067 return ep.argmax(t)
1068
1069
1070 @compare_all
1071 def test_argmax_axis(t: Tensor) -> Tensor:
1072 return ep.argmax(t, axis=0)
1073
1074
1075 @compare_all
1076 def test_logical_and(t: Tensor) -> Tensor:
1077 return ep.logical_and(t < 3, t > 1)
1078
1079
1080 @compare_all
1081 def test_logical_and_scalar(t: Tensor) -> Tensor:
1082 return ep.logical_and(True, t < 3)
1083
1084
1085 @compare_all
1086 def test_logical_or(t: Tensor) -> Tensor:
1087 return ep.logical_or(t > 3, t < 1)
1088
1089
1090 @compare_all
1091 def test_logical_or_scalar(t: Tensor) -> Tensor:
1092 return ep.logical_or(True, t < 1)
1093
1094
1095 @compare_all
1096 def test_logical_not(t: Tensor) -> Tensor:
1097 return ep.logical_not(t > 3)
1098
1099
1100 @compare_all
1101 def test_isnan_false(t: Tensor) -> Tensor:
1102 return ep.isnan(t)
1103
1104
1105 @compare_all
1106 def test_isnan_true(t: Tensor) -> Tensor:
1107 return ep.isnan(t + ep.nan)
1108
1109
1110 @compare_all
1111 def test_isinf(t: Tensor) -> Tensor:
1112 return ep.isinf(t)
1113
1114
1115 @compare_all
1116 def test_isinf_posinf(t: Tensor) -> Tensor:
1117 return ep.isinf(t + ep.inf)
1118
1119
1120 @compare_all
1121 def test_isinf_neginf(t: Tensor) -> Tensor:
1122 return ep.isinf(t - ep.inf)
1123
1124
1125 @compare_all
1126 def test_zeros_like(t: Tensor) -> Tensor:
1127 return ep.zeros_like(t)
1128
1129
1130 @compare_all
1131 def test_ones_like(t: Tensor) -> Tensor:
1132 return ep.ones_like(t)
1133
1134
1135 @compare_all
1136 def test_full_like(t: Tensor) -> Tensor:
1137 return ep.full_like(t, 5)
1138
1139
1140 @pytest.mark.parametrize("value", [1, -1, 2])
1141 @compare_all
1142 def test_onehot_like(dummy: Tensor, value: float) -> Tensor:
1143 t = ep.arange(dummy, 18).float32().reshape((6, 3))
1144 indices = ep.arange(t, 6) // 2
1145 return ep.onehot_like(t, indices, value=value)
1146
1147
1148 @compare_all
1149 def test_zeros_scalar(t: Tensor) -> Tensor:
1150 return ep.zeros(t, 5)
1151
1152
1153 @compare_all
1154 def test_zeros_tuple(t: Tensor) -> Tensor:
1155 return ep.zeros(t, (2, 3))
1156
1157
1158 @compare_all
1159 def test_ones_scalar(t: Tensor) -> Tensor:
1160 return ep.ones(t, 5)
1161
1162
1163 @compare_all
1164 def test_ones_tuple(t: Tensor) -> Tensor:
1165 return ep.ones(t, (2, 3))
1166
1167
1168 @compare_all
1169 def test_full_scalar(t: Tensor) -> Tensor:
1170 return ep.full(t, 5, 4.0)
1171
1172
1173 @compare_all
1174 def test_full_tuple(t: Tensor) -> Tensor:
1175 return ep.full(t, (2, 3), 4.0)
1176
1177
1178 @compare_equal
1179 def test_uniform_scalar(t: Tensor) -> Shape:
1180 return ep.uniform(t, 5).shape
1181
1182
1183 @compare_equal
1184 def test_uniform_tuple(t: Tensor) -> Shape:
1185 return ep.uniform(t, (2, 3)).shape
1186
1187
1188 @compare_equal
1189 def test_normal_scalar(t: Tensor) -> Shape:
1190 return ep.normal(t, 5).shape
1191
1192
1193 @compare_equal
1194 def test_normal_tuple(t: Tensor) -> Shape:
1195 return ep.normal(t, (2, 3)).shape
1196
1197
1198 @compare_all
1199 def test_argsort(dummy: Tensor) -> Tensor:
1200 t = ep.arange(dummy, 6).float32().reshape((2, 3))
1201 return ep.argsort(t)
1202
1203
1204 @compare_all
1205 def test_sort(dummy: Tensor) -> Tensor:
1206 t = -ep.arange(dummy, 6).float32().reshape((2, 3))
1207 return ep.sort(t)
1208
1209
1210 @compare_all
1211 def test_topk_values(dummy: Tensor) -> Tensor:
1212 t = (ep.arange(dummy, 27).reshape((3, 3, 3)) ** 2 * 10000 % 1234).float32()
1213 values, _ = ep.topk(t, 2)
1214 return values
1215
1216
1217 @compare_all
1218 def test_topk_indices(dummy: Tensor) -> Tensor:
1219 t = -(ep.arange(dummy, 27).reshape((3, 3, 3)) ** 2 * 10000 % 1234).float32()
1220 _, indices = ep.topk(t, 2)
1221 return indices
1222
1223
1224 @compare_all
1225 def test_transpose(dummy: Tensor) -> Tensor:
1226 t = ep.arange(dummy, 8).float32().reshape((2, 4))
1227 return ep.transpose(t)
1228
1229
1230 @compare_all
1231 def test_transpose_axes(dummy: Tensor) -> Tensor:
1232 t = ep.arange(dummy, 60).float32().reshape((3, 4, 5))
1233 return ep.transpose(t, axes=(1, 2, 0))
1234
1235
1236 @compare_all
1237 def test_where(t: Tensor) -> Tensor:
1238 return ep.where(t >= 3, t, -t)
1239
1240
1241 @compare_all
1242 def test_where_first_scalar(t: Tensor) -> Tensor:
1243 return ep.where(t >= 3, 2, -t)
1244
1245
1246 @compare_all
1247 def test_where_second_scalar(t: Tensor) -> Tensor:
1248 return ep.where(t >= 3, t, 2)
1249
1250
1251 @compare_all
1252 def test_where_first_scalar64(dummy: Tensor) -> Tensor:
1253 t = ep.arange(dummy, 60).float64().reshape((3, 4, 5))
1254 return ep.where(t >= 3, 2, -t)
1255
1256
1257 @compare_all
1258 def test_where_second_scalar64(dummy: Tensor) -> Tensor:
1259 t = ep.arange(dummy, 60).float64().reshape((3, 4, 5))
1260 return ep.where(t >= 3, t, 2)
1261
1262
1263 @compare_all
1264 def test_where_both_scalars(t: Tensor) -> Tensor:
1265 return ep.where(t >= 3, 2, 5)
1266
1267
1268 @compare_all
1269 def test_tile(t: Tensor) -> Tensor:
1270 return ep.tile(t, (3,) * t.ndim)
1271
1272
1273 @compare_all
1274 def test_matmul(dummy: Tensor) -> Tensor:
1275 t = ep.arange(dummy, 8).float32().reshape((2, 4))
1276 return ep.matmul(t, t.T)
1277
1278
1279 @compare_all
1280 def test_matmul_operator(dummy: Tensor) -> Tensor:
1281 t = ep.arange(dummy, 8).float32().reshape((2, 4))
1282 return t @ t.T
1283
1284
1285 @compare_allclose(rtol=1e-6)
1286 def test_softmax(t: Tensor) -> Tensor:
1287 return ep.softmax(t)
1288
1289
1290 @compare_allclose(rtol=1e-5)
1291 def test_log_softmax(t: Tensor) -> Tensor:
1292 return ep.log_softmax(t)
1293
1294
1295 @compare_allclose
1296 def test_crossentropy(dummy: Tensor) -> Tensor:
1297 t = ep.arange(dummy, 50).reshape((10, 5)).float32()
1298 t = t / t.max()
1299 return ep.crossentropy(t, t.argmax(axis=-1))
1300
1301
1302 @pytest.mark.parametrize(
1303 "array, output",
1304 itertools.product(
1305 [
1306 np.array([[1, 2], [3, 4]]),
1307 np.array([[[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]]),
1308 np.arange(100).reshape((10, 10)),
1309 ],
1310 ["sign", "logdet"],
1311 ),
1312 ids=map(
1313 lambda *l: "_".join(*l),
1314 itertools.product(
1315 ["matrix_finite", "stack_of_matrices", "matrix_infinite"],
1316 ["sign", "logdet"],
1317 ),
1318 ),
1319 )
1320 @compare_allclose
1321 def test_slogdet(dummy: Tensor, array: Tensor, output: str) -> Tensor:
1322 a = ep.from_numpy(dummy, array).float32()
1323 outputs = dict()
1324 outputs["sign"], outputs["logdet"] = ep.slogdet(a)
1325 return outputs[output]
1326
1327
1328 @pytest.mark.parametrize("axis", [0, 1, -1])
1329 @compare_all
1330 def test_stack(t1: Tensor, t2: Tensor, axis: int) -> Tensor:
1331 return ep.stack([t1, t2], axis=axis)
1332
1333
1334 @compare_all
1335 def test_concatenate_axis0(dummy: Tensor) -> Tensor:
1336 t1 = ep.arange(dummy, 12).float32().reshape((4, 3))
1337 t2 = ep.arange(dummy, 20, 32, 2).float32().reshape((2, 3))
1338 return ep.concatenate([t1, t2], axis=0)
1339
1340
1341 @compare_all
1342 def test_concatenate_axis1(dummy: Tensor) -> Tensor:
1343 t1 = ep.arange(dummy, 12).float32().reshape((3, 4))
1344 t2 = ep.arange(dummy, 20, 32, 2).float32().reshape((3, 2))
1345 return ep.concatenate([t1, t2], axis=1)
1346
1347
1348 @pytest.mark.parametrize("axis", [0, 1, -1])
1349 @compare_all
1350 def test_expand_dims(t: Tensor, axis: int) -> Tensor:
1351 return ep.expand_dims(t, axis)
1352
1353
1354 @pytest.mark.parametrize("axis", [None, 0, 1, (0, 1)])
1355 @compare_all
1356 def test_squeeze(t: Tensor, axis: Optional[AxisAxes]) -> Tensor:
1357 t = t.expand_dims(axis=0).expand_dims(axis=1)
1358 return ep.squeeze(t, axis=axis)
1359
1360
1361 @compare_all
1362 def test_arange(dummy: Tensor) -> Tensor:
1363 return ep.arange(dummy, 6)
1364
1365
1366 @compare_all
1367 def test_arange_start(dummy: Tensor) -> Tensor:
1368 return ep.arange(dummy, 5, 10)
1369
1370
1371 @compare_all
1372 def test_arange_step(dummy: Tensor) -> Tensor:
1373 return ep.arange(dummy, 4, 8, 2)
1374
1375
1376 @compare_all
1377 def test_cumsum(t: Tensor) -> Tensor:
1378 return ep.cumsum(t)
1379
1380
1381 @compare_all
1382 def test_cumsum_axis(t: Tensor) -> Tensor:
1383 return ep.cumsum(t, axis=0)
1384
1385
1386 @compare_all
1387 def test_flip(t: Tensor) -> Tensor:
1388 return ep.flip(t)
1389
1390
1391 @compare_all
1392 def test_flip_axis(t: Tensor) -> Tensor:
1393 return ep.flip(t, axis=0)
1394
1395
1396 @pytest.mark.parametrize("indexing", ["ij", "xy"])
1397 @pytest.mark.parametrize("i", [0, 1])
1398 @compare_all
1399 def test_meshgrid_a(dummy: Tensor, indexing: str, i: int) -> Tensor:
1400 t1 = ep.arange(dummy, 5)
1401 t2 = ep.arange(dummy, 3)
1402 results = ep.meshgrid(t1, t2, indexing=indexing)
1403 assert len(results) == 2
1404 return results[i]
1405
1406
1407 @pytest.mark.parametrize(
1408 "mode,value", [("constant", 0), ("constant", -2), ("reflect", 0)]
1409 )
1410 @compare_all
1411 def test_pad(dummy: Tensor, mode: str, value: float) -> Tensor:
1412 t = ep.arange(dummy, 120).reshape((2, 3, 4, 5)).float32()
1413 return ep.pad(t, ((0, 0), (0, 0), (2, 3), (1, 2)), mode=mode, value=value)
1414
1415
1416 @compare_all
1417 def test_index_update_row(dummy: Tensor) -> Tensor:
1418 x = ep.ones(dummy, (3, 4))
1419 return ep.index_update(x, ep.index[1], ep.ones(x, 4) * 66.0)
1420
1421
1422 @compare_all
1423 def test_index_update_row_scalar(dummy: Tensor) -> Tensor:
1424 x = ep.ones(dummy, (3, 4))
1425 return ep.index_update(x, ep.index[1], 66.0)
1426
1427
1428 @compare_all
1429 def test_index_update_column(dummy: Tensor) -> Tensor:
1430 x = ep.ones(dummy, (3, 4))
1431 return ep.index_update(x, ep.index[:, 1], ep.ones(x, 3) * 66.0)
1432
1433
1434 @compare_all
1435 def test_index_update_column_scalar(dummy: Tensor) -> Tensor:
1436 x = ep.ones(dummy, (3, 4))
1437 return ep.index_update(x, ep.index[:, 1], 66.0)
1438
1439
1440 @compare_all
1441 def test_index_update_indices(dummy: Tensor) -> Tensor:
1442 x = ep.ones(dummy, (3, 4))
1443 ind = ep.from_numpy(dummy, np.array([0, 1, 2, 1]))
1444 return ep.index_update(x, ep.index[ind, ep.arange(x, 4)], ep.ones(x, 4) * 33.0)
1445
1446
1447 @compare_all
1448 def test_index_update_indices_scalar(dummy: Tensor) -> Tensor:
1449 x = ep.ones(dummy, (3, 4))
1450 ind = ep.from_numpy(dummy, np.array([0, 1, 2, 1]))
1451 return ep.index_update(x, ep.index[ind, ep.arange(x, 4)], 33.0)
1452
1453
1454 @compare_all
1455 def test_lt(t1: Tensor, t2: Tensor) -> Tensor:
1456 return t1 < t2
1457
1458
1459 @compare_all
1460 def test_lt_scalar(t1: Tensor, t2: Tensor) -> Tensor:
1461 return 3 < t2
1462
1463
1464 @compare_all
1465 def test_le(t1: Tensor, t2: Tensor) -> Tensor:
1466 return t1 <= t2
1467
1468
1469 @compare_all
1470 def test_le_scalar(t1: Tensor, t2: Tensor) -> Tensor:
1471 return 3 <= t2
1472
1473
1474 @compare_all
1475 def test_gt(t1: Tensor, t2: Tensor) -> Tensor:
1476 return t1 > t2
1477
1478
1479 @compare_all
1480 def test_gt_scalar(t1: Tensor, t2: Tensor) -> Tensor:
1481 return 3 > t2
1482
1483
1484 @compare_all
1485 def test_ge(t1: Tensor, t2: Tensor) -> Tensor:
1486 return t1 >= t2
1487
1488
1489 @compare_all
1490 def test_ge_scalar(t1: Tensor, t2: Tensor) -> Tensor:
1491 return 3 >= t2
1492
1493
1494 @compare_all
1495 def test_eq(t1: Tensor, t2: Tensor) -> Tensor:
1496 return t1 == t2
1497
1498
1499 @compare_all
1500 def test_eq_scalar(t1: Tensor, t2: Tensor) -> Tensor:
1501 return cast(Tensor, 3 == t2)
1502
1503
1504 @compare_all
1505 def test_ne(t1: Tensor, t2: Tensor) -> Tensor:
1506 return t1 != t2
1507
1508
1509 @compare_all
1510 def test_ne_scalar(t1: Tensor, t2: Tensor) -> Tensor:
1511 return cast(Tensor, 3 != t2)
1512
1513
1514 @compare_all
1515 def test_float_int_lt(t1: Tensor, t2int: Tensor) -> Tensor:
1516 return t1 < t2int
1517
1518
1519 @compare_all
1520 def test_float_int_le(t1: Tensor, t2int: Tensor) -> Tensor:
1521 return t1 <= t2int
1522
1523
1524 @compare_all
1525 def test_float_int_gt(t1: Tensor, t2int: Tensor) -> Tensor:
1526 return t1 > t2int
1527
1528
1529 @compare_all
1530 def test_float_int_ge(t1: Tensor, t2int: Tensor) -> Tensor:
1531 return t1 >= t2int
1532
1533
1534 @compare_all
1535 def test_float_int_eq(t1: Tensor, t2int: Tensor) -> Tensor:
1536 return t1 == t2int
1537
1538
1539 @compare_all
1540 def test_float_int_ne(t1: Tensor, t2int: Tensor) -> Tensor:
1541 return t1 != t2int
1542
1543
1544 @compare_all
1545 def test_int_float_lt(t1int: Tensor, t2: Tensor) -> Tensor:
1546 return t1int < t2
1547
1548
1549 @compare_all
1550 def test_int_float_le(t1int: Tensor, t2: Tensor) -> Tensor:
1551 return t1int <= t2
1552
1553
1554 @compare_all
1555 def test_int_float_gt(t1int: Tensor, t2: Tensor) -> Tensor:
1556 return t1int > t2
1557
1558
1559 @compare_all
1560 def test_int_float_ge(t1int: Tensor, t2: Tensor) -> Tensor:
1561 return t1int >= t2
1562
1563
1564 @compare_all
1565 def test_int_float_eq(t1int: Tensor, t2: Tensor) -> Tensor:
1566 return t1int == t2
1567
1568
1569 @compare_all
1570 def test_int_float_ne(t1int: Tensor, t2: Tensor) -> Tensor:
1571 return t1int != t2
1572
1573
1574 @compare_all
1575 def test_norms_l0(t: Tensor) -> Tensor:
1576 return t.norms.l0()
1577
1578
1579 @compare_all
1580 def test_norms_l1(t: Tensor) -> Tensor:
1581 return t.norms.l1()
1582
1583
1584 @compare_all
1585 def test_norms_l2(t: Tensor) -> Tensor:
1586 return t.norms.l2()
1587
1588
1589 @compare_all
1590 def test_norms_linf(t: Tensor) -> Tensor:
1591 return t.norms.linf()
1592
1593
1594 @compare_all
1595 def test_norms_lp(t: Tensor) -> Tensor:
1596 return t.norms.lp(2)
1597
1598
1599 @compare_all
1600 def test_norms_cache(t: Tensor) -> Tensor:
1601 return t.norms.l1() + t.norms.l2()