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

Align clamp naming with PyTorch #140

Merged
merged 2 commits into from
Apr 24, 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.1a115"
version = "0.0.1a116"
description = "A library to manipulate batches of examples"
readme = "README.md"
authors = ["Thibaut Durand <durand.tibo+gh@gmail.com>"]
Expand Down
16 changes: 8 additions & 8 deletions src/redcat/basetensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,19 +1147,19 @@ def abs_(self) -> None:

def clamp(
self,
min_value: int | float | None = None,
max_value: int | float | None = None,
min: int | float | None = None, # noqa: A002
max: int | float | None = None, # noqa: A002
) -> TBatchedTensor:
r"""Clamps all elements in ``self`` into the range ``[min_value,
max_value]``.

Note: ``min_value`` and ``max_value`` cannot be both ``None``.

Args:
min_value (int, float or ``None``, optional): Specifies
min (int, float or ``None``, optional): Specifies
the lower bound. If ``min_value`` is ``None``,
there is no lower bound. Default: ``None``
max_value (int, float or ``None``, optional): Specifies
max (int, float or ``None``, optional): Specifies
the upper bound. If ``max_value`` is ``None``,
there is no upper bound. Default: ``None``

Expand All @@ -1173,17 +1173,17 @@ def clamp(
>>> import torch
>>> from redcat import BatchedTensor
>>> batch = BatchedTensor(torch.arange(10).view(2, 5))
>>> batch.clamp(min_value=2, max_value=5)
>>> batch.clamp(min=2, max=5)
tensor([[2, 2, 2, 3, 4],
[5, 5, 5, 5, 5]], batch_dim=0)
>>> batch.clamp(min_value=2)
>>> batch.clamp(min=2)
tensor([[2, 2, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> batch.clamp(max_value=7)
>>> batch.clamp(max=7)
tensor([[0, 1, 2, 3, 4],
[5, 6, 7, 7, 7]], batch_dim=0)
"""
return torch.clamp(self, min=min_value, max=max_value)
return torch.clamp(self, min=min, max=max)

def clamp_(
self,
Expand Down
1 change: 1 addition & 0 deletions src/redcat/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __torch_function__(
args: tuple[Any, ...] = (),
kwargs: dict[str, Any] | None = None,
) -> BatchedTensor:
# print(func, types, args, kwargs)
kwargs = kwargs or {}
batch_dims = get_batch_dims(args, kwargs)
check_batch_dims(batch_dims)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1728,31 +1728,31 @@ def test_batched_tensor_abs__custom_batch_dim() -> None:
def test_batched_tensor_clamp() -> None:
assert (
BatchedTensor(torch.arange(10).view(2, 5))
.clamp(min_value=2, max_value=5)
.clamp(min=2, max=5)
.equal(BatchedTensor(torch.tensor([[2, 2, 2, 3, 4], [5, 5, 5, 5, 5]])))
)


def test_batched_tensor_clamp_only_max_value() -> None:
assert (
BatchedTensor(torch.arange(10).view(2, 5))
.clamp(max_value=5)
.clamp(max=5)
.equal(BatchedTensor(torch.tensor([[0, 1, 2, 3, 4], [5, 5, 5, 5, 5]])))
)


def test_batched_tensor_clamp_only_min_value() -> None:
assert (
BatchedTensor(torch.arange(10).view(2, 5))
.clamp(min_value=2)
.clamp(min=2)
.equal(BatchedTensor(torch.tensor([[2, 2, 2, 3, 4], [5, 6, 7, 8, 9]])))
)


def test_batched_tensor_clamp_custom_dims() -> None:
assert (
BatchedTensor(torch.arange(10).view(2, 5), batch_dim=1)
.clamp(min_value=2, max_value=5)
.clamp(min=2, max=5)
.equal(BatchedTensor(torch.tensor([[2, 2, 2, 3, 4], [5, 5, 5, 5, 5]]), batch_dim=1))
)

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_tensorseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -2131,31 +2131,31 @@ def test_batched_tensor_seq_abs__custom_dims() -> None:
def test_batched_tensor_seq_clamp() -> None:
assert (
BatchedTensorSeq(torch.arange(10).view(2, 5))
.clamp(min_value=2, max_value=5)
.clamp(min=2, max=5)
.equal(BatchedTensorSeq(torch.tensor([[2, 2, 2, 3, 4], [5, 5, 5, 5, 5]])))
)


def test_batched_tensor_seq_clamp_only_max_value() -> None:
assert (
BatchedTensorSeq(torch.arange(10).view(2, 5))
.clamp(max_value=5)
.clamp(max=5)
.equal(BatchedTensorSeq(torch.tensor([[0, 1, 2, 3, 4], [5, 5, 5, 5, 5]])))
)


def test_batched_tensor_seq_clamp_only_min_value() -> None:
assert (
BatchedTensorSeq(torch.arange(10).view(2, 5))
.clamp(min_value=2)
.clamp(min=2)
.equal(BatchedTensorSeq(torch.tensor([[2, 2, 2, 3, 4], [5, 6, 7, 8, 9]])))
)


def test_batched_tensor_seq_clamp_custom_dims() -> None:
assert (
BatchedTensorSeq(torch.arange(10).view(2, 5), batch_dim=1, seq_dim=0)
.clamp(min_value=2, max_value=5)
.clamp(min=2, max=5)
.equal(
BatchedTensorSeq(
torch.tensor([[2, 2, 2, 3, 4], [5, 5, 5, 5, 5]]), batch_dim=1, seq_dim=0
Expand Down