-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit tests for rounding scale down to nearest power of 2
- Loading branch information
1 parent
56132a3
commit 5e0a199
Showing
2 changed files
with
80 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,74 @@ | ||
import pytest | ||
import torch | ||
|
||
from torchao.float8.float8_utils import _round_down_to_power_of_2 | ||
from torchao.float8.float8_utils import _round_scale_down_to_power_of_2 | ||
|
||
|
||
# source for notable single-precision cases: | ||
# https://en.wikipedia.org/wiki/Single-precision_floating-point_format | ||
# | ||
# TODO(danielvegamyhre): add case for largest normal fp32 value: 2**127 * (2 - 2**-23) | ||
# need to investigate why exp2(floor(log2(x)))=inf, but bitshift returns real value. | ||
@pytest.mark.parametrize( | ||
"input_shape", | ||
"input", | ||
[ | ||
(1,), | ||
(2, 3), | ||
(8, 2048, 4, 1024), | ||
1.0, | ||
# smallest positive subnormal number | ||
2**-126 * 2**-23, | ||
# largest subnormal number | ||
2**-126 * (1 - 2**-23), | ||
# smallest positive normal number | ||
2**-126, | ||
# largest number less than one | ||
1.0 - 2**-24, | ||
# smallest number larger than one | ||
1.0 + 2**-23, | ||
torch.tensor([float("inf")]), | ||
], | ||
) | ||
def test_round_scale_down_to_power_of_2_valid_inputs(input: float): | ||
input_tensor = torch.tensor(input, dtype=torch.float32) | ||
result = _round_scale_down_to_power_of_2(input_tensor) | ||
|
||
# get expected value for comparison | ||
# TODO(danielvegamyhre): support subnormal values | ||
expected_result = torch.exp2(torch.floor(torch.log2(input_tensor))) | ||
smallest_normal_fp32_value = torch.tensor([2**-126], dtype=torch.float32) | ||
expected_result = torch.max(expected_result, smallest_normal_fp32_value) | ||
|
||
assert torch.equal( | ||
result, expected_result | ||
), f"input: {input_tensor}, expected {expected_result}, but got {result}" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"multiplier", | ||
"invalid_input", | ||
[ | ||
1.0, | ||
2.5, | ||
10.0, | ||
torch.tensor([0.0]), | ||
torch.tensor([-1.0]), | ||
torch.tensor([float("nan")]), | ||
torch.tensor([-float("inf")]), | ||
], | ||
) | ||
def test_round_down_to_power_of_2(input_shape: tuple[int], multiplier: int): | ||
input_tensor = torch.rand(*input_shape, dtype=torch.float32) * multiplier | ||
expected_output = torch.exp2(torch.floor(torch.log2(input_tensor))) | ||
result = _round_down_to_power_of_2(input_tensor) | ||
assert torch.equal( | ||
result, expected_output | ||
), f"expected {expected_output}, but got {result}" | ||
def test_round_scale_down_to_power_of_2_invalid_inputs(invalid_input: torch.Tensor): | ||
with pytest.raises(AssertionError, match="scale must be positive fp32 value"): | ||
_round_scale_down_to_power_of_2(invalid_input) | ||
|
||
|
||
def test_non_float32_input(): | ||
non_float32_tensor = torch.tensor([3.0], dtype=torch.float64) | ||
with pytest.raises(AssertionError, match="input must be float32 tensor"): | ||
_round_down_to_power_of_2(non_float32_tensor) | ||
@pytest.mark.parametrize( | ||
"invalid_dtype", | ||
[ | ||
torch.bfloat16, | ||
torch.float16, | ||
torch.float64, | ||
torch.int8, | ||
torch.uint8, | ||
torch.int32, | ||
torch.uint32, | ||
torch.int64, | ||
], | ||
) | ||
def test_non_float32_input(invalid_dtype: torch.dtype): | ||
non_float32_tensor = torch.tensor([3.0], dtype=invalid_dtype) | ||
with pytest.raises(AssertionError, match="scale must be float32 tensor"): | ||
_round_scale_down_to_power_of_2(non_float32_tensor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters