@hviana/sema 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/example/demo.js +25 -25
- package/dist/example/train_base.d.ts +21 -19
- package/dist/example/train_base.js +1952 -1684
- package/dist/src/alphabet.d.ts +3 -3
- package/dist/src/alphabet.js +27 -24
- package/dist/src/alu/src/alu.d.ts +194 -169
- package/dist/src/alu/src/alu.js +414 -374
- package/dist/src/alu/src/expr.d.ts +43 -36
- package/dist/src/alu/src/expr.js +278 -252
- package/dist/src/alu/src/index.d.ts +98 -9
- package/dist/src/alu/src/index.js +63 -9
- package/dist/src/alu/src/kernel-arith.d.ts +5 -1
- package/dist/src/alu/src/kernel-arith.js +285 -195
- package/dist/src/alu/src/kernel-bits.d.ts +5 -1
- package/dist/src/alu/src/kernel-bits.js +115 -81
- package/dist/src/alu/src/kernel-logic.js +63 -33
- package/dist/src/alu/src/kernel-nd.js +195 -155
- package/dist/src/alu/src/kernel-numeric.d.ts +80 -16
- package/dist/src/alu/src/kernel-numeric.js +331 -259
- package/dist/src/alu/src/operation.d.ts +132 -112
- package/dist/src/alu/src/operation.js +163 -149
- package/dist/src/alu/src/parser.d.ts +175 -168
- package/dist/src/alu/src/parser.js +421 -382
- package/dist/src/alu/src/resonance.d.ts +25 -16
- package/dist/src/alu/src/resonance.js +56 -49
- package/dist/src/alu/src/text.d.ts +7 -3
- package/dist/src/alu/src/text.js +37 -31
- package/dist/src/alu/src/value.d.ts +14 -14
- package/dist/src/alu/src/value.js +160 -150
- package/dist/src/alu/test/alu.test.js +656 -543
- package/dist/src/bytes.d.ts +5 -1
- package/dist/src/bytes.js +40 -33
- package/dist/src/config.d.ts +102 -102
- package/dist/src/config.js +83 -83
- package/dist/src/derive/src/deduction.d.ts +62 -60
- package/dist/src/derive/src/deduction.js +109 -103
- package/dist/src/derive/src/index.d.ts +7 -1
- package/dist/src/derive/src/priority-queue.d.ts +8 -8
- package/dist/src/derive/src/priority-queue.js +61 -57
- package/dist/src/derive/src/rewrite.d.ts +18 -15
- package/dist/src/derive/src/rewrite.js +67 -59
- package/dist/src/derive/src/trie.d.ts +56 -56
- package/dist/src/derive/src/trie.js +179 -175
- package/dist/src/derive/test/derive.test.js +100 -96
- package/dist/src/extension.d.ts +16 -13
- package/dist/src/geometry.d.ts +42 -17
- package/dist/src/geometry.js +266 -235
- package/dist/src/index.d.ts +17 -2
- package/dist/src/index.js +7 -1
- package/dist/src/ingest-cache.d.ts +30 -25
- package/dist/src/ingest-cache.js +127 -108
- package/dist/src/mind/articulation.d.ts +5 -1
- package/dist/src/mind/articulation.js +112 -77
- package/dist/src/mind/attention.d.ts +108 -40
- package/dist/src/mind/attention.js +871 -749
- package/dist/src/mind/canonical.d.ts +19 -4
- package/dist/src/mind/canonical.js +31 -27
- package/dist/src/mind/graph-search.d.ts +225 -201
- package/dist/src/mind/graph-search.js +821 -742
- package/dist/src/mind/index.d.ts +10 -2
- package/dist/src/mind/junction.d.ts +58 -31
- package/dist/src/mind/junction.js +237 -172
- package/dist/src/mind/learning.d.ts +52 -16
- package/dist/src/mind/learning.js +165 -143
- package/dist/src/mind/match.d.ts +69 -20
- package/dist/src/mind/match.js +318 -259
- package/dist/src/mind/mechanisms/alu.js +16 -16
- package/dist/src/mind/mechanisms/cast.d.ts +13 -9
- package/dist/src/mind/mechanisms/cast.js +456 -363
- package/dist/src/mind/mechanisms/confluence.d.ts +12 -8
- package/dist/src/mind/mechanisms/confluence.js +183 -152
- package/dist/src/mind/mechanisms/cover.d.ts +8 -2
- package/dist/src/mind/mechanisms/cover.js +210 -148
- package/dist/src/mind/mechanisms/extraction.d.ts +34 -8
- package/dist/src/mind/mechanisms/extraction.js +288 -234
- package/dist/src/mind/mechanisms/recall.d.ts +10 -6
- package/dist/src/mind/mechanisms/recall.js +185 -126
- package/dist/src/mind/mind.d.ts +154 -129
- package/dist/src/mind/mind.js +288 -258
- package/dist/src/mind/pipeline-mechanism.d.ts +124 -112
- package/dist/src/mind/pipeline-mechanism.js +172 -161
- package/dist/src/mind/pipeline.d.ts +14 -4
- package/dist/src/mind/pipeline.js +189 -125
- package/dist/src/mind/primitives.d.ts +32 -8
- package/dist/src/mind/primitives.js +117 -99
- package/dist/src/mind/rationale.d.ts +98 -86
- package/dist/src/mind/rationale.js +121 -113
- package/dist/src/mind/reasoning.d.ts +13 -2
- package/dist/src/mind/reasoning.js +166 -129
- package/dist/src/mind/recognition.d.ts +4 -1
- package/dist/src/mind/recognition.js +208 -183
- package/dist/src/mind/resonance.d.ts +22 -5
- package/dist/src/mind/resonance.js +0 -0
- package/dist/src/mind/trace.d.ts +25 -6
- package/dist/src/mind/trace.js +58 -50
- package/dist/src/mind/traverse.d.ts +58 -16
- package/dist/src/mind/traverse.js +357 -304
- package/dist/src/mind/types.d.ts +127 -120
- package/dist/src/mind/types.js +69 -60
- package/dist/src/rabitq-hnsw/src/database.d.ts +179 -177
- package/dist/src/rabitq-hnsw/src/database.js +359 -342
- package/dist/src/rabitq-hnsw/src/heap.d.ts +12 -12
- package/dist/src/rabitq-hnsw/src/heap.js +80 -75
- package/dist/src/rabitq-hnsw/src/hnsw.d.ts +103 -103
- package/dist/src/rabitq-hnsw/src/hnsw.js +454 -428
- package/dist/src/rabitq-hnsw/src/index.d.ts +6 -1
- package/dist/src/rabitq-hnsw/src/prng.d.ts +10 -10
- package/dist/src/rabitq-hnsw/src/prng.js +27 -25
- package/dist/src/rabitq-hnsw/src/rabitq.d.ts +69 -69
- package/dist/src/rabitq-hnsw/src/rabitq.js +262 -246
- package/dist/src/rabitq-hnsw/src/store.d.ts +132 -132
- package/dist/src/rabitq-hnsw/src/store.js +843 -752
- package/dist/src/rabitq-hnsw/test/hnsw.test.js +1054 -805
- package/dist/src/sema.d.ts +35 -0
- package/dist/src/sema.js +77 -0
- package/dist/src/store-sqlite.d.ts +166 -144
- package/dist/src/store-sqlite.js +660 -578
- package/dist/src/store.d.ts +630 -593
- package/dist/src/store.js +1581 -1440
- package/dist/src/vec.d.ts +9 -5
- package/dist/src/vec.js +73 -61
- package/package.json +2 -2
- package/src/sema.ts +102 -0
package/dist/src/vec.d.ts
CHANGED
|
@@ -14,17 +14,21 @@ export declare function normalize(v: Vec): Vec;
|
|
|
14
14
|
export declare function cosine(a: Vec, b: Vec): number;
|
|
15
15
|
/** Set vector epsilon thresholds. Called once by Mind at construction. */
|
|
16
16
|
export declare function setVecConfig(cfg: {
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
normalizeEpsilon?: number;
|
|
18
|
+
cosineEpsilon?: number;
|
|
19
19
|
}): void;
|
|
20
20
|
export interface Permutation {
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
fwd: Uint32Array;
|
|
22
|
+
inv: Uint32Array;
|
|
23
23
|
}
|
|
24
24
|
/** The keyring: one independent permutation per seat.
|
|
25
25
|
* Independent keys do not commute, so an address in a tree is the
|
|
26
26
|
* path itself — "seat 2 inside seat 1" ≠ "seat 1 inside seat 2". */
|
|
27
|
-
export declare function makeKeyring(
|
|
27
|
+
export declare function makeKeyring(
|
|
28
|
+
D: number,
|
|
29
|
+
seats: number,
|
|
30
|
+
rand: () => number,
|
|
31
|
+
): Permutation[];
|
|
28
32
|
/** Apply permutation: out[i] = v[table[i]]. */
|
|
29
33
|
export declare function permute(v: Vec, table: Uint32Array): Vec;
|
|
30
34
|
/** Permute into existing buffer — zero allocation. */
|
package/dist/src/vec.js
CHANGED
|
@@ -4,44 +4,49 @@
|
|
|
4
4
|
// No weights. No gradients.
|
|
5
5
|
/** Deterministic PRNG (32-bit mixer). */
|
|
6
6
|
export function rng(seed) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
let s = seed >>> 0;
|
|
8
|
+
return () => {
|
|
9
|
+
s = (s + 0x6d2b79f5) >>> 0;
|
|
10
|
+
let t = s;
|
|
11
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
12
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
13
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
14
|
+
};
|
|
15
15
|
}
|
|
16
16
|
/** Standard normal sample (Box–Muller). */
|
|
17
17
|
function gaussian(rand) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
let u = 0, v = 0;
|
|
19
|
+
while (u === 0) {
|
|
20
|
+
u = rand();
|
|
21
|
+
}
|
|
22
|
+
while (v === 0) {
|
|
23
|
+
v = rand();
|
|
24
|
+
}
|
|
25
|
+
return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
|
|
24
26
|
}
|
|
25
27
|
export const zeros = (D) => new Float32Array(D);
|
|
26
28
|
export const copy = (v) => new Float32Array(v);
|
|
27
29
|
/** Random point on the unit sphere. */
|
|
28
30
|
export function randomUnit(D, rand) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
const v = zeros(D);
|
|
32
|
+
for (let i = 0; i < D; i++) {
|
|
33
|
+
v[i] = gaussian(rand);
|
|
34
|
+
}
|
|
35
|
+
return normalize(v);
|
|
33
36
|
}
|
|
34
37
|
/** target += src * scale (in place). */
|
|
35
38
|
export function addInto(target, src, scale = 1) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
for (let i = 0; i < target.length; i++) {
|
|
40
|
+
target[i] += src[i] * scale;
|
|
41
|
+
}
|
|
42
|
+
return target;
|
|
39
43
|
}
|
|
40
44
|
export function dot(a, b) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
let s = 0;
|
|
46
|
+
for (let i = 0; i < a.length; i++) {
|
|
47
|
+
s += a[i] * b[i];
|
|
48
|
+
}
|
|
49
|
+
return s;
|
|
45
50
|
}
|
|
46
51
|
const norm = (v) => Math.sqrt(dot(v, v));
|
|
47
52
|
// Epsilon thresholds (settable once via setVecConfig).
|
|
@@ -49,61 +54,68 @@ let _normalizeEpsilon = 1e-12;
|
|
|
49
54
|
let _cosineEpsilon = 1e-12;
|
|
50
55
|
/** In-place normalization. */
|
|
51
56
|
export function normalize(v) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
const n = norm(v);
|
|
58
|
+
if (n > _normalizeEpsilon) {
|
|
59
|
+
for (let i = 0; i < v.length; i++) {
|
|
60
|
+
v[i] /= n;
|
|
56
61
|
}
|
|
57
|
-
|
|
62
|
+
}
|
|
63
|
+
return v;
|
|
58
64
|
}
|
|
59
65
|
/** Resonance: 1 = same, 0 = unrelated. */
|
|
60
66
|
export function cosine(a, b) {
|
|
61
|
-
|
|
62
|
-
|
|
67
|
+
const na = norm(a), nb = norm(b);
|
|
68
|
+
return na > _cosineEpsilon && nb > _cosineEpsilon ? dot(a, b) / (na * nb) : 0;
|
|
63
69
|
}
|
|
64
70
|
/** Set vector epsilon thresholds. Called once by Mind at construction. */
|
|
65
71
|
export function setVecConfig(cfg) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
72
|
+
if (cfg.normalizeEpsilon !== undefined) {
|
|
73
|
+
_normalizeEpsilon = cfg.normalizeEpsilon;
|
|
74
|
+
}
|
|
75
|
+
if (cfg.cosineEpsilon !== undefined) {
|
|
76
|
+
_cosineEpsilon = cfg.cosineEpsilon;
|
|
77
|
+
}
|
|
71
78
|
}
|
|
72
79
|
/** One fixed random permutation (Fisher–Yates). */
|
|
73
80
|
function makePermutation(D, rand) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
81
|
+
const fwd = new Uint32Array(D);
|
|
82
|
+
for (let i = 0; i < D; i++) {
|
|
83
|
+
fwd[i] = i;
|
|
84
|
+
}
|
|
85
|
+
for (let i = D - 1; i > 0; i--) {
|
|
86
|
+
const j = Math.floor(rand() * (i + 1));
|
|
87
|
+
const t = fwd[i];
|
|
88
|
+
fwd[i] = fwd[j];
|
|
89
|
+
fwd[j] = t;
|
|
90
|
+
}
|
|
91
|
+
const inv = new Uint32Array(D);
|
|
92
|
+
for (let i = 0; i < D; i++) {
|
|
93
|
+
inv[fwd[i]] = i;
|
|
94
|
+
}
|
|
95
|
+
return { fwd, inv };
|
|
87
96
|
}
|
|
88
97
|
/** The keyring: one independent permutation per seat.
|
|
89
98
|
* Independent keys do not commute, so an address in a tree is the
|
|
90
99
|
* path itself — "seat 2 inside seat 1" ≠ "seat 1 inside seat 2". */
|
|
91
100
|
export function makeKeyring(D, seats, rand) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
101
|
+
const ring = [];
|
|
102
|
+
for (let s = 0; s < seats; s++) {
|
|
103
|
+
ring.push(makePermutation(D, rand));
|
|
104
|
+
}
|
|
105
|
+
return ring;
|
|
96
106
|
}
|
|
97
107
|
/** Apply permutation: out[i] = v[table[i]]. */
|
|
98
108
|
export function permute(v, table) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
109
|
+
const out = new Float32Array(v.length);
|
|
110
|
+
for (let i = 0; i < v.length; i++) {
|
|
111
|
+
out[i] = v[table[i]];
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
103
114
|
}
|
|
104
115
|
/** Permute into existing buffer — zero allocation. */
|
|
105
116
|
export function permuteInto(out, v, table) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
117
|
+
for (let i = 0; i < v.length; i++) {
|
|
118
|
+
out[i] = v[table[i]];
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
109
121
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hviana/sema",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.5",
|
|
4
|
+
"description": "Sema: a non-parametric, instance-based reasoning system.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
7
7
|
"types": "dist/src/index.d.ts",
|
package/src/sema.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addInto,
|
|
3
|
+
normalize,
|
|
4
|
+
Permutation,
|
|
5
|
+
permute,
|
|
6
|
+
permuteInto,
|
|
7
|
+
randomUnit,
|
|
8
|
+
rng,
|
|
9
|
+
Vec,
|
|
10
|
+
zeros,
|
|
11
|
+
} from "./vec.js";
|
|
12
|
+
|
|
13
|
+
/** The one structure. A node's vector is the gist of its whole subtree; it also
|
|
14
|
+
* carries the structure the DAG store interns — its leaf bytes, or its kids. */
|
|
15
|
+
export interface Sema {
|
|
16
|
+
v: Vec;
|
|
17
|
+
leaf: Uint8Array | null;
|
|
18
|
+
kids: Sema[] | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const sema = (
|
|
22
|
+
v: Vec,
|
|
23
|
+
leaf: Uint8Array | null = null,
|
|
24
|
+
kids: Sema[] | null = null,
|
|
25
|
+
): Sema => ({ v, leaf, kids });
|
|
26
|
+
|
|
27
|
+
/** Whether a node is a CHUNK — a leaf-parent whose children are ALL leaves,
|
|
28
|
+
* the perception tree's smallest grouped unit. The one predicate behind
|
|
29
|
+
* region collection, canonical segmentation seams, and sub-span indexing;
|
|
30
|
+
* named here beside the type so no consumer restates the shape inline. */
|
|
31
|
+
export const isChunk = (n: Sema): n is Sema & { kids: Sema[] } =>
|
|
32
|
+
n.kids !== null && n.kids.every((k) => k.kids === null);
|
|
33
|
+
|
|
34
|
+
/** The medium: dimension, keyring, and noise source. */
|
|
35
|
+
export interface Space {
|
|
36
|
+
D: number;
|
|
37
|
+
seats: Permutation[];
|
|
38
|
+
rand: () => number;
|
|
39
|
+
maxGroup: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Reusable permute buffer for fold.
|
|
43
|
+
let _foldBuf: Vec | null = null;
|
|
44
|
+
|
|
45
|
+
/** Bind one vector into a seat — the elementary half of fold. Used to index an
|
|
46
|
+
* episode from either side and to pour a partner into a form's halo. */
|
|
47
|
+
export const bindSeat = (space: Space, v: Vec, seat: number): Vec =>
|
|
48
|
+
permute(v, space.seats[seat].fwd);
|
|
49
|
+
|
|
50
|
+
// ── Company signatures ──────────────────────────────────────────────────
|
|
51
|
+
//
|
|
52
|
+
// A halo is a superposition of EPISODE SIGNATURES: it answers "who does this
|
|
53
|
+
// form keep company with", and two forms share a concept when they keep the
|
|
54
|
+
// SAME company (the same partner nodes). Pouring the partner's raw GIST was
|
|
55
|
+
// an approximation of that: it worked while the hierarchical fold decorrelated
|
|
56
|
+
// unrelated gists quickly, but any byte-overlap between partners leaks CONTENT
|
|
57
|
+
// similarity into COMPANY similarity, silently shifting the halo null model
|
|
58
|
+
// that conceptThreshold's derivation (unrelated halos ⇒ cosine 0 ± 1/√D)
|
|
59
|
+
// depends on. A signature makes the semantics exact and fold-independent:
|
|
60
|
+
// a deterministic unit vector derived from the partner's IDENTITY, so two
|
|
61
|
+
// halos correlate exactly as much as their company overlaps — never because
|
|
62
|
+
// their partners merely contain similar bytes.
|
|
63
|
+
//
|
|
64
|
+
// Seeded by node id: ids are content-addressed mint order, stable for a given
|
|
65
|
+
// corpus (including checkpoint/resume, which re-derives identical ids), and
|
|
66
|
+
// halos are per-store training artifacts that are never compared across
|
|
67
|
+
// stores.
|
|
68
|
+
|
|
69
|
+
const _sigCache = new WeakMap<Space, Map<number, Vec>>();
|
|
70
|
+
const SIG_CACHE_MAX = 65_536;
|
|
71
|
+
|
|
72
|
+
/** The company signature of node `id` — the halo's pour unit (see above). */
|
|
73
|
+
export function companySignature(space: Space, id: number): Vec {
|
|
74
|
+
let cache = _sigCache.get(space);
|
|
75
|
+
if (!cache) _sigCache.set(space, cache = new Map());
|
|
76
|
+
const hit = cache.get(id);
|
|
77
|
+
if (hit) return hit;
|
|
78
|
+
const v = randomUnit(space.D, rng((id ^ 0x9e3779b9) >>> 0));
|
|
79
|
+
if (cache.size >= SIG_CACHE_MAX) cache.clear(); // flat cap; regeneration is cheap
|
|
80
|
+
cache.set(id, v);
|
|
81
|
+
return v;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** fold — combine ordered children into one gist.
|
|
85
|
+
* Each child is turned with its seat's own key, superposed, normalized. */
|
|
86
|
+
export function fold(space: Space, kids: Vec[]): Vec {
|
|
87
|
+
if (kids.length > space.seats.length) {
|
|
88
|
+
throw new Error(
|
|
89
|
+
`fold: ${kids.length} children but the keyring has only ${space.seats.length} seats`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
const out = zeros(space.D);
|
|
93
|
+
if (!_foldBuf || _foldBuf.length !== space.D) {
|
|
94
|
+
_foldBuf = new Float32Array(space.D);
|
|
95
|
+
}
|
|
96
|
+
const buf = _foldBuf;
|
|
97
|
+
for (let i = 0; i < kids.length; i++) {
|
|
98
|
+
permuteInto(buf, kids[i], space.seats[i].fwd);
|
|
99
|
+
addInto(out, buf);
|
|
100
|
+
}
|
|
101
|
+
return normalize(out);
|
|
102
|
+
}
|