This code takes in a PackedSequence as input and computes the forward correctly.
Hopefully this makes it simpler to understand and write your own code that takes in a PackedSequence:

  
      github.com
  
  
    pytorch/pytorch/blob/a462edd0f6696a4cac4dd04c60d1ad3c9bc0b99c/torch/nn/_functions/rnn.py#L118-L154 124

def VariableRecurrent(batch_sizes, inner):
def forward(input, hidden, weight):
    output = []
    input_offset = 0
    last_batch_size = batch_sizes[0]
    hiddens = []
    flat_hidden = not isinstance(hidden, tuple)
    if flat_hidden:
        hidden = (hidden,)
    for batch_size in batch_sizes:
        step_input = input[input_offset:input_offset + batch_size]
        input_offset += batch_size


        dec = last_batch_size - batch_size
        if dec > 0:
            hiddens.append(tuple(h[-dec:] for h in hidden))
            hidden = tuple(h[:-dec] for h in hidden)
        last_batch_size = batch_size


        if flat_hidden:


  This file has been truncated. show original