-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
412 lines (358 loc) · 12.5 KB
/
Copy pathmod.rs
File metadata and controls
412 lines (358 loc) · 12.5 KB
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
#![allow(
clippy::assign_op_pattern,
clippy::too_many_arguments,
clippy::manual_range_contains,
clippy::needless_range_loop,
clippy::type_complexity
)]
//! HPC extensions for ndarray — ported from rustynum.
//!
//! This module provides high-performance computing extensions:
//! - BLAS Level 1/2/3 operations as extension traits
//! - Statistics (median, var, std, percentile)
//! - Activation functions (sigmoid, softmax, log_softmax)
//! - HDC (Hyperdimensional Computing) operations
//! - CogRecord 4-channel cognitive units
//! - Graph operations with VerbCodebook
//! - BF16 and Int8 quantized GEMM
//! - LAPACK factorizations (LU, Cholesky, QR)
//! - FFT (forward, inverse, real-to-complex)
//! - VML (vectorized math library)
// SIMD capability singleton — detect once, all modules share
pub mod simd_caps;
// LazyLock frozen SIMD dispatch — function pointers selected once at startup
pub mod simd_dispatch;
pub mod blas_level1;
pub mod blas_level2;
pub mod blas_level3;
pub mod statistics;
pub mod activations;
pub mod hdc;
pub mod bitwise;
pub mod projection;
pub mod cogrecord;
pub mod graph;
pub mod quantized;
pub mod lapack;
pub mod fft;
pub mod vml;
pub mod packed;
// Cognitive layer types (migrated from rustynum-core)
#[allow(missing_docs)]
pub mod fingerprint;
#[allow(missing_docs)]
pub mod plane;
#[allow(missing_docs)]
pub mod seal;
#[allow(missing_docs)]
pub mod node;
#[allow(missing_docs)]
pub mod cascade;
#[allow(missing_docs)]
pub mod heel_f64x8;
#[allow(missing_docs)]
pub mod amx_matmul;
#[allow(missing_docs)]
pub mod bf16_truth;
#[allow(missing_docs)]
pub mod causality;
#[allow(missing_docs)]
pub mod causal_diff;
#[allow(missing_docs)]
pub mod styles;
#[allow(missing_docs)]
pub mod nars;
#[allow(missing_docs)]
pub mod blackboard;
#[allow(missing_docs)]
pub mod bnn;
#[allow(missing_docs)]
pub mod clam;
#[allow(missing_docs)]
pub mod clam_search;
#[allow(missing_docs)]
pub mod clam_compress;
#[allow(missing_docs)]
pub mod arrow_bridge;
#[allow(missing_docs)]
pub mod merkle_tree;
// Sprint 1: Quick Wins (hot-path gap fill)
#[allow(missing_docs)]
pub mod cam_index;
#[allow(missing_docs)]
pub mod prefilter;
#[allow(missing_docs)]
pub mod binding_matrix;
#[allow(missing_docs)]
pub mod qualia_gate;
#[allow(missing_docs)]
pub mod dn_tree;
// Sprint 3: CLAM + BNN Ports
#[allow(missing_docs)]
pub mod bnn_cross_plane;
#[allow(missing_docs)]
pub mod bnn_causal_trajectory;
// Qualia system: 16-channel phenomenal coloring
#[allow(missing_docs)]
pub mod qualia;
// Sprint 2: Core Cognitive Layer
#[allow(missing_docs)]
pub mod kernels;
#[allow(missing_docs)]
pub mod organic;
#[allow(missing_docs)]
pub mod substrate;
#[allow(missing_docs)]
pub mod tekamolo;
#[allow(missing_docs)]
pub mod vsa;
#[allow(missing_docs)]
pub mod spo_bundle;
#[allow(missing_docs)]
pub mod deepnsm;
#[allow(missing_docs)]
pub mod surround_metadata;
#[allow(missing_docs, dead_code)]
pub mod cyclic_bundle;
#[allow(missing_docs, dead_code)]
pub mod compression_curves;
#[allow(missing_docs)]
pub mod crystal_encoder;
#[allow(missing_docs)]
pub mod udf_kernels;
// p64 bridge: Palette64/3D attention, NARS, CausalEdge64 compat
#[allow(missing_docs)]
pub mod p64_bridge;
// Session C: bgz17 dual-path integration
#[allow(missing_docs)]
pub mod bgz17_bridge;
#[allow(missing_docs)]
pub mod palette_distance;
#[allow(missing_docs)]
pub mod layered_distance;
#[allow(missing_docs)]
pub mod parallel_search;
#[allow(missing_docs)]
#[allow(missing_docs)]
#[allow(missing_docs)]
// ZeckF64 progressive edge encoding + batch/top-k
#[allow(missing_docs)]
pub mod zeck;
// SIMD-accelerated spatial / byte-scan / hash utilities
pub mod distance;
pub mod byte_scan;
pub mod spatial_hash;
// Variable-width palette index codec (Minecraft-style bit packing)
#[allow(missing_docs)]
pub mod palette_codec;
// SIMD-accelerated HPC modules (block properties, nibble light data, AABB collision)
#[allow(missing_docs)]
pub mod property_mask;
#[allow(missing_docs)]
pub mod nibble;
#[allow(missing_docs)]
pub mod aabb;
// Holographic phase-space operations (ported from rustynum-holo)
#[allow(missing_docs)]
#[allow(clippy::needless_range_loop)]
#[allow(clippy::too_many_arguments)]
pub mod holo;
// CAM-PQ: Content-Addressable Memory as Product Quantization
// Unifies FAISS PQ6x8 and CLAM 48-bit archetypes. 170× compression, 500M cands/sec.
#[allow(missing_docs)]
pub mod cam_pq;
/// GGUF model file reader — extract f32 weights from quantized models.
#[allow(missing_docs)]
pub mod gguf;
/// Streaming GGUF → bgz17 indexer. One tensor at a time, bounded RAM.
#[allow(missing_docs)]
pub mod gguf_indexer;
/// Safetensors header parser + streaming indexer for BF16 model weights.
#[allow(missing_docs)]
pub mod safetensors;
/// HTTP range reader — Read + Seek over HTTP for streaming GGUF from HuggingFace.
#[allow(missing_docs)]
pub mod http_reader;
/// Jina embedding codec — GGUF → Base17 → Palette → CausalEdge64.
#[allow(missing_docs)]
pub mod jina;
/// Shared model primitives — safetensors, SIMD layers, API types.
#[allow(missing_docs)]
pub mod models;
/// GPT-2 inference engine — full forward pass + OpenAI-compatible API types.
#[allow(missing_docs)]
pub mod gpt2;
/// Stable Diffusion inference — CLIP + UNet + VAE + DDIM scheduler.
#[allow(missing_docs)]
pub mod stable_diffusion;
/// OpenChat 3.5 inference — Mistral-7B architecture (GQA + RoPE + RMSNorm + SiLU).
#[allow(missing_docs)]
pub mod openchat;
// jitson: JSON config → scan pipeline (parser, validator, template, precompile, packed)
// Always available — no Cranelift dependency.
#[allow(missing_docs)]
pub mod jitson;
// jitson_cranelift: Cranelift JIT compilation backend (ScanParams, JitEngine, ScanKernel)
// Only compiled with the "jit-native" feature flag.
#[cfg(feature = "jit-native")]
#[allow(missing_docs)]
pub mod jitson_cranelift;
pub mod ocr_simd;
pub mod ocr_felt;
#[cfg(test)]
mod e2e_tests {
//! End-to-end pipeline test: Fingerprint → Node → Seal → Cascade → CLAM → Causality → BNN
use super::fingerprint::Fingerprint;
use super::node::{Node, SPO, S__, _P_, __O};
use super::seal::Seal;
use super::cascade::{Cascade, Band};
use super::clam::{ClamTree, knn_brute};
use super::bf16_truth::PackedQualia;
use super::causality::{causality_decompose, CausalityDirection};
use super::bnn::bnn_dot;
use super::blackboard::Blackboard;
#[test]
fn pipeline_fingerprint_to_node_to_seal() {
// 1. Create two nodes and accumulate evidence
let mut a = Node::random(42);
let mut b = Node::random(99);
// 2. Measure distance (SPO full)
let d = a.distance(&mut b, SPO);
match d {
super::plane::Distance::Measured { disagreement, overlap, .. } => {
assert!(overlap > 0, "random nodes should have overlap");
assert!(disagreement > 0, "different seeds should disagree");
}
super::plane::Distance::Incomparable => panic!("random nodes should be comparable"),
}
// 3. Seal integrity: build from scratch for deterministic test
let mut p = super::plane::Plane::new();
p.encounter("hello");
p.encounter("hello");
let root = p.merkle();
assert_eq!(p.verify(&root), Seal::Wisdom);
// 4. Mutate and detect change
p.encounter("world");
assert_eq!(p.verify(&root), Seal::Staunen);
}
#[test]
fn pipeline_cascade_search() {
let vec_bytes = 256;
let num_vectors = 50;
// Build a database of random fingerprints
let mut database = Vec::with_capacity(vec_bytes * num_vectors);
for i in 0..num_vectors {
let fp = Fingerprint::<32>::from_words({
let mut words = [0u64; 32];
let mut seed = (i as u64).wrapping_add(1).wrapping_mul(0x9E3779B97F4A7C15);
for w in words.iter_mut() {
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1);
*w = seed;
}
words
});
database.extend_from_slice(fp.as_bytes());
}
// Query = first vector (should find itself at distance 0)
let query = database[0..vec_bytes].to_vec();
let cascade = Cascade::from_threshold(vec_bytes as u64 * 4, vec_bytes);
let results = cascade.query(&query, &database, vec_bytes, num_vectors);
assert!(results.iter().any(|r| r.index == 0 && r.hamming == 0));
}
#[test]
fn pipeline_clam_knn() {
let vec_len = 32;
let n = 20;
// Create distinct vectors: all zeros except vector i has byte i set to 0xFF
let mut data = vec![0u8; n * vec_len];
for i in 0..n {
data[i * vec_len + (i % vec_len)] = 0xFF;
}
// Query = vector 0 (byte 0 is 0xFF, rest zeros)
let query = data[0..vec_len].to_vec();
// Brute force k-NN
let result = knn_brute(&data, vec_len, &query, 5);
assert_eq!(result.hits.len(), 5);
// First hit should be exact match (distance 0)
assert_eq!(result.hits[0].1, 0);
assert_eq!(result.hits[0].0, 0);
// Build CLAM tree
let tree = ClamTree::build(&data, vec_len, 3);
assert!(!tree.nodes.is_empty());
assert_eq!(tree.root().cardinality, n);
}
#[test]
fn pipeline_causality_decomposition() {
let mut a = PackedQualia::zero();
let b = PackedQualia::zero();
a.resonance[4] = 10; // warmth: positive → Forward
a.resonance[6] = -5; // social: negative → Backward
a.resonance[8] = 3; // sacredness: positive → Forward
let dec = causality_decompose(&a, &b, None);
assert_eq!(dec.warmth_dir, CausalityDirection::Forward);
assert_eq!(dec.social_dir, CausalityDirection::Backward);
assert_eq!(dec.sacredness_dir, CausalityDirection::Forward);
}
#[test]
fn pipeline_bnn_inference() {
let act = Fingerprint::<256>::ones();
let weight = Fingerprint::<256>::ones();
let result = bnn_dot(&act, &weight);
assert_eq!(result.match_count, 16384);
assert!((result.score - 1.0).abs() < 1e-6);
let zero = Fingerprint::<256>::zero();
let result2 = bnn_dot(&zero, &weight);
assert_eq!(result2.match_count, 0);
assert!((result2.score - (-1.0)).abs() < 1e-6);
}
#[test]
fn pipeline_blackboard_arena() {
let mut bb = Blackboard::new();
bb.alloc_vec_f32("activations", vec![0.0f32; 16384]);
bb.alloc_vec_u8("fingerprint", vec![0u8; 2048]);
let act = bb.get_mut::<Vec<f32>>("activations").unwrap();
act[0] = 1.0;
assert_eq!(act.len(), 16384);
let fp = bb.get_mut::<Vec<u8>>("fingerprint").unwrap();
fp[0] = 0xFF;
assert_eq!(fp.len(), 2048);
// Verify reads work
assert_eq!(bb.get::<Vec<f32>>("activations").unwrap()[0], 1.0);
assert_eq!(bb.get::<Vec<u8>>("fingerprint").unwrap()[0], 0xFF);
assert!(bb.contains("activations"));
assert!(!bb.contains("nonexistent"));
}
#[test]
fn pipeline_full_e2e() {
// Full pipeline: Node → truth → causality → cascade → BNN
let mut node_a = Node::random(1);
let mut node_b = Node::random(2);
// Extract truth values
let truth_a = node_a.truth(SPO);
let truth_b = node_b.truth(SPO);
assert!(truth_a.evidence > 0);
assert!(truth_b.evidence > 0);
// Measure per-plane distances
let d_s = node_a.distance(&mut node_b, S__);
let d_p = node_a.distance(&mut node_b, _P_);
let d_o = node_a.distance(&mut node_b, __O);
let d_full = node_a.distance(&mut node_b, SPO);
// All should be Measured for random nodes
assert!(matches!(d_s, super::plane::Distance::Measured { .. }));
assert!(matches!(d_p, super::plane::Distance::Measured { .. }));
assert!(matches!(d_o, super::plane::Distance::Measured { .. }));
assert!(matches!(d_full, super::plane::Distance::Measured { .. }));
// Seal verification
let root = node_a.s.merkle();
assert_eq!(node_a.s.verify(&root), Seal::Wisdom);
// Cascade band classification
let cascade = Cascade::from_threshold(1000, 2048);
assert_eq!(cascade.expose(100), Band::Foveal);
assert_eq!(cascade.expose(1500), Band::Reject);
// BNN inference
let bits_a = node_a.s.bits().clone();
let bits_b = node_b.s.bits().clone();
let bnn_result = bnn_dot(&bits_a, &bits_b);
assert!(bnn_result.score > -1.0 && bnn_result.score < 1.0);
}
}