Skip to content
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
18 changes: 18 additions & 0 deletions src/ntops/kernels/clamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import functools

import ninetoothed
import ninetoothed.language as ntl
from ninetoothed import Tensor

from ntops.kernels.element_wise import arrangement


def application(input, min_val, max_val, output):
output = ntl.clamp(input, min_val, max_val) # noqa: F841


@functools.cache
def make(ndim):
tensors = (Tensor(ndim), Tensor(ndim), Tensor(ndim), Tensor(ndim))

return ninetoothed.make(arrangement, application, tensors)
12 changes: 12 additions & 0 deletions src/ntops/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ntops.kernels.bitwise_not
import ntops.kernels.bitwise_or
import ntops.kernels.bmm
import ntops.kernels.clamp
import ntops.kernels.cos
import ntops.kernels.div
import ntops.kernels.eq
Expand Down Expand Up @@ -113,6 +114,17 @@ def bmm(input, mat2, *, out=None):
return out


def clamp(input, min=None, max=None, *, out=None):
if out is None:
out = torch.empty_like(input)

kernel = ntops.kernels.clamp.make(input.ndim)

kernel(input, min, max, out)

return out


def cos(input, *, out=None):
if out is None:
out = torch.empty_like(input)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_clamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
import torch

import ntops.torch
from tests.skippers import skip_if_cuda_not_available
from tests.utils import generate_arguments


@skip_if_cuda_not_available
@pytest.mark.parametrize(*generate_arguments())
def test_cuda(shape, dtype, atol, rtol):
device = "cuda"

input = torch.randn(shape, dtype=dtype, device=device)
min = torch.randn(shape, dtype=dtype, device=device)
max = torch.randn(shape, dtype=dtype, device=device)

ninetoothed_output = ntops.torch.clamp(input, min, max)
reference_output = torch.clamp(input, min, max)

assert torch.allclose(ninetoothed_output, reference_output, atol=atol, rtol=rtol)