fix: only unsqueeze hidden state for single-layer unidirectional decoder - #46
Open
Mukller wants to merge 1 commit into
Open
fix: only unsqueeze hidden state for single-layer unidirectional decoder#46Mukller wants to merge 1 commit into
Mukller wants to merge 1 commit into
Conversation
Mukller
commented
Jul 10, 2026
Mukller
left a comment
Author
There was a problem hiding this comment.
Code Review: fix unsqueeze for single-layer unidirectional decoder
Summary
Fixes an unconditional hidden.unsqueeze(0) call that was applied even when the decoder is bidirectional or has multiple layers — cases where hidden has already been reshaped to (hidden_factor, batch_size, hidden_size) by the preceding view() call. Applying unsqueeze(0) after the reshape would produce an incorrect 4D tensor, causing a dimension mismatch in the LSTM decoder.
Critical Issues
| # | File | Line | Issue | Severity |
|---|---|---|---|---|
| 1 | model.py |
~130 | hidden.unsqueeze(0) was called unconditionally, adding an extra dimension even when bidirectional/multi-layer hidden state was already reshaped |
🔴 Critical |
What the Fix Does
# Before (bug)
if self.bidirectional or self.num_layers > 1:
hidden = hidden.view(self.hidden_factor, batch_size, self.hidden_size)
hidden = hidden.unsqueeze(0) # always runs — wrong for bidi/multi-layer
# After (correct)
if self.bidirectional or self.num_layers > 1:
hidden = hidden.view(self.hidden_factor, batch_size, self.hidden_size)
else:
hidden = hidden.unsqueeze(0) # only for single-layer unidirectionalWhat Looks Good
- Minimal fix with no extraneous changes
- The
elsebranch is the correct guard — theview()already handles the multi-layer/bidi case, andunsqueeze(0)is only needed for the simple single-layer decoder to add thenum_layers=1dimension expected bynn.LSTM
Verdict
Request Changes → now fixed. This bug would silently produce wrong results or runtime errors when using bidirectional or stacked LSTM decoders.
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.
Bug Fix: Incorrect hidden state shape in
inference()for bidirectional/multi-layer RNNWhen using a bidirectional or multi-layer decoder,
inference()already reshapesthe hidden state via
.view(hidden_factor, batch_size, hidden_size)to producethe correct
(num_layers * num_directions, batch, hidden_size)tensor.However,
.unsqueeze(0)is then applied unconditionally, adding an extraleading dimension and producing the wrong shape
(1, hidden_factor, batch, hidden_size).GRU/LSTM expects
(num_layers * num_directions, batch, hidden_size)— the extradimension causes a
RuntimeError: Expected hidden size (2, N, H), got [1, 2, N, H].Before (wrong — unsqueeze applied regardless of bidirectional/num_layers):
After (fixed — unsqueeze only for simple single-layer unidirectional case):
Closes #35