|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | + |
| 4 | +import flag_gems |
| 5 | + |
| 6 | +from .accuracy_utils import ( |
| 7 | + FLOAT_DTYPES, |
| 8 | + POINTWISE_SHAPES, |
| 9 | + SCALARS, |
| 10 | + gems_assert_close, |
| 11 | + to_reference, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.fmod_tensor |
| 16 | +@pytest.mark.parametrize("shape", POINTWISE_SHAPES) |
| 17 | +@pytest.mark.parametrize("dtype", FLOAT_DTYPES) |
| 18 | +def test_fmod_tensor(shape, dtype): |
| 19 | + inp1 = torch.randn(shape, dtype=dtype, device=flag_gems.device) |
| 20 | + inp2 = torch.randn(shape, dtype=dtype, device=flag_gems.device) |
| 21 | + inp2 = torch.where(inp2 == 0, torch.ones_like(inp2), inp2) |
| 22 | + ref_inp1 = to_reference(inp1, True) |
| 23 | + ref_inp2 = to_reference(inp2, True) |
| 24 | + ref_out = torch.fmod(ref_inp1, ref_inp2) |
| 25 | + with flag_gems.use_gems(): |
| 26 | + res_out = torch.fmod(inp1, inp2) |
| 27 | + gems_assert_close(res_out, ref_out, dtype) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.fmod_scalar |
| 31 | +@pytest.mark.parametrize("shape", POINTWISE_SHAPES) |
| 32 | +@pytest.mark.parametrize("scalar", SCALARS) |
| 33 | +@pytest.mark.parametrize("dtype", FLOAT_DTYPES) |
| 34 | +def test_fmod_scalar(shape, scalar, dtype): |
| 35 | + inp1 = torch.randn(shape, dtype=dtype, device=flag_gems.device) |
| 36 | + inp2 = scalar if scalar != 0 else 1.0 |
| 37 | + ref_inp1 = to_reference(inp1, True) |
| 38 | + ref_out = torch.fmod(ref_inp1, inp2) |
| 39 | + with flag_gems.use_gems(): |
| 40 | + res_out = torch.fmod(inp1, inp2) |
| 41 | + atol = 1e-3 if abs(scalar) < 0.01 else 1e-4 |
| 42 | + gems_assert_close(res_out, ref_out, dtype, atol=atol) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.fmod_tensor_ |
| 46 | +@pytest.mark.parametrize("shape", POINTWISE_SHAPES) |
| 47 | +@pytest.mark.parametrize("dtype", FLOAT_DTYPES) |
| 48 | +def test_fmod_tensor_inplace(shape, dtype): |
| 49 | + inp1 = torch.randn(shape, dtype=dtype, device=flag_gems.device) |
| 50 | + inp2 = torch.randn(shape, dtype=dtype, device=flag_gems.device) |
| 51 | + inp2 = torch.where(inp2 == 0, torch.ones_like(inp2), inp2) |
| 52 | + ref_inp1 = to_reference(inp1.clone(), True) |
| 53 | + ref_inp2 = to_reference(inp2, True) |
| 54 | + ref_out = ref_inp1.fmod_(ref_inp2) |
| 55 | + with flag_gems.use_gems(): |
| 56 | + res_out = inp1.fmod_(inp2) |
| 57 | + gems_assert_close(res_out, ref_out, dtype) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.fmod_scalar_ |
| 61 | +@pytest.mark.parametrize("shape", POINTWISE_SHAPES) |
| 62 | +@pytest.mark.parametrize("scalar", SCALARS) |
| 63 | +@pytest.mark.parametrize("dtype", FLOAT_DTYPES) |
| 64 | +def test_fmod_scalar_inplace(shape, scalar, dtype): |
| 65 | + inp1 = torch.randn(shape, dtype=dtype, device=flag_gems.device) |
| 66 | + inp2 = scalar if scalar != 0 else 1.0 |
| 67 | + ref_inp1 = to_reference(inp1.clone(), True) |
| 68 | + ref_out = ref_inp1.fmod_(inp2) |
| 69 | + with flag_gems.use_gems(): |
| 70 | + res_out = inp1.fmod_(inp2) |
| 71 | + atol = 1e-3 if abs(scalar) < 0.01 else 1e-4 |
| 72 | + gems_assert_close(res_out, ref_out, dtype, atol=atol) |
0 commit comments