Hi,
The difference is that the current formula for repeat needs to access the original input (and so saves it) while expand does not.
So, if you share these ops across multiple backward, the expand version does not have any issue (since nothing was saved, nothing was cleared by the first backward) but the repeat version does not have the saved Tensors anymore and lead to this error.
If you want to use repeat, you will have to do it at each forward I’m afraid.
The difference of behavior though could be removed (and repeat made a bit faster), opened an issue to track that: https://github.com/pytorch/pytorch/issues/40701 11