Tensor.backward does not work unless create_graph = true

Windows 10, using libtorch 2.0.1 GPU version but running on a single CPU.

I am running an experiment with a model that has an atypical architecture. I can not call .backward more than once unless I specify create_graph = true (i.e. out.backward({}, true)). In this case I get the warning :

[W C:\actions-runner_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\autograd\engine.cpp:1156] Warning: Using backward() with create_graph=True will create a reference cycle between the parameter and its gradient which can cause a memory leak. We recommend using autograd.grad when creating the graph to avoid this. If you have to use this function, make sure to reset the .grad fields of your parameters to None after use to break the cycle and avoid the leak. (function operator ())

I fear the code’s behaviour is not what I intended. The architecture’s training on one sample goes like this:

Net A takes a 1D vector as input, outputs a 1D vector.
Nets B0 takes as input subparts of Net A’s output, that are stitched and reshaped into a batch of 1D vectors. It outputs a batch of 1D vectors, which is compared (MSE loss) to the expected output. Loss.backward is then called.
The process is repeted sequentially for nets B1, B2, … BN. All B nets have exactly the same architecture, but different weights. Their respective inputs are made of different (but overlapping) subparts of Net A’s output.

In pseudo code:

A_Input = from_blob(rawInput)
A_Output = NetA(A_Input);

for i from 0 to N {

B_Input = assemble(A_Output, i); // operations in this function let gradients flow through
B_Output = NetB(B_Input);
out = mse_loss(B_output, targets[i]);
out.backward(); // does not work, I have to use out.backward({}, true)


When using out.backward(), I get a runtime error, on the second loop iteration, in libtorch_2.0.1_gpu_DEBUG_cu1_18\libtorch\include\ATen\core\ivalue.inl.h, line 975, in IValue value().

Do you have any idea of what could cause this issue ?