qualia_plugin_snn.postprocessing package

Submodules

Module contents

Qualia-Plugin-SNN postprocessing package contains postprocessing modules adapted for or dedicated to Spiking Neural Networks.

class qualia_plugin_snn.postprocessing.EnergyEstimationMetric[source]

Bases: PostProcessing[Module]

Analytical energy estimation metric.

From An Analytical Estimation of Spiking Neural Networks Energy Efficiency, Lemaire et al. ICONIP2022.

@inproceedings{EnergyEstimationMetricICONIP2022,
    title = {An Analytical Estimation of Spiking Neural Networks Energy Efficiency},
    author = {Lemaire, Edgar and Cordone, Loïc and Castagnetti, Andrea
              and Novac, Pierre-Emmanuel and Courtois, Jonathan and Miramond, Benoît},
    booktitle = {Proceedings of the 29th International Conference on Neural Information Processing},
    pages = {574--587},
    year = {2023},
    doi = {10.1007/978-3-031-30105-6_48},
    series = {ICONIP},
}

Supports sequential (non-residual) formal and spiking convolutional neural networks with the following layers:

energy_values: Final[dict[int, dict[str, float]]] = {8: {'add': 0.03, 'mul': 0.2}, 32: {'add': 0.1, 'mul': 3.1}}

Energy values for different bit widths and operations.

Default from Computing’s Energy Problem (and what we can do about it), Mark Horowitz, ISSCC 2014.

__init__(mem_width: int, fifo_size: int = 0, total_spikerate_exclude_nonbinary: bool = True, op_estimation_type: dict[str, str] | None = None, sram_estimation_type: str | None = None) None[source]

Construct qualia_plugin_snn.postprocessing.EnergyEstimationMetric.EnergyEstimationMetric.

