I think that defining the parameters outside Function is the way to do it and I don’t know of any nicer ways to do it. I think Function isn’t meant to store state between multiple forward/backward passes.
The ctx object in Function's forward and backward gets created when forward is called and destroyed when backward is called (unless retain_graph is used I think, but I’m not too familiar with how that works).