Gradient updates not generated in remote backpropagation with custom GRU/RNN
Describe the bug
No gradient is produced following the remote backpropagation step neither in the model's parameters nor in the worker's parameters. To Reproduce
Steps to reproduce the behavior:
- Extract all the files in the
minimal_GRU_2.0.ziparchive to a folder of your choice. The files' download link is at the bottom of this Issue - Navigate to the folder where the files have been extracted
- Run the following command:
python3 minimal_gru_init.py - See the [0,0,...0] tensors being generated as model's gradient parameters, meaning that the gradients did not get updated.
- Inspect the code. The issue lies in file
minimal_gru_init.pyin the functiontrain_remote_model_one_epochat line:loss.backward().
To inspect the gradient, just check the lines:
for param_remote in remote_model.parameters():
#produces a matrix containing just zeroes
print(param_remote.grad.copy().get())
Expected behavior
I would expect that the parameter's gradient updates are actually generated. In a local version of the same code, where backpropagation is performed locally, the gradient update is indeed generated for all parameters.
Screenshots
LOCAL gradient (first parameter of the model) - Correct behaviour

REMOTE gradient (first parameter of the model): - Erroneous behaviour

**Desktop **
- OS: Ubuntu
- Version 18.04 LTS
Additional context I've been stuck on this issue for a weeks now and it's extremely important for the work I'm conducting with PySyft to have it fixed. Any help would be greatly appreciated. I tried to experiment with AutogradTensor to see if there was any difference in terms of gradient produced, but the gradient updates still were not produced.
Also note that you're going to need the .size() method to test RNNs-GRUs. You may clone the repository of this PR to get its implementation: https://github.com/OpenMined/PySyft/pull/2343
Minimal code example and dataset: minimal_GRU_2.0.zip
@LaRiffle @robert-wagner @iamtrask @mari-linhares Thoughts? . Any ideas or help would be greatly appreciated.
Hey @DanyEle What happens if you don't call .copy before .get
You can't .get() on a data attribute (syft.exceptions.CannotRequestObjectAttribute)
Rip okay. What is the result of copy locally?
If you want to inspect in a preserving manner you can directly access the remote object:
worker = sy.hook.local_worker.get_worker(param_remote.location)
print(worker._objects[param_remote.id_at_location].grad)
You get 8 param grad, the 6 first are only zeros but not the two last one: zeros: encoder.weight, rnn.weight_ih, rnn.weight_hh, rnn.bias_ih, rnn.bias_hh non-zeros: decoder.weight & decoder.bias
Try this for your forward pass: (check carefully the differences with the send/get stufff to understand better what was not working)
def forward(self, input, hidden, worker=None):
print(input)
emb = self.drop(self.encoder(input))
output = []
if self.rnn_type in ['GRU', 'GRUCell']:
hx = hidden
for i in range(emb.shape[0]): #Daniele: was emb.size(0)
hx = self.rnn(emb[i, :], hx)
output.append(hx.unsqueeze(0))
output = torch.cat(output, dim=0)
else:
# else do smthg to instantiate output
raise ValueError
output = self.drop(output)
decoded = self.decoder(output.view(output.size(0) * output.size(1), output.size(2)))
return (decoded.view(output.size(0), output.size(1), decoded.size(1)), hx)
And to print the grad safely do:
#BACKPROPAGATION - ISSUES OCCUR HERE! Gradients are [0,0,...,0]
for k, param_remote in remote_model.named_parameters():
print(k)
worker = sy.hook.local_worker.get_worker(param_remote.location)
print(worker._objects[param_remote.id_at_location].grad)
#print(param_remote.grad.get())
It works on my side I get non-zero gradients
The minimal_gru_init.py does not run completely as we said because the hook on size() can't work: just replace the occurence of size() with .shape in your implem, remove the hook of size() and this error should disappear
Yes, it's indeed working!! Also the quantization example, after removing calls to clone() on the input , but operating in place on the variables, is working!!!! :)