Hi,
One solution I would recommend to make this into out of place ops is to use list and torch.stack (or torch.cat if the dimension already exists):
res = []
for i in range(foo):
    new_x_i = bar(x[i])
    res.append(new_x_i)
return torch.stack(res, 0)

If you want to stick with inplace operations, and want to avoid the “RuntimeError: leaf variable has been moved into the graph interior” error, simply clone the input you get: x = x.clone() at the beginning of your function.