Open bahriwissal opened 3 months ago
Hi @bahriwissal, our OpacusDPTrainer uses DPCallback (https://github.com/microsoft/dp-transformers/blob/f9fae445b1d3bb28355dbaac6720c007abb974ce/src/dp_transformers/dp_utils.py#L188C28-L188C38) which handles DP related operations in substeps and steps. Indeed self.optimizer.zero_grad()
should be called for opacus after each step so we do that as well in DPCallback here: https://github.com/microsoft/dp-transformers/blob/f9fae445b1d3bb28355dbaac6720c007abb974ce/src/dp_transformers/dp_utils.py#L78
I hope this helps, let us know if you have any further questions.
System Info
Error in line 2330 in the function
self.optimizer.step()
Gradients haven't been cleared since the last optimizer step. In order to obtain privacy guarantees you must call optimizer.zero_grad()on each stepExplanation Hello, I am trying to train a Tabula (https://github.com/zhao-zilong/Tabula) model using differential privacy. I rewrote my Tabula trainer to use the OpacusDPTrainer, but I encountered an error stating that optimizer.step() requires a call to optimizer.zero_grad() at each step to obtain privacy guarantees. I tried to resolve the problem by reimplementing the _inner_training_loop function from the transformers trainer and forcing self.optimizer.zero_grad() at each step, but I still get the same error.
Code Function start at line 1939, the modification is added to line 2032 ` def _inner_training_loop(