Read the packed-sequence schema of gru / rnn_tanh / rnn_relu correctly - #2798
Open
LeSingh1 wants to merge 1 commit into
Open
Read the packed-sequence schema of gru / rnn_tanh / rnn_relu correctly#2798LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
Feeding a PackedSequence to nn.RNN or nn.GRU aborts conversion with
AttributeError: 'list' object has no attribute 'val'
torch has two schemas for each of these ops, and the packed one inserts
batch_sizes ahead of hx:
aten::rnn_tanh.input(Tensor input, Tensor hx, Tensor[] params, ...,
bool bidirectional, bool batch_first)
aten::rnn_tanh.data(Tensor data, Tensor batch_sizes, Tensor hx,
Tensor[] params, ..., bool bidirectional)
Both bind exactly 9 inputs, so the expected=9 check passes and nothing
catches the shift: every argument from index 1 on is read one slot early,
and has_bias = inputs[3].val lands on the Tensor[] weight list.
lstm already handles this, but its test does not transfer: lstm's hx is a
Tensor[], so a list at index 1 identifies the unpacked schema, while here hx
is a single Tensor and index 1 is a Var either way. The Tensor[] weight list
is the only list-valued argument, so where it lands identifies the schema.
Note gru is corrected here too but a packed gru still stops later, at the
pre-existing '_pad_packed_sequence: Only static shape of PackedSequence
object is supported' -- _add_gru_layer builds its hidden list with mb.fill
over a run-time shape, so the output batch dim is symbolic.
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.
Problem
Feeding a
PackedSequencetonn.RNNornn.GRUaborts conversion:torch has two schemas for each of these ops, and the packed one inserts
batch_sizesahead ofhx:Both bind exactly 9 inputs, so the
expected=9check passes and nothing catches the shift — every argument from index 1 on is read one slot early.has_bias = inputs[3].vallands on theTensor[] paramslist, which is where theAttributeErrorcomes from.gruandrnn_reluhave the identical pair of schemas and the identical bug.lstmalready handles this (has_batch_sizes = not isinstance(inputs[1], Iterable)); the single-hidden-state ops just never got the same treatment.Fix
A shared
_parse_rnn_argshelper used bygru,rnn_tanhandrnn_relu.Distinguishing the schemas needs care, because
lstm's test does not transfer:lstm'shxis aTensor[], so a list at index 1 means the unpacked schema — but for these opshxis a singleTensor, so index 1 is a Var either way. What is unambiguous is theTensor[] paramsweight list, the only list-valued argument: it sits at index 2 for.inputand index 3 for.data. Soisinstance(inputs[2], Iterable)identifies the schema, and everything after is read at the matching offset..datacarries nobatch_firstargument, because the output of_pack_padded_sequenceis always laid out batch first — same aslstmalready assumes.Test
TestRNNWithPackedSequence::test_rnnruns a packednn.RNNthroughpack_padded_sequence/pad_packed_sequencefor both nonlinearities (tanh->aten::rnn_tanh.data,relu->aten::rnn_relu.data) and bothbatch_firstsettings, comparing against torch. It mirrors the existingTestLSTMWithPackedSequence, which is what made thelstmhandling visible in the first place.All 8 parametrizations fail with the
AttributeErrorwithout the fix and pass with it (max abs difference vs torch ~1.2e-07).A note on GRU
grugets the same corrected parsing, but a packed GRU still does not convert end to end — it now fails further downstream withThat is a separate, pre-existing limitation:
_add_gru_layerbuilds its hidden-state list withmb.fillover a shape computed at run time, so the GRU output carries a symbolic batch dimension that_pad_packed_sequencerejects.lstmavoids it because_add_mil_lstmkeeps a static shape. I left that alone rather than widen this change — the argument-offset fix stands on its own, and it turns a misleadingAttributeErrordeep inside argument parsing into an accurate message about what is actually unsupported. Happy to look at the GRU lowering separately if it is wanted.Verification
on macOS / Apple silicon, torch 2.12, mlprogram + neuralnetwork.