I’m not sure, it’s hard to spot bugs in code that you can’t run. Why do you do this:
x = x.select(0, maxlen-1).contiguous()

Don’t you want to return predictions for the whole sequence? It seems to me that you’re only taking the last output.