How to realize VAR in pytorch

I want to realize VAR by pytorch, where for time shot t, Y[t]=W1Y[t-1]+W2Y[t-3]. The general part of my pytorch code is just like the below one:

f1=torch.nn.Linear(input_dim,output_dim)
f2=torch.nn.Linear(input_dim,output_dim)
for i in range(3,Y.shape[0]):
    Y[i]=Y[i]+f1(Y[i-1])+f2(Y[i-3])

However, when I backward the loss function of Y, there became an error:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [1, 100]], which is output 0 of UnsqueezeBackward0, is at version 5040; expected version 5039 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

Is there an easy way to realize VAR by pytorch? Thank you!