What is the attribute called 'grad_fn'?

I run my lstm code, it work.
This is my lstm module:

This is my lstm code:

And then, I want to transform my lstm module into lstmcell module.

This is my lstmcell module:

This is my lstmcell code:

It runs so slow! And a error occured when I set ‘retain_graph’ False and j==1:

RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.

I want to make my lstmcell module more faster .So how should I do?

I noticed that the attribute called ‘grad_fn’ of ‘prediction’ are different between my two case. One is ThAddmmBackward, the other is StackBackward.This is the only difference I found.

Thank you!