I guess the issue is created by flattening hn, which has the shape [num_layers*num_directions, batch_size, hidden_size] as described in the docs 9, so you would need to check the shape before and after the flattening.
You might have missed my last sentence, so your code also cannot be debugged, since you posted a picture