Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/cuda/tile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
Scalar,
Tile,

abs,
add,
arange,
argmax,
Expand Down Expand Up @@ -183,6 +184,7 @@
"Scalar",
"Tile",

"abs",
"add",
"arange",
"argmax",
Expand Down
14 changes: 14 additions & 0 deletions src/cuda/tile/_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def __eq__(self, other) -> "TileOrScalar":
def __ne__(self, other) -> "TileOrScalar":
...

@function
def __abs__(self) -> "TileOrScalar":
...


Scalar = int | float | ScalarProtocol

Expand Down Expand Up @@ -370,6 +374,10 @@ def __eq__(self, other) -> "Tile":
def __ne__(self, other) -> "Tile":
...

@function
def __abs__(self) -> "Tile":
...


Shape = Union[int, tuple[int, ...]]
Order = Union[tuple[int, ...], Literal['C'], Literal['F']]
Expand Down Expand Up @@ -1755,6 +1763,12 @@ def ceil(x, /) -> TileOrScalar:
pass


@_doc_unary_op
@function
def abs(x, /) -> TileOrScalar:
pass


@function
def negative(x, /) -> TileOrScalar:
"""Same as `-x`.
Expand Down
27 changes: 27 additions & 0 deletions test/test_unary_elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ def test_array_abs(shape, tile, dtype, tmp_path):
assert_equal(y, abs(x))


@pytest.mark.parametrize("dtype", bool_dtypes + int_dtypes + float_dtypes, ids=dtype_id)
def test_array_ct_abs(shape, tile, dtype, tmp_path):
x = make_tensor(shape, dtype=dtype, device='cuda')
y = torch.zeros_like(x, device="cuda")
kernel = array_kernel('ct_abs', "ty = ct.abs(tx)", tmp_path)
launch_unary(kernel, x, y, tile)
assert_equal(y, abs(x))


@pytest.mark.parametrize("is_constant", [False, True])
@pytest.mark.parametrize("dtype", int_dtypes + float_dtypes, ids=dtype_id)
def test_scalar_abs(shape, tile, is_constant, dtype, tmp_path):
Expand All @@ -241,6 +250,24 @@ def test_scalar_abs(shape, tile, is_constant, dtype, tmp_path):
assert_equal(y, abs(x))


@pytest.mark.parametrize("is_constant", [False, True])
@pytest.mark.parametrize("dtype", int_dtypes + float_dtypes, ids=dtype_id)
def test_scalar_ct_abs(shape, tile, is_constant, dtype, tmp_path):
if dtype in int_dtypes:
x = -5
dtype_str = "int"
else:
x = -5.0
dtype_str = "float"
y = torch.zeros(shape, dtype=dtype, device='cuda')
if not is_constant:
kernel = scalar_kernel('ct_abs', 'c = ct.abs(x)', tmp_path)
else:
kernel = const_scalar_kernel('ct_abs', dtype_str, 'c = ct.abs(x)', tmp_path)
launch_unary(kernel, x, y, tile)
assert_equal(y, abs(x))


@pytest.mark.parametrize("bitwise_not_func", ['~', 'ct.bitwise_not'])
@pytest.mark.parametrize("dtype", int_dtypes + bool_dtypes, ids=dtype_id)
def test_array_bitwise_not(shape, tile, dtype, tmp_path, bitwise_not_func):
Expand Down