Repository navigation
fix: Support zero-length sequences in RNN unrolls - #682
Open
georgeguimaraes wants to merge 1 commit into
Open
georgeguimaraes wants to merge 1 commit into
georgeguimaraes wants to merge 1 commit into
Conversation
The unrolls read the first step of the sequence to get the shape of the output, and dynamic_unroll slices one step in its loop body. Nx traces the body even when the loop runs 0 times, so a sequence of length 0 raised an error. - unroll_initial_zeros runs the cell on one step of zeros, so it does not read the sequence. - static_unroll starts from an empty output and concatenates the steps. - dynamic_unroll pads the sequence and the mask with one spare step, so the slices in the loop body are valid for any length. The loop never reads the spare step. - A scalar mask now covers every step, instead of one column that the slice clamps to. This needs zero-size tensors in Nx (elixir-nx/nx#1885 and elixir-nx/nx#1886).
georgeguimaraes
marked this pull request as draft
October 19, 2026 12:43
polvalente
approved these changes
Oct 20, 2026
georgeguimaraes
marked this pull request as ready for review
October 26, 2026 11:58
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Depends on elixir-nx/nx#1885 and elixir-nx/nx#1886 (zero-size tensors in Nx). Do not merge before an Nx release with zero-size tensors:
static_unrollnow starts from an empty output, so on Nx 1.0 every static unroll fails. The Nx requirement must go up to that release in the same change.With zero-size tensors in Nx, an LSTM or GRU over a sequence of length 0 raised an error. The unrolls read the first step to get the output shape, and Nx traces the
whilebody ofdynamic_unrolleven when it runs 0 times.unroll_initial_zerosruns the cell on one step of zeros.static_unrollstarts from an empty output.dynamic_unrollpads the sequence with one spare step that the loop never reads.Cost: the padding makes a dynamic LSTM about 2% slower on EXLA (40.1 ms to 40.9 ms, batch 32, 128 steps, 256 units).