Quote likely you are doing something wrong elsewhere and store a non-leaf (like .to() before storing).
As a rule of thumb retain_grad=True is only for cases when you know exactly why you need it, not for when some error message tells you (maybe we should improve the message).
Best regards
Thomas