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:
objectHolds 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
- class qualia_plugin_snn.postprocessing.EnergyEstimationMetric.EnergyEstimationMetricLoggerFields[source]
Bases:
NamedTupleInterface object for CSV logging.
Should contain the same fields as
EnergyMetricsand returned byEnergyMetrics.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
- mem_total: float
Energy for potential, weights, bias and input/output read/write, i.e., Erdram + Ewrram
- total: float
Total energy, i.e., energy for memory read/write, synaptic operations and event adress computation
- 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:
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'])
- class qualia_plugin_snn.postprocessing.EnergyEstimationMetric.EnergyMetrics[source]
Bases:
objectHolds 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
- 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 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
EnergyEstimationMetricLoggerFieldsobject with all of this class fields and properties and return it.- Returns:
the
EnergyEstimationMetricLoggerFieldswith all data from this object copied into it- Return type:
- __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
- 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:
torch.nn.ReLUfor formal neural networksspikingjelly.activation_based.neuron.IFNodefor spiking neural networksspikingjelly.activation_based.neuron.LIFNodefor spiking neural networks
- 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:
- Returns:
Energy for the given bit width, operation and estimation type
- Raises:
ValueError – When
op_estimation_typeis invalid, orbit_widthis out of bounds for ‘saturation’op_estimation_type- Return type:
- _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_typeis set, use_set_op_estimation_type()to infer the values according to the wanted bit width and type.Otherwise, if
bit_widthis inenergy_valuesuse the predefined energy values inenergy_values.Otherwise, defaults to using predefined 32-bit values from
energy_values.
- _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:
- _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:
- _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:
- _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:
- _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.
- _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.
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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
- Returns:
Energy for write operations for the output of a convolutional layer in a formal neural network
- Return type:
- _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:
- _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
- Returns:
Energy for write operations for the output of a fully-connected layer in a formal neural network
- Return type:
- _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.
- _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.
- _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:
- _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:
- _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:
- _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:
- _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:
- _mac_ops_add_fnn(_: TAddLayer) int[source]
Count number of multiply-accumulate operations inside an add layer in a formal neural network.
- _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.
- _e_ops_add_fnn(layer: TAddLayer) float[source]
Compute energy for multiply-accumulate and accumulate operations inside an add layer in formal neural network.
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _mac_addr_add_fnn(_: TAddLayer) int[source]
Count number of multiply-accumulate operations for addressing an add layer in a formal neural network.
0
- _acc_addr_add_fnn(layer: TAddLayer) int[source]
Count number of accumulate operations for addressing an add layer in a formal neural network.
Nin.
- _e_addr_add_fnn(layer: TAddLayer) float[source]
Compute energy for addressing an add layer in a formal neural network.
- _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:
- Returns:
A list of EnergyMetrics for each layer and a total with fields populated with energy estimation
- Return type:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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
- Returns:
Average energy for write operations for the output of a layer in a spiking neural network
- Return type:
- _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:
- _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:
- Returns:
Average energy for write operations for the potentials of a convolutional layer in a spiking neural network
- Return type:
- _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:
- _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:
- Returns:
Average energy for write operations for the potentials of a fully-connected layer in a spiking neural network
- Return type:
- _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:
- _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:
- Returns:
Average energy for read operations for the potentials of a convolutional layer in a spiking neural network
- Return type:
- _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:
- _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:
- Returns:
Average energy for read operations for the potentials of a fully-connected layer in a spiking neural network
- Return type:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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:
- _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
- Returns:
A list of EnergyMetrics for each layer and a total with fields populated with energy estimation
- Return type:
- _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:
- _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.fxfunctionstorch.fx.graph._Namespace.create_name()andtorch.fx.graph._snake_case()so that it is compatible withqualia_codegen_core.graph.ModelGraph.ModelGraph.- Parameters:
trainresult (TrainResult) – TrainResult containing configuration for
pytorch_lightning.Trainersuch asmicroai_core.microai.TrainResult.batch_sizeandmicroai_core.microai.TrainResult.dataaugmentationsframework (SpikingJelly) – A
microai_plugin_snn.learningframework.SpikingJelly.SpikingJellyinstancemodel (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
Trueif the input or outputs are binary respectively,Falseotherwise.- 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.ModelGraphthat 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.ModelGraphthat 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_typeis ‘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.