|
| 1 | +import torch |
| 2 | +from neurobench.metrics.abstract.workload_metric import AccumulatedMetric |
| 3 | +from collections import defaultdict |
| 4 | + |
| 5 | + |
| 6 | +neuron_ops_reset_operations = { |
| 7 | + "Leaky": { |
| 8 | + "subtract": 4, |
| 9 | + "zero": 4, |
| 10 | + }, |
| 11 | + "Synaptic": { |
| 12 | + "subtract": 6, |
| 13 | + "zero": 8, |
| 14 | + }, |
| 15 | + "Lapicque": { |
| 16 | + "subtract": 11, |
| 17 | + "zero": 11, |
| 18 | + }, |
| 19 | + "Alpha": { |
| 20 | + "subtract": 18, |
| 21 | + "zero": 24, |
| 22 | + }, |
| 23 | +} |
| 24 | +""" |
| 25 | +The `neuron_ops_reset_operations` dictionary defines the computational cost associated |
| 26 | +with resetting the membrane potential of neurons for different neuron types. The reset |
| 27 | +mechanisms are categorized into two types: |
| 28 | +
|
| 29 | +1. **Subtract**: Represents a reset mechanism where the membrane potential is reduced |
| 30 | + by a certain value. The value associated with this mechanism indicates the computational |
| 31 | + cost (in terms of basic operations) required to perform this type of reset. |
| 32 | +
|
| 33 | +2. **Zero**: Represents a reset mechanism where the membrane potential is reset to zero. |
| 34 | + The value associated with this mechanism indicates the computational cost (in terms of |
| 35 | + basic operations) required to perform this type of reset. |
| 36 | +
|
| 37 | +### Neuron Types and Their Computational Costs: |
| 38 | +- **Leaky**: |
| 39 | + - Subtract mechanism: 4 operations |
| 40 | + - Zero mechanism: 4 operations |
| 41 | +- **Synaptic**: |
| 42 | + - Subtract mechanism: 6 operations |
| 43 | + - Zero mechanism: 8 operations |
| 44 | +- **Lapicque**: |
| 45 | + - Subtract mechanism: 11 operations |
| 46 | + - Zero mechanism: 11 operations |
| 47 | +- **Alpha**: |
| 48 | + - Subtract mechanism: 18 operations |
| 49 | + - Zero mechanism: 24 operations |
| 50 | +
|
| 51 | +### Purpose: |
| 52 | +The values in this dictionary represent the computational cost (measured in terms of |
| 53 | +basic operations like addition, subtraction, etc.) required for each neuron type to |
| 54 | +reset its membrane potential using a specific reset mechanism. |
| 55 | +""" |
| 56 | + |
| 57 | + |
| 58 | +class NeuronOperations(AccumulatedMetric): |
| 59 | + """ |
| 60 | + Neuron operations metric. |
| 61 | +
|
| 62 | + This metric computes the number of operations performed by neurons during the |
| 63 | + forward pass of the model. The operations are tracked per neuron, per layer. |
| 64 | +
|
| 65 | + The `NeuronOperations` metric is designed to measure the computational workload |
| 66 | + associated with neuron activity in spiking neural networks. Specifically, it tracks |
| 67 | + the number of operations required to update the membrane potential of neurons during |
| 68 | + the forward pass. These operations include the reset mechanisms defined in the |
| 69 | + `neuron_ops_reset_operations` dictionary, such as "subtract" and "zero". |
| 70 | +
|
| 71 | + """ |
| 72 | + |
| 73 | + def __init__(self): |
| 74 | + """Initialize the NeuronOperations metric.""" |
| 75 | + super().__init__(requires_hooks=True) |
| 76 | + self.total_samples = 0 |
| 77 | + self.dense = defaultdict(int) |
| 78 | + self.macs = defaultdict(int) |
| 79 | + |
| 80 | + def reset(self): |
| 81 | + """Reset the metric state for a new evaluation.""" |
| 82 | + self.total_samples = 0 |
| 83 | + self.dense = defaultdict(int) |
| 84 | + self.macs = defaultdict(int) |
| 85 | + |
| 86 | + def __call__(self, model, preds, data): |
| 87 | + """ |
| 88 | + Accumulate the neuron operations. |
| 89 | +
|
| 90 | + Args: |
| 91 | + model: A NeuroBenchModel. |
| 92 | + preds: A tensor of model predictions. |
| 93 | + data: A tuple of data and labels. |
| 94 | + Returns: |
| 95 | + float: Number of membrane potential updates. |
| 96 | +
|
| 97 | + """ |
| 98 | + for hook in model.activation_hooks: |
| 99 | + layer_type = hook.layer.__class__.__name__ |
| 100 | + reset_mechanism = hook.layer._reset_mechanism |
| 101 | + updates = 0 |
| 102 | + |
| 103 | + if len(hook.pre_fire_mem_potential) > 1: |
| 104 | + pre_fire_mem = torch.stack(hook.pre_fire_mem_potential[1:]) |
| 105 | + post_fire_mem = torch.stack(hook.post_fire_mem_potential[1:]) |
| 106 | + updates += torch.count_nonzero(pre_fire_mem - post_fire_mem).item() |
| 107 | + if hook.post_fire_mem_potential: |
| 108 | + updates += hook.post_fire_mem_potential[0].numel() |
| 109 | + |
| 110 | + self.macs[layer_type] += ( |
| 111 | + updates * neuron_ops_reset_operations[layer_type][reset_mechanism] |
| 112 | + ) |
| 113 | + self.dense[layer_type] += ( |
| 114 | + hook.post_fire_mem_potential[0].numel() |
| 115 | + * len(hook.post_fire_mem_potential) |
| 116 | + * neuron_ops_reset_operations[layer_type][reset_mechanism] |
| 117 | + ) |
| 118 | + |
| 119 | + self.total_samples += data[0].size(0) |
| 120 | + |
| 121 | + return self.compute() |
| 122 | + |
| 123 | + def compute(self): |
| 124 | + """ |
| 125 | + Compute the total membrane updates normalized by the number of samples. |
| 126 | +
|
| 127 | + Returns: |
| 128 | + float: Compute the total updates to each neuron's membrane potential within the model, |
| 129 | + aggregated across all neurons and normalized by the number of samples processed. |
| 130 | +
|
| 131 | + """ |
| 132 | + if self.total_samples == 0: |
| 133 | + return {"Effective Neuron Ops": 0, "Neuron Dense Ops": 0} |
| 134 | + |
| 135 | + macs = sum(self.macs.values()) |
| 136 | + dense = sum(self.dense.values()) |
| 137 | + |
| 138 | + return { |
| 139 | + "Effective Neuron Ops": macs / self.total_samples, |
| 140 | + "Neuron Dense Ops": dense / self.total_samples, |
| 141 | + } |
0 commit comments