qualia_plugin_snn.postprocessing.OperationCounter module

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

class qualia_plugin_snn.postprocessing.OperationCounter.OperationCounterLoggerFields[source]

Bases: NamedTuple

Interface object for CSV logging.

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

Parameters:
  • name – Layer name

  • syn_acc – Number of accumulate operations for synaptic computation

  • syn_mac – Number of multiply-accumulate operations for synaptic computation

  • addr_acc – Number of accumulate operations for addressing

  • addr_mac – Number of multiply-accumulate operations for addressing

  • total_acc – Total number of accumulate operations

  • total_mac – Total number of multiply-accumulate operations

  • mem_read – Number of memory write operations

  • mem_write – Number of memory read operations

  • 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

syn_acc: float

Number of accumulate operations for synaptic computation

syn_mac: float

Number of multiply-accumulate operations for synaptic computation

addr_acc: float

Number of accumulate operations for addressing

addr_mac: float

Number of multiply-accumulate operations for addressing

total_acc: float

Total number of accumulate operations

total_mac: float

Total number of multiply-accumulate operations

mem_write: float

Number of memory write operations

mem_read: float

Number of memory read operations

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

static __new__(_cls, name: str, syn_acc: float, syn_mac: float, addr_acc: float, addr_mac: float, total_acc: float, total_mac: float, mem_write: float, mem_read: float, input_spikerate: float | None, output_spikerate: float | None, input_count: Number | None, output_count: Number | None, input_is_binary: bool, output_is_binary: bool, is_sj: bool | Literal['Hybrid'])

Create new instance of OperationCounterLoggerFields(name, syn_acc, syn_mac, addr_acc, addr_mac, total_acc, total_mac, mem_write, mem_read, input_spikerate, output_spikerate, input_count, output_count, input_is_binary, output_is_binary, is_sj)

Parameters:
  • name (str)

  • syn_acc (float)

  • syn_mac (float)

  • addr_acc (float)

  • addr_mac (float)

  • total_acc (float)

  • total_mac (float)

  • mem_write (float)

  • mem_read (float)

  • input_spikerate (float | None)

  • output_spikerate (float | None)

  • input_count (Number | None)

  • output_count (Number | None)

  • input_is_binary (bool)

  • output_is_binary (bool)

  • is_sj (bool | Literal['Hybrid'])

class qualia_plugin_snn.postprocessing.OperationCounter.OperationMetrics[source]

Bases: object

Holds the computed average operations per inference for each layer.

Parameters:
  • name – Layer name

  • syn_acc – Number of accumulate operations for synaptic computation

  • syn_mac – Number of multiply-accumulate operations for synaptic computation

  • addr_acc – Number of accumulate operations for addressing

  • addr_mac – Number of multiply-accumulate operations for addressing

  • mem_read – Number of memory write operations

  • mem_write – Number of memory read operations

  • 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

syn_acc: float

Number of accumulate operations for synaptic computation

syn_mac: float

Number of multiply-accumulate operations for synaptic computation

addr_acc: float

Number of accumulate operations for addressing

addr_mac: float

Number of multiply-accumulate operations for addressing

mem_write: float

Number of memory write operations

mem_read: float

Number of memory read operations

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 total_acc: float

Total count of accumulate operations.

property total_mac: float

Total count of accumulate operations.

asnamedtuple() OperationCounterLoggerFields[source]

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

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

Returns:

the OperationCounterLoggerFields with all data from this object copied into it

Return type:

OperationCounterLoggerFields

asdict() dict[str, str | int | float | bool | None][source]

Return the data from this class as a dictionary.

Returns:

a dictionary with each attribute and property of this dataclass as keys and the associated values

Return type:

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

__init__(name: str, syn_acc: float, syn_mac: float, addr_acc: float, addr_mac: float, mem_write: float, mem_read: 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.OperationCounter.OperationCounter[source]

Bases: EnergyEstimationMetric

Operation counter metric.

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

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

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

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

Construct qualia_plugin_snn.postprocessing.OperationCounter.OperationCounter.

Parameters:

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

Return type:

None

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

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

Supports the following layers:

Parameters:

modelgraph (ModelGraph) – Model to compute energy on

Returns:

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

Return type:

list[OperationMetrics]

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

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

Supports the following layers:

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

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

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

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

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

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

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

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

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

  • timesteps (int) – Number of timesteps

  • input_spikerates (dict[str, float])

  • output_spikerates (dict[str, float])

Returns:

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

Return type:

list[OperationMetrics]

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

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

Parameters:

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

Returns:

The text summary

Return type:

str

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

Compute operation count metric from Lemaire et al, 2022.

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

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

  • model_conf (ModelConfigDict) – Unused

Returns:

The unmodified trainresult

Return type:

tuple[TrainResult, ModelConfigDict]