|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | + |
| 4 | +import flag_gems |
| 5 | + |
| 6 | +from . import accuracy_utils as utils |
| 7 | + |
| 8 | +# worktree explicitly excludes bfloat16 via PRIMARY_FLOAT_DTYPES |
| 9 | +PRIMARY_FLOAT_DTYPES = [torch.float16, torch.float32] |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.amp_foreach_non_finite_check_and_unscale_ |
| 13 | +@pytest.mark.parametrize("dtype", PRIMARY_FLOAT_DTYPES) |
| 14 | +def test_amp_foreach_non_finite_check_and_unscale_(dtype): |
| 15 | + """Test _amp_foreach_non_finite_check_and_unscale_ with normal tensors.""" |
| 16 | + # PyTorch expects inv_scale and found_inf to be float32 |
| 17 | + inv_scale = torch.tensor(2.0, device=flag_gems.device, dtype=torch.float32) |
| 18 | + found_inf = torch.tensor(0.0, device=flag_gems.device, dtype=torch.float32) |
| 19 | + |
| 20 | + tensors = [ |
| 21 | + torch.randn(16, 32, device=flag_gems.device, dtype=dtype), |
| 22 | + torch.randn(8, 16, device=flag_gems.device, dtype=dtype), |
| 23 | + ] |
| 24 | + |
| 25 | + # Reference |
| 26 | + ref_tensors = [utils.to_reference(t.clone()) for t in tensors] |
| 27 | + ref_found_inf = utils.to_reference(found_inf.clone()) |
| 28 | + ref_inv_scale = utils.to_reference(inv_scale.clone()) |
| 29 | + getattr(torch, "_amp_foreach_non_finite_check_and_unscale_")( |
| 30 | + ref_tensors, ref_found_inf, ref_inv_scale |
| 31 | + ) |
| 32 | + |
| 33 | + # GEMS |
| 34 | + res_tensors = [t.clone() for t in tensors] |
| 35 | + res_found_inf = found_inf.clone() |
| 36 | + with flag_gems.use_gems(): |
| 37 | + getattr(torch, "_amp_foreach_non_finite_check_and_unscale_")( |
| 38 | + res_tensors, res_found_inf, inv_scale |
| 39 | + ) |
| 40 | + |
| 41 | + # Compare mutated inputs (in-place operation) |
| 42 | + for i, (inp, ref_inp) in enumerate(zip(res_tensors, ref_tensors)): |
| 43 | + utils.gems_assert_close(inp, ref_inp, dtype) |
| 44 | + |
| 45 | + # Compare found_inf (also mutated in-place) |
| 46 | + utils.gems_assert_equal(res_found_inf, ref_found_inf) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.amp_foreach_non_finite_check_and_unscale_ |
| 50 | +@pytest.mark.parametrize("dtype", PRIMARY_FLOAT_DTYPES) |
| 51 | +def test_amp_foreach_non_finite_check_and_unscale__inf(dtype): |
| 52 | + """Test _amp_foreach_non_finite_check_and_unscale_ with inf values.""" |
| 53 | + # PyTorch expects inv_scale and found_inf to be float32 |
| 54 | + inv_scale = torch.tensor(2.0, device=flag_gems.device, dtype=torch.float32) |
| 55 | + found_inf = torch.tensor(0.0, device=flag_gems.device, dtype=torch.float32) |
| 56 | + |
| 57 | + tensors = [ |
| 58 | + torch.tensor( |
| 59 | + [1.0, 2.0, float("inf"), 4.0], device=flag_gems.device, dtype=dtype |
| 60 | + ), |
| 61 | + torch.tensor([5.0, 6.0, 7.0], device=flag_gems.device, dtype=dtype), |
| 62 | + ] |
| 63 | + |
| 64 | + # Reference |
| 65 | + ref_tensors = [utils.to_reference(t.clone()) for t in tensors] |
| 66 | + ref_found_inf = utils.to_reference(found_inf.clone()) |
| 67 | + ref_inv_scale = utils.to_reference(inv_scale.clone()) |
| 68 | + getattr(torch, "_amp_foreach_non_finite_check_and_unscale_")( |
| 69 | + ref_tensors, ref_found_inf, ref_inv_scale |
| 70 | + ) |
| 71 | + |
| 72 | + # GEMS |
| 73 | + res_tensors = [t.clone() for t in tensors] |
| 74 | + res_found_inf = found_inf.clone() |
| 75 | + with flag_gems.use_gems(): |
| 76 | + getattr(torch, "_amp_foreach_non_finite_check_and_unscale_")( |
| 77 | + res_tensors, res_found_inf, inv_scale |
| 78 | + ) |
| 79 | + |
| 80 | + # Compare mutated inputs (in-place operation) |
| 81 | + # Note: inf values remain unchanged, only finite values are scaled |
| 82 | + for i, (inp, ref_inp) in enumerate(zip(res_tensors, ref_tensors)): |
| 83 | + utils.gems_assert_close(inp, ref_inp, dtype) |
| 84 | + |
| 85 | + # Compare found_inf (also mutated in-place) - should be 1.0 when inf is present |
| 86 | + utils.gems_assert_equal(res_found_inf, ref_found_inf) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.amp_foreach_non_finite_check_and_unscale_ |
| 90 | +@pytest.mark.parametrize("dtype", PRIMARY_FLOAT_DTYPES) |
| 91 | +def test_amp_foreach_non_finite_check_and_unscale__nan(dtype): |
| 92 | + """Test _amp_foreach_non_finite_check_and_unscale_ with nan values.""" |
| 93 | + # PyTorch expects inv_scale and found_inf to be float32 |
| 94 | + inv_scale = torch.tensor(2.0, device=flag_gems.device, dtype=torch.float32) |
| 95 | + found_inf = torch.tensor(0.0, device=flag_gems.device, dtype=torch.float32) |
| 96 | + |
| 97 | + tensors = [ |
| 98 | + torch.tensor( |
| 99 | + [1.0, 2.0, float("nan"), 4.0], device=flag_gems.device, dtype=dtype |
| 100 | + ), |
| 101 | + torch.tensor([5.0, 6.0, 7.0], device=flag_gems.device, dtype=dtype), |
| 102 | + ] |
| 103 | + |
| 104 | + # Reference |
| 105 | + ref_tensors = [utils.to_reference(t.clone()) for t in tensors] |
| 106 | + ref_found_inf = utils.to_reference(found_inf.clone()) |
| 107 | + ref_inv_scale = utils.to_reference(inv_scale.clone()) |
| 108 | + getattr(torch, "_amp_foreach_non_finite_check_and_unscale_")( |
| 109 | + ref_tensors, ref_found_inf, ref_inv_scale |
| 110 | + ) |
| 111 | + |
| 112 | + # GEMS |
| 113 | + res_tensors = [t.clone() for t in tensors] |
| 114 | + res_found_inf = found_inf.clone() |
| 115 | + with flag_gems.use_gems(): |
| 116 | + getattr(torch, "_amp_foreach_non_finite_check_and_unscale_")( |
| 117 | + res_tensors, res_found_inf, inv_scale |
| 118 | + ) |
| 119 | + |
| 120 | + # Compare mutated inputs with equal_nan=True since tensors contain NaN |
| 121 | + for i, (inp, ref_inp) in enumerate(zip(res_tensors, ref_tensors)): |
| 122 | + utils.gems_assert_close(inp, ref_inp, dtype, equal_nan=True) |
| 123 | + |
| 124 | + # Compare found_inf - should be 1.0 when nan is present |
| 125 | + utils.gems_assert_equal(res_found_inf, ref_found_inf) |
0 commit comments