Utilities¶
Core utility modules for bitstream encoding/decoding, random number generation, and signal analysis.
Bitstream Encoding¶
sc_neurocore.utils.bitstreams.generate_bernoulli_bitstream(p, length, rng=None)
¶
Generate a Bernoulli bitstream of given length with probability p of '1'. This is the core SC primitive: a sequence of 0/1 bits where the proportion of 1s ~ p. Parameters
p : float Probability of 1 (unipolar encoding, 0 <= p <= 1). length : int Number of bits in the stream. rng : RNG, optional RNG instance. If None, a fresh RNG is created. Returns
np.ndarray Array of shape (length,) with dtype=uint8, values in {0,1}.
Source code in src/sc_neurocore/utils/bitstreams.py
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 | |
sc_neurocore.utils.bitstreams.generate_sobol_bitstream(p, length, seed=None)
¶
Generate a bitstream using a Sobol sequence (Low Discrepancy Sequence). LDS provides faster convergence than random Bernoulli sequences (O(1/N) vs O(1/sqrt(N))).
Parameters¶
p : float Target probability. length : int Length of the bitstream. seed : int, optional Seed for the Sobol engine.
Returns¶
np.ndarray Array of shape (length,) with dtype=uint8, values in {0,1}.
Source code in src/sc_neurocore/utils/bitstreams.py
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 | |
sc_neurocore.utils.bitstreams.bitstream_to_probability(bitstream)
¶
Decode a unipolar bitstream back into a probability estimate. p_hat = (# of ones) / length
Source code in src/sc_neurocore/utils/bitstreams.py
98 99 100 101 102 103 104 105 | |
sc_neurocore.utils.bitstreams.value_to_unipolar_prob(x, x_min, x_max, clip=True)
¶
Map a scalar x from [x_min, x_max] into a unipolar probability [0,1]. Linear mapping: p = (x - x_min) / (x_max - x_min) If clip=True, x is clipped into [x_min, x_max].
Source code in src/sc_neurocore/utils/bitstreams.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
sc_neurocore.utils.bitstreams.unipolar_prob_to_value(p, x_min, x_max)
¶
Map a unipolar probability p in [0,1] back to a scalar in [x_min, x_max]. Inverse of value_to_unipolar_prob.
Source code in src/sc_neurocore/utils/bitstreams.py
167 168 169 170 171 172 173 174 175 176 177 178 | |
sc_neurocore.utils.bitstreams.BitstreamEncoder
dataclass
¶
Helper for encoding continuous scalar values into SC bitstreams using linear unipolar mapping. Example
encoder = BitstreamEncoder(x_min=0.0, x_max=0.1, length=1024, seed=123) bitstream = encoder.encode(0.06) # 60% ones p_hat = bitstream_to_probability(bitstream) x_rec = encoder.decode(bitstream)
Source code in src/sc_neurocore/utils/bitstreams.py
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 | |
sc_neurocore.utils.bitstreams.BitstreamAverager
dataclass
¶
Sliding-window probability estimator for bitstreams.
Example¶
avg = BitstreamAverager(window=100) for _ in range(100): ... avg.push(1) avg.estimate() 1.0 avg.push(0) avg.estimate() < 1.0 True
Source code in src/sc_neurocore/utils/bitstreams.py
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 | |
Bipolar Encoding¶
sc_neurocore.utils.bitstreams.generate_bipolar_bitstream(x, length, rng=None)
¶
Generate a bipolar SC bitstream encoding a value in [-1, +1].
Bipolar encoding: value x in [-1, 1] maps to probability p = (x + 1) / 2. Bit=1 with probability p, bit=0 with probability 1-p. Decoding: x = 2 * mean(bits) - 1.
Bipolar multiplication uses XNOR: P(A XNOR B) encodes A*B in bipolar.
Source code in src/sc_neurocore/utils/bitstreams.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
sc_neurocore.utils.bitstreams.bipolar_to_value(bitstream)
¶
Decode a bipolar bitstream to a value in [-1, +1].
x = 2 * mean(bits) - 1
Source code in src/sc_neurocore/utils/bitstreams.py
127 128 129 130 131 132 133 134 | |
SC Division (CORDIV, Li et al. 2014)¶
sc_neurocore.utils.bitstreams.sc_divide(numerator, denominator)
¶
Stochastic computing division via CORDIV circuit.
Li, Qian, Riedel & Bazargan, IEEE Trans. Signal Process. 62(9), 2014.
at each bit position t,
- x[t]=1 → z[t] = 1
- x[t]=0, y[t]=1 → z[t] = 0
- x[t]=0, y[t]=0 → z[t] = z[t-1] (hold)
Converges to P(z=1) ≈ P(x=1) / P(y=1) when P(x) ≤ P(y).
Parameters¶
numerator : np.ndarray Bitstream (uint8, {0,1}) of length L. denominator : np.ndarray Bitstream (uint8, {0,1}) of length L. Must have higher or equal density.
Returns¶
np.ndarray Quotient bitstream of length L.
Source code in src/sc_neurocore/utils/bitstreams.py
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 | |
Adaptive Bitstream Length¶
Compute minimum bitstream length for target precision via Hoeffding, Chebyshev, or variance bounds.
sc_neurocore.utils.bitstreams.adaptive_length(p, epsilon=0.01, confidence=0.95, method='hoeffding', min_length=64, max_length=65536)
¶
Compute minimum bitstream length for target precision.
Given probability p and error tolerance epsilon, returns the smallest L such that |p_hat - p| < epsilon with the given confidence.
Parameters¶
p : float Encoded probability in [0, 1]. epsilon : float Maximum acceptable absolute error. confidence : float Confidence level (e.g. 0.95 for 95%). method : str Bound type: "hoeffding" (tighter), "chebyshev", or "variance" (no confidence). min_length : int Minimum returned length. max_length : int Maximum returned length (hardware cap).
Returns¶
int Minimum bitstream length (rounded up to nearest power of 2 for Sobol compatibility).
Source code in src/sc_neurocore/utils/bitstreams.py
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 | |
LDS Decorrelation (Sobol/Halton)¶
Multi-dimensional low-discrepancy sequences for per-synapse decorrelation.
sc_neurocore.utils.lds_decorrelation.generate_decorrelated_bitstreams(probabilities, length=1024, method='sobol', seed=None)
¶
Generate decorrelated bitstreams for a probability matrix.
Each element of the probability matrix gets its own LDS dimension, ensuring zero correlation between any pair of bitstreams.
Parameters¶
probabilities : np.ndarray Probability matrix, any shape. Values in [0, 1]. length : int Bitstream length per element. method : str "sobol" or "halton". seed : int or None Random seed for scrambling.
Returns¶
np.ndarray Shape (*probabilities.shape, length), dtype uint8.
Source code in src/sc_neurocore/utils/lds_decorrelation.py
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 | |
sc_neurocore.utils.lds_decorrelation.star_discrepancy_estimate(samples, n_test=10000)
¶
Estimate star discrepancy of a sample set (quality metric for LDS).
Lower discrepancy → more uniform coverage → better SC precision.
Parameters¶
samples : np.ndarray Shape (n_samples, d), values in [0, 1]. n_test : int Number of random test points.
Returns¶
float Estimated star discrepancy.
Source code in src/sc_neurocore/utils/lds_decorrelation.py
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 | |
Random Number Generation¶
sc_neurocore.utils.rng.RNG
¶
Thin wrapper around NumPy RNG for reproducible per-neuron streams.
Example¶
rng = RNG(seed=42) vals = rng.random(5) vals.shape (5,) RNG(seed=42).random(5) == vals # deterministic array([ True, True, True, True, True])
Source code in src/sc_neurocore/utils/rng.py
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 | |
Adaptive Utilities¶
sc_neurocore.utils.adaptive
¶
AdaptiveInference
dataclass
¶
Manages Progressive Precision / Early Exit for SC.
Source code in src/sc_neurocore/utils/adaptive.py
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 | |
run_adaptive(step_func)
¶
Runs the SC process step-by-step until convergence or max_length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step_func
|
Callable[[], float]
|
Function that executes one step and returns current estimate. |
required |
Source code in src/sc_neurocore/utils/adaptive.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
Connectome Generation¶
sc_neurocore.utils.connectomes
¶
ConnectomeGenerator
¶
Generates biologically plausible connectivity matrices.
Source code in src/sc_neurocore/utils/connectomes.py
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 | |
generate_watts_strogatz(n_neurons, k_neighbors, p_rewire)
staticmethod
¶
Watts-Strogatz Small-World Model.
- Start with a regular ring lattice (connect to k neighbors).
- Randomly rewire edges with probability p.
Returns:
| Type | Description |
|---|---|
ndarray[Any, Any]
|
Adjacency Matrix (Binary) |
Source code in src/sc_neurocore/utils/connectomes.py
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 | |
generate_scale_free(n_neurons)
staticmethod
¶
Barabasi-Albert Scale-Free Model (Preferential Attachment).
Source code in src/sc_neurocore/utils/connectomes.py
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 | |
Decorrelators¶
sc_neurocore.utils.decorrelators
¶
Decorrelator
dataclass
¶
Base class for bitstream decorrelators.
Source code in src/sc_neurocore/utils/decorrelators.py
14 15 16 17 18 19 20 21 | |
ShufflingDecorrelator
dataclass
¶
Bases: Decorrelator
Decorrelates a bitstream by randomly shuffling bits within a window. This preserves the exact bit count (probability) but destroys temporal correlations.
Source code in src/sc_neurocore/utils/decorrelators.py
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 | |
LFSRRegenDecorrelator
dataclass
¶
Bases: Decorrelator
Regenerates a new bitstream with the same probability estimate but using a different random source (LFSR-like or just new RNG).
Source code in src/sc_neurocore/utils/decorrelators.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
Fault Injection¶
sc_neurocore.utils.fault_injection
¶
FaultInjector
¶
Simulates hardware faults in Stochastic Computing bitstreams.
Source code in src/sc_neurocore/utils/fault_injection.py
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 | |
inject_bit_flips(bitstream, error_rate)
staticmethod
¶
Randomly flips bits with probability 'error_rate'.
Source code in src/sc_neurocore/utils/fault_injection.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
inject_stuck_at(bitstream, fault_rate, value)
staticmethod
¶
Simulates Stuck-At-0 or Stuck-At-1 faults.
Source code in src/sc_neurocore/utils/fault_injection.py
42 43 44 45 46 47 48 49 50 51 52 | |
FSM Activations¶
sc_neurocore.utils.fsm_activations
¶
FSMActivation
dataclass
¶
Base class for FSM-based stochastic activation functions.
The FSM takes a bitstream input and transitions between states. The output bit is determined by the current state (e.g., if state > N/2, out=1). This implements saturating non-linearities like Tanh or Sigmoid efficiently.
Source code in src/sc_neurocore/utils/fsm_activations.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
TanhFSM
dataclass
¶
Bases: FSMActivation
Implements a Tanh-like function using a linear FSM.
States: 0 to N-1 Input 0: state -> max(0, state - 1) Input 1: state -> min(N-1, state + 1) Output: 1 if state >= N/2 else 0
Source code in src/sc_neurocore/utils/fsm_activations.py
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 | |
ReLKFSM
dataclass
¶
Bases: FSMActivation
Implements a Rectified Linear (ReLU-like) behavior. Can be complex in SC, often approximated or used with bipolar coding. Here we implement a simple saturating counter.
Source code in src/sc_neurocore/utils/fsm_activations.py
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 | |
Model Bridge¶
sc_neurocore.utils.model_bridge
¶
SCBridge
¶
Bridge between standard DL frameworks (like PyTorch) and SC-NeuroCore.
Source code in src/sc_neurocore/utils/model_bridge.py
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 | |
load_from_state_dict(state_dict, layer_mapping)
staticmethod
¶
Load weights from a state_dict (numpy or torch tensors) into SC layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
Dict[str, Any]
|
Dictionary mapping "layer_name.weight" to arrays. |
required |
layer_mapping
|
Dict[str, Any]
|
Dictionary mapping "layer_name" to SCLayer instances. |
required |
Source code in src/sc_neurocore/utils/model_bridge.py
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 | |
export_to_numpy(layers)
staticmethod
¶
Export SC weights back to numpy dictionary.
Source code in src/sc_neurocore/utils/model_bridge.py
83 84 85 86 87 88 89 90 91 92 93 94 | |
normalize_weights(weights)
¶
Normalizes weights to [0, 1] range for unipolar SC.
Source code in src/sc_neurocore/utils/model_bridge.py
20 21 22 23 24 25 26 27 28 | |