-
Notifications
You must be signed in to change notification settings - Fork 5
/
np.py
438 lines (319 loc) · 16.5 KB
/
np.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
from .abstract import *
import numpy as np
from sys import byteorder, version_info
class NumPyBoolPermutation(MemoizedPermutation):
_permutations: 'dict[int | tuple[int, ...], Self]' = {}
rng = np.random.default_rng()
def __init__(self, array: np.ndarray):
self.data: np.ndarray = array
@classmethod
def random(cls) -> 'NumPyBoolPermutation':
return NumPyBoolPermutation(cls.rng.permutation(DIMENSION))
def __mul__(self, other: 'NumPyBoolPermutation') -> 'NumPyBoolPermutation':
return NumPyBoolPermutation(self.data[other.data])
def __invert__(self) -> 'NumPyBoolPermutation':
inv_permutation = np.empty_like(self.data)
inv_permutation[self.data] = np.arange(DIMENSION)
return NumPyBoolPermutation(inv_permutation)
def __call__(self, hv: 'NumPyBoolBHV') -> 'NumPyBoolBHV':
return hv.permute_bits(self)
NumPyBoolPermutation.IDENTITY = NumPyBoolPermutation(np.arange(DIMENSION))
class NumPyBoolBHV(AbstractBHV):
def __init__(self, array: np.ndarray):
self.data: np.ndarray = array
@classmethod
def rand(cls) -> 'NumPyBoolBHV':
return NumPyBoolBHV(np.random.randint(0, high=2, size=DIMENSION, dtype=np.bool_))
@classmethod
def random(cls, active: float) -> 'NumPyBoolBHV':
assert 0. <= active <= 1.
return NumPyBoolBHV(np.random.binomial(1, active, DIMENSION))
# FIXME, neither conforms to the default implementation
# Breadcrumbs: It doesn't seem *that* off, maybe unpackbits order, maybe
# def select(self, when1: 'NumPyBoolBHV', when0: 'NumPyBoolBHV') -> 'NumPyBoolBHV':
# return self.pack64().select(when1.pack64(), when0.pack64()).unpack()
# # return NumPyBoolBHV(np.where(self.data, when1.data, when0.data))
def swap_even_odd(self) -> 'NumPyBoolBHV':
n = np.where(np.arange(DIMENSION) % 2, np.roll(self.data, 1), np.roll(self.data, -1))
return NumPyBoolBHV(n)
def rehash(self) -> 'NumPyBoolBHV':
return self.pack8().rehash().unpack()
def roll_bits(self, n: int) -> 'NumPyBoolBHV':
return NumPyBoolBHV(np.roll(self.data, n))
def permute_bits(self, permutation: 'NumPyBoolPermutation') -> 'NumPyBoolBHV':
return NumPyBoolBHV(self.data[permutation.data])
def permute(self, permutation_id: 'int | tuple[int, ...]') -> 'NumPyBoolBHV':
return self.permute_bits(NumPyBoolPermutation.get(permutation_id))
@classmethod
def threshold(cls, vs: list['NumPyBoolBHV'], t: int) -> 'NumPyBoolBHV':
data = [v.data for v in vs]
tensor = np.stack(data)
counts = tensor.sum(axis=-2, dtype=np.uint8 if len(vs) < 256 else np.uint32)
return NumPyBoolBHV(np.greater(counts, t))
@classmethod
def _direct_majority(cls, vs: list['NumPyBoolBHV']) -> 'NumPyBoolBHV':
data = [v.data for v in vs]
extra = [cls.rand().data] if len(vs) % 2 == 0 else []
tensor = np.stack(data + extra)
counts = tensor.sum(axis=-2, dtype=np.uint8 if len(vs) < 256 else np.uint32)
threshold = (len(vs) + len(extra))//2
return NumPyBoolBHV(np.greater(counts, threshold))
@classmethod
def majority(cls, vs: list['NumPyBoolBHV']) -> 'NumPyBoolBHV':
extra = [cls.rand()] if len(vs) % 2 == 0 else []
threshold = (len(vs) + len(extra))//2
return cls.threshold(vs + extra, threshold)
@classmethod
def level(cls, active_fraction: float) -> Self:
bits = int(DIMENSION*active_fraction)
zs = np.zeros(DIMENSION, dtype=np.bool_)
zs[:bits] = np.ones(bits, dtype=np.bool_)
return NumPyBoolBHV(zs)
@classmethod
def _counting_representative(cls, vs: list['NumPyBoolBHV']) -> 'NumPyBoolBHV':
data = [v.data for v in vs]
tensor = np.stack(data)
counts = tensor.sum(axis=-2, dtype=np.uint8 if len(vs) < 256 else np.uint32)
refs = np.random.randint(low=0, high=len(vs), size=DIMENSION)
return NumPyBoolBHV(refs < counts)
@classmethod
def _sampling_representative(cls, vs: list['NumPyBoolBHV']) -> 'NumPyBoolBHV':
data = [v.data for v in vs]
tensor = np.stack(data)
ids = np.random.randint(low=0, high=len(vs), size=DIMENSION)
result = tensor[ids, np.arange(DIMENSION)]
return NumPyBoolBHV(result)
@classmethod
def representative(cls, vs: list['NumPyBoolBHV']) -> 'NumPyBoolBHV':
if len(vs) < 32:
return cls._counting_representative(vs)
else:
return cls._sampling_representative(vs)
def __eq__(self, other: 'NumPyBoolBHV') -> bool:
return np.array_equal(self.data, other.data)
def __xor__(self, other: 'NumPyBoolBHV') -> 'NumPyBoolBHV':
return NumPyBoolBHV(np.bitwise_xor(self.data, other.data))
def __and__(self, other: 'NumPyBoolBHV') -> 'NumPyBoolBHV':
return NumPyBoolBHV(np.bitwise_and(self.data, other.data))
def __or__(self, other: 'NumPyBoolBHV') -> 'NumPyBoolBHV':
return NumPyBoolBHV(np.bitwise_or(self.data, other.data))
def __invert__(self) -> 'NumPyBoolBHV':
return NumPyBoolBHV(np.bitwise_not(self.data))
def active(self) -> int:
return int(np.sum(self.data))
def pack8(self) -> 'NumPyPacked8BHV':
return NumPyPacked8BHV(np.packbits(self.data, bitorder='little'))
def pack64(self) -> 'NumPyPacked64BHV':
return NumPyPacked64BHV(np.packbits(self.data, bitorder='little').view(dtype=np.uint64))
def to_bytes(self):
return self.pack8().to_bytes()
@classmethod
def from_bytes(cls, bs):
return NumPyPacked8BHV.from_bytes(bs).unpack()
def bits(self):
return iter(self.data.view(np.uint8))
def bitstring(self) -> str:
b = ord('0')
return (self.data.view(np.uint8) + b).tobytes().decode('ascii')
@classmethod
def from_bitstream(cls, bits_s: 'Iterator[bool]') -> Self:
data = np.fromiter(bits_s, dtype=np.bool_, count=DIMENSION)
return cls(data)
@classmethod
def from_bitstring(cls, s: str) -> Self:
b = ord('0')
return cls((np.frombuffer(bytes(s, 'ascii'), dtype=np.uint8, count=DIMENSION) - b).view(np.bool_))
NumPyBoolBHV.ZERO = NumPyBoolBHV(np.zeros(DIMENSION, dtype=np.bool_))
NumPyBoolBHV.ONE = NumPyBoolBHV(np.ones(DIMENSION, dtype=np.bool_))
NumPyBoolBHV._FEISTAL_SUBKEYS = NumPyBoolBHV.nrand2(NumPyBoolBHV._FEISTAL_ROUNDS, 4)
NumPyBoolBHV.EVEN = NumPyBoolBHV((np.arange(DIMENSION, dtype=np.uint8) % 2).astype(np.bool_))
NumPyBoolBHV.ODD = NumPyBoolBHV(((np.arange(DIMENSION, dtype=np.uint8) + 1) % 2).astype(np.bool_))
class NumPyBytePermutation(MemoizedPermutation):
_permutations: 'dict[int | tuple[int, ...], Self]' = {}
rng = np.random.default_rng()
def __init__(self, array: np.ndarray):
self.data: np.ndarray = array
@classmethod
def random(cls) -> 'NumPyBytePermutation':
return NumPyBytePermutation(cls.rng.permutation(DIMENSION//8))
def __mul__(self, other: 'NumPyBytePermutation') -> 'NumPyBytePermutation':
return NumPyBytePermutation(self.data[other.data])
def __invert__(self) -> 'NumPyBytePermutation':
inv_permutation = np.empty_like(self.data)
inv_permutation[self.data] = np.arange(DIMENSION//8)
return NumPyBytePermutation(inv_permutation)
def __call__(self, hv: 'NumPyPacked8BHV') -> 'NumPyPacked8BHV':
return hv.permute_bytes(self)
NumPyBytePermutation.IDENTITY = NumPyBytePermutation(np.arange(DIMENSION//8))
class NumPyPacked8BHV(AbstractBHV):
def __init__(self, array: np.ndarray):
self.data: np.ndarray = array
@classmethod
def rand(cls) -> 'NumPyPacked8BHV':
return NumPyPacked8BHV(np.random.randint(0, 255, DIMENSION//8, dtype=np.uint8))
@classmethod
def random(cls, active: float) -> 'NumPyPacked8BHV':
assert 0. <= active <= 1.
return NumPyBoolBHV.random(active).pack8()
def roll_bytes(self, n: int) -> 'NumPyPacked8BHV':
assert abs(n) < DIMENSION//8, "only supports DIMENSION/8 rolls"
return NumPyPacked8BHV(np.roll(self.data, n))
def swap_even_odd(self) -> 'NumPyPacked8BHV':
return self.repack64().swap_even_odd().repack8()
def permute_bytes(self, permutation: 'NumPyBytePermutation') -> 'NumPyPacked8BHV':
return NumPyPacked8BHV(self.data[permutation.data])
def permute(self, permutation_id: 'int | tuple[int, ...]') -> 'NumPyPacked8BHV':
return self.permute_bytes(NumPyBytePermutation.get(permutation_id))
def rehash(self) -> 'NumPyPacked8BHV':
byte_data = self.to_bytes()
rehashed_byte_data = hashlib.shake_256(byte_data).digest(DIMENSION//8)
return NumPyPacked8BHV.from_bytes(rehashed_byte_data)
@classmethod
def threshold(cls, vs: 'list[NumPyPacked8BHV]', t: int) -> 'NumPyPacked8BHV':
return NumPyBoolBHV.threshold([v.unpack() for v in vs], t).pack8()
@classmethod
def level(cls, active_fraction: float) -> Self:
return NumPyBoolBHV.level(active_fraction).pack8()
@classmethod
def representative(cls, vs: 'list[NumPyPacked8BHV]') -> 'NumPyPacked8BHV':
return NumPyBoolBHV.representative([v.unpack() for v in vs]).pack8()
def __eq__(self, other: 'NumPyPacked8BHV') -> bool:
return np.array_equal(self.data, other.data)
def __xor__(self, other: 'NumPyPacked8BHV') -> 'NumPyPacked8BHV':
return NumPyPacked8BHV(np.bitwise_xor(self.data, other.data))
def __and__(self, other: 'NumPyPacked8BHV') -> 'NumPyPacked8BHV':
return NumPyPacked8BHV(np.bitwise_and(self.data, other.data))
def __or__(self, other: 'NumPyPacked8BHV') -> 'NumPyPacked8BHV':
return NumPyPacked8BHV(np.bitwise_or(self.data, other.data))
def __invert__(self) -> 'NumPyPacked8BHV':
return NumPyPacked8BHV(np.bitwise_not(self.data))
if version_info[1] >= 10:
def active(self) -> int:
return int.from_bytes(self.data.tobytes(), byteorder).bit_count()
else:
lookup = np.array([bin(i).count("1") for i in range(256)])
def active(self) -> int:
return self.lookup[self.data].sum()
def unpack(self) -> 'NumPyBoolBHV':
return NumPyBoolBHV(np.unpackbits(self.data, bitorder="little"))
def repack64(self) -> 'NumPyPacked64BHV':
return NumPyPacked64BHV(self.data.view(dtype=np.uint64))
def to_bytes(self):
return self.data.tobytes()
@classmethod
def from_bytes(cls, bs):
return cls(np.frombuffer(bs, dtype=np.uint8, count=DIMENSION//8))
NumPyPacked8BHV.ZERO = NumPyPacked8BHV(np.zeros(DIMENSION//8, dtype=np.uint8))
NumPyPacked8BHV.ONE = NumPyPacked8BHV(np.full(DIMENSION//8, fill_value=255, dtype=np.uint8))
NumPyPacked8BHV._FEISTAL_SUBKEYS = NumPyPacked8BHV.nrand2(NumPyPacked8BHV._FEISTAL_ROUNDS, 4)
NumPyPacked8BHV.EVEN = NumPyPacked8BHV(np.full(DIMENSION//8, 0x55, dtype=np.uint8))
NumPyPacked8BHV.ODD = NumPyPacked8BHV(np.full(DIMENSION//8, 0xaa, dtype=np.uint8))
class NumPyWordPermutation(MemoizedPermutation):
_permutations: 'dict[int | tuple[int, ...], Self]' = {}
rng = np.random.default_rng()
def __init__(self, array: np.ndarray):
self.data: np.ndarray = array
@classmethod
def random(cls) -> 'NumPyWordPermutation':
return NumPyWordPermutation(cls.rng.permutation(DIMENSION//64))
def __mul__(self, other: 'NumPyWordPermutation') -> 'NumPyWordPermutation':
return NumPyWordPermutation(self.data[other.data])
def __invert__(self) -> 'NumPyWordPermutation':
inv_permutation = np.empty_like(self.data)
inv_permutation[self.data] = np.arange(DIMENSION//64)
return NumPyWordPermutation(inv_permutation)
def __call__(self, hv: 'NumPyPacked64BHV') -> 'NumPyPacked64BHV':
return hv.permute_words(self)
NumPyWordPermutation.IDENTITY = NumPyWordPermutation(np.arange(DIMENSION//64))
class NumPyPacked64BHV(AbstractBHV):
rng = np.random.SFC64()
def __init__(self, array: np.ndarray):
self.data: np.ndarray = array
@classmethod
def rand(cls) -> 'NumPyPacked64BHV':
return NumPyPacked64BHV(cls.rng.random_raw(DIMENSION//64))
@classmethod
def random(cls, active: float) -> 'NumPyPacked64BHV':
assert 0. <= active <= 1.
return NumPyBoolBHV.random(active).pack64()
@classmethod
def majority(cls, vs: list['NumPyPacked64BHV']) -> 'NumPyPacked64BHV':
if len(vs) <= 9:
return cls._majority_via_custom(vs)
else:
return cls._majority_via_unpacked(vs)
@classmethod
def _majority_via_unpacked(cls, vs: list['NumPyPacked64BHV']) -> 'NumPyPacked64BHV':
return NumPyBoolBHV.majority([v.unpack() for v in vs]).pack64()
@classmethod
def representative(cls, vs: list['NumPyPacked64BHV']) -> 'NumPyPacked64BHV':
return NumPyBoolBHV.representative([v.unpack() for v in vs]).pack64()
@classmethod
def threshold(cls, vs: list[Self], t: int) -> Self:
return NumPyBoolBHV.threshold([v.unpack() for v in vs], t).pack64()
@classmethod
def level(cls, active_fraction: float) -> Self:
return NumPyBoolBHV.level(active_fraction).pack64()
def swap_even_odd(self) -> 'NumPyPacked64BHV':
return self.EVEN.select(self.roll_word_bits(1), self.roll_word_bits(-1))
def rehash(self) -> 'NumPyPacked64BHV':
return self.repack8().rehash().repack64()
def roll_words(self, n: int) -> 'NumPyPacked64BHV':
assert abs(n) < DIMENSION//64, "only supports DIMENSION/64 rolls"
return NumPyPacked64BHV(np.roll(self.data, n))
def roll_word_bits(self, n: int) -> 'NumPyPacked64BHV':
assert abs(n) < 64, "only supports 64 rolls"
n = (n + 64) % 64
# https://github.com/numba/numba/issues/6381
if n == 0:
return NumPyPacked64BHV(self.data)
else:
return NumPyPacked64BHV(np.bitwise_or(np.right_shift(self.data, np.uint64(n)), np.left_shift(self.data, np.uint64(64 - n))))
# roll_words and roll_word_bits could be combined for more options allowing positive and negative combinations
# ((1 2 3 4) (a b c d) (α β γ δ))
# rolled by 1, -2 for example results in
# ((γ δ α β) (3 4 1 2) (c d a b))
def roll_bits(self, o):
b = o // 64
e = o % 64
l = self.roll_words(b)
h = self.roll_words(b - 1)
lo = l.roll_word_bits(e)
ho = h.roll_word_bits(e)
# equiv: NumPyPacked64BHV.from_bitstream((i % 64) >= e for i in range(DIMENSION))
c = NumPyPacked64BHV(np.full(DIMENSION//64, -1 << e, dtype=np.uint64))
return c.select(lo, ho)
def permute_words(self, permutation: 'NumPyWordPermutation') -> 'NumPyPacked64BHV':
return NumPyPacked64BHV(self.data[permutation.data])
def permute(self, permutation_id: 'int | tuple[int, ...]') -> 'NumPyPacked64BHV':
return self.permute_words(NumPyWordPermutation.get(permutation_id))
def __eq__(self, other: 'NumPyPacked64BHV') -> bool:
return np.array_equal(self.data, other.data)
def __xor__(self, other: 'NumPyPacked64BHV') -> 'NumPyPacked64BHV':
return NumPyPacked64BHV(np.bitwise_xor(self.data, other.data))
def __and__(self, other: 'NumPyPacked64BHV') -> 'NumPyPacked64BHV':
return NumPyPacked64BHV(np.bitwise_and(self.data, other.data))
def __or__(self, other: 'NumPyPacked64BHV') -> 'NumPyPacked64BHV':
return NumPyPacked64BHV(np.bitwise_or(self.data, other.data))
def __invert__(self) -> 'NumPyPacked64BHV':
return NumPyPacked64BHV(np.bitwise_not(self.data))
if version_info[2] >= 10:
def active(self) -> int:
return int.from_bytes(self.data.tobytes(), byteorder).bit_count()
else:
def active(self) -> int:
return self.repack8().active()
def unpack(self) -> 'NumPyBoolBHV':
return NumPyBoolBHV(np.unpackbits(self.data.view(np.uint8), count=DIMENSION, bitorder='little'))
def repack8(self) -> 'NumPyPacked8BHV':
return NumPyPacked8BHV(self.data.view(np.uint8))
def to_bytes(self):
return self.repack8().to_bytes()
@classmethod
def from_bytes(cls, bs):
return NumPyPacked8BHV.from_bytes(bs).repack64()
NumPyPacked64BHV.ZERO = NumPyPacked64BHV(np.zeros(DIMENSION//64, dtype=np.uint64))
NumPyPacked64BHV.ONE = NumPyPacked64BHV(np.full(DIMENSION//64, fill_value=-1, dtype=np.uint64))
NumPyPacked64BHV._FEISTAL_SUBKEYS = NumPyPacked64BHV.nrand2(NumPyPacked64BHV._FEISTAL_ROUNDS, 4)
NumPyPacked64BHV.EVEN = NumPyPacked64BHV(np.full(DIMENSION//64, 0x5555555555555555, dtype=np.uint64))
NumPyPacked64BHV.ODD = NumPyPacked64BHV(np.full(DIMENSION//64, 0xaaaaaaaaaaaaaaaa, dtype=np.uint64))