synsense / sinabs

A deep learning library for spiking neural networks which is based on PyTorch, focuses on fast training and supports inference on neuromorphic hardware.
https://sinabs.readthedocs.io
GNU Affero General Public License v3.0
77 stars 8 forks source link

[Bug] DynapcnnCompatibleNetwork.make_config() will monitor wrong layer if not set "monitor_layers" as ["dvs", -1] #151

Closed ssinhaleite closed 9 months ago

ssinhaleite commented 9 months ago

Description

e.g. if I call the DynapcnnCompatibleNetwork.make_config() as below:

DynapcnnCompatibleNetwork.make_config(chip_layers_ordering=["dvs", 0, 1, 2, 3], monitor_layers=["dvs", 3])

fron line # 256 to line # 259. The input argument "monitor_layers" is used as below:

        monitor_chip_layers = [self.find_chip_layer(lyr) for lyr in monitor_layers if lyr != "dvs"]
        if "dvs" in monitor_layers:
            monitor_chip_layers.append("dvs")
        config_builder.monitor_layers(config, monitor_chip_layers)

But when the method "self.find_chip_layer(lyr)" executed, the string "dvs" is eliminated, but self.chip_layers_ordering still contains the "dvs" string. So it will return the wrong layer index in the self.find_chip_layer(lyr).

Solution

Modify the function

    def find_chip_layer(self, layer_idx):
        """
        Given an index of a layer in the model, find the corresponding chip layer where it is placed

        Parameters
        ----------
        layer_idx: int
            Index of a layer

        Returns
        -------
        chip_lyr_idx: int
            Index of the layer on the chip where the model layer is placed.
        """
        if len(self.chip_layers_ordering) != len(self.compatible_layers):
            raise Exception("Looks like the model has not been mapped onto a device.")

        return self.chip_layers_ordering[layer_idx]

as

    def find_chip_layer(self, layer_idx):
        """
        Given an index of a layer in the model, find the corresponding chip layer where it is placed

        Parameters
        ----------
        layer_idx: int
            Index of a layer

        Returns
        -------
        chip_lyr_idx: int
            Index of the layer on the chip where the model layer is placed.
        """
        if len(self.chip_layers_ordering) != len(self.compatible_layers):
            raise Exception("Looks like the model has not been mapped onto a device.")

        chip_layers = self.chip_layers_ordering.copy()
        if "dvs" in self.chip_layers_ordering:
            # solve the idx mismatch if "dvs" in self.chip_layers_ordering
            chip_layers.remove("dvs")
        return chip_layers[layer_idx]