|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | + |
| 4 | +import flag_gems |
| 5 | + |
| 6 | +from . import accuracy_utils as utils |
| 7 | +from .conftest import QUICK_MODE |
| 8 | + |
| 9 | +BINCOUNT_SIZES = [16, 100, 1024, 10000] if not QUICK_MODE else [100, 1024] |
| 10 | +BINCOUNT_MAXVALS = [10, 100, 1000] if not QUICK_MODE else [100] |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.bincount |
| 14 | +@pytest.mark.parametrize("size", BINCOUNT_SIZES) |
| 15 | +@pytest.mark.parametrize("max_val", BINCOUNT_MAXVALS) |
| 16 | +def test_accuracy_bincount(size, max_val): |
| 17 | + """Test bincount without weights.""" |
| 18 | + inp = torch.randint(0, max_val, (size,), dtype=torch.int64, device=flag_gems.device) |
| 19 | + ref_inp = utils.to_reference(inp) |
| 20 | + |
| 21 | + ref_out = torch.bincount(ref_inp) |
| 22 | + with flag_gems.use_gems(): |
| 23 | + res_out = torch.bincount(inp) |
| 24 | + |
| 25 | + utils.gems_assert_equal(res_out, ref_out) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.bincount |
| 29 | +@pytest.mark.parametrize("size", BINCOUNT_SIZES) |
| 30 | +@pytest.mark.parametrize("max_val", BINCOUNT_MAXVALS) |
| 31 | +@pytest.mark.parametrize("dtype", utils.FLOAT_DTYPES) |
| 32 | +def test_accuracy_bincount_with_weights(size, max_val, dtype): |
| 33 | + """Test bincount with weights.""" |
| 34 | + inp = torch.randint(0, max_val, (size,), dtype=torch.int64, device=flag_gems.device) |
| 35 | + weights = torch.randn(size, dtype=dtype, device=flag_gems.device) |
| 36 | + ref_inp = utils.to_reference(inp) |
| 37 | + ref_weights = utils.to_reference(weights) |
| 38 | + |
| 39 | + ref_out = torch.bincount(ref_inp, weights=ref_weights) |
| 40 | + with flag_gems.use_gems(): |
| 41 | + res_out = torch.bincount(inp, weights=weights) |
| 42 | + |
| 43 | + utils.gems_assert_close(res_out, ref_out, dtype) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.bincount |
| 47 | +@pytest.mark.parametrize("size", BINCOUNT_SIZES) |
| 48 | +@pytest.mark.parametrize("max_val", BINCOUNT_MAXVALS) |
| 49 | +@pytest.mark.parametrize("minlength", [0, 50, 2000]) |
| 50 | +def test_accuracy_bincount_with_minlength(size, max_val, minlength): |
| 51 | + """Test bincount with minlength parameter.""" |
| 52 | + inp = torch.randint(0, max_val, (size,), dtype=torch.int64, device=flag_gems.device) |
| 53 | + ref_inp = utils.to_reference(inp) |
| 54 | + |
| 55 | + ref_out = torch.bincount(ref_inp, minlength=minlength) |
| 56 | + with flag_gems.use_gems(): |
| 57 | + res_out = torch.bincount(inp, minlength=minlength) |
| 58 | + |
| 59 | + utils.gems_assert_equal(res_out, ref_out) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.bincount |
| 63 | +def test_accuracy_bincount_empty(): |
| 64 | + """Test bincount with empty input.""" |
| 65 | + inp = torch.tensor([], dtype=torch.int64, device=flag_gems.device) |
| 66 | + ref_inp = utils.to_reference(inp) |
| 67 | + |
| 68 | + ref_out = torch.bincount(ref_inp) |
| 69 | + with flag_gems.use_gems(): |
| 70 | + res_out = torch.bincount(inp) |
| 71 | + |
| 72 | + utils.gems_assert_equal(res_out, ref_out) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.bincount |
| 76 | +def test_accuracy_bincount_single(): |
| 77 | + """Test bincount with single element.""" |
| 78 | + inp = torch.tensor([5], dtype=torch.int64, device=flag_gems.device) |
| 79 | + ref_inp = utils.to_reference(inp) |
| 80 | + |
| 81 | + ref_out = torch.bincount(ref_inp) |
| 82 | + with flag_gems.use_gems(): |
| 83 | + res_out = torch.bincount(inp) |
| 84 | + |
| 85 | + utils.gems_assert_equal(res_out, ref_out) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.bincount |
| 89 | +def test_accuracy_bincount_all_zeros(): |
| 90 | + """Test bincount with all zeros.""" |
| 91 | + inp = torch.zeros(100, dtype=torch.int64, device=flag_gems.device) |
| 92 | + ref_inp = utils.to_reference(inp) |
| 93 | + |
| 94 | + ref_out = torch.bincount(ref_inp) |
| 95 | + with flag_gems.use_gems(): |
| 96 | + res_out = torch.bincount(inp) |
| 97 | + |
| 98 | + utils.gems_assert_equal(res_out, ref_out) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.bincount |
| 102 | +@pytest.mark.parametrize("dtype", utils.FLOAT_DTYPES) |
| 103 | +def test_accuracy_bincount_weights_edge_cases(dtype): |
| 104 | + """Test bincount with edge case weights.""" |
| 105 | + inp = torch.tensor([0, 1, 2, 1, 0], dtype=torch.int64, device=flag_gems.device) |
| 106 | + weights = torch.tensor( |
| 107 | + [1.0, 2.0, 3.0, 4.0, 5.0], dtype=dtype, device=flag_gems.device |
| 108 | + ) |
| 109 | + ref_inp = utils.to_reference(inp) |
| 110 | + ref_weights = utils.to_reference(weights) |
| 111 | + |
| 112 | + ref_out = torch.bincount(ref_inp, weights=ref_weights) |
| 113 | + with flag_gems.use_gems(): |
| 114 | + res_out = torch.bincount(inp, weights=weights) |
| 115 | + |
| 116 | + utils.gems_assert_close(res_out, ref_out, dtype) |
0 commit comments