PySyft icon indicating copy to clipboard operation
PySyft copied to clipboard

Gradient updates not generated in remote backpropagation with custom GRU/RNN

Open DanyEle opened this issue 7 years ago • 9 comments

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:

  1. Extract all the files in the minimal_GRU_2.0.zip archive to a folder of your choice. The files' download link is at the bottom of this Issue
  2. Navigate to the folder where the files have been extracted
  3. Run the following command: python3 minimal_gru_init.py
  4. See the [0,0,...0] tensors being generated as model's gradient parameters, meaning that the gradients did not get updated.
  5. Inspect the code. The issue lies in file minimal_gru_init.py in the function train_remote_model_one_epoch at 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

local_gradient

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

image

**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

DanyEle avatar Jul 17 '19 06:07 DanyEle

@LaRiffle @robert-wagner @iamtrask @mari-linhares Thoughts? . Any ideas or help would be greatly appreciated.

DanyEle avatar Jul 17 '19 07:07 DanyEle

Hey @DanyEle What happens if you don't call .copy before .get

robert-wagner avatar Jul 17 '19 14:07 robert-wagner

You can't .get() on a data attribute (syft.exceptions.CannotRequestObjectAttribute)

LaRiffle avatar Jul 17 '19 14:07 LaRiffle

Rip okay. What is the result of copy locally?

robert-wagner avatar Jul 17 '19 14:07 robert-wagner

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)

LaRiffle avatar Jul 17 '19 14:07 LaRiffle

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

LaRiffle avatar Jul 17 '19 14:07 LaRiffle

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

LaRiffle avatar Jul 17 '19 15:07 LaRiffle

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

LaRiffle avatar Jul 17 '19 15:07 LaRiffle

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!!!! :)

DanyEle avatar Jul 17 '19 17:07 DanyEle