lava-nc / lava-dl

Deep Learning library for Lava
https://lava-nc.org
BSD 3-Clause "New" or "Revised" License
149 stars 71 forks source link

RuntimeError when using Recurrent blocks #285

Open naveedunjum opened 6 months ago

naveedunjum commented 6 months ago

Describe the bug When I try to use the cuba Recurrent blocks in my network, I get RuntimeError: Output 0 of SelectBackward0 is a view and is being modified inplace. This view was created inside a custom Function (or because an input was returned as-is) and the autograd logic to handle view+inplace would override the custom backward associated with the custom Function, leading to incorrect gradients. This behavior is forbidden. You can fix this by cloning the output of the custom Function. I tried this with various networks, and also replaced the Dense layer with the Recurrent layer in the XOR regression or the Oxford Tutorial Here is the network I am using which is same as the XOR network with recurrent block:

class Network(torch.nn.Module):
    def __init__(self):
        super(Network, self).__init__()

        neuron_params = {
                'threshold'     : 0.1,
                'current_decay' : 1,
                'voltage_decay' : 0.1,
                'requires_grad' : True,     
            }

        self.blocks = torch.nn.ModuleList([
                slayer.block.cuba.Dense(neuron_params, 100, 256),
                slayer.block.cuba.Recurrent(neuron_params, 256, 256),
                slayer.block.cuba.Dense(neuron_params, 256, 1),
            ])

    def forward(self, spike):
        for block in self.blocks:
            spike = block(spike)
        return spike

To reproduce current behavior Steps to reproduce the behavior:

  1. Replace the Dense layer with the Recurrent layer in the XOR regression and Oxford Tutorial
  2. I get this error ... RuntimeError: Output 0 of SelectBackward0 is a view and is being modified inplace. This view was created inside a custom Function (or because an input was returned as-is) and the autograd logic to handle view+inplace would override the custom backward associated with the custom Function, leading to incorrect gradients. This behavior is forbidden. You can fix this by cloning the output of the custom Function.

Expected behavior Normally it should work without any problems, because the network is working well with the Dense layers.

Environment (please complete the following information):

PhilippPlank commented 4 months ago

Thank you for reporting this issue. @bamsumit Could you take a look :)

bamsumit commented 1 month ago

@naveedunjum can you check it again with the latest codebase? There was a change pushed recently.