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

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

from ntops.kernels.element_wise import arrangement


def application(input, output):
output = ntl.cos(input) # noqa: F841


@functools.cache
def make(ndim):
return ninetoothed.make(arrangement, application, (Tensor(ndim), Tensor(ndim)))
12 changes: 12 additions & 0 deletions src/ntops/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ntops.kernels.add
import ntops.kernels.addmm
import ntops.kernels.bmm
import ntops.kernels.cos
import ntops.kernels.div
import ntops.kernels.exp
import ntops.kernels.gelu
Expand Down Expand Up @@ -62,6 +63,17 @@ def bmm(input, mat2, *, out=None):
return out


def cos(input, *, out=None):
if out is None:
out = torch.empty_like(input)

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

kernel(input, out)

return out


def div(input, other, *, rounding_mode=None, out=None):
if out is None:
out = torch.empty_like(input)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_cos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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):
# TODO: Test for `float16` later.
if dtype is torch.float16:
return

device = "cuda"

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

ninetoothed_output = ntops.torch.cos(input)
reference_output = torch.cos(input)

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