If I understand this correctly, then, if self.active the model should produce an output and allow backpropagation as usual.
If self.active == False then the model should run only once, detach the output from the computation graph and store it for future use.
If that is what you want then your code is fine.
One remark though, the detached output from this line
detach = self.out[self.counter]

ought to be called detached not detach because detach is a verb, not a noun, and won’t convey any useful meaning to anyone with good english. That is partly why I was confused by your question and needed to see your code.