qualia_plugin_snn.postprocessing.EnergyEstimationMetric module

Provide the EnergyEstimationMetric postprocessing module based on Lemaire et al., 2022.

class qualia_plugin_snn.postprocessing.EnergyEstimationMetric.SpikeCounter[source]

Bases: object

Holds the statistics and properties of an input or output tensor of a layer after inference.

Parameters:
  • spike_count – Number of spikes recorded

  • tensor_sum – Sum of elements of the tensor

  • size – Number of elements in the tensor

  • binary – True if all elements are binary (0 or 1), False otherwise

  • sample_count – Number of times the tensor has been updated

spike_count: int | float | bool
tensor_sum: int | float | bool
size: int
binary: bool
sample_count: int
__init__(spike_count: int | float | bool, tensor_sum: int | float | bool, size: int, binary: bool, sample_count: int) None
Parameters:
Return type:

None

class qualia_plugin_snn.postprocessing.EnergyEstimationMetric.EnergyEstimationMetricLoggerFields[source]

Bases: NamedTuple

Interface object for CSV logging.

Should contain the same fields as EnergyMetrics and returned by EnergyMetrics.asnamedtuple().

Parameters:
  • name – Layer name

  • mem_pot – Energy for potential memory read/write

  • mem_weights – Energy for weights memory read

  • mem_bias – Energy for bias memory read

  • mem_io – Energy for input/output memory read/write

  • ops – Energy for synaptic operations

  • addr – Energy for event address computation

  • input_spikerate – Average input spike rate per timestep

  • output_spikerate – Average output spike rate per timestep

  • input_is_binary – If input tensor only contains binary values, i.e., spikes

  • output_is_binary – If output tensor only contains binary values, i.e., spikes

  • is_sj – If the layer is a SpikingJelly layer and has been processed as part of a Spiking Neural Network

name: str

Layer name

mem_pot: float

Energy for potential memory read/write

mem_weights: float

Energy for weights memory read

mem_bias: float

Energy for bias memory read

mem_io: float

Energy for input/output memory read/write

mem_total: float

Energy for potential, weights, bias and input/output read/write, i.e., Erdram + Ewrram

ops: float

Energy for synaptic operations

addr: float

Energy for event address computation

opsaddr: float

Energy for synaptic operations and event address computation, i.e., Eops+addr

total: float

Total energy, i.e., energy for memory read/write, synaptic operations and event adress computation

input_spikerate: float

Average input spike rate per timestep

output_spikerate: float

Average output spike rate per timestep

input_count: int | float | bool

Input count per timestep

output_count: int | float | bool

Output count per timestep

input_is_binary: bool

If input tensor only contains binary values, i.e., spikes

output_is_binary: bool

If output tensor only contains binary values, i.e., spikes

is_sj: bool | Literal['Hybrid']

If the layer is a SpikingJelly layer and has been processed as part of a Spiking Neural Network

static __new__(_cls, name: str, mem_pot: float, mem_weights: float, mem_bias: float, mem_io: float, mem_total: float, ops: float, addr: float, opsaddr: float, total: float, input_spikerate: float, output_spikerate: float, input_count: Number, output_count: Number, input_is_binary: bool, output_is_binary: bool, is_sj: bool | Literal['Hybrid'])

Create new instance of EnergyEstimationMetricLoggerFields(name, mem_pot, mem_weights, mem_bias, mem_io, mem_total, ops, addr, opsaddr, total, input_spikerate, output_spikerate, input_count, output_count, input_is_binary, output_is_binary, is_sj)

Parameters:
class qualia_plugin_snn.postprocessing.EnergyEstimationMetric.EnergyMetrics[source]

Bases: object

Holds the computed average energy per inference for each layer.

Parameters:
  • name – Layer name

  • mem_pot – Energy for potential memory read/write

  • mem_weights – Energy for weights memory read

  • mem_bias – Energy for bias memory read

  • mem_io – Energy for input/output memory read/write

  • ops – Energy for synaptic operations

  • addr – Energy for event address computation

  • input_spikerate – Average input spike rate per timestep

  • output_spikerate – Average output spike rate per timestep

  • input_count – Input count per timestep

  • output_count – Output count per timestep

  • input_is_binary – If input tensor only contains binary values, i.e., spikes

  • output_is_binary – If output tensor only contains binary values, i.e., spikes

  • is_sj – If the layer is a SpikingJelly layer and has been processed as part of a Spiking Neural Network

name: str

Layer name

mem_pot: float

Energy for potential memory read/write

mem_weights: float

Energy for weights memory read

mem_bias: float

Energy for bias memory read

mem_io: float

Energy for input/output memory read/write

ops: float

Energy for synaptic operations

addr: float

Energy for event address computation

input_spikerate: float | None

Average input spike rate per timestep

output_spikerate: float | None

Average output spike rate per timestep

input_count: int | float | bool | None

Input count per timestep

output_count: int | float | bool | None

Output count per timestep

input_is_binary: bool

If input tensor only contains binary values, i.e., spikes

output_is_binary: bool

If output tensor only contains binary values, i.e., spikes

is_sj: bool | Literal['Hybrid']

If the layer is a SpikingJelly layer and has been processed as part of a Spiking Neural Network

property mem_total: float

Energy for potential, weights, bias and input/output read/write. Erdram + Ewrram.

property opsaddr: float

Energy for synaptic operations and event address computation. Eops+addr.

property total: float

Total energy. Energy for memory read/write, synaptic operations and event adress computation.

asnamedtuple() EnergyEstimationMetricLoggerFields[source]

Return the data from this class as a NamedTuple for use with the CSV logger.

Instanciate a EnergyEstimationMetricLoggerFields object with all of this class fields and properties and return it.

Returns:

the EnergyEstimationMetricLoggerFields with all data from this object copied into it

Return type:

EnergyEstimationMetricLoggerFields

__init__(name: str, mem_pot: float, mem_weights: float, mem_bias: float, mem_io: float, ops: float, addr: float, input_spikerate: float | None, output_spikerate: float | None, input_count: int | float | bool | None, output_count: int | float | bool | None, input_is_binary: bool, output_is_binary: bool, is_sj: bool | Literal['Hybrid']) None
Parameters:
Return type:

None

class qualia_plugin_snn.postprocessing.EnergyEstimationMetric.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.