I think you are right about the stored context information being the source of my problem. I’ve temporarily reduced the memory requirement of my fn() and tested it within a loop. After a few iterations the remaining memory stops to grow and stays at ~900 MB. I don’t think I accidentally store the computation graph, since

I’ve detached the result of fn(), and
I’ve deleted every Tensor out of desperation without effect.


Do you have any idea why the size of CUDA context grows if I execute fn() multiple times? I was planning on doing something like this:
def main():
    fn(parameter_set1)
    fn(parameter_set2)
    ...

If fn() needs more memory than what is available at maximum minus the CUDA context, the second call fails. Unfortunately this is the case for me.