Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support value range for tensors with default df #941

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions forge/test/operators/pytorch/eltwise_binary/test_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def verify(
if value_range is None:
value_range = ValueRanges.SMALL

# Old behavior when dev_data_format was not set
if dev_data_format is None:
value_range = ValueRanges.SMALL_POSITIVE

operator = getattr(torch, test_vector.operator)

kwargs = test_vector.kwargs if test_vector.kwargs else {}
Expand Down
3 changes: 2 additions & 1 deletion forge/test/operators/pytorch/eltwise_unary/test_unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def verify(
math_fidelity=test_vector.math_fidelity,
pcc=test_vector.pcc,
warm_reset=warm_reset,
value_range=ValueRanges.SMALL,
# Old behavior when dev_data_format was not set
value_range=ValueRanges.SMALL if test_vector.dev_data_format is not None else ValueRanges.SMALL_POSITIVE,
)


Expand Down
3 changes: 2 additions & 1 deletion forge/test/operators/pytorch/matmul/test_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def verify(
input_source_flag=input_source_flag,
dev_data_format=dev_data_format,
math_fidelity=math_fidelity,
value_range=ValueRanges.SMALL,
# Old behavior when dev_data_format was not set
value_range=ValueRanges.SMALL if dev_data_format is not None else ValueRanges.SMALL_POSITIVE,
)


Expand Down
3 changes: 2 additions & 1 deletion forge/test/operators/pytorch/nn/test_softmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def verify(
input_source_flag=input_source_flag,
dev_data_format=dev_data_format,
math_fidelity=math_fidelity,
value_range=ValueRanges.SMALL,
# Old behavior when dev_data_format was not set
value_range=ValueRanges.SMALL if dev_data_format is not None else ValueRanges.SMALL_POSITIVE,
)


Expand Down
14 changes: 13 additions & 1 deletion forge/test/operators/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ def get_value_range(
) -> OperatorParameterTypes.RangeValue:
"""Returns a range for the given data format and dtype"""

# When dev_data_format is None, the range should be the same as Float32/float32
if dev_data_format is None:
dev_data_format = forge.DataFormat.Float32
if dtype is None:
dtype = torch.float32

if dev_data_format in cls.data_format_ranges:
data_format_ranges = cls.data_format_ranges[dev_data_format]
else:
Expand Down Expand Up @@ -196,7 +202,13 @@ def get_random_torch_input(
) -> List[torch.Tensor]:

if dtype is None:
return torch.rand(input_shape, generator=generator)
input = torch.rand(input_shape, generator=generator)
range = cls.get_value_range(dev_data_format=dev_data_format, dtype=dtype, value_range=value_range)
min, max = range
if min == 0 and max == 1:
# No need to scale the input, just return it
return input
return cls.move_from_small_to_big_value_range(input, min, max)
elif dtype in cls.DTypes.floats:
input = torch.rand(input_shape, dtype=dtype, generator=generator)
range = cls.get_value_range(dev_data_format=dev_data_format, dtype=dtype, value_range=value_range)
Expand Down
Loading