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

Add support for torch.nansum #184

Merged
merged 1 commit into from
May 7, 2023
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "redcat"
version = "0.0.1a156"
version = "0.0.1a157"
description = "A library to manipulate batches of examples"
readme = "README.md"
authors = ["Thibaut Durand <durand.tibo+gh@gmail.com>"]
Expand Down
6 changes: 6 additions & 0 deletions src/redcat/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3502,6 +3502,12 @@ def median(input: BatchedTensor, **kwargs) -> Tensor | torch.return_types.median
return torch.median(input.data, **kwargs)


@implements(torch.nansum)
def nansum(input: BatchedTensor, **kwargs) -> Tensor: # noqa: A002
r"""See ``torch.nansum`` documentation."""
return torch.nansum(input.data, **kwargs)


@implements(torch.prod)
def prod(input: BatchedTensor, **kwargs) -> Tensor: # noqa: A002
r"""See ``torch.prod`` documentation."""
Expand Down
1 change: 1 addition & 0 deletions src/redcat/tensorseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
HANDLED_FUNCTIONS = {
torch.mean: tensor.mean,
torch.median: tensor.median,
torch.nansum: tensor.nansum,
torch.prod: tensor.prod,
torch.sum: tensor.torchsum,
}
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5324,6 +5324,31 @@ def test_torch_median_keepdim() -> None:
)


##################################
# Tests for torch.nansum #
##################################


def test_torch_nansum() -> None:
assert torch.nansum(
BatchedTensor(torch.tensor([[0, 1, 2, 3, 4], [5, 6, 7, 8, float("nan")]]))
).equal(torch.tensor(36))


def test_torch_nansum_dim_1() -> None:
assert torch.nansum(
BatchedTensor(torch.tensor([[0, 1, 2, 3, 4], [5, 6, 7, 8, float("nan")]])), dim=1
).equal(torch.tensor([10, 26]))


def test_torch_nansum_keepdim() -> None:
assert torch.nansum(
BatchedTensor(torch.tensor([[0, 1, 2, 3, 4], [5, 6, 7, 8, float("nan")]])),
dim=1,
keepdim=True,
).equal(torch.tensor([[10], [26]]))


################################
# Tests for torch.prod #
################################
Expand Down
25 changes: 21 additions & 4 deletions tests/unit/test_tensor_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def test_torch_mean(cls: type[BatchedTensor]) -> None:
@mark.parametrize("dim", (0, 1))
@mark.parametrize("keepdim", (True, False))
def test_torch_mean_kwargs(cls: type[BatchedTensor], dim: int, keepdim: bool) -> None:
x = torch.rand(6, 10).mul(100)
x = torch.rand(6, 10)
assert torch.mean(cls(x), dim=dim, keepdim=keepdim).equal(
torch.mean(x, dim=dim, keepdim=keepdim)
)
Expand All @@ -567,7 +567,7 @@ def test_torch_median(cls: type[BatchedTensor]) -> None:
@mark.parametrize("dim", (0, 1))
@mark.parametrize("keepdim", (True, False))
def test_torch_median_kwargs(cls: type[BatchedTensor], dim: int, keepdim: bool) -> None:
x = torch.rand(6, 10).mul(100)
x = torch.rand(6, 10)
assert objects_are_equal(
torch.median(cls(x), dim=dim, keepdim=keepdim), torch.median(x, dim=dim, keepdim=keepdim)
)
Expand All @@ -592,6 +592,23 @@ def test_torch_mul_tensor() -> None:
)


@mark.parametrize("cls", BATCH_CLASSES)
def test_torch_nansum(cls: type[BatchedTensor]) -> None:
x = torch.rand(6, 10)
x[x < 10] = float("nan")
assert torch.nansum(cls(x)).equal(torch.nansum(x))


@mark.parametrize("cls", BATCH_CLASSES)
@mark.parametrize("dim", (0, 1))
@mark.parametrize("keepdim", (True, False))
def test_torch_nansum_kwargs(cls: type[BatchedTensor], dim: int, keepdim: bool) -> None:
x = torch.rand(6, 10)
assert torch.nansum(cls(x), dim=dim, keepdim=keepdim).equal(
torch.nansum(x, dim=dim, keepdim=keepdim)
)


def test_torch_neg() -> None:
assert torch.neg(BatchedTensor(torch.full((2, 3), 2.0))).equal(
BatchedTensor(torch.full((2, 3), -2.0))
Expand All @@ -608,7 +625,7 @@ def test_torch_prod(cls: type[BatchedTensor]) -> None:
@mark.parametrize("dim", (0, 1))
@mark.parametrize("keepdim", (True, False))
def test_torch_prod_kwargs(cls: type[BatchedTensor], dim: int, keepdim: bool) -> None:
x = torch.rand(6, 10).mul(100)
x = torch.rand(6, 10)
assert torch.prod(cls(x), dim=dim, keepdim=keepdim).equal(
torch.prod(x, dim=dim, keepdim=keepdim)
)
Expand Down Expand Up @@ -645,5 +662,5 @@ def test_torch_sum(cls: type[BatchedTensor]) -> None:
@mark.parametrize("dim", (0, 1))
@mark.parametrize("keepdim", (True, False))
def test_torch_sum_kwargs(cls: type[BatchedTensor], dim: int, keepdim: bool) -> None:
x = torch.rand(6, 10).mul(100)
x = torch.rand(6, 10)
assert torch.sum(cls(x), dim=dim, keepdim=keepdim).equal(torch.sum(x, dim=dim, keepdim=keepdim))
25 changes: 25 additions & 0 deletions tests/unit/test_tensorseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -7135,6 +7135,31 @@ def test_torch_median_keepdim() -> None:
)


##################################
# Tests for torch.nansum #
##################################


def test_torch_nansum() -> None:
assert torch.nansum(
BatchedTensorSeq(torch.tensor([[0, 1, 2, 3, 4], [5, 6, 7, 8, float("nan")]]))
).equal(torch.tensor(36))


def test_torch_nansum_dim_1() -> None:
assert torch.nansum(
BatchedTensorSeq(torch.tensor([[0, 1, 2, 3, 4], [5, 6, 7, 8, float("nan")]])), dim=1
).equal(torch.tensor([10, 26]))


def test_torch_nansum_keepdim() -> None:
assert torch.nansum(
BatchedTensorSeq(torch.tensor([[0, 1, 2, 3, 4], [5, 6, 7, 8, float("nan")]])),
dim=1,
keepdim=True,
).equal(torch.tensor([[10], [26]]))


################################
# Tests for torch.prod #
################################
Expand Down