MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / test_PermutationRNG

Function test_PermutationRNG

imperative/python/test/unit/random/test_rng.py:663–706  ·  view source on GitHub ↗
(symbolic)

Source from the content-addressed store, hash-verified

661)
662@pytest.mark.parametrize("symbolic", [True, False])
663def test_PermutationRNG(symbolic):
664 m1 = RNG(seed=111, device="xpu0")
665 m2 = RNG(seed=111, device="xpu1")
666 m3 = RNG(seed=222, device="xpu0")
667 out1 = m1.permutation(1000)
668 out1_ = m1.uniform(size=(1000,))
669 out2 = m2.permutation(1000)
670 out3 = m3.permutation(1000)
671
672 np.testing.assert_allclose(out1.numpy(), out2.numpy(), atol=1e-6)
673 assert out1.device == "xpu0" and out2.device == "xpu1"
674 assert not (out1.numpy() == out3.numpy()).all()
675 assert not (out1.numpy() == out1_.numpy()).all()
676
677 out = m1.permutation(1000)
678 out_shp = out.shape
679 if isinstance(out_shp, tuple):
680 assert out_shp == (1000,)
681 else:
682 assert all(out.shape.numpy() == np.array([1000]))
683
684 def sum_result(res, fun):
685 return sum([1 if i == v else 0 for i, v in enumerate(fun(res.numpy()))])
686
687 assert sum_result(out, lambda x: x) < 500
688 assert sum_result(out, np.sort) == 1000
689
690 def func():
691 out = m1.permutation(Tensor(7))
692 out_shp = out.shape
693 if isinstance(out_shp, tuple):
694 assert out_shp == (1,)
695 else:
696 assert all(out.shape.numpy() == np.array([1]))
697 n, m = 6, 3
698 out = m1.permutation(Tensor(np.arange(n * m), dtype="float32").reshape(n, m))
699 out_shp = out.shape
700 if isinstance(out_shp, tuple):
701 assert out_shp == (n, m)
702 else:
703 assert all(out.shape.numpy() == np.array([n, m]))
704
705 func = trace(symbolic=symbolic)(func)
706 func()
707
708
709@pytest.mark.skipif(

Callers

nothing calls this directly

Calls 9

permutationMethod · 0.95
uniformMethod · 0.95
RNGClass · 0.90
traceClass · 0.90
sum_resultFunction · 0.85
allMethod · 0.80
arrayMethod · 0.80
funcFunction · 0.70
numpyMethod · 0.45

Tested by

no test coverage detected