specksim#

Specksim is a high performance Spiking-Convolutional Neural Network simulator which is written in C++.

class SpecksimNetwork(graph: EventFilterGraph, graph_members: List[SamnaFilterNode], initial_sleep_duration: float = 1.0, subsequent_sleep_duration: float = 0.1)[source]#

Specksim simulation container object.

Parameters:
  • graph (samna.graph.EventFilterGraph) – A samna graph that contains the network layers as samna filters.

  • graph_members (List["SamnaFilterNode"]) – A list of samna filters.

  • initial_sleep_duration (float) – Sleep between writing and reading from the samna graph structure. This is

  • thread. (needed because the graph runs on a separate) –

  • subsequent_sleep_duration (float) – In order to not drop any events, we can sleep for more time.

add_monitor(spike_layer_number: int)[source]#

Add a monitor to the `spike_layer_number`th IAF layer.

Parameters:

spike_layer_number (int) – `spike_layer_number`th IAF layer to monitor

add_monitors(spike_layer_numbers: List[int])[source]#

Convenience function to add monitor to multiple spike layers.

Parameters:

spike_layer_numbers (List[int]) – Numbers of the spike spike layers to monitor.

clear_monitors()[source]#

Clear all monitors

forward(xytp: record) record[source]#

Applies the network forward pass given events.

Parameters:

xytp (np.record) – Input events as a numpy record array with keys (“x”, “y”, “t”, “p”)

Returns:

np.record – Output events as a numpy record array of the same type as the input events.

get_nth_spiking_layer(spike_layer_number: int) SpecksimIAFFilterNode[source]#

Get nth spiking layer for reading

Parameters:

spike_layer_number (int) – `spike_layer_number`th IAFFilter

Returns:

IAFFilter`spike_layer_number`th IAFFilter

output_dtype = dtype([('x', '<u4'), ('y', '<u4'), ('t', '<u4'), ('p', '<u4')])#
read_all_monitors()[source]#

Convenience method to read all the monitors

read_monitor(spike_layer_number: int) record[source]#

Read the events from the `spike_layer_number`th hidden spiking layer

Parameters:

spike_layer_number (int) – `spike_layer_number`th spiking layer to monitor.

Returns:

np.record

Events from `spike_layer_number`th spiking layer as a numpy

record array.

read_monitors(spike_layer_numbers: List[int]) Dict[int, record][source]#

Convenience method to read from multiple monitors

Parameters:

spike_layer_numbers (List[int]) – a list of spike layer numbers.

Returns:

Dict[int, np.record]

Dict with keys of spike_layer_numbers and events in np.record

format with 4 keys x, y, p, t

read_spiking_layer_states(spike_layer_number: int) List[List[List[int]]][source]#

Read the states of the `spike_layer_number`th spiking layer.

Parameters:

spike_layer_number (int) – `spike_layer_number`th spiking layer to read states from.

Returns:

List[List[List[int]]] – 3-dimensional list of states in (channel, y, x)

reset_states()[source]#

Reset the states of every spiking layer in the network to 0.

static specksim_spikes_to_xytp(spikes: List[Spike], output_dtype: dtype) record[source]#

Takes in specksim spikes and converts them to record array of with keys “x”, “y”, “t” and “p”

Parameters:
  • spikes (List[samna.specksim.events.Spike]) – A list of specksim spikes coming from the output

  • network. (of the) –

  • output_dtype – type of the output spikes. This is defined in the class implementation.

Returns:

np.record – A record array of the given output type

static xytp_to_specksim_spikes(xytp: record) List[Spike][source]#

Takes in xytp and returns a list of spikes compatible with specksim

Parameters:

xytp (np.record) – A numpy record array with 4 keys ‘x’, ‘y’, ‘t’ and ‘p’

Returns:

List[samna.specksim.events.Spike] – A list of specksim compatible spike events

calculate_weight_scale(layer: AvgPool2d)[source]#

Calculate the weight scale for the next weight layer given an AvgPool layer. This is necessary, because only real supported pooling layer is SumPooling for the simulator.

Parameters:

layer (nn.AvgPool2d) – torch Average pooling layer.

convert_convolutional_layer(layer: Conv2d, input_shape: Tuple[int, int, int], weight_scale: float = 1.0) Tuple[SpecksimConvolutionalFilterNode, Tuple[int, int, int]][source]#

Convert a convolutional layer to samna filter

Parameters:
  • layer (nn.Conv2d) – A PyTorch Convolutional Layer. The biases has to be disabled.

  • input_shape (Tuple[int, int, int]) – Input shape of the layer in (channel, y, x)

  • weight_scale (float) – Multiply the layer weights. This is often necessary when converting

  • AvgPool2d (models with) –

Returns:

Tuple[ConvFilter, Tuple[int, int, int]] – Returns the samna filter and the output shape of the layer

convert_iaf_layer(layer: IAF | IAFSqueeze, input_shape: Tuple[int, int, int]) Tuple[SpecksimIAFFilterNode, Tuple[int, int, int]][source]#

Convert a sinabs IAF layer into a specksim IAF Filter

Parameters:
  • layer (Union[sl.IAF, sl.IAFSqueeze]) – A Sinabs IAF layer

  • input_shape (Tuple[int, int, int]) – Input shape in (channel, y, x)

Returns:

Tuple[IAFFilter, Tuple[int, int, int]] – A specksim IAF Filter and the output shape

convert_linear_to_convolutional(layer: Linear, input_shape: Tuple[int, int, int]) Conv2d[source]#

Converts a linear layer to a convolutional layer. For original refer to: sinabs/backend/dynapcnn/dynapcnn_layer.py::DynapcnnLayer::_convert_linear_to_conv

Parameters:
  • layer (nn.Linear) – A linear layer

  • input_shape (Tuple[int, int, int]) – Input shape in (channel, y, x)

Returns:

nn.Conv2d – A convolutional layer

convert_pooling_layer(layer: AvgPool2d | SumPool2d, input_shape: Tuple[int, int, int]) Tuple[SpecksimSumPoolingFilterNode, Tuple[int, int, int]][source]#

Converts a pooling layer to a samna filter.

Parameters:
  • layer (Union[nn.AvgPool2d, sinabs.layers.SumPool2d]) – A pooling layer.

  • input_shape (Tuple[int, int, int]) – Input shape to the pooling layer in (channel, y, x)

Returns:

Tuple[SumPoolFilter, Tuple[int, int, int]] – Returns a tuple of sum pooling filter and the output shape in (channel, y, x)

from_sequential(network: Sequential, input_shape: Tuple[int, int, int]) SpecksimNetwork[source]#

Convert a sinabs network to a SpecksimNetwork

Parameters:
  • network (nn.Sequential) – A sequential sinabs model.

  • input_shape (Tuple[int, int, int]) – Network input shape in channel, y, x

Returns:

SpecksimNetwork

A container for the samna event-based filter that simualtes

the network

to_tuple(x)#