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

[FRONTEND] Throw an error when we would downcast an integral constant to a dtype it does not fit in #5866

Merged
merged 2 commits into from
Feb 13, 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
12 changes: 12 additions & 0 deletions python/test/unit/language/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ def kernel(X, SIZE: tl.constexpr):
kernel[(1, )](x, SIZE=SIZE, num_warps=4)


def test_scalar_overflow(device):

@triton.jit
def kernel():
huge_int: tl.constexpr = 0xFFFFFFFFFFFFFF
x = tl.full((), 32, dtype=tl.int32)
y = x + huge_int

with pytest.raises(triton.TritonError, match="out of range"):
kernel[(1, )]()


# generic test functions
def _test_unary(dtype_x, expr, numpy_expr=None, device='cuda', num_ctas=1):
check_type_supported(dtype_x, device) # early return if dtype_x is not supported
Expand Down
5 changes: 5 additions & 0 deletions python/triton/language/semantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ def binary_op_type_checking_impl(lhs: tl.tensor | numbers.Number, rhs: tl.tensor
or rhs_is_scalar and rhs_scalar < 0 and ret_sca_ty.is_int_unsigned()):
raise ValueError("Cannot perform a binary operation between an unsigned tensor and a negative scalar. "
"Perform a explicit cast on one of them.")
if ret_sca_ty.is_int():
if lhs_is_scalar and not (ret_sca_ty.get_int_min_value() <= lhs_scalar <= ret_sca_ty.get_int_max_value()):
raise ValueError(f"Scalar {lhs_scalar} is out of range for type {ret_sca_ty}")
if rhs_is_scalar and not (ret_sca_ty.get_int_min_value() <= rhs_scalar <= ret_sca_ty.get_int_max_value()):
raise ValueError(f"Scalar {rhs_scalar} is out of range for type {ret_sca_ty}")
lhs = full(
(), lhs_scalar, dtype=ret_sca_ty, builder=builder) if lhs_is_scalar else cast(lhs, ret_sca_ty, builder)
rhs = full(
Expand Down
Loading