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 prod to BatchedTensor #193

Merged
merged 1 commit into from
May 9, 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.1a163"
version = "0.0.1a164"
description = "A library to manipulate batches of examples"
readme = "README.md"
authors = ["Thibaut Durand <durand.tibo+gh@gmail.com>"]
Expand Down
31 changes: 31 additions & 0 deletions src/redcat/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,37 @@ def nansum(self, *args, **kwargs) -> Tensor:
"""
return torch.nansum(self, *args, **kwargs)

def prod(self, *args, **kwargs) -> Tensor:
r"""Computes the product of all elements.

Args:
*args: See the documentation of ``torch.Tensor.nansum``
**kwargs: See the documentation of ``torch.Tensor.nansum``

Returns:
``torch.Tensor``: The product of all elements.

Example usage:

.. code-block:: python

>>> import torch
>>> from redcat import BatchedTensor
>>> BatchedTensor(
... torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1]])
... ).prod()
tensor(362880)
>>> BatchedTensor(
... torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1]])
... ).prod(dim=1)
tensor([ 120, 3024])
>>> BatchedTensor(
... torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1]])
... ).prod(dim=1, keepdim=True)
tensor([[ 120], [3024]])
"""
return torch.prod(self, *args, **kwargs)

def sum(self, *args, **kwargs) -> Tensor:
r"""Computes the sum of all elements.

Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,38 @@ def test_batched_tensor_nansum_custom_dims() -> None:
)


def test_batched_tensor_prod() -> None:
assert (
BatchedTensor(torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1]]))
.prod()
.equal(torch.tensor(362880))
)


def test_batched_tensor_prod_keepdim_false() -> None:
assert (
BatchedTensor(torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1]]))
.prod(dim=1)
.equal(torch.tensor([120, 3024]))
)


def test_batched_tensor_prod_keepdim_true() -> None:
assert (
BatchedTensor(torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1]]))
.prod(dim=1, keepdim=True)
.equal(torch.tensor([[120], [3024]]))
)


def test_batched_tensor_prod_custom_dims() -> None:
assert (
BatchedTensor(torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1]]), batch_dim=1)
.prod()
.equal(torch.tensor(362880))
)


@mark.parametrize("dtype", (torch.float, torch.long))
def test_batched_tensor_sum(dtype: torch.dtype) -> None:
assert (
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_tensorseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -5023,6 +5023,38 @@ def test_batched_tensor_seq_nansum_custom_dims() -> None:
)


def test_batched_tensor_seq_prod() -> None:
assert (
BatchedTensorSeq(torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1]]))
.prod()
.equal(torch.tensor(362880))
)


def test_batched_tensor_seq_prod_keepdim_false() -> None:
assert (
BatchedTensorSeq(torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1]]))
.prod(dim=1)
.equal(torch.tensor([120, 3024]))
)


def test_batched_tensor_seq_prod_keepdim_true() -> None:
assert (
BatchedTensorSeq(torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1]]))
.prod(dim=1, keepdim=True)
.equal(torch.tensor([[120], [3024]]))
)


def test_batched_tensor_seq_prod_custom_dims() -> None:
assert (
BatchedTensorSeq(torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1]]), batch_dim=1, seq_dim=0)
.prod()
.equal(torch.tensor(362880))
)


@mark.parametrize("dtype", (torch.float, torch.long))
def test_batched_tensor_seq_sum(dtype: torch.dtype) -> None:
assert (
Expand Down