tumble-weed:

I thought it would be better to give executable code, will give snippet next time.


Ideally, you would come up with a minimal runnable example. If you look at the time economics of a forum like this, when we all do that it helps all of us get better answers to our questions, because the “answer time” is a relatively scarce resource.



 tumble-weed:

my problem seems to have gotten solved after doing x.grad.data.zero_(),


Usually you would call opt.zero_grad() or model.zero_grad().



 tumble-weed:

why allow the clone to affect the original gradients?


Because people want that and use .detach() when they don’t: .clone() means “different memory” but connected in autograd, .detach() means “disconnect in autograd”, but same memory.
Best regards
Thomas