Check this solution:

  
    
    
    Implementing Truncated Backpropagation Through Time autograd
  
  
    Here is an implementation that will work for any k1 and k2 and will reduce memory usage as much as possible. 
If k2 is not huge and the one_step_module is relatively big, the slowdown of doing multiple backward should be negligible. 
The code is not super clean and has been tested only against current master branch (where Variable and Tensor are merged) so you might need slight modifications if you use 0.3. 
Hope this helps. 

class TBPTT():
    def __init__(self, one_step_module, loss_module, k…