Parameters:
  • mem_width (int) – Memory access size in bits, e.g. 16 for 16-bit quantization

  • fifo_size (int) – Size of the input/output FIFOs for each layer in SPLEAT

  • total_spikerate_exclude_nonbinary (bool) – If True, exclude non-binary inputs/outputs from total spikerate computation

  • op_estimation_type (dict[str, str] | None) – Optional estimation type for the energy values, one of ‘ICONIP’, ‘saturation’, ‘linear’, ‘quadratic’, defaults to ‘ICONIP’, see _set_energy_values() and _set_op_estimation_type()

  • sram_estimation_type (str | None) – Optional SRAM estimation algorithm, ‘old’ (ICONIP2022) or ‘new’ (T. Louis), defaults to ‘old’, see :meth:``_e_ram`

Return type:

None

_set_op_estimation_type(bit_width: int, op_type: str, op_estimation_type: str | int) float[source]

Set the estimation type for the energy values.

If op_estimation_type is not in [‘ICONIP’, ‘saturation’, ‘linear’, ‘quadratic’], raise ValueError.

If op_estimation_type is ‘’ICONIP’, use the energy values from the ICONIP 2022 paper (i.e. 32-bit values).

If op_estimation_type is ‘saturation’, use self.energy_values[8][type] for 8-bit and below, and self.energy_values[32][type] for bit widths between 9 and 32.

If op_estimation_type is ‘linear’, use self.energy_values[8][type] and self.energy_values[32][type] to estimate the energy values by solving a linear equation: y = m*bit_width + c.

If op_estimation_type is ‘quadratic’, use self.energy_values[8][type] and self.energy_values[32][type] to estimate the energy values by solving a quadratic equation: y = a*bit_width^2 + b*bit_width + c.

Parameters:
  • bit_width (int) – Bit width to compute energy for

  • op_type (str) – Operation type, e.g., ‘add’ or ‘mul’

  • op_estimation_type (str | int) – The estimation type for the energy values, one of ‘ICONIP’, ‘saturation’, ‘linear’, ‘quadratic’

Returns:

Energy for the given bit width, operation and estimation type

Raises:

ValueError – When op_estimation_type is invalid, or bit_width is out of bounds for ‘saturation’ op_estimation_type

Return type:

float

_set_energy_values(bit_width: int, op_estimation_type: dict[str, str] | None) None[source]

Set the operation energy values for the given bit width.

If op_estimation_type is set, use _set_op_estimation_type() to infer the values according to the wanted bit width and type.

Otherwise, if bit_width is in energy_values use the predefined energy values in energy_values.

Otherwise, defaults to using predefined 32-bit values from energy_values.

Parameters:
  • bit_width (int) – The bit width for the energy values.

  • op_estimation_type (dict[str, str] | None) – The estimation type for the energy values.

Return type:

None

_rdin_conv_fnn(layer: TConvLayer) int[source]

Count number of read operations for the input of a convolutional layer in a formal neural network.

Eq. 3: Cin × Cout × Hout × Wout × Wkernel × Hkernel.

Parameters:

layer (TConvLayer) – A convolutional layer

Returns:

Number of read operations for the input of a convolutional layer in a formal neural network

Return type:

int

_e_rdin_conv_fnn(layer: TConvLayer, e_rdram: Callable[[int], float]) float[source]

Compute energy for read operations for the input of a convolution layer in a formal neural network.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Energy for read operations for the input of a convolution layer in a formal neural network

Return type:

float

_rdin_fc_fnn(layer: TDenseLayer) int[source]

Count number of read operations for the input of a fully-connected layer in a formal neural network.

Eq. 4: Nin.

Parameters:

layer (TDenseLayer) – A fully-connected layer

Returns:

Number of read operations for the input of a fully-connected layer in a formal neural network

Return type:

int

_e_rdin_fc_fnn(layer: TDenseLayer, e_rdram: Callable[[int], float]) float[source]

Compute energy for read operations for the input of a fully-connected layer in a formal neural network.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Energy for read operations for the input of a fully-connected layer in a formal neural network

Return type:

float

_rdin_add_fnn(layer: TAddLayer) int[source]

Count number of read operations for the input of an add layer in a formal neural network.

#InLayers × Nin.

Parameters:

layer (TAddLayer) – An add layer

Returns:

Number of read operations for the input of an add layer in a formal neural network

Return type:

int

_e_rdin_add_fnn(layer: TAddLayer, e_rdram: Callable[[int], float]) float[source]

Compute energy for read operations for the input of an add layer in a formal neural network.

Parameters:
  • layer (TAddLayer) – An add layer

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Energy for read operations for the input of an add layer in a formal neural network

Return type:

float

_rdweights_conv_fnn(layer: TConvLayer) int[source]

Count number of read operations for the weights of a convolutional layer (excluding bias) in a formal neural network.

Eq. 6.1f: Cin × Wkernel × Hkernel × Cout × Wout × Hout.

Parameters:

layer (TConvLayer) – A convolutional layer

Returns:

Number of read operations for the weights of a convolutional layer (excluding bias) in a formal neural network

Return type:

int

_e_rdweights_conv_fnn(layer: TConvLayer, e_rdram: Callable[[int], float]) float[source]

Compute energy for read operations for the weights of a convolutional layer (excluding bias) in a formal neural network.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Energy for read operations for the weights of a convolutional layer (excluding bias) in a formal neural network

Return type:

float

_rdbias_conv_fnn(layer: TConvLayer) int[source]

Count number of read operations for the biases of a convolutional layer in a formal neural network.

Eq. 6.1s: Cout × Wout × Hout.

Parameters:

layer (TConvLayer) – A convolutional layer

Returns:

Number of read operations for the biases of a convolutional layer in a formal neural network

Return type:

int

_e_rdbias_conv_fnn(layer: TConvLayer, e_rdram: Callable[[int], float]) float[source]

Compute energy for read operations for the biases of a convolutional layer in a formal neural network.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Energy for read operations for the biases of a convolutional layer in a formal neural network or 0 if the layer does not use biases

Return type:

float

_rdweights_fc_fnn(layer: TDenseLayer) int[source]

Count number of read operations for the weights of a fully-connected layer (excluding bias) in a formal neural network.

Eq. 6.2f: Nin × Nout.

Parameters:

layer (TDenseLayer) – A fully-connected layer

Returns:

Number of read operations for the weights of a fully-connected layer (excluding bias) in a formal neural network

Return type:

int

_e_rdweights_fc_fnn(layer: TDenseLayer, e_rdram: Callable[[int], float]) float[source]

Compute energy for read operations for weights of a fully-connected layer (excluding bias) in a formal neural network.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Energy for read operations for the weights of a fully-connected layer (excluding bias) in a formal neural network

Return type:

float

_rdbias_fc_fnn(layer: TDenseLayer) int[source]

Count number of read operations for the biases of a fully-connected layer in a formal neural network.

Eq. 6.2s: Nout.

Parameters:

layer (TDenseLayer) – A fully-connected layer

Returns:

Number of read operations for the biases of a fully-connected layer in a formal neural network

Return type:

int

_e_rdbias_fc_fnn(layer: TDenseLayer, e_rdram: Callable[[int], float]) float[source]

Compute energy for read operations for the biases of a fully-connected layer in a formal neural network.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Energy for read operations for the biases of a fully-connected layer in a formal neural network or 0 if the layer does not use biases

Return type:

float

_wrout_conv_fnn(layer: TConvLayer) int[source]

Count number of write operations for the output of a convolutional layer in a formal neural network.

Eq. 11: Cout × Hout × Wout.

Parameters:

layer (TConvLayer) – A convolutional layer

Returns:

Number of write operations for the output of a convolutional layer in a formal neural network

Return type:

int

_e_wrout_conv_fnn(layer: TConvLayer, e_wrram: Callable[[int], float]) float[source]

Compute energy for write operations for the output of a convolutional layer in a formal neural network.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • e_rdram – Function to compute memory write access energy for a given memory size

  • e_wrram (Callable[[int], float])

Returns:

Energy for write operations for the output of a convolutional layer in a formal neural network

Return type:

float

_wrout_fc_fnn(layer: TDenseLayer) int[source]

Count number of write operations for the output of a fully-connected layer in a formal neural network.

Eq. 12: Nout.

Parameters:

layer (TDenseLayer) – A fully-connected layer

Returns:

Number of write operations for the output of a fully-connected layer in a formal neural network

Return type:

int

_e_wrout_fc_fnn(layer: TDenseLayer, e_wrram: Callable[[int], float]) float[source]

Compute energy for write operations for the output of a fully-connected layer in a formal neural network.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • e_rdram – Function to compute memory write access energy for a given memory size

  • e_wrram (Callable[[int], float])

Returns:

Energy for write operations for the output of a fully-connected layer in a formal neural network

Return type:

float

_wrout_add_fnn(layer: TAddLayer) int[source]

Count number of write operations for the output of an add layer in a formal neural network.

Nout.

Parameters:

layer (TAddLayer) – An add layer

Returns:

Number of write operations for the output of an add layer in a formal neural network

Return type:

int

_e_wrout_add_fnn(layer: TAddLayer, e_wrram: Callable[[int], float]) float[source]

Compute energy for write operations for the output of an add layer in a formal neural network.

Parameters:
  • layer (TAddLayer) – An add layer

  • e_rdram – Function to compute memory write access energy for a given memory size

  • e_wrram (Callable[[int], float])

Returns:

Energy for write operations for the output of an add layer in a formal neural network

Return type:

float

_mac_ops_conv_fnn(layer: TConvLayer) int[source]

Count number of multiply-accumulate operations inside a convolutional layer in a formal neural network.

Eq. 1.1: Cout × Hout × Wout × Cin × Hkernel × Wkernel.

Parameters:

layer (TConvLayer) – A convolutional layer

Returns:

Number of multiply-accumulate operations inside a convolutional layer in a formal neural network.

Return type:

int

_e_ops_conv_fnn(layer: TConvLayer) float[source]

Compute energy for multiply-accumulate and accumulate operations inside convolutional layer in a formal neural network.

Parameters:

layer (TConvLayer) – A convolutional layer

Returns:

Energy for multiply-accumulate and accumulate operations inside a convolutional layer in a formal neural network.

Return type:

float

_mac_ops_fc_fnn(layer: TDenseLayer) int[source]

Count number of multiply-accumulate operations inside a fully-connected layer in a formal neural network.

Eq. 2.1: Nin × Nout.

Parameters:

layer (TDenseLayer) – A fully-connected layer

Returns:

Number of multiply-accumulate operations inside a fully-connected layer in a formal neural network.

Return type:

int

_acc_ops_fc_fnn(layer: TDenseLayer) int[source]

Count number of accumulate operations inside a fully-connected layer in a formal neural network.

Eq. 2.2: Nout.

Parameters:

layer (TDenseLayer) – A fully-connected layer

Returns:

Number of accumulate operations inside a fully-connected layer in a formal neural network.

Return type:

int

_e_ops_fc_fnn(layer: TDenseLayer) float[source]

Compute energy for multiply-accumulate and accumulate operations inside fully-connected layer in formal neural network.

Parameters:

layer (TDenseLayer) – A fully-connected layer

Returns:

Energy for multiply-accumulate and accumulate operations inside a fully-connected layer in formal neural network.

Return type:

float

_mac_ops_add_fnn(_: TAddLayer) int[source]

Count number of multiply-accumulate operations inside an add layer in a formal neural network.

Parameters:
Returns:

Return type:

int

_acc_ops_add_fnn(layer: TAddLayer) int[source]

Count number of accumulate operations inside an add layer in a formal neural network.

(#InLayers -1 ) × Nin Nout.

Parameters:

layer (TAddLayer) – An add layer

Returns:

Number of accumulate operations inside an add layer in a formal neural network.

Return type:

int

_e_ops_add_fnn(layer: TAddLayer) float[source]

Compute energy for multiply-accumulate and accumulate operations inside an add layer in formal neural network.

Parameters:

layer (TAddLayer) – A fully-connected layer

Returns:

Energy for multiply-accumulate and accumulate operations inside an add layer in formal neural network.

Return type:

float

_mac_addr_conv_fnn(_: TConvLayer) int[source]

Count number of multiply-accumulate operations for addressing a convolutional layer in a formal neural network.

0

Returns:

0

Parameters:

_ (TConvLayer)

Return type:

int

_acc_addr_conv_fnn(layer: TConvLayer) int[source]

Count number of accumulate operations for addressing a convolutional layer in a formal neural network.

Cin × Hin × Win + Cout × Hout × Wout + Cout × Hkernel × Wkernel.

Parameters:

layer (TConvLayer) – A convolutional layer

Returns:

Number of accumulate operations for addressing a convolutional layer in a formal neural network

Return type:

int

_e_addr_conv_fnn(layer: TConvLayer) float[source]

Compute energy for addressing a convolutional layer in a formal neural network.

Parameters:

layer (TConvLayer) – A convolutional layer

Returns:

Energy for addressing a convolutional layer in a formal neural network.

Return type:

float

_mac_addr_fc_fnn(_: TDenseLayer) int[source]

Count number of multiply-accumulate operations for addressing a fully-connected layer in a formal neural network.

0

Returns:

0

Parameters:

_ (TDenseLayer)

Return type:

int

_acc_addr_fc_fnn(layer: TDenseLayer) int[source]

Count number of accumulate operations for addressing a fully-connected layer in a formal neural network.

Nin × Nout.

Parameters:

layer (TDenseLayer) – A fully-connected layer

Returns:

Number of accumulate operations for addressing a fully-connected layer in a formal neural network

Return type:

int

_e_addr_fc_fnn(layer: TDenseLayer) float[source]

Compute energy for addressing a fully-connected layer in a formal neural network.

Parameters:

layer (TDenseLayer) – A fully-connected layer

Returns:

Energy for addressing a fully-connected layer in a formal neural network.

Return type:

float

_mac_addr_add_fnn(_: TAddLayer) int[source]

Count number of multiply-accumulate operations for addressing an add layer in a formal neural network.

0

Returns:

0

Parameters:

_ (TAddLayer)

Return type:

int

_acc_addr_add_fnn(layer: TAddLayer) int[source]

Count number of accumulate operations for addressing an add layer in a formal neural network.

Nin.

Parameters:

layer (TAddLayer) – An add layer

Returns:

Number of accumulate operations for addressing an add layer in a formal neural network

Return type:

int

_e_addr_add_fnn(layer: TAddLayer) float[source]

Compute energy for addressing an add layer in a formal neural network.

Parameters:

layer (TAddLayer) – An add layer

Returns:

Energy for addressing an add layer in a formal neural network.

Return type:

float

_compute_model_energy_fnn(modelgraph: ModelGraph, e_rdram: Callable[[int], float], e_wrram: Callable[[int], float]) list[EnergyMetrics][source]

Compute the energy per inference for each layer of a formal neural network.

Supports the following layers:

Parameters:
  • modelgraph (ModelGraph) – Model to compute energy on

  • e_rdram (Callable[[int], float]) – Function to compute memory read energy for a given memory size

  • e_wrram (Callable[[int], float]) – Function to compute memory write energy for a given memory size

Returns:

A list of EnergyMetrics for each layer and a total with fields populated with energy estimation

Return type:

list[EnergyMetrics]

_rdin_snn(layer: TBaseLayer, input_spikerate: float) float[source]

Count average number of read operations for the input of a layer in a spiking neural network.

Eq. 5: θl-1.

Parameters:
  • layer (TBaseLayer) – A convolutional or fully-connected layer

  • input_spikerate (float) – Average spike per input per inference

Returns:

Average number of read operations for the input of a layer in a spiking neural network

Return type:

float

_e_rdin_snn(layer: TBaseLayer, input_spikerate: float, e_rdram: Callable[[int], float]) float[source]

Compute average energy for read operations for the input of a layer in a spiking neural network.

Parameters:
  • layer (TBaseLayer) – A convolutional or fully-connected layer

  • input_spikerate (float) – Average spike per input per inference

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Average energy for read operations for the input of a layer in a spiking neural network

Return type:

float

_rdweights_conv_snn(layer: TConvLayer, input_spikerate: float) float[source]

Count average number of read operations for weights of convolutional layer (excluding bias) in spiking neural network.

Eq. 7f: θl-1 × Cout × Wkernel × Hkernel.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • input_spikerate (float) – Average spike per input per inference

Returns:

Average number of read operations for weights of convolutional layer (excluding bias) in spiking neural network

Return type:

float

_e_rdweights_conv_snn(layer: TConvLayer, input_spikerate: float, e_rdram: Callable[[int], float]) float[source]

Compute average energy for read operations for convolutional layer weights (excluding bias) in spiking neural network.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • input_spikerate (float) – Average spike per input per inference

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Average energy for read operations for weights of convolutional layer (excluding bias) in spiking neural network

Return type:

float

_rdbias_conv_snn(layer: TConvLayer, timesteps: int) int[source]

Count number of read operations for the biases of a convolutional layer in a spiking neural network.

Eq. 7s: Cout × Wout × Hout.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • timesteps (int) – Number of timesteps

Returns:

Number of read operations for the biases of a convolutional layer in a spiking neural network

Return type:

int

_e_rdbias_conv_snn(layer: TConvLayer, timesteps: int, e_rdram: Callable[[int], float]) float[source]

Compute energy for read operations for the biases of a convolutional layer in a spiking neural network.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • timesteps (int) – Number of timesteps

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Energy for read operations for the biases of a convolutional layer in a spiking neural network or 0 if the layer does not use biases

Return type:

float

_rdweights_fc_snn(layer: TDenseLayer, input_spikerate: float) float[source]

Count average number of read operations for weights of fully-connected layer (excluding bias) in spiking neural network.

Eq. 8f: θl-1 × Nout.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • input_spikerate (float) – Average spike per input per inference

Returns:

Average number of read operations for weights of fully-connected layer (excluding bias) in spiking neural network

Return type:

float

_e_rdweights_fc_snn(layer: TDenseLayer, input_spikerate: float, e_rdram: Callable[[int], float]) float[source]

Compute average energy for weights read operations of fully-connected layer (excluding bias) in spiking neural network.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • input_spikerate (float) – Average spike per input per inference

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Average energy for read operations for weights of fully-connected layer (excluding bias) in spiking neural network

Return type:

float

_rdbias_fc_snn(layer: TDenseLayer, timesteps: int) int[source]

Count number of read operations for the biases of a fully-connected layer in a spiking neural network.

Eq. 6.2s: Nout.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • timesteps (int) – Number of timesteps

Returns:

Number of read operations for the biases of a fully-connected layer in a spiking neural network

Return type:

int

_e_rdbias_fc_snn(layer: TDenseLayer, timesteps: int, e_rdram: Callable[[int], float]) float[source]

Compute energy for read operations for the biases of a fully-connected layer in a spiking neural network.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • timesteps (int) – Number of timesteps

  • e_rdram (Callable[[int], float]) – Function to compute memory read access energy for a given memory size

Returns:

Energy for read operations for the biases of a fully-connected layer in a spiking neural network or 0 if the layer does not use biases

Return type:

float

_wrout_snn(layer: TBaseLayer, output_spikerate: float) float[source]

Count average number of write operations for the output of a layer in a spiking neural network.

Eq. 13: Noutput.

Parameters:
  • layer (TBaseLayer) – A convolutional or fully-connected layer

  • output_spikerate (float) – Average spike per output per inference

Returns:

Average number of write operations for the output of a layer in a spiking neural network

Return type:

float

_e_wrout_snn(layer: TBaseLayer, output_spikerate: float, e_wrram: Callable[[int], float]) float[source]

Compute average energy for write operations for the output of a layer in a spiking neural network.

Parameters:
  • layer (TBaseLayer) – A convolutional or fully-connected layer

  • output_spikerate (float) – Average spike per output per inference

  • e_rdram – Function to compute memory write access energy for a given memory size

  • e_wrram (Callable[[int], float])

Returns:

Average energy for write operations for the output of a layer in a spiking neural network

Return type:

float

_wrpot_conv_snn(layer: TConvLayer, input_spikerate: float, timesteps: int) float[source]

Count average number of write operations for the potentials of a convolutional layer in a spiking neural network.

Eq 14: θl-1 × Cout × Wkernel × Hkernel + Cout × Hout × Wout.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • timesteps (int) – Number of timesteps

  • input_spikerate (float) – Average spike per input per inference

Returns:

Average number of write operations for the potentials of a convolutional layer in a spiking neural network

Return type:

float

_e_wrpot_conv_snn(layer: TConvLayer, input_spikerate: float, timesteps: int, e_wrram: Callable[[int], float]) float[source]

Compute average energy for write operations for the potentials of a convolutional layer in a spiking neural network.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • timesteps (int) – Number of timesteps

  • input_spikerate (float) – Average spike per input per inference

  • e_rdram – Function to compute memory write access energy for a given memory size

  • e_wrram (Callable[[int], float])

Returns:

Average energy for write operations for the potentials of a convolutional layer in a spiking neural network

Return type:

float

_wrpot_fc_snn(layer: TDenseLayer, input_spikerate: float, timesteps: int) float[source]

Count average number of write operations for the potentials of a fully-connected layer in a spiking neural network.

Eq 15: θl-1 × Nout + Nout.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • timesteps (int) – Number of timesteps

  • input_spikerate (float) – Average spike per input per inference

Returns:

Average number of write operations for the potentials of a fully-connected layer in a spiking neural network

Return type:

float

_e_wrpot_fc_snn(layer: TDenseLayer, input_spikerate: float, timesteps: int, e_wrram: Callable[[int], float]) float[source]

Compute average energy for write operations for the potentials of a fully-connected layer in a spiking neural network.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • timesteps (int) – Number of timesteps

  • input_spikerate (float) – Average spike per input per inference

  • e_rdram – Function to compute memory write access energy for a given memory size

  • e_wrram (Callable[[int], float])

Returns:

Average energy for write operations for the potentials of a fully-connected layer in a spiking neural network

Return type:

float

_rdpot_conv_snn(layer: TConvLayer, input_spikerate: float, timesteps: int) float[source]

Count average number of read operations for the potentials of a convolutional layer in a spiking neural network.

Eq 9: θl-1 × Cout × Wkernel × Hkernel + Cout × Hout × Wout.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • timesteps (int) – Number of timesteps

  • input_spikerate (float) – Average spike per input per inference

Returns:

Average number of read operations for the potentials of a convolutional layer in a spiking neural network

Return type:

float

_e_rdpot_conv_snn(layer: TConvLayer, input_spikerate: float, timesteps: int, e_rdram: Callable[[int], float]) float[source]

Compute average energy for write operations for the potentials of a convolutional layer in a spiking neural network.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • timesteps (int) – Number of timesteps

  • input_spikerate (float) – Average spike per input per inference

  • e_rdram (Callable[[int], float]) – Function to compute memory write access energy for a given memory size

Returns:

Average energy for read operations for the potentials of a convolutional layer in a spiking neural network

Return type:

float

_rdpot_fc_snn(layer: TDenseLayer, input_spikerate: float, timesteps: int) float[source]

Count average number of read operations for the potentials of a fully-connected layer in a spiking neural network.

Eq 10: θl-1 × Nout + Nout.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • timesteps (int) – Number of timesteps

  • input_spikerate (float) – Average spike per input per inference

Returns:

Average number of read operations for the potentials of a fully-connected layer in a spiking neural network

Return type:

float

_e_rdpot_fc_snn(layer: TDenseLayer, input_spikerate: float, timesteps: int, e_wrram: Callable[[int], float]) float[source]

Compute average energy for read operations for the potentials of a fully-connected layer in a spiking neural network.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • timesteps (int) – Number of timesteps

  • input_spikerate (float) – Average spike per input per inference

  • e_rdram – Function to compute memory write access energy for a given memory size

  • e_wrram (Callable[[int], float])

Returns:

Average energy for read operations for the potentials of a fully-connected layer in a spiking neural network

Return type:

float

_mac_ops_conv_snn(layer: TConvLayer, timesteps: int, leak: bool) int[source]

Count number of multiply-accumulate operations inside a convolutional layer in a spiking neural network.

Eq. 1.3: T × Cout × Hout × Wout.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • timesteps (int) – Number of timesteps

  • leak (bool) – Whether the neuron has leak (LIF) or not (IF)

Returns:

Number of multiply-accumulate operations inside a convolutional layer in a spiking neural network, 0 if no leak

Return type:

int

_acc_ops_conv_snn(layer: TConvLayer, input_spikerate: float, output_spikerate: float, timesteps: int) float[source]

Count average number of accumulate operations inside a convolutional layer in a spiking neural network.

Eq. 1.4: θl-1 × ceil(Hkernel / S) × ceil(Wkernel / S) × Cout + T × Cout × Hout × Wout + θl.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • input_spikerate (float) – Average spike per input per inference

  • output_spikerate (float) – Average spike per output per inference

  • timesteps (int) – Number of timesteps

Returns:

Average number of accumulate operations inside a convolutional layer in a spiking neural network.

Return type:

float

_e_ops_conv_snn(layer: TConvLayer, input_spikerate: float, output_spikerate: float, timesteps: int, leak: bool) float[source]

Compute average energy for multiply-accumulate and accumulate inside a convolutional layer in spiking neural network.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • input_spikerate (float) – Average spike per input per inference

  • output_spikerate (float) – Average spike per output per inference

  • timesteps (int) – Number of timesteps

  • leak (bool) – Whether the neuron has leak (LIF) or not (IF)

Returns:

Average energy for multiply-accumulate and accumulate inside a convolutional layer in spiking neural network.

Return type:

float

_mac_ops_fc_snn(layer: TDenseLayer, timesteps: int, leak: bool) int[source]

Count number of multiply-accumulate operations inside a fully-connected layer in a spiking neural network.

Eq. 2.3: T × Nout.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • timesteps (int) – Number of timesteps

  • leak (bool) – Whether the neuron has leak (LIF) or not (IF)

Returns:

Number of multiply-accumulate operations inside a fully-connected layer in a spiking neural network, 0 if no leak

Return type:

int

_acc_ops_fc_snn(layer: TDenseLayer, input_spikerate: float, output_spikerate: float, timesteps: int) float[source]

Count average number of accumulate operations inside a fully-connected layer in a spiking neural network.

Eq. 2.4: θl-1 × Nout + T × Nout + θl.

Warning 1: the article counts Nin times too many as 1 input spike triggers only Nout MAC (each output neuron).

Warning 2: the article is missing the third term which corresponds to the reset.

Original equation (not used here): Eq. 2.4: θl-1 × Nin × Nout + T × Nout.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • input_spikerate (float) – Average spike per input per inference

  • output_spikerate (float) – Average spike per output per inference

  • timesteps (int) – Number of timesteps

Returns:

Average number of accumulate operations inside a fully-connected layer in a spiking neural network.

Return type:

float

_e_ops_fc_snn(layer: TDenseLayer, input_spikerate: float, output_spikerate: float, timesteps: int, leak: bool) float[source]

Compute average energy for multiply-accumulate and accumulate inside fully-connected layer in spiking neural network.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • input_spikerate (float) – Average spike per input per inference

  • output_spikerate (float) – Average spike per output per inference

  • timesteps (int) – Number of timesteps

  • leak (bool) – Whether the neuron has leak (LIF) or not (IF)

Returns:

Average energy for multiply-accumulate and accumulate inside a fully-connected layer in spiking neural network.

Return type:

float

_mac_addr_conv_snn(layer: TConvLayer, input_spikerate: float) float[source]

Count average number of multiply-accumulate operations for addressing a convolutional layer in a spiking neural network.

Eq. 16.2: θl-1 × 2.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • input_spikerate (float) – Average spike per input per inference

Returns:

average number of multiply-accumulate operations for addressing a convolutional layer in a spiking neural network.

Return type:

float

_acc_addr_conv_snn(layer: TConvLayer, input_spikerate: float) float[source]

Count average number of accumulate operations for addressing a convolutional layer in a spiking neural network.

Eq 16.3: θl-1 × Cout × Hkernel × Wkernel.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • input_spikerate (float) – Average spike per input per inference

Returns:

Average number of accumulate operations for addressing a convolutional layer in a spiking neural network

Return type:

float

_e_addr_conv_snn(layer: TConvLayer, input_spikerate: float) float[source]

Compute average energy for addressing a convolutional layer in a spiking neural network.

Parameters:
  • layer (TConvLayer) – A convolutional layer

  • input_spikerate (float) – Average spike per input per inference

Returns:

Average energy for addressing a convolutional layer in a spiking neural network.

Return type:

float

_mac_addr_fc_snn(_: TDenseLayer) int[source]

Count number of multiply-accumulate operations for addressing a fully-connected layer in a spiking neural network.

0

Returns:

0

Parameters:

_ (TDenseLayer)

Return type:

int

_acc_addr_fc_snn(layer: TDenseLayer, input_spikerate: float) float[source]

Count average number of accumulate operations for addressing a fully-connected layer in a spiking neural network.

Eq. 17.2: θl-1 × Nout.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • input_spikerate (float) – Average spike per input per inference

Returns:

Average number of accumulate operations for addressing a fully-connected layer in a spiking neural network

Return type:

float

_e_addr_fc_snn(layer: TDenseLayer, input_spikerate: float) float[source]

Compute average energy for addressing a fully-connected layer in a spiking neural network.

Parameters:
  • layer (TDenseLayer) – A fully-connected layer

  • input_spikerate (float) – Average spike per input per inference

Returns:

Average energy for addressing a fully-connected layer in a spiking neural network.

Return type:

float

_compute_model_energy_snn(modelgraph: ModelGraph, input_spikerates: dict[str, float], output_spikerates: dict[str, float], input_is_binary: dict[str, bool], output_is_binary: dict[str, bool], input_counts: dict[str, int | float | bool], output_counts: dict[str, int | float | bool], is_module_sj: dict[str, bool], timesteps: int, e_rdram: Callable[[int], float], e_wrram: Callable[[int], float]) list[EnergyMetrics][source]

Compute the energy per inference for each layer of a spiking neural network.

Supports the following layers:

Input spike rates are per-timestep, this function multiplies by the number of timesteps to get the spike rates per infernce which are used by the operation count and energy computation functions.

Parameters:
  • modelgraph (ModelGraph) – Model to compute energy on

  • input_spikerate – Dict of layer names and average spike per input per timestep for the layer

  • output_spikerate – Dict of layer names and average spike per output per timestep for the layer

  • input_is_binary (dict[str, bool]) – Dict of layer names and whether its input is binary (spike) or not

  • output_is_binary (dict[str, bool]) – Dict of layer names and whether its output is binary (spike) or not

  • input_counts (dict[str, int | float | bool]) – Dict of layer names and number of inputs for the layer

  • output_counts (dict[str, int | float | bool]) – Dict of layer names and number of outputs for the layer

  • timesteps (int) – Number of timesteps

  • e_rdram (Callable[[int], float]) – Function to compute memory read energy for a given memory size

  • e_wrram (Callable[[int], float]) – Function to compute memory write energy for a given memory size

  • input_spikerates (dict[str, float])

  • output_spikerates (dict[str, float])

  • is_module_sj (dict[str, bool])

Returns:

A list of EnergyMetrics for each layer and a total with fields populated with energy estimation

Return type:

list[EnergyMetrics]

_energy_summary(ems: list[EnergyMetrics]) str[source]

Generate a human-friendly text summary of the energy metrics per layer.

Parameters:

ems (list[EnergyMetrics]) – List of EnergyMetrics per layer and the total

Returns:

The text summary

Return type:

str

_record_spike_count(trainresult: TrainResult, framework: SpikingJelly, model: SNN, dataset: RawData) tuple[dict[str, bool], dict[str, SpikeCounter], dict[str, SpikeCounter]][source]

Compute spike rate averaged over each neuron and timestep.

A forward hook is added to each layer in order to record its input and output during inference performed over the given dataset.

The layer name is computed using the internal torch.fx functions torch.fx.graph._Namespace.create_name() and torch.fx.graph._snake_case() so that it is compatible with qualia_codegen_core.graph.ModelGraph.ModelGraph.

Parameters:
  • trainresult (TrainResult) – TrainResult containing configuration for pytorch_lightning.Trainer such as microai_core.microai.TrainResult.batch_size and microai_core.microai.TrainResult.dataaugmentations

  • framework (SpikingJelly) – A microai_plugin_snn.learningframework.SpikingJelly.SpikingJelly instance

  • model (SNN) – A model to evaluate spike rate on

  • dataset (RawData) – The data to use for inference

Returns:

A tuple of 4 dicts, first 2 dicts map a layer name to its input and output spike rates per timestep respectively, the other 2 dicts map a layer name to True if the input or outputs are binary respectively, False otherwise.

Return type:

tuple[dict[str, bool], dict[str, SpikeCounter], dict[str, SpikeCounter]]

_process_model(trainresult: TrainResult) tuple[ModelGraph | None, dict[str, float] | None, dict[str, float] | None, dict[str, bool] | None, dict[str, bool] | None, dict[str, int | float | bool] | None, dict[str, int | float | bool] | None, dict[str, bool] | None][source]

Process the model to generate layer graph and compute activity in case of SNN.

Use Qualia CodeGen in order to build a qualia_codegen_core.graph.ModelGraph.ModelGraph that is easier to parse.

Execute inference pass on dataset using _record_spike_count() to collect statistics of spike activity in case of SNN.

Parameters:

trainresult (TrainResult) – TrainResult containing the SNN or FNN model, the dataset and the training configuration

Returns:

A tuple of Qualia-CodeGen’s ModelGraph and various SNN metrics as dicts of layer name and associated value, in order: input spike rate, output spike rate, input is binary, output is binary, input spike count, output spike count, module is sj

Return type:

tuple[ModelGraph | None, dict[str, float] | None, dict[str, float] | None, dict[str, bool] | None, dict[str, bool] | None, dict[str, int | float | bool] | None, dict[str, int | float | bool] | None, dict[str, bool] | None]

__call__(trainresult: TrainResult, model_conf: ModelConfigDict) tuple[TrainResult, ModelConfigDict][source]

Compute energy estimation metric from Lemaire et al, 2022.

Uses Qualia CodeGen in order to build a qualia_codegen_core.graph.ModelGraph.ModelGraph that is easier to parse. Call either _compute_model_energy_snn() or _compute_model_energy_fnn() depending on whether the model is an SNN or an FNN. Print the resulting metrics and log them to a CSV file inside the logs/<bench.name>/EnergyEstimationMetric directory.

Parameters:
  • trainresult (TrainResult) – TrainResult containing the SNN or FNN model, the dataset and the training configuration

  • model_conf (ModelConfigDict) – Unused

Returns:

The unmodified trainresult

Return type:

tuple[TrainResult, ModelConfigDict]

_e_ram(mem_params: int, bits: int) float[source]

Energy for a single RAM access (read or write).

If _sram_estimation_type is ‘new’, use T. Louis method with multiple data packed over a single 10pJ 64-bit access, regardless of total memory size.

Otherwise, use ICONIP2022 method with a single access for each data with energy proportional to total memory size, computed with a linear regression over Horowitz 2014 values of 8KiB, 32KiB and 1MiB SRAM.

Parameters:
  • mem_params (int) – Number of element stored in this memory

  • bits (int) – Data width in bits

Returns:

Energy for a single read or write access in this memory

Return type:

float

property _e_add: float

Energy for a single add operation.

property _e_mul: float

Energy for a single mul operation.

class qualia_plugin_snn.postprocessing.FuseBatchNorm[source]

Bases: FuseBatchNorm

Extend qualia_core.postprocessing.FuseBatchNorm.FuseBatchNorm with support for Spiking Neural Networks.

spikingjelly.activation_based.neuron.BaseNode and spikingjelly.activation_based.base.StepModule are added to qualia_core.postprocessing.FuseBatchNorm.FuseBatchNorm.custom_layers to avoid tracing inside.

SpikingJelly-wrapped layers are added to lookup patterns:

  • (spikingjelly.activation_based.layer.Conv1d, spikingjelly.activation_based.layer.BatchNorm1d

  • (spikingjelly.activation_based.layer.Conv2d, spikingjelly.activation_based.layer.BatchNorm2d

  • (spikingjelly.activation_based.layer.Conv3d, spikingjelly.activation_based.layer.BatchNorm3d

Extended attributes timesteps and is_snn are copied into target model.

extra_custom_layers: tuple[type[Module | StepModule], ...] = (<class 'spikingjelly.activation_based.neuron.BaseNode'>, <class 'spikingjelly.activation_based.base.StepModule'>)
__init__(evaluate: bool = True) None[source]

Construct qualia_plugin_snn.postprocessing.FuseBatchNorm.FuseBatchNorm.

Patterns are extended to include SpikingJelly-wrapped layers.

Parameters:

evaluate (bool)

Return type:

None

fuse(model: Module, graphmodule_cls: type[GraphModule], framework: PyTorch, inplace: bool = False) GraphModule[source]

Fuse BatchNorm to Conv and copy source model timesteps and is_snn attributes to target model.

Parameters:
  • mode – PyTorch model with Conv-BatchNorm layers to fuse

  • inplace (bool) – Modify model in place instead of deep-copying

  • model (Module)

  • graphmodule_cls (type[GraphModule])

  • framework (PyTorch)

Returns:

Resulting model with BatchNorm fused to Conv

Return type:

GraphModule

class qualia_plugin_snn.postprocessing.OperationCounter[source]

Bases: EnergyEstimationMetric

Operation counter metric.

From An Analytical Estimation of Spiking Neural Networks Energy Efficiency, Lemaire et al. ICONIP2022.

@inproceedings{EnergyEstimationMetricICONIP2022,
    title = {An Analytical Estimation of Spiking Neural Networks Energy Efficiency},
    author = {Lemaire, Edgar and Cordone, Loïc and Castagnetti, Andrea
              and Novac, Pierre-Emmanuel and Courtois, Jonathan and Miramond, Benoît},
    booktitle = {Proceedings of the 29th International Conference on Neural Information Processing},
    pages = {574--587},
    year = {2023},
    doi = {10.1007/978-3-031-30105-6_48},
    series = {ICONIP},
}

Supports sequential (non-residual) formal and spiking convolutional neural networks with the following layers:

__init__(total_spikerate_exclude_nonbinary: bool = True) None[source]

Construct qualia_plugin_snn.postprocessing.OperationCounter.OperationCounter.

Parameters:

total_spikerate_exclude_nonbinary (bool) – If True, exclude non-binary inputs/outputs from total spikerate computation

Return type:

None

_compute_model_operations_fnn(modelgraph: ModelGraph) list[OperationMetrics][source]

Compute the operations per inference for each layer of a formal neural network.

Supports the following layers:

Parameters:

modelgraph (ModelGraph) – Model to compute energy on

Returns:

A list of OperationMetrics for each layer and a total with fields populated with operation estimation

Return type:

list[OperationMetrics]

_compute_model_operations_snn(modelgraph: ModelGraph, input_spikerates: dict[str, float], output_spikerates: dict[str, float], input_is_binary: dict[str, bool], output_is_binary: dict[str, bool], input_counts: dict[str, int | float | bool], output_counts: dict[str, int | float | bool], is_module_sj: dict[str, bool], timesteps: int) list[OperationMetrics][source]

Compute the operations per inference for each layer of a spiking neural network.

Supports the following layers:

Input spike rates are per-timestep, this function multiplies by the number of timesteps to get the spike rates per infernce which are used by the operation count functions.

Parameters:
  • modelgraph (ModelGraph) – Model to computer operations on

  • input_spikerate – Dict of layer names and average spike per input per timestep for the layer

  • output_spikerate – Dict of layer names and average spike per output per timestep for the layer

  • input_is_binary (dict[str, bool]) – Dict of layer names and whether its input is binary (spike) or not

  • output_is_binary (dict[str, bool]) – Dict of layer names and whether its output is binary (spike) or not

  • input_counts (dict[str, int | float | bool]) – Dict of layer names and number of inputs for the layer

  • output_counts (dict[str, int | float | bool]) – Dict of layer names and number of outputs for the layer

  • is_module_sj (dict[str, bool]) – Whether the layer is a spiking layer (a SpikingJelly module)

  • timesteps (int) – Number of timesteps

  • input_spikerates (dict[str, float])

  • output_spikerates (dict[str, float])

Returns:

A list of OperationMetrics for each layer and a total with fields populated with operation count

Return type:

list[OperationMetrics]

_operations_summary(oms: list[OperationMetrics]) str[source]

Generate a human-friendly text summary of the operations per layer.

Parameters:

oms (list[OperationMetrics]) – List of OperationMetrics per layer and the total

Returns:

The text summary

Return type:

str

__call__(trainresult: TrainResult, model_conf: ModelConfigDict) tuple[TrainResult, ModelConfigDict][source]

Compute operation count metric from Lemaire et al, 2022.

First process the model to extract the graph and activity in case of SNN using _process_model(). Then call either _compute_model_operations_snn() or _compute_model_operations_fnn() depending on whether the model is an SNN or an FNN. Print the resulting metrics and log them to a CSV file inside the logs/<bench.name>/OperationCounter directory.

Parameters:
  • trainresult (TrainResult) – TrainResult containing the SNN or FNN model, the dataset and the training configuration

  • model_conf (ModelConfigDict) – Unused

Returns:

The unmodified trainresult

Return type:

tuple[TrainResult, ModelConfigDict]

class qualia_plugin_snn.postprocessing.QualiaCodeGen[source]

Bases: QualiaCodeGen

Qualia-CodeGen converter calling Qualia-CodeGen-Plugin-SNN to handle Spiking Neural Network layers.

deployers: ModuleType

qualia_plugin_snn.deployment.qualia_codegen default deployers.

Includes qualia_plugin_spleat.deployment.qualia_codegen.Linux.Linux.

__init__(quantize: str, long_width: int | None = None, outdir: str | None = None, metrics: list[str] | None = None, model_name: str = 'cnn', dump_featuremaps: bool = False, timestep_mode: Literal['duplicate', 'iterate'] = 'duplicate') None[source]

Construct qualia_plugin_snn.postprocessing.QualiaCodeGen.QualiaCodeGen.

See qualia_core.postprocessing.QualiaCodeGen.QualiaCodeGen for more information.

Parameters:
  • quantize (str) – Quantization data type

  • long_width (int | None) – Long number bit width

  • outdir (str | None) – Output directory

  • metrics (list[str] | None) – List of metrics to implement

  • model_name (str) – Model name to assign to the main inference function, default is 'cnn'

  • dump_featuremaps (bool) – Generate code in model call chain to dump output of all layers to JSON files

  • timestep_mode (Literal['duplicate', 'iterate']) – Input timestep handling mode, either 'duplicate' to duplicate static input data over timesteps, or 'iterate' to iterate over existing input data timestep dimension

Return type:

None

convert_model_to_modelgraph(model: Module) ModelGraph | None[source]

Convert PyTorch model to a Qualia-CodeGen ModelGraph graph representation.

Uses the qualia_codegen_plugin_snn.graph.TorchModelGraph.TorchModelGraph from Qualia-CodeGen-Plugin-SNN in order to support Spiking Neural Networks. The following layers are passed as custom_layers:

SpikingJelly step_mode is forced to 's' for single-step operation to simplify visit of the graph.

Parameters:

model (Module) – PyTorch model

Returns:

Qualia-CodeGen ModelGraph or None in case of error

Return type:

ModelGraph | None

convert_modelgraph_to_c(modelgraph: ModelGraph, output_path: Path) str | bool[source]

Generate C code for the given ModelGraph using Qualia-CodeGen.

Uses the qualia_codegen_plugin_snn.Converter.Converter from Qualia-CodeGen-Plugin-SNN in order to support Spiking Neural Networks.

Parameters:
Returns:

String containing the single-file C code

Return type:

str | bool