Iterative pruning shouldn’t, by itself, cause any OOM issues (although I imagine your network is quite big and pruning in general needs to compute and attach masks at each iteration).
I’m suspicious of the retain_graph=True bit. That’s literally not allowing the memory used to store the graph to be freed, which probably causes the memory accumulation and eventual OOM. Instead of just setting that to true, can we try to find out what’s causing that error to be raised in the first place? It might have something to do with your specific architecture or training loop, because it doesn’t happen in general.
Do you have a minimal lightweight example that reproduces that error?