How to store the state and resume the state of the PrivacyEngine?

general: <p>Hi,</p>
<p>I run my computations on a server cluster where computation jobs have a time limit, but my learning process of multiple epochs typically takes longer than this time limit. Therefore, I regularly store the state of my computations (i.e., after every epoch), and the resume the computations when the job finished and I started a new one.</p>
<p>This especially includes using the .load_state_dict() function of the model and the optimizer.</p>
<p>Now I would like to integrate differential privacy with Opacus into my computations, and I am asking myself whether and how I can store the states of Opacus, and resume it? Specifically, my questions are:</p>
<ol>
<li>Can I just torch.save() the PrivacyEngine object?</li>
<li>If the answer to 1) is no, is there a .state_dict property and .load_state_dict() function for the PrivacyEngine?</li>
<li>Can I alternatively just run the model.load_state_dict() and optimizer.load_state_dict(), then update these by running privacy_engine.make_private(), and use the objects as normal?</li>
<li>In the case of 3), can I store the epsilon = privacy_engine.get_epsilon(DELTA) after every epoch (along with the model state and the optimizer state), and add the previous epsilon value to the epsilon value obtained after resuming the calculations? Or is this not an additive relation and I would end up with a wrong epsilon value when resuming the calculations?</li>
</ol>
<p>Thank you very much in advance!</p>

ffuuugor: <p>Hi,<br>
Thanks for your question - ability to save/load checkpoints is an important feature, and it’s good for us to have some input on how people could be using it.</p>
<p>While we’re considering how to include this into Opacus API here’s what you need to know to make it work today.</p>
<ol>
<li>
<code>PrivacyEngine</code> doesn’t maintain links to model, optimizer, or data_loader. The only important state maintained by <code>PrivacyEngine</code> is accountant. Accountant’s state is just a list of numerical tuples, so <code>torch.save()</code> or any other pickle mechanism should do. That said, you probably should save the accountant (<code>privacy_engine.accountant</code>), not the privacy engine itself. That’s because we also maintain the link to the dataset used in the first call to the <code>make_private()</code> method - to do a sanity check the dataset is not being swapped in the middle (accounting is performed on per-dataset basis)</li>
<li>While <code>GradSampleModule</code> functionally is just a wrapper around <code>nn.Module</code>, saving/loading probably doesn’t work for them out of the box. I’d say your best bet is to save/load underlying model and then wrap it with <code>GradSampleModule</code> every time you’re restoring from the checkpoint.</li>
</ol>
<p>To summarise, here are the steps you need to take</p>
<p>On saving:</p>
<ol>
<li>Save accountant: <code>torch.save(privacy_engine.accountant)</code>
</li>
<li>Save model: <code>torch.save(model. _module.state_dict())</code>
</li>
<li>If your optimizer has state (e.g. dynamic lr or momentum) - save wrapped optimizer: <code>optimizer.original_optimizer.state_dict()</code>
</li>
</ol>
<p>On loading:</p>
<ol>
<li>Initialize empty PrivacyEngine</li>
<li>Load accountant and replace brand new with the one you’ve just initialized: <code>privacy_engine.accountant = accountant_you_have_just_loaded_from_checkpoint</code>
</li>
<li>Load your non-private nn.Module as normal</li>
<li>Load your non-private optimizer as normal</li>
<li>Pass loaded model and optimzier to <code>privacy_engine.make_private()</code>
</li>
</ol>

general: <p>Thank you very much Igor for this well-structured answer, I will try it out</p>