Skip to content

Bugfix: Fix slice to support empty range - #161

Merged
benikm91 merged 1 commit into
dimwit-dev:mainfrom
benikm91:slice-for-empty-range
Aug 26, 2026
Merged

Bugfix: Fix slice to support empty range#161
benikm91 merged 1 commit into
dimwit-dev:mainfrom
benikm91:slice-for-empty-range

Conversation

@benikm91

@benikm91 benikm91 commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator
t.slice(0 until 0)

throws an error, despite having a potential use, e.g., autoregressive prediction starting with an empty sequence that has a tensor structure Tensor2[Sequence, Embedding, Float32], but the Sequence dimension has extent 0 initially.

This failed because the translation from Scala Range to Python Range used range.head and range.last instead of start and end values. head and last fail on an empty range. This PR fixes this mistake by using start and end, while respecting that Scala Ranges can be inclusive.

Additionally, it fixes a bug where (7 to 0 by -1) did not work, as the end would be -1 in Python (exclusive range), but it must be encoded with None in Python.

Additionally, it adds a require statement that checks whether the passed range makes sense, failing fast with a clear error message.

@benikm91
benikm91 requested a review from marcelluethi August 25, 2026 13:03
@benikm91
benikm91 force-pushed the slice-for-empty-range branch from 64b74f7 to 1aa924e Compare August 25, 2026 13:23
@benikm91
benikm91 force-pushed the slice-for-empty-range branch from 1aa924e to de633c4 Compare August 25, 2026 13:26

@marcelluethi marcelluethi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allowing empty ranges makes a lot of sense. The implementation and tests look good to me.

@benikm91
benikm91 merged commit ca09e4c into dimwit-dev:main Aug 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants