forked from lava-nc/lava-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
183 lines (158 loc) · 5.38 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
"""HDF5 net description manipulation utilities."""
from typing import Tuple, Union
import h5py
import numpy as np
from enum import IntEnum, unique
@unique
class SYNAPSE_SIGN_MODE(IntEnum):
"""Enum for synapse sign mode. Options are {``MIXED : 1``,
``EXCITATORY : 2`` and ``INHIBITORY : 2``}.
"""
MIXED = 1
EXCITATORY = 2
INHIBITORY = 3
class NetDict:
"""Provides dictionary like access to h5py object without the h5py quirks
Parameters
----------
filename : str or None, optional
filename of h5py file to be loaded. It is only invoked if hdf5 file
handle ``f`` is ``None``. Default is None.
mode : str, optional
file open mode, by default 'r'.
f : h5py.File or h5py.Group, optional
hdf5 file object handle. Overwrites the function of filename if it is
not ``None``. Default is None.
"""
def __init__(
self,
filename: Union[str, None] = None,
mode: str = 'r',
f: Union[h5py.File, h5py.Group] = None,
) -> None:
self.f = h5py.File(filename, mode) if f is None else f
self.str_keys = ['type']
self.array_keys = [
'shape', 'stride', 'padding', 'dilation', 'groups', 'delay',
'iDecay', 'refDelay', 'scaleRho', 'tauRho', 'theta', 'vDecay',
'vThMant', 'wgtExp', 'sinDecay', 'cosDecay'
]
self.copy_keys = ['weight', 'bias', 'weight/real', 'weight/imag']
def keys(self) -> h5py._hl.base.KeysViewHDF5:
return self.f.keys()
def __len__(self) -> int:
return len(self.f)
def __getitem__(self, key: str) -> h5py.Dataset:
if key in self.str_keys:
value = self.f[key]
if len(value.shape) > 0:
value = value[0]
else:
value = value[()]
return value.decode('ascii')
elif key in self.copy_keys:
return self.f[key][()].astype(int).copy()
elif key in self.array_keys:
return self.f[key][()]
elif isinstance(key, int) and f'{key}' in self.f.keys():
return NetDict(f=self.f[f'{key}'])
else:
return NetDict(f=self.f[key])
def __setitem__(self, key: str) -> None:
raise NotImplementedError('Set feature is not implemented.')
def optimize_weight_bits(weight: np.ndarray) -> Tuple[
np.ndarray, int, int, SYNAPSE_SIGN_MODE
]:
"""Optimizes the weight matrix to best fit in Loihi's synapse.
Parameters
----------
weight : np.ndarray
standard 8 bit signed weight matrix.
Returns
-------
np.ndarray
optimized weight matrix
int
weight bits
int
weight_exponent
SYNAPSE_SIGN_MODE
synapse sign mode
"""
max_weight = np.max(weight)
min_weight = np.min(weight)
if max_weight > 254 and min_weight < 0:
print(f'[WARNING] weight matrix cannot be optimized to fit in synapse.')
print(f' (max weight too large: {max_weight}, clipped to 254)')
weight = np.clip(weight, -256, 254)
max_weight = np.max(weight)
if min_weight < -256 and max_weight > 0:
print(f'[WARNING] weight matrix cannot be optimized to fit in synapse.')
print(f' (min weight too small: {min_weight}, clipped to -256)')
weight = np.clip(weight, -256, 254)
min_weight = np.min(weight)
if max_weight < 0:
sign_mode = SYNAPSE_SIGN_MODE.INHIBITORY
is_signed = 0
elif min_weight >= 0:
sign_mode = SYNAPSE_SIGN_MODE.EXCITATORY
is_signed = 0
else:
sign_mode = SYNAPSE_SIGN_MODE.MIXED
is_signed = 1
if sign_mode == SYNAPSE_SIGN_MODE.MIXED:
pos_scale = 127 / max_weight
neg_scale = -128 / min_weight
scale = np.min([pos_scale, neg_scale])
elif sign_mode == SYNAPSE_SIGN_MODE.INHIBITORY:
scale = -256 / min_weight
elif sign_mode == SYNAPSE_SIGN_MODE.EXCITATORY:
scale = 255 / max_weight
# scale down weights so they are less than or equal to 8 bits
scale_bits = min(np.floor(np.log2(scale)), 0)
weight = np.right_shift(weight, int(-scale_bits))
weight_exponent = int(-scale_bits)
precision_found = False
n = 8
while (precision_found is False) and (n > 0):
roundingError = np.sum(
np.abs(weight / (2**n) - np.round(weight / (2**n)))
)
if roundingError == 0:
precision_found = True
else:
n -= 1
# Scale down weight by gcf of 2
weight = np.right_shift(weight , int(n))
weight_exponent += int(n)
num_weight_bits = int(8 + is_signed) # Prevents truncation
return (
weight.astype(int),
num_weight_bits,
weight_exponent,
sign_mode
)
def num_delay_bits(delays: np.ndarray) -> int:
"""Calculates the number of delay bits required.
Parameters
----------
delays : np.ndarray
delay vector
Returns
-------
int
number of delay bits.
"""
if delays.min() < 0:
raise ValueError(
f'Negative delay encountered. '
f'Found {delays.min()=}.'
)
if delays.max() >= 63:
raise ValueError(
f'Max delay exceeded limit of 62. '
f'Found {delays.max()=}.'
)
return np.ceil(np.log2(delays.max())).astype(int)