From b68e588bd9e3d7cd9402d464800d7418a9438b8c Mon Sep 17 00:00:00 2001 From: Willian-Girao Date: Wed, 30 Oct 2024 21:16:06 +0100 Subject: [PATCH] DONE - chip deployment with DVS config_builder.build_config() returns 'Network is valid'. --- sinabs/backend/dynapcnn/chips/dynapcnn.py | 26 +++++++++---------- sinabs/backend/dynapcnn/dynapcnn_network.py | 8 +++--- .../dynapcnn/dynapcnnnetwork_module.py | 6 +---- .../backend/dynapcnn/sinabs_edges_handler.py | 2 +- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/sinabs/backend/dynapcnn/chips/dynapcnn.py b/sinabs/backend/dynapcnn/chips/dynapcnn.py index eaf952eb..6e4fb536 100644 --- a/sinabs/backend/dynapcnn/chips/dynapcnn.py +++ b/sinabs/backend/dynapcnn/chips/dynapcnn.py @@ -33,7 +33,6 @@ def get_dvs_layer_config_dict(cls, layer: DVSLayer): ... # def write_dvs_layer_config(cls, layer: DVSLayer, config: DVSLayerConfig): # for param, value in layer.get_config_dict().items(): # setattr(config, param, value) - @classmethod def write_dvs_layer_config( @@ -51,7 +50,7 @@ def write_dvs_layer_config( ---------- - layer (DVSLayer): Layer instance from which to generate the config - layer2core_map (Dict): Keys are layer indices, values are corresponding - cores on hardware. Needed to map the destinations.] + cores on hardware. Needed to map the destinations. - destination_indices (List): Indices of destination layers for `layer` - chip_layer (DVSLayerConfig): Configuration object of the corrsesponding on-chip core. Will be changed in-place based on `layer`. @@ -295,7 +294,7 @@ def build_config( ---------- - layers (Dict): Keys are layer indices, values are DynapcnnLayer instances. - layer2core_map (Dict): Keys are layer indices, values are corresponding - cores on hardware. Needed to map the destinations.] + cores on hardware. Needed to map the destinations. - destination_indices (List): Indices of destination layers for `layer` - dvs_node_info (dict): contains information associated with the `DVSLayer` node (if no DVS node exists it'll return `None`). @@ -307,16 +306,6 @@ def build_config( config = cls.get_default_config() config.dvs_layer.pass_sensor_events = False - # Uses the DVS camera. - if isinstance(dvs_node_info, dict): - chip_layer = config.dvs_layer - sw_layer = dvs_node_info['module'] - destination_indices = dvs_node_info['destinations'] - # Write camera configuration. - cls.write_dvs_layer_config(sw_layer, layer2core_map, destination_indices, chip_layer) - - # TODO - for now it's being handled separatly but it might make more sense to handle it within `layers`. - # Loop over layers in network and write corresponding configurations for layer_index, ith_dcnnl in layers.items(): if isinstance(ith_dcnnl, DynapcnnLayer): @@ -329,6 +318,17 @@ def build_config( chip_layer=chip_layer, destination_indices=destination_map[layer_index], ) + elif isinstance(ith_dcnnl, DVSLayer) and isinstance(dvs_node_info, dict): + # Uses the DVS camera. + chip_layer = config.dvs_layer + sw_layer = ith_dcnnl + destination_indices = destination_map[layer_index] + # Write camera configuration. + cls.write_dvs_layer_config( + layer=sw_layer, + layer2core_map=layer2core_map, + destination_indices=destination_indices, + chip_layer=chip_layer) else: # shouldn't happen since type checks are made previously. raise TypeError( diff --git a/sinabs/backend/dynapcnn/dynapcnn_network.py b/sinabs/backend/dynapcnn/dynapcnn_network.py index d195c6de..67cb8f0e 100644 --- a/sinabs/backend/dynapcnn/dynapcnn_network.py +++ b/sinabs/backend/dynapcnn/dynapcnn_network.py @@ -508,8 +508,6 @@ def _make_config( the provided device can be found. """ config_builder = ChipFactory(device).get_config_builder() - - # TODO not handling DVSLayer yet. has_dvs_layer = self.has_dvs_layer() if chip_layers_ordering is not None: @@ -545,7 +543,7 @@ def _make_config( ) if has_dvs_layer: - # TODO not handling DVSLayer yet. + # TODO - DVS layer has been incorporated: what should happen here? pass self._layer2core_map = layer2core_map @@ -554,10 +552,10 @@ def _make_config( layers=self.dynapcnn_layers, destination_map=self.layer_destination_map, layer2core_map=layer2core_map, - dvs_node_info=dvs_node_info, + dvs_node_info=self.dvs_node_info, ) - # TODO not handling DVSLayer yet (this is from the old implementation, should be revised). + # TODO - this is from the old implementation, should be revised. if self.input_shape and self.input_shape[0] == 1: config.dvs_layer.merge = True diff --git a/sinabs/backend/dynapcnn/dynapcnnnetwork_module.py b/sinabs/backend/dynapcnn/dynapcnnnetwork_module.py index 009946a8..7f290c57 100644 --- a/sinabs/backend/dynapcnn/dynapcnnnetwork_module.py +++ b/sinabs/backend/dynapcnn/dynapcnnnetwork_module.py @@ -80,11 +80,7 @@ def destination_map(self): @property def dynapcnn_layers(self): # Convert string-indices to integers-indices - dynapcnn_layers = {int(idx): lyr for idx, lyr in self._dynapcnn_layers.items()} - # Insert DVS node if DVS was enabled. - if isinstance(self.dvs_node_info, Dict): - dynapcnn_layers[self.dvs_node_info['layer_id']] = self.dvs_node_info['module'] - return dynapcnn_layers + return {int(idx): lyr for idx, lyr in self._dynapcnn_layers.items()} @property def entry_points(self): diff --git a/sinabs/backend/dynapcnn/sinabs_edges_handler.py b/sinabs/backend/dynapcnn/sinabs_edges_handler.py index 656c93d4..d050bcd3 100644 --- a/sinabs/backend/dynapcnn/sinabs_edges_handler.py +++ b/sinabs/backend/dynapcnn/sinabs_edges_handler.py @@ -71,7 +71,7 @@ def fix_dvs_module_edges(edges: Set[Edge], indx_2_module_map: Dict[int, nn.Modul if any(isinstance(module, dvs_node) for dvs_node in (DVSLayer, Crop2d, FlipDims)) } - if len(dvslayer_nodes) == 1: + if len(dvslayer_nodes) <= 1: # No module within the DVSLayer instance appears as an independent node - nothing to do here. return