I think avoiding multiple calls of backward() resolves.
For example, initialize loss with None and accumulating loss calculated in each iteration and call backward after the for loop like
loss = None
for _ range(iters):
    bp.inference()
    cur_loss = torch.abs(target - inference.beliefs).sum()
    if loss is None: loss = cur_loss
    else: loss += cur_loss
loss.backward()

Toy example is computational-graph-is-only-built-once.ipynb · GitHub