utils#

sinabs.backend.dynapcnn.utils.convert_cropping2dlayer_to_crop2d(layer: Cropping2dLayer, input_shape: Tuple[int, int]) Crop2d[source]#

Convert a sinabs layer of type Cropping2dLayer to Crop2d layer.

Parameters:
  • layer (Cropping2dLayer) – Cropping2dLayer.

  • input_shape (Tuple[int, int]) – (height, width) input dimensions.

Returns:

Equivalent Crop2d layer.

Return type:

Crop2d

sinabs.backend.dynapcnn.utils.extend_readout_layer(model: DynapcnnNetwork) DynapcnnNetwork[source]#

Return a copied and extended model with the readout layer extended to 4 times the number of output channels. For Speck 2E and 2F, to get readout with correct output index, we need to extend the final layer to 4 times the number of output.

Parameters:

model (DynapcnnNetwork) – the model to be extended

Returns:

the extended model

Return type:

DynapcnnNetwork

sinabs.backend.dynapcnn.utils.get_device_id(device_type: str, index: int) str[source]#

Generate a device id string given a device type and its index.

Parameters:
  • device_type (str) – Device type

  • index (int) – Device index

Returns:

A string of the form device_type:index

Return type:

str

sinabs.backend.dynapcnn.utils.infer_input_shape(snn: Module, input_shape: Tuple[int, int, int] | None = None) Tuple[int, int, int][source]#

Infer expected shape of input for snn either from input_shape or from DVSLayer instance within snn which provides it.

If neither are available, raise an InputConfigurationError. If both are the case, verify that the information is consistent.

Parameters:
  • snn (nn.Module) – The SNN whose input shape is to be inferred.

  • input_shape (tuple or None) – Explicitly provide input shape. If not None, must be of the format (channels, height, width).

Returns:

The input shape to snn, in the format (channels, height, width)

Return type:

Tuple[int, int, int]

sinabs.backend.dynapcnn.utils.merge_bn(weight_layer: WeightLayer, bn: BatchNorm1d | BatchNorm2d) WeightLayer[source]#

Merge a convolutional or linear layer with subsequent batch normalization.

Parameters:
  • weight_layer (WeightLayer) – torch.nn.Conv2d or nn.Linear. Convolutional or linear layer

  • bn (BatchNorm1d | BatchNorm2d) – torch.nn.Batchnorm2d or nn.Batchnorm1d. Batch normalization.

Returns:

Weight layer including batch normalization.

Return type:

WeightLayer

sinabs.backend.dynapcnn.utils.merge_conv_bn(conv: Conv2d, bn: BatchNorm2d) Conv2d[source]#

Merge a convolutional layer with subsequent batch normalization.

Parameters:
  • conv (Conv2d) – torch.nn.Conv2d. Convolutional layer.

  • bn (BatchNorm2d) – torch.nn.Batchnorm2d. Batch normalization.

Returns:

Convolutional layer including batch normalization.

Return type:

Conv2d

sinabs.backend.dynapcnn.utils.parse_device_id(device_id: str) Tuple[str, int][source]#

Parse device id into device type and device index.

Parameters:

device_id (str) – Device id typically of the form device_type:index. In case no index is specified, the default index of zero is returned.

Returns:

(device_type, index) Returns a tuple with the index and device type.

Return type:

Tuple[str, int]

sinabs.backend.dynapcnn.utils.standardize_device_id(device_id: str) str[source]#

Standardize device id string.

Parameters:

device_id (str) – Device id string. Could be of the form device_type or device_type:index

Returns:

Returns a sanitized device id of the form device_type:index

Return type:

str

sinabs.backend.dynapcnn.utils.topological_sorting(edges: Set[Tuple[int, int]]) List[int][source]#

Performs a topological sorting (using Kahn’s algorithm) of a graph described by a list of edges. An entry node X of the graph have to be flagged inside edges by a tuple (‘input’, X).

Parameters:

edges (set) – the edges describing the acyclic graph.

Returns:

The nodes sorted by the graph’s topology.

Return type:

List[int]