Do in-place operations make sense here? Are they possible?

I’m trying to do something like the following:

``````b = 2
d = 2
al = 3
sl = 2*al - 1

A = Variable(torch.FloatTensor(b, al, d))
S = Variable(torch.FloatTensor(b, sl, d))
placeholder = Variable(torch.FloatTensor(1, 2*d))
f = nn.Linear(2*d, d)

def calc_f(x1, x2):
placeholder[:, :d] = x1
placeholder[:, d:] = x2
return f(placeholder)

S[0, 0] = A[0, 0]
S[1, 0] = A[1, 0]
S[0, 1] = A[0, 1]
S[1, 1] = A[1, 1]
S[0, 2] = A[0, 2]
S[1, 2] = calc_f(S[1, 0], S[1, 1])
S[0, 3] = calc_f(S[0, 1], S[0, 2])
S[1, 3] = A[1, 2]
S[0, 4] = calc_f(S[0, 0], S[0, 3]) # these could be batched
S[1, 4] = calc_f(S[1, 2], S[1, 3]) # these could be batched
``````

Basically, would like to compute computations on various sequences, and sometimes the computation can be batched (but not always). I’m doing something similar without in-place ops but have to constantly concat/chunk, which quickly becomes the bottleneck. If I can do these type of in-place operations, then I can avoid any chunk/concat calls. I feel like there should be way to implement this using in-place operations while avoiding sharing storage, but I keep getting this error:

RuntimeError: in-place operations can be only used on variables that don’t share storage with any other variables, but detected that there are 3 objects sharing it

Any ideas?