Could you try to initialize outputs as a list, and append all out tensors to it.
Once you are done, create a tensor via outputs = torch.stack(outputs) and return it.
I assume the creation of outputs as a tensor, which requires gradients might break the computation graph.
I would assume you’ll get a RuntimeError such as:
RuntimeError: leaf variable has been moved into the graph interior