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

Correct sequence length errors for reduce_first/last #807

Merged
merged 2 commits into from
Nov 30, 2022
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
4 changes: 2 additions & 2 deletions thinc/backends/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ def reduce_first(self, X: Floats2d, lengths: Ints1d) -> Tuple[Floats2d, Ints1d]:
if lengths.size == 0:
return self.alloc2f(0, X.shape[1]), lengths
if not self.xp.all(lengths > 0):
raise ValueError(f"all sequence lengths must be >= 0")
raise ValueError(f"all sequence lengths must be > 0")
starts_ends = self.alloc1i(lengths.shape[0] + 1, zeros=False)
starts_ends[0] = 0
starts_ends[1:] = lengths.cumsum()
Expand All @@ -1201,7 +1201,7 @@ def reduce_last(self, X: Floats2d, lengths: Ints1d) -> Tuple[Floats2d, Ints1d]:
if lengths.size == 0:
return self.alloc2f(0, X.shape[1]), lengths
if not self.xp.all(lengths > 0):
raise ValueError(f"all sequence lengths must be >= 0")
raise ValueError(f"all sequence lengths must be > 0")
lasts = lengths.cumsum() - 1
if lasts[-1] + 1 != X.shape[0]:
raise IndexError("lengths must sum up to the number of rows")
Expand Down
4 changes: 2 additions & 2 deletions thinc/tests/backends/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ def test_reduce_first(ops, dtype):
ops.xp.testing.assert_allclose(Y, [[1.0, 6.0], [4.0, 9.0]])

lengths = ops.asarray1i([3, 0, 2])
with pytest.raises(ValueError, match=r"all sequence lengths must be >= 0"):
with pytest.raises(ValueError, match=r"all sequence lengths must be > 0"):
ops.reduce_last(X, lengths)

lengths = ops.asarray1i([3, 2, 1])
Expand Down Expand Up @@ -866,7 +866,7 @@ def test_reduce_last(ops, dtype):
ops.xp.testing.assert_allclose(Y, [[3.0, 8.0], [5.0, 10.0]])

lengths = ops.asarray1i([3, 0, 2])
with pytest.raises(ValueError, match=r"all sequence lengths must be >= 0"):
with pytest.raises(ValueError, match=r"all sequence lengths must be > 0"):
ops.reduce_last(X, lengths)

lengths = ops.asarray1i([3, 2, 1])
Expand Down