Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set user defined spike_exp level globally when creating netx network #249

Merged
merged 54 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
4328af7
Merge branch 'lava-nc:main' into main
bamsumit May 18, 2022
1789ef9
Merge branch 'main' of github.com:bamsumit/lava-dl into main
bamsumit Jul 26, 2022
0174ac5
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Sep 29, 2022
c77d7b2
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Oct 5, 2022
4ae5d38
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Oct 24, 2022
45ed337
CI fix for poetry on windows error
bamsumit Oct 24, 2022
391ee3d
Merge branch 'main' of github.com:bamsumit/lava-dl into main
bamsumit Oct 24, 2022
0196460
removed a caching option
bamsumit Oct 24, 2022
8ac07b9
Reverted caching changes
bamsumit Oct 24, 2022
327392b
Reverted caching changes
bamsumit Oct 24, 2022
cd18e65
pipx intallation of poetry
bamsumit Oct 24, 2022
a506edd
removed cache in python installation
bamsumit Oct 24, 2022
19b66a6
Testing testing
bamsumit Oct 24, 2022
5d15056
Updated CI
bamsumit Oct 24, 2022
4d719c3
Change to python v4
bamsumit Oct 24, 2022
e3cb93b
Remove poetry cache
bamsumit Oct 24, 2022
982c656
comments cleanup
bamsumit Oct 24, 2022
f4beb58
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Oct 24, 2022
7dfec3e
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Nov 29, 2022
721187c
Updated Readme to point to lava-dl decolle
bamsumit Nov 29, 2022
d2b82c1
language update
bamsumit Nov 29, 2022
5c94b6a
Updated illustration image
bamsumit Nov 29, 2022
c3348b6
Updated readme
bamsumit Nov 29, 2022
6922b21
Merge branch 'main' into main
bamsumit Dec 1, 2022
f825a07
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Jan 11, 2023
d08ee6b
Merge branch 'main' of github.com:bamsumit/lava-dl into main
bamsumit Jan 11, 2023
6b752b7
Updated benchmarking notebooks to use new callback_fxs api
bamsumit Jan 11, 2023
bd40deb
Fixed netx.hdf5 to handle scale dimensions on input.
bamsumit Jan 13, 2023
24a0892
Linting fixes
bamsumit Jan 13, 2023
bb478da
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Jan 13, 2023
1b9abcf
Restore data from lfs
bamsumit Jan 13, 2023
e37c45a
Temp removing file
bamsumit Jan 13, 2023
0d2bda6
added mnist network file
bamsumit Jan 13, 2023
25ca0f1
added lfs git attributes
bamsumit Jan 13, 2023
a2286c6
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Jan 19, 2023
006cbb2
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Mar 3, 2023
22ced7c
Merge branch 'main' of github.com:bamsumit/lava-dl into main
bamsumit Mar 3, 2023
8fce3c0
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Mar 13, 2023
bb40881
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit May 4, 2023
456d27b
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit May 22, 2023
438249b
Renamed test file to match soruce heirarchy.
bamsumit May 22, 2023
0ba32b4
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit May 22, 2023
4498c0c
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Aug 3, 2023
e1dc89a
Fix delay clamping and handling
bamsumit Aug 4, 2023
59dc6cd
Fixed linting
bamsumit Aug 4, 2023
d180d97
Merge branch 'main' of github.com:lava-nc/lava-dl into main
bamsumit Aug 9, 2023
272a217
Merge branch 'lava-nc:main' into main
bamsumit Sep 26, 2023
d09d98f
Device parameters for sigma
bamsumit Sep 26, 2023
c336a4a
Updated export
bamsumit Sep 26, 2023
b717156
Merge branch 'lava-nc:main' into main
bamsumit Sep 29, 2023
e86a5bf
Enable global spike_exp precision when creating netx network
bamsumit Oct 10, 2023
9e6219d
Merge branch 'lava-nc:main' into main
bamsumit Oct 10, 2023
4535261
Fixed conflicts
bamsumit Oct 10, 2023
1f5e296
Merge error fix
bamsumit Oct 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 35 additions & 13 deletions src/lava/lib/dl/netx/hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class Network(AbstractProcess):
neuron's reset parameter. None means no reset. Defaults to None.
reset_offset: int
determines the phase shift of network reset if enabled. Defaults to 0.
spike_exp: int
determines the decimal place of graded spike. Defaults to 6.
"""

def __init__(self,
Expand All @@ -62,7 +64,8 @@ def __init__(self,
input_message_bits: Optional[int] = 0,
input_shape: Optional[Tuple[int, ...]] = None,
reset_interval: Optional[int] = None,
reset_offset: int = 0) -> None:
reset_offset: int = 0,
spike_exp: int = 6) -> None:
super().__init__(net_config=net_config,
num_layers=num_layers,
input_message_bits=input_message_bits)
Expand All @@ -75,6 +78,7 @@ def __init__(self,
self.input_shape = input_shape
self.reset_interval = reset_interval
self.reset_offset = reset_offset
self.spike_exp = spike_exp

self.net_str = ''
self.layers = self._create()
Expand Down Expand Up @@ -102,7 +106,8 @@ def __len__(self) -> int:
def get_neuron_params(neuron_config: h5py.Group,
input: bool = False,
reset_interval: Optional[int] = None,
reset_offset: int = 0) -> AbstractProcess:
reset_offset: int = 0,
spike_exp: int = 6) -> AbstractProcess:
"""Provides the correct neuron configuration process and parameters
from the neuron description in hdf5 config.

Expand All @@ -118,6 +123,8 @@ def get_neuron_params(neuron_config: h5py.Group,
reset_offset: int
the offset/phase of reset. It is only valid of reset_interval is
not None.
spike_exp: int
determines the decimal place of graded spike. Defaults to 6.

Returns
-------
Expand Down Expand Up @@ -159,7 +166,7 @@ def get_neuron_params(neuron_config: h5py.Group,
neuron_process = Delta
neuron_params = {'neuron_proc': neuron_process,
'vth': neuron_config['vThMant'],
'spike_exp': 6,
'spike_exp': spike_exp,
'state_exp': 6,
'num_message_bits': num_message_bits}
elif 'sigma_output' in neuron_config.keys():
Expand All @@ -170,7 +177,7 @@ def get_neuron_params(neuron_config: h5py.Group,
neuron_process = SigmaDelta
neuron_params = {'neuron_proc': neuron_process,
'vth': neuron_config['vThMant'],
'spike_exp': 6,
'spike_exp': spike_exp,
'state_exp': 6,
'num_message_bits': num_message_bits}
return neuron_params
Expand Down Expand Up @@ -233,7 +240,8 @@ def array_entry(data: Union[int, Tuple[int, int], None]) -> str:
@staticmethod
def create_input(layer_config: h5py.Group,
reset_interval: Optional[int] = None,
reset_offset: int = 0) -> Tuple[Input, str]:
reset_offset: int = 0,
spike_exp: int = 6) -> Tuple[Input, str]:
"""Creates input layer from layer configuration.

Parameters
Expand All @@ -245,6 +253,8 @@ def create_input(layer_config: h5py.Group,
reset_offset: int
the offset/phase of reset. It is only valid of reset_interval is
not None.
spike_exp: int
determines the decimal place of graded spike. Defaults to 6.

Returns
-------
Expand All @@ -257,7 +267,8 @@ def create_input(layer_config: h5py.Group,
neuron_params = Network.get_neuron_params(layer_config['neuron'],
reset_interval=reset_interval,
reset_offset=reset_offset,
input=True)
input=True,
spike_exp=spike_exp)

if 'weight' in layer_config.keys():
weight = int(layer_config['weight'])
Expand Down Expand Up @@ -298,7 +309,8 @@ def create_input(layer_config: h5py.Group,
def create_dense(layer_config: h5py.Group,
input_message_bits: int = 0,
reset_interval: Optional[int] = None,
reset_offset: int = 0) -> Tuple[Dense, str]:
reset_offset: int = 0,
spike_exp: int = 6) -> Tuple[Dense, str]:
"""Creates dense layer from layer configuration

Parameters
Expand All @@ -313,6 +325,8 @@ def create_dense(layer_config: h5py.Group,
reset_offset: int
the offset/phase of reset. It is only valid of reset_interval is
not None.
spike_exp: int
determines the decimal place of graded spike. Defaults to 6.

Returns
-------
Expand All @@ -335,7 +349,8 @@ def create_dense(layer_config: h5py.Group,
'gradedSpike': False}
neuron_params = Network.get_neuron_params(neuron_config,
reset_interval=reset_interval,
reset_offset=reset_offset)
reset_offset=reset_offset,
spike_exp=spike_exp)
if "weight/imag" in layer_config.f:
weight_real = layer_config['weight/real']
weight_imag = layer_config['weight/imag']
Expand Down Expand Up @@ -418,7 +433,8 @@ def create_conv(layer_config: h5py.Group,
input_shape: Tuple[int, int, int],
input_message_bits: int = 0,
reset_interval: Optional[int] = None,
reset_offset: int = 0) -> Tuple[Conv, str]:
reset_offset: int = 0,
spike_exp: int = 6) -> Tuple[Conv, str]:
"""Creates conv layer from layer configuration

Parameters
Expand All @@ -435,6 +451,8 @@ def create_conv(layer_config: h5py.Group,
reset_offset: int
the offset/phase of reset. It is only valid of reset_interval is
not None.
spike_exp: int
determines the decimal place of graded spike. Defaults to 6.

Returns
-------
Expand All @@ -450,7 +468,8 @@ def expand(x):
shape = tuple(layer_config['shape'][::-1]) # WHC (XYZ)
neuron_params = Network.get_neuron_params(layer_config['neuron'],
reset_interval=reset_interval,
reset_offset=reset_offset)
reset_offset=reset_offset,
spike_exp=spike_exp)
weight = layer_config['weight'][:, :, ::-1, ::-1]
weight = weight.reshape(weight.shape[:4]).transpose((0, 3, 2, 1))
stride = expand(layer_config['stride'])
Expand Down Expand Up @@ -534,7 +553,8 @@ def _create(self) -> List[AbstractProcess]:
layer, table = self.create_input(
layer_config[i],
reset_interval=reset_interval,
reset_offset=reset_offset)
reset_offset=reset_offset,
spike_exp=self.spike_exp)
if i >= self.skip_layers:
layers.append(layer)
reset_offset += 1
Expand All @@ -559,7 +579,8 @@ def _create(self) -> List[AbstractProcess]:
input_shape=input_shape,
input_message_bits=input_message_bits,
reset_interval=reset_interval,
reset_offset=reset_offset)
reset_offset=reset_offset,
spike_exp=self.spike_exp)
if i >= self.skip_layers:
layers.append(layer)
reset_offset += 1
Expand Down Expand Up @@ -587,7 +608,8 @@ def _create(self) -> List[AbstractProcess]:
layer_config=layer_config[i],
input_message_bits=input_message_bits,
reset_interval=reset_interval,
reset_offset=reset_offset)
reset_offset=reset_offset,
spike_exp=self.spike_exp)
if i >= self.skip_layers:
layers.append(layer)
reset_offset += 1
Expand Down
34 changes: 34 additions & 0 deletions tests/lava/lib/dl/netx/test_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,40 @@ def test_pilotnet_sdnn(self) -> None:
f'Error was {error}.'
)

def test_pilotnet_sdnn_spike_exp(self) -> None:
"""Tests the output of pilotnet sdnn with spike exp."""
net_config = root + '/gts/pilotnet_sdnn/network.net'
net = netx.hdf5.Network(net_config=net_config, spike_exp=0)
input = np.load(root + '/gts/pilotnet_sdnn/input.npy')
source = io.source.RingBuffer(data=input)
sink = io.sink.RingBuffer(shape=net.out_layer.shape,
buffer=len(net.layers))
source.s_out.connect(net.in_layer.neuron.a_in)
net.out_layer.out.connect(sink.a_in)

num_steps = len(net.layers)
run_condition = RunSteps(num_steps=num_steps)
run_config = TestRunConfig(select_tag='fixed_pt')
net.run(condition=run_condition, run_cfg=run_config)
output = sink.data.get()
net.stop()

scale = (1 << (6 - net.spike_exp))
gt = np.load(root + '/gts/pilotnet_sdnn/output.npy') / scale
error = np.abs(output - gt).mean()
if verbose:
print('Network:')
print(net)
print(f'{output=}')
print('PilotNet SDNN spike error:', error)

self.assertTrue(
error < 2 * scale,
f'Output spike and ground truth do not match for PilotNet SDNN. '
f'Found {output[output != gt] = } and {gt[output != gt] = }. '
f'Error was {error}.'
)

def test_axonal_delay_ntidigits(self) -> None:
"""Tests the output of ntidigits hdf5 description. This network
consists of axonal delay. So this tests specifically tests for
Expand Down