spikinggamma 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. spikinggamma-0.1.0/LICENSE +7 -0
  2. spikinggamma-0.1.0/PKG-INFO +54 -0
  3. spikinggamma-0.1.0/README.md +33 -0
  4. spikinggamma-0.1.0/pyproject.toml +39 -0
  5. spikinggamma-0.1.0/setup.cfg +4 -0
  6. spikinggamma-0.1.0/spikinggamma/__init__.py +0 -0
  7. spikinggamma-0.1.0/spikinggamma/dendrite/__init__.py +0 -0
  8. spikinggamma-0.1.0/spikinggamma/dendrite/base.py +38 -0
  9. spikinggamma-0.1.0/spikinggamma/dendrite/split.py +51 -0
  10. spikinggamma-0.1.0/spikinggamma/dendrite/synapse/__init__.py +0 -0
  11. spikinggamma-0.1.0/spikinggamma/dendrite/synapse/base.py +3 -0
  12. spikinggamma-0.1.0/spikinggamma/dendrite/synapse/conv2d.py +41 -0
  13. spikinggamma-0.1.0/spikinggamma/dendrite/synapse/linear.py +23 -0
  14. spikinggamma-0.1.0/spikinggamma/neuron.py +43 -0
  15. spikinggamma-0.1.0/spikinggamma/signal/__init__.py +0 -0
  16. spikinggamma-0.1.0/spikinggamma/signal/activation.py +19 -0
  17. spikinggamma-0.1.0/spikinggamma/signal/base.py +26 -0
  18. spikinggamma-0.1.0/spikinggamma/signal/default.py +27 -0
  19. spikinggamma-0.1.0/spikinggamma/signal/dropout.py +54 -0
  20. spikinggamma-0.1.0/spikinggamma/spike/__init__.py +0 -0
  21. spikinggamma-0.1.0/spikinggamma/spike/base.py +59 -0
  22. spikinggamma-0.1.0/spikinggamma/spike/identity.py +64 -0
  23. spikinggamma-0.1.0/spikinggamma/spike/kernel/base.py +92 -0
  24. spikinggamma-0.1.0/spikinggamma/spike/kernel/gamma.py +84 -0
  25. spikinggamma-0.1.0/spikinggamma/spike/saturation.py +60 -0
  26. spikinggamma-0.1.0/spikinggamma/spike/sigma_delta.py +196 -0
  27. spikinggamma-0.1.0/spikinggamma/utils/__init__.py +0 -0
  28. spikinggamma-0.1.0/spikinggamma/utils/datasets/__init__.py +0 -0
  29. spikinggamma-0.1.0/spikinggamma/utils/datasets/common.py +24 -0
  30. spikinggamma-0.1.0/spikinggamma/utils/datasets/dvsgesture.py +29 -0
  31. spikinggamma-0.1.0/spikinggamma/utils/datasets/shd.py +45 -0
  32. spikinggamma-0.1.0/spikinggamma/utils/datasets/ssc.py +57 -0
  33. spikinggamma-0.1.0/spikinggamma/utils/enums.py +7 -0
  34. spikinggamma-0.1.0/spikinggamma/utils/helpers.py +89 -0
  35. spikinggamma-0.1.0/spikinggamma.egg-info/PKG-INFO +54 -0
  36. spikinggamma-0.1.0/spikinggamma.egg-info/SOURCES.txt +38 -0
  37. spikinggamma-0.1.0/spikinggamma.egg-info/dependency_links.txt +1 -0
  38. spikinggamma-0.1.0/spikinggamma.egg-info/requires.txt +4 -0
  39. spikinggamma-0.1.0/spikinggamma.egg-info/top_level.txt +1 -0
  40. spikinggamma-0.1.0/tests/test_neuron.py +78 -0
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2026 Roel Koopman, Sebastian Otte, Sander Bohte
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ Metadata-Version: 2.4
2
+ Name: spikinggamma
3
+ Version: 0.1.0
4
+ Summary: Use SpikingGamma with PyTorch
5
+ Author-email: Roel Koopman <roel.koopman@cwi.nl>, Sebastian Otte <sebastian.otte@uni-luebeck.de>, Sander Bohte <s.m.bohte@cwi.nl>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/RoelMK/SpikingGamma
8
+ Project-URL: Issues, https://github.com/RoelMK/SpikingGamma/issues
9
+ Keywords: spikinggamma,spiking neural networks,snn,pytorch
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
13
+ Classifier: Programming Language :: Python :: 3
14
+ Requires-Python: >=3.10
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Requires-Dist: torch
18
+ Provides-Extra: datasets
19
+ Requires-Dist: tonic; extra == "datasets"
20
+ Dynamic: license-file
21
+
22
+
23
+ # Introduction
24
+ This package allows you to directly use the SpikingGamma neuron model in your neural networks. It works on top of PyTorch.
25
+
26
+ # Installation
27
+ SpikingGamma is not yet on PyPI. At this moment, only installation from the source is supported.
28
+
29
+ To install directly from the source:
30
+ ```console
31
+ pip install git+https://github.com/RoelMK/SpikingGamma
32
+ ```
33
+ or
34
+ ```console
35
+ git clone https://github.com/RoelMK/SpikingGamma
36
+ cd SpikingGamma
37
+ pip install -e .
38
+ ```
39
+ PyTorch is installed automatically if it is missing; to use a specific CUDA build, install PyTorch first.
40
+
41
+ The dataset loaders in `spikinggamma.utils.datasets` and the example notebooks also need `tonic`, which the `datasets` extra installs:
42
+ ```console
43
+ pip install "spikinggamma[datasets] @ git+https://github.com/RoelMK/SpikingGamma"
44
+ ```
45
+ or `pip install -e ".[datasets]"` from a clone.
46
+
47
+ # Tutorials
48
+ You can start by exploring the notebooks in the `examples` folder. Each example is a Jupyter notebook that can be run directly in a Jupyter environment.
49
+
50
+ | Notebook | Description |
51
+ |---|---|
52
+ | 1_getting_started.ipynb | Learn the basics: train and test a simple model. |
53
+ | 2_scaling_up.ipynb | Build and train a larger CNN. |
54
+ | 3_speeding_up.ipynb | Accelerate training through parallelization. |
@@ -0,0 +1,33 @@
1
+
2
+ # Introduction
3
+ This package allows you to directly use the SpikingGamma neuron model in your neural networks. It works on top of PyTorch.
4
+
5
+ # Installation
6
+ SpikingGamma is not yet on PyPI. At this moment, only installation from the source is supported.
7
+
8
+ To install directly from the source:
9
+ ```console
10
+ pip install git+https://github.com/RoelMK/SpikingGamma
11
+ ```
12
+ or
13
+ ```console
14
+ git clone https://github.com/RoelMK/SpikingGamma
15
+ cd SpikingGamma
16
+ pip install -e .
17
+ ```
18
+ PyTorch is installed automatically if it is missing; to use a specific CUDA build, install PyTorch first.
19
+
20
+ The dataset loaders in `spikinggamma.utils.datasets` and the example notebooks also need `tonic`, which the `datasets` extra installs:
21
+ ```console
22
+ pip install "spikinggamma[datasets] @ git+https://github.com/RoelMK/SpikingGamma"
23
+ ```
24
+ or `pip install -e ".[datasets]"` from a clone.
25
+
26
+ # Tutorials
27
+ You can start by exploring the notebooks in the `examples` folder. Each example is a Jupyter notebook that can be run directly in a Jupyter environment.
28
+
29
+ | Notebook | Description |
30
+ |---|---|
31
+ | 1_getting_started.ipynb | Learn the basics: train and test a simple model. |
32
+ | 2_scaling_up.ipynb | Build and train a larger CNN. |
33
+ | 3_speeding_up.ipynb | Accelerate training through parallelization. |
@@ -0,0 +1,39 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "spikinggamma"
7
+ version = "0.1.0"
8
+ description = "Use SpikingGamma with PyTorch"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ keywords = ["spikinggamma", "spiking neural networks", "snn", "pytorch"]
12
+ authors = [
13
+ {name = "Roel Koopman", email = "roel.koopman@cwi.nl" },
14
+ {name = "Sebastian Otte", email = "sebastian.otte@uni-luebeck.de" },
15
+ {name = "Sander Bohte", email = "s.m.bohte@cwi.nl" }
16
+ ]
17
+ license = "MIT"
18
+ # For a list of valid classifiers, see https://pypi.org/classifiers/
19
+ classifiers = [
20
+ "Development Status :: 3 - Alpha",
21
+ "Intended Audience :: Science/Research",
22
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
23
+ "Programming Language :: Python :: 3",
24
+ ]
25
+ dependencies = [
26
+ "torch",
27
+ ]
28
+
29
+ [project.optional-dependencies]
30
+ datasets = ["tonic"]
31
+
32
+ [project.urls]
33
+ "Homepage" = "https://github.com/RoelMK/SpikingGamma"
34
+ "Issues" = "https://github.com/RoelMK/SpikingGamma/issues"
35
+
36
+
37
+ [tool.setuptools.packages.find]
38
+ where = ["."]
39
+ include = ["spikinggamma*"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
File without changes
File without changes
@@ -0,0 +1,38 @@
1
+ import torch.nn as nn
2
+ import torch
3
+
4
+ from spikinggamma.utils.enums import StepMode
5
+
6
+ class BaseDendriticFunction(nn.Module):
7
+ def __init__(self, merge_TB=True):
8
+ super().__init__()
9
+ self.merge_TB = merge_TB
10
+
11
+ def is_step_mode_compatible(self, step_mode: StepMode) -> bool:
12
+ return True
13
+
14
+ def reset(self):
15
+ pass
16
+
17
+ def detach(self):
18
+ pass
19
+
20
+ def forward(self, y_hat_k: torch.Tensor, step_mode: StepMode = StepMode.SINGLE) -> torch.Tensor:
21
+ if not self.is_step_mode_compatible(step_mode):
22
+ raise ValueError(f"Dendritic function {self.__class__.__name__} is not compatible with step mode {step_mode}.")
23
+
24
+ if (step_mode == StepMode.MULTI_STATEFUL or step_mode == StepMode.MULTI_STATELESS) and self.merge_TB:
25
+ T, B = y_hat_k.shape[:2]
26
+ # Spike encoders may return non-contiguous views (for example after
27
+ # broadcasting a threshold). ``reshape`` preserves the inexpensive
28
+ # view when possible and makes a contiguous copy when necessary.
29
+ y_hat_k = y_hat_k.reshape(-1, *y_hat_k.shape[2:])
30
+
31
+ out = self._forward(y_hat_k, step_mode)
32
+
33
+ if (step_mode == StepMode.MULTI_STATEFUL or step_mode == StepMode.MULTI_STATELESS) and self.merge_TB:
34
+ out = out.reshape(T, B, *out.shape[1:])
35
+ return out
36
+
37
+ def _forward(self, y_hat_k: torch.Tensor, step_mode: StepMode) -> torch.Tensor:
38
+ raise NotImplementedError
@@ -0,0 +1,51 @@
1
+
2
+ from typing import Tuple
3
+ import torch
4
+ import torch.nn as nn
5
+ from spikinggamma.dendrite.base import BaseDendriticFunction
6
+ from spikinggamma.dendrite.synapse.base import BaseSynapticFunction
7
+ from spikinggamma.utils.enums import StepMode
8
+
9
+ class SplitDendriticFunction(BaseDendriticFunction):
10
+ def __init__(self, shape_out: Tuple[int, ...],
11
+ n_kernels: int,
12
+ synaptic_function: BaseSynapticFunction,
13
+ bias: bool = False,):
14
+ super().__init__()
15
+ self.synaptic_function = synaptic_function
16
+ if bias:
17
+ self.bias = nn.Parameter(torch.zeros(*shape_out))
18
+ else:
19
+ self.register_parameter("bias", None)
20
+ self.kernel_weights = nn.Parameter(
21
+ torch.empty(*shape_out, n_kernels)
22
+ )
23
+ self.reset_parameters()
24
+
25
+ def reset_parameters(self):
26
+ if self.kernel_weights is not None:
27
+ raw_weights = torch.randn_like(self.kernel_weights) * 0.1
28
+ with torch.no_grad():
29
+ self.kernel_weights.copy_(raw_weights)
30
+ if getattr(self, "bias", None) is not None:
31
+ with torch.no_grad():
32
+ self.bias.zero_()
33
+
34
+
35
+ def get_kernel_weights(self) -> torch.Tensor:
36
+ return self.kernel_weights
37
+
38
+ def compute_xkw(self, xk: torch.Tensor) -> torch.Tensor:
39
+ if self.kernel_weights is not None:
40
+ xkw = xk * self.get_kernel_weights()
41
+ else:
42
+ xkw = xk
43
+ return xkw
44
+
45
+ def _forward(self, y_hat_k: torch.Tensor, step_mode: StepMode) -> torch.Tensor:
46
+ xk = self.synaptic_function(y_hat_k)
47
+ xkw = self.compute_xkw(xk)
48
+ x = xkw.sum(dim=-1)
49
+ if self.bias is not None:
50
+ x = x + self.bias
51
+ return x
@@ -0,0 +1,3 @@
1
+
2
+ class BaseSynapticFunction:
3
+ pass
@@ -0,0 +1,41 @@
1
+ from spikinggamma.dendrite.synapse.base import BaseSynapticFunction
2
+ import torch
3
+ import torch.nn as nn
4
+ import torch.nn.functional as F
5
+
6
+
7
+ class Conv2dPerNeuron(nn.Conv2d, BaseSynapticFunction): # same filters for all buckets
8
+ def __init__(
9
+ self,
10
+ in_channels,
11
+ out_channels,
12
+ kernel_size,
13
+ stride=1,
14
+ padding=0,
15
+ dilation=1,
16
+ groups=1,
17
+ bias=False,
18
+ ):
19
+ super().__init__(
20
+ in_channels,
21
+ out_channels,
22
+ kernel_size,
23
+ stride,
24
+ padding,
25
+ dilation,
26
+ groups,
27
+ bias,
28
+ )
29
+
30
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
31
+ B, C_in, H, W, n_kernels = x.shape
32
+ x = x.permute(0, 4, 1, 2, 3).reshape(B * n_kernels, C_in, H, W)
33
+ weight = self.weight
34
+ bias = self.bias if self.bias is not None else None
35
+ y = F.conv2d(
36
+ x, weight, bias, self.stride, self.padding, self.dilation, self.groups
37
+ )
38
+ _, C_out, H_out, W_out = y.shape
39
+ y = y.reshape(B, n_kernels, C_out, H_out, W_out)
40
+ y = y.permute(0, 2, 3, 4, 1)
41
+ return y.contiguous()
@@ -0,0 +1,23 @@
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from spikinggamma.dendrite.synapse.base import BaseSynapticFunction
5
+
6
+
7
+
8
+ class LinearPerNeuron(nn.Linear, BaseSynapticFunction):
9
+ def __init__(
10
+ self,
11
+ in_features,
12
+ out_features,
13
+ bias=False,
14
+ ):
15
+ super().__init__(in_features, out_features, bias=bias)
16
+
17
+ def forward(self, inp: torch.Tensor) -> torch.Tensor:
18
+ inp = inp.transpose(1, 2)
19
+ weight = self.weight
20
+ bias = self.bias if self.bias is not None else None
21
+ out = F.linear(inp, weight, bias)
22
+ return out.transpose(1, 2)
23
+
@@ -0,0 +1,43 @@
1
+ from typing import Optional
2
+ import torch
3
+ import torch.nn as nn
4
+ from spikinggamma.dendrite.base import BaseDendriticFunction
5
+ from spikinggamma.signal.base import BaseSignalFunction
6
+ from spikinggamma.utils.enums import StepMode
7
+ from spikinggamma.spike.base import BaseSpikeFunction
8
+
9
+ class SpikingGammaNeuron(nn.Module):
10
+ def __init__(self, dendritic_fn: BaseDendriticFunction, signal_fn: Optional[BaseSignalFunction] = None, spike_fn: Optional[BaseSpikeFunction] = None, return_all=False):
11
+ super().__init__()
12
+ self.dendritic_fn = dendritic_fn
13
+ self.signal_fn = signal_fn
14
+ self.spike_fn = spike_fn
15
+ self.return_all = return_all
16
+
17
+ def detach(self):
18
+ self.dendritic_fn.detach()
19
+ if self.signal_fn is not None:
20
+ self.signal_fn.detach()
21
+ if self.spike_fn is not None:
22
+ self.spike_fn.detach()
23
+
24
+ def reset(self):
25
+ self.dendritic_fn.reset()
26
+ if self.signal_fn is not None:
27
+ self.signal_fn.reset()
28
+ if self.spike_fn is not None:
29
+ self.spike_fn.reset()
30
+
31
+ def forward(self, y_hat_k_in: torch.Tensor, step_mode: StepMode = StepMode.SINGLE) -> torch.Tensor:
32
+ x = self.dendritic_fn(y_hat_k_in, step_mode)
33
+ if self.signal_fn is None and self.spike_fn is None:
34
+ return x
35
+ y = x if self.signal_fn is None else self.signal_fn(y_hat_k_in, x, step_mode)
36
+ if self.spike_fn is None:
37
+ if self.return_all:
38
+ return y, {'x': x}
39
+ return y
40
+ y_hat_k_out, s_out = self.spike_fn(y, step_mode)
41
+ if self.return_all:
42
+ return y_hat_k_out, s_out, {'x': x, 'y': y}
43
+ return y_hat_k_out, s_out
File without changes
@@ -0,0 +1,19 @@
1
+ import torch
2
+ import torch.nn as nn
3
+
4
+ class ZeroActivatedReLUFunction(torch.autograd.Function):
5
+ @staticmethod
6
+ def forward(ctx, input):
7
+ ctx.save_for_backward(input)
8
+ return torch.relu(input)
9
+
10
+ @staticmethod
11
+ def backward(ctx, grad_output):
12
+ input, = ctx.saved_tensors
13
+ grad_input = grad_output.clone()
14
+ grad_input[input < 0] = 0
15
+ return grad_input
16
+
17
+ class ZeroActivatedReLU(nn.Module):
18
+ def forward(self, x):
19
+ return ZeroActivatedReLUFunction.apply(x)
@@ -0,0 +1,26 @@
1
+
2
+ from spikinggamma.utils.enums import StepMode
3
+ import torch
4
+ import torch.nn as nn
5
+
6
+ class BaseSignalFunction(nn.Module):
7
+ def __init__(self):
8
+ super().__init__()
9
+
10
+ def is_step_mode_compatible(self, step_mode: StepMode) -> bool:
11
+ return True
12
+
13
+ def reset(self):
14
+ pass
15
+
16
+ def detach(self):
17
+ pass
18
+
19
+ def _forward(self, y_hat_k: torch.Tensor, x: torch.Tensor, step_mode: StepMode) -> torch.Tensor:
20
+ raise NotImplementedError
21
+
22
+ def forward(self, y_hat_k: torch.Tensor, x: torch.Tensor, step_mode: StepMode = StepMode.SINGLE) -> torch.Tensor:
23
+ if not self.is_step_mode_compatible(step_mode):
24
+ raise ValueError(f"Signal function {self.__class__.__name__} is not compatible with step mode {step_mode}.")
25
+ y = self._forward(y_hat_k, x, step_mode)
26
+ return y
@@ -0,0 +1,27 @@
1
+ import torch
2
+ import torch.nn as nn
3
+ from spikinggamma.signal.base import BaseSignalFunction
4
+ from spikinggamma.signal.activation import ZeroActivatedReLU
5
+ from spikinggamma.utils.enums import StepMode
6
+
7
+ class DefaultSignalFunction(BaseSignalFunction):
8
+ # act_fn is ZeroActivatedReLU by default
9
+ def __init__(self,
10
+ pre_act_fn: nn.Module = nn.Identity(),
11
+ act_fn: nn.Module = ZeroActivatedReLU(),
12
+ post_act_fn: nn.Module = nn.Identity()):
13
+ super().__init__()
14
+ self.pre_act_fn = pre_act_fn
15
+ self.act_fn = act_fn
16
+ self.post_act_fn = post_act_fn
17
+
18
+ def reset(self):
19
+ pass
20
+
21
+ def _forward(self, y_hat_k: torch.Tensor, x: torch.Tensor, step_mode: StepMode) -> torch.Tensor:
22
+ x = self.pre_act_fn(x)
23
+ y = self.act_fn(x)
24
+ y_out = (
25
+ self.post_act_fn(y)
26
+ )
27
+ return y_out
@@ -0,0 +1,54 @@
1
+ import torch
2
+ import torch.nn as nn
3
+
4
+ from spikinggamma.utils.enums import StepMode
5
+
6
+
7
+ class LockedDropout(nn.Module):
8
+ """Drop feature channels with one mask shared across time.
9
+
10
+ Sequence inputs are expected to be shaped ``(T, B, ...)``. Each sample
11
+ receives its own mask, but that mask is held fixed for all ``T`` steps.
12
+ Explicit SINGLE or MULTI_STATEFUL calls reuse their mask until ``reset``.
13
+ Without an explicit step mode, non-sequence inputs retain ordinary
14
+ element-wise dropout semantics.
15
+ """
16
+
17
+ def __init__(self, p: float = 0.5) -> None:
18
+ super().__init__()
19
+ if not 0.0 <= p <= 1.0:
20
+ raise ValueError("dropout probability must be between 0 and 1")
21
+ self.p = float(p)
22
+ self.register_buffer("_mask", None, persistent=False)
23
+
24
+ def reset(self) -> None:
25
+ self._mask = None
26
+
27
+ def forward(
28
+ self, x: torch.Tensor, step_mode: StepMode = StepMode.MULTI_STATELESS
29
+ ) -> torch.Tensor:
30
+ if not self.training or self.p == 0.0:
31
+ return x
32
+ if self.p == 1.0:
33
+ return torch.zeros_like(x)
34
+
35
+ mask_shape = (
36
+ x.shape[1:]
37
+ if step_mode != StepMode.SINGLE and x.ndim >= 3
38
+ else x.shape
39
+ )
40
+ mask = self._mask if step_mode != StepMode.MULTI_STATELESS else None
41
+ if (
42
+ mask is None
43
+ or mask.shape != mask_shape
44
+ or mask.device != x.device
45
+ or mask.dtype != x.dtype
46
+ ):
47
+ mask = x.new_empty(mask_shape).bernoulli_(1.0 - self.p)
48
+ mask.div_(1.0 - self.p)
49
+ if step_mode != StepMode.MULTI_STATELESS:
50
+ self._mask = mask
51
+ return x * mask
52
+
53
+ def extra_repr(self) -> str:
54
+ return f"p={self.p}"
File without changes
@@ -0,0 +1,59 @@
1
+ from dataclasses import dataclass
2
+ from typing import Optional
3
+ import torch
4
+ import torch.nn as nn
5
+
6
+ from spikinggamma.utils.enums import StepMode
7
+
8
+
9
+ @dataclass
10
+ class SpikeConfig:
11
+ base_spike_threshold: float = 0.3
12
+ use_dynamic_threshold: bool = False # Set during inference, stateless in between samples
13
+ adaptation_mf: float = 0.3
14
+ spike_grad_multiplier: Optional[float] = None # will compute the multiplier during backward if None
15
+ saturation_gate_softness: float = 0. # can_saturate, disabled by default
16
+ saturation_gate_start_fraction: float = 0.9 # can_saturate
17
+ enable_saturation_gate: bool = True # can_saturate
18
+
19
+ @property
20
+ def can_saturate(self) -> bool:
21
+ return not (self.use_dynamic_threshold) and self.enable_saturation_gate
22
+
23
+
24
+
25
+ class BaseSpikeFunction(nn.Module):
26
+ spike_threshold: torch.Tensor # may be shape () or neuron specific shape, note that this shape is of the input to the spike function, not the output!
27
+
28
+ def __init__(self, spike_config: SpikeConfig):
29
+ super().__init__()
30
+ self.spike_config = spike_config
31
+ self.register_buffer("spike_threshold", torch.tensor(spike_config.base_spike_threshold))
32
+
33
+ def _load_from_state_dict(self, state_dict, prefix, *args, **kwargs):
34
+ # A checkpoint may hold one threshold per neuron, while a freshly constructed module holds a
35
+ # scalar. Torch silently copies only element 0 of a 1-d tensor into a 0-d buffer instead of
36
+ # raising, so match the stored shape first.
37
+ key = prefix + "spike_threshold"
38
+ if key in state_dict and state_dict[key].shape != self.spike_threshold.shape:
39
+ self.spike_threshold = self.spike_threshold.new_empty(state_dict[key].shape)
40
+ super()._load_from_state_dict(state_dict, prefix, *args, **kwargs)
41
+
42
+ def _forward(self, y: torch.Tensor, step_mode: StepMode) -> tuple[torch.Tensor, torch.Tensor]:
43
+ raise NotImplementedError
44
+
45
+ def forward(self, y: torch.Tensor, step_mode: StepMode = StepMode.SINGLE) -> tuple[torch.Tensor, torch.Tensor]:
46
+ if not self.is_step_mode_compatible(step_mode):
47
+ raise ValueError(f"Step mode {step_mode} not compatible with {self.__class__.__name__}")
48
+ # y can be (B, *shape) or (T, B, *shape)
49
+ y_hat_k, s = self._forward(y, step_mode)
50
+ return y_hat_k, s
51
+
52
+ def is_step_mode_compatible(self, step_mode: StepMode) -> bool:
53
+ return True
54
+
55
+ def reset(self):
56
+ pass
57
+
58
+ def detach(self):
59
+ pass
@@ -0,0 +1,64 @@
1
+ from typing import Optional
2
+ import torch
3
+
4
+ from spikinggamma.utils.enums import StepMode
5
+ from spikinggamma.spike.base import BaseSpikeFunction, SpikeConfig
6
+ from spikinggamma.spike.kernel.base import BaseKernelFunction, BaseKernelState, prototype_y_hat_k
7
+
8
+ # Takes input as spikes and passes them through the kernels.
9
+
10
+ class IdentitySpikeFunction(BaseSpikeFunction):
11
+ init_from_kernel_state: Optional[BaseKernelState] = None
12
+
13
+ def __init__(
14
+ self,
15
+ kernel_function: BaseKernelFunction,
16
+ spike_config: SpikeConfig,
17
+ ):
18
+ super().__init__(spike_config)
19
+ self.kernel_function = kernel_function
20
+ self.kernel_state = self.kernel_function.construct_kernel_state()
21
+ self.reset()
22
+
23
+ def reset(self):
24
+ self.kernel_state.reset()
25
+ self.init_from_kernel_state = None
26
+
27
+ def detach(self):
28
+ self.kernel_state.detach()
29
+ if self.init_from_kernel_state is not None:
30
+ self.init_from_kernel_state.detach()
31
+
32
+ def _forward(self, y: torch.Tensor, step_mode: StepMode) -> tuple[torch.Tensor, torch.Tensor]:
33
+ if step_mode == StepMode.SINGLE:
34
+ self.kernel_function.step(self.kernel_state)
35
+ self.kernel_function.add(self.kernel_state, y * 2 * self.spike_threshold)
36
+ y_hat_k = prototype_y_hat_k(self.kernel_state.compute_y_hat_k(), y.shape, self.kernel_function.n_kernels, y.device, y.dtype)
37
+ return y_hat_k, y
38
+ else:
39
+ init_state = self.init_from_kernel_state
40
+ if init_state is not None and not init_state.is_empty():
41
+ init_state.detach()
42
+ else:
43
+ init_state = None
44
+
45
+ kernel_state = self.kernel_function.construct_kernel_state()
46
+ kernel_state.initialize_like(y[0], self.kernel_function.n_kernels)
47
+ if init_state is not None:
48
+ kernel_state.init_from(init_state)
49
+
50
+ y_hat_k = torch.empty(
51
+ (*y.shape, self.kernel_function.n_kernels),
52
+ device=y.device,
53
+ dtype=y.dtype
54
+ )
55
+ for t in range(y.shape[0]):
56
+ self.kernel_function.step(kernel_state)
57
+ self.kernel_function.add(kernel_state, y[t] * 2 * self.spike_threshold)
58
+ y_hat_k[t] = prototype_y_hat_k(kernel_state.compute_y_hat_k(), y[t].shape, self.kernel_function.n_kernels, y.device, y.dtype)
59
+
60
+ if step_mode == StepMode.MULTI_STATEFUL:
61
+ self.init_from_kernel_state = kernel_state
62
+ return y_hat_k, y
63
+
64
+