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

[INTERPRETER] Support unary ops #3279

Merged
merged 1 commit into from
Mar 5, 2024
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
5 changes: 4 additions & 1 deletion python/test/unit/language/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def kernel(Z, X, SIZE: tl.constexpr):
z_ref = eval(expr if numpy_expr is None else numpy_expr)
# triton result
x_tri = to_triton(x, device=device, dst_type=dtype_x)
z_tri = to_triton(np.empty_like(z_ref), device=device, dst_type=dtype_x)
z_tri = to_triton(np.empty_like(x), device=device, dst_type=dtype_x)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI. There was a memory leak in our tests

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my info, how does this leak memory?

Copy link
Contributor Author

@Jokeren Jokeren Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let z_ref be a scalar, np.empty_like is also a scalar, and so is z_tri. to_triton doesn't splat a scalar, but only reinterpret a value to the given dst_type. However, the output of the kernel on the next line will be an array of 128 elements.

I'm wondering whether we have similar problems at other places.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it's an out-of-bounds memory access (I usually reserve "memory leak" for memory that's allocated and never freed, but maybe you use a different definition). btw .contiguous() is another way to fix this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably to_triton should just call contiguous().

kernel[(1, )](z_tri, x_tri, SIZE=SIZE, num_warps=4, num_ctas=num_ctas)
# compare
np.testing.assert_allclose(z_ref, to_numpy(z_tri), rtol=0.01)
Expand Down Expand Up @@ -849,6 +849,7 @@ def where_scalar_condition(a_ptr, out_ptr, BLOCK_SIZE: tl.constexpr):
# ---------------


@pytest.mark.interpreter
@pytest.mark.parametrize("dtype_x, expr",
[(dtype_x, ' -x') for dtype_x in dtypes_with_bfloat16] + [(dtype_x, ' ~x')
for dtype_x in int_dtypes])
Expand All @@ -862,6 +863,7 @@ def test_unary_op(dtype_x, expr, num_ctas, device):
# ----------------


@pytest.mark.interpreter
@pytest.mark.parametrize("dtype_x, expr, x", [(dtype_x, expr, x)
for dtype_x in ["float32", "float64"]
for expr in ['exp', 'log', 'cos', 'sin']
Expand All @@ -875,6 +877,7 @@ def test_math_op(dtype_x, expr, device, x):
# ----------------


@pytest.mark.interpreter
@pytest.mark.parametrize("dtype_x", [(dtype_x) for dtype_x in dtypes_with_bfloat16])
def test_abs(dtype_x, device):
_test_unary(dtype_x, 'tl.abs(x)', 'np.abs(x) ', device=device)
Expand Down
9 changes: 8 additions & 1 deletion python/triton/runtime/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@ def create_advance(self, ptr, offsets):
ret.offsets[i].data += offsets[i].data
return ret

def get_all_ones_value(self, type):
np_type = self.np_dtype(type)
if "int" in np_type.name:
return TensorHandle(np.full(1, -1, dtype=np_type), type)
else:
raise TypeError(f"unsupported type {type}")


def _patch_attr(obj, name, member, builder):
new_member = lambda *args, member=member, **kwargs: (member(*args, **
Expand Down Expand Up @@ -577,7 +584,7 @@ def fallback(*args, **kwargs):
for name, member in inspect.getmembers(math):
if name in mapping:
setattr(math, name, make_numpy(name))
else:
elif callable(member): # We only wrap functions
setattr(math, name, make_fallback(name))


Expand Down
Loading