Skip to content

Commit 6b25e11

Browse files
committed
Parameterize relative and absolute tolerances
1 parent e070b35 commit 6b25e11

3 files changed

Lines changed: 31 additions & 19 deletions

File tree

tests/test_addmm.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,17 @@ def addmm(input, mat1, mat2, beta=1, alpha=1):
5555

5656

5757
@pytest.mark.parametrize(
58-
"device, dtype, atol",
59-
matmul._device_dtype_config(get_available_devices(), fp16_atol=0.075),
58+
"device, dtype, rtol, atol",
59+
matmul._device_dtype_config(
60+
get_available_devices(),
61+
fp16_rtol=1e-5,
62+
fp16_atol=0.075,
63+
),
6064
)
6165
@pytest.mark.parametrize("k", (512,))
6266
@pytest.mark.parametrize("n", (512,))
6367
@pytest.mark.parametrize("m", (512,))
64-
def test(m, n, k, dtype, device, atol):
68+
def test(m, n, k, dtype, device, rtol, atol):
6569
randn_dtype = dtype if dtype != torch.float8_e5m2 else torch.float16
6670

6771
input = torch.randn((m, n), dtype=randn_dtype, device=device)
@@ -87,4 +91,4 @@ def test(m, n, k, dtype, device, atol):
8791
output = addmm(input, mat1, mat2, beta=beta, alpha=alpha)
8892
expected = torch.addmm(input, mat1, mat2, beta=beta, alpha=alpha)
8993

90-
assert torch.allclose(output, expected, atol=atol)
94+
assert torch.allclose(output, expected, rtol=rtol, atol=atol)

tests/test_float8_capability.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ def _parameter_capabilities(parameters):
183183
capabilities = {}
184184

185185
for parameter in parameters:
186-
device, dtype, atol = parameter.values
186+
device, dtype, rtol, atol = parameter.values
187187
references = tuple(
188188
mark.args[0]
189189
for mark in parameter.marks
190190
if mark.name == "requires_capability"
191191
)
192-
capabilities[(device, dtype)] = (atol, references)
192+
capabilities[(device, dtype)] = ((rtol, atol), references)
193193

194194
return capabilities
195195

@@ -198,17 +198,21 @@ def test_float8_parameters_use_device_specific_capabilities():
198198
import tests.test_matmul as matmul
199199

200200
capabilities = _parameter_capabilities(
201-
matmul._device_dtype_config(("cuda", "mlu"), fp16_atol=0.25)
201+
matmul._device_dtype_config(
202+
("cuda", "mlu"),
203+
fp16_rtol=0.5,
204+
fp16_atol=0.25,
205+
)
202206
)
203207

204-
assert capabilities[("cuda", torch.float16)] == (0.25, ())
205-
assert capabilities[("mlu", torch.float16)] == (0.25, ())
208+
assert capabilities[("cuda", torch.float16)] == ((0.5, 0.25), ())
209+
assert capabilities[("mlu", torch.float16)] == ((0.5, 0.25), ())
206210
assert capabilities[("cuda", torch.float8_e5m2)] == (
207-
0.125,
211+
(1e-5, 0.125),
208212
("tests.capabilities.float8:float8_e5m2_cuda",),
209213
)
210214
assert capabilities[("mlu", torch.float8_e5m2)] == (
211-
0.125,
215+
(1e-5, 0.125),
212216
("tests.capabilities.float8:float8_e5m2_mlu",),
213217
)
214218

@@ -222,6 +226,6 @@ def test_matmul_and_addmm_use_joint_device_dtype_parameters():
222226
mark.args[0] for mark in function.pytestmark if mark.name == "parametrize"
223227
)
224228

225-
assert "device, dtype, atol" in parameter_names
229+
assert "device, dtype, rtol, atol" in parameter_names
226230
assert "device" not in parameter_names
227-
assert "dtype, atol" not in parameter_names
231+
assert "dtype, rtol, atol" not in parameter_names

tests/test_matmul.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,18 @@ def matmul(lhs, rhs):
6464
return output
6565

6666

67-
def _device_dtype_config(devices, fp16_atol):
67+
def _device_dtype_config(devices, *, fp16_rtol, fp16_atol):
6868
config = []
6969

7070
for device in devices:
71-
config.append(pytest.param(device, torch.float16, fp16_atol))
71+
config.append(pytest.param(device, torch.float16, fp16_rtol, fp16_atol))
7272

7373
if hasattr(torch, "float8_e5m2"):
7474
config.append(
7575
pytest.param(
7676
device,
7777
torch.float8_e5m2,
78+
1e-5,
7879
0.125,
7980
marks=pytest.mark.requires_capability(
8081
f"tests.capabilities.float8:float8_e5m2_{device}"
@@ -86,13 +87,17 @@ def _device_dtype_config(devices, fp16_atol):
8687

8788

8889
@pytest.mark.parametrize(
89-
"device, dtype, atol",
90-
_device_dtype_config(get_available_devices(), fp16_atol=_FP16_ATOL),
90+
"device, dtype, rtol, atol",
91+
_device_dtype_config(
92+
get_available_devices(),
93+
fp16_rtol=_FP16_RTOL,
94+
fp16_atol=_FP16_ATOL,
95+
),
9196
)
9297
@pytest.mark.parametrize("k", (512,))
9398
@pytest.mark.parametrize("n", (512,))
9499
@pytest.mark.parametrize("m", (512,))
95-
def test(m, n, k, dtype, device, atol):
100+
def test(m, n, k, dtype, device, rtol, atol):
96101
randn_dtype = dtype if dtype != torch.float8_e5m2 else torch.float16
97102

98103
input = torch.randn((m, k), dtype=randn_dtype, device=device)
@@ -108,5 +113,4 @@ def test(m, n, k, dtype, device, atol):
108113
output = matmul(input, other)
109114
expected = torch.matmul(input, other)
110115

111-
rtol = _FP16_RTOL if dtype == torch.float16 else 1e-5
112116
assert torch.allclose(output, expected, rtol=rtol, atol=atol)

0 commit comments

Comments
 (0)