|
1 | 1 | import random |
2 | 2 | import time |
3 | 3 |
|
| 4 | +import numpy as np |
4 | 5 | import pytest |
| 6 | +import scipy |
5 | 7 | import torch |
6 | 8 |
|
7 | 9 | import flag_gems |
@@ -43,6 +45,26 @@ def test_multinomial_with_replacement(shape, dtype, n_samples): |
43 | 45 | assert torch.sum(res_dist == 0) / res_dist.numel() < 0.001 |
44 | 46 |
|
45 | 47 |
|
| 48 | +@pytest.mark.multinomial |
| 49 | +@pytest.mark.parametrize("shape", [(1024, 10)]) |
| 50 | +@pytest.mark.parametrize("dtype", [torch.float16, torch.float32]) |
| 51 | +@pytest.mark.parametrize("n_samples", [2048]) |
| 52 | +def test_multinomial_with_replacement_1(shape, dtype, n_samples): |
| 53 | + # First use multinomial to generate a series of indices, then |
| 54 | + # use the index counts as the input probabilities (scaled) |
| 55 | + rand_indices = torch.multinomial(torch.rand(shape), n_samples, True).to(device) |
| 56 | + inp_counts = torch.nn.functional.one_hot(rand_indices).sum(1) |
| 57 | + with flag_gems.use_gems(): |
| 58 | + out_indices = torch.multinomial(inp_counts.to(dtype=dtype), n_samples, True) |
| 59 | + out_counts = torch.nn.functional.one_hot(out_indices).sum(1) |
| 60 | + |
| 61 | + # Do a simple Chi-square test |
| 62 | + assert torch.equal(inp_counts.sum(-1), out_counts.sum(-1)) |
| 63 | + |
| 64 | + _, pvalue = scipy.stats.chisquare(out_counts.tolist(), inp_counts.tolist(), axis=-1) |
| 65 | + assert np.sum(pvalue < 0.05) / len(pvalue) < 0.1 |
| 66 | + |
| 67 | + |
46 | 68 | @pytest.mark.multinomial |
47 | 69 | @pytest.mark.parametrize("pool", utils.UT_SHAPES_2D) |
48 | 70 | @pytest.mark.parametrize("dtype", utils.FLOAT_DTYPES) |
|
0 commit comments