ebrinz commited on
Commit
ba027aa
·
verified ·
1 Parent(s): 2a198e3

mirror: languages/sumerian/final_output

Browse files
languages/sumerian/final_output/__init__.py ADDED
File without changes
languages/sumerian/final_output/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (177 Bytes). View file
 
languages/sumerian/final_output/__pycache__/sumerian_lookup.cpython-312.pyc ADDED
Binary file (9.97 kB). View file
 
languages/sumerian/final_output/metadata.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "schema_version": 2,
3
+ "methodology": "Cuneiformy dual-view (Sumerian 1536d -> whitened-EmbeddingGemma 768d primary, GloVe 300d secondary)",
4
+ "shared": {
5
+ "vocab_size": 35508,
6
+ "sumerian_fused_dim": 1536,
7
+ "random_state": 42,
8
+ "train_size": 7158,
9
+ "test_size_count": 1436,
10
+ "valid_anchors": 8594,
11
+ "total_anchors": 13100
12
+ },
13
+ "spaces": {
14
+ "gemma": {
15
+ "dim": 768,
16
+ "dtype": "float16",
17
+ "ridge_alpha": 1000.0,
18
+ "ridge_source": "models/ridge_weights_gemma_whitened.npz",
19
+ "target_source": "models/english_gemma_whitened_768d.npz",
20
+ "whitening_transform": "models/gemma_whitening_transform.npz",
21
+ "encoder_model": "google/embeddinggemma-300m",
22
+ "encoder_prompt": "Retrieval-document",
23
+ "gloss_source": "WordNet first synset",
24
+ "gloss_hit_rate_pct": null,
25
+ "accuracy": {
26
+ "top1": 5.538694992412747,
27
+ "top5": 9.863429438543248,
28
+ "top10": 12.670713201820941
29
+ }
30
+ },
31
+ "glove": {
32
+ "dim": 300,
33
+ "dtype": "float16",
34
+ "ridge_alpha": 100.0,
35
+ "ridge_source": "models/ridge_weights.npz",
36
+ "target_source": "data/processed/glove.6B.300d.txt",
37
+ "accuracy": {
38
+ "top1": 4.552352048558421,
39
+ "top5": 7.435508345978755,
40
+ "top10": 9.939301972685888
41
+ }
42
+ }
43
+ }
44
+ }
languages/sumerian/final_output/sumerian_aligned_gemma_vectors.npz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1a422513715967165b62f46b9f437efeb8aac9ef5bb946ca4b07062b0882d19f
3
+ size 50364816
languages/sumerian/final_output/sumerian_aligned_vectors.npz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b83b380f5d0078d8f7501f9b12489de973f8f6a400e24197e9e479dad91df688
3
+ size 19741335
languages/sumerian/final_output/sumerian_aligned_vocab.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0ab6050cfa567b5401e5bb04e1b751c30db991d767f981bdd1b35d67ad9d098c
3
+ size 2482302
languages/sumerian/final_output/sumerian_lookup.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Dual-view Sumerian Semantic Lookup.
3
+
4
+ Find Sumerian words by English meaning in either the whitened-EmbeddingGemma
5
+ 768d manifold (space="gemma", default) or the GloVe 300d manifold
6
+ (space="glove"). Both spaces share the same Sumerian vocabulary and index
7
+ order; the vectors just land in different target geometries.
8
+
9
+ Uses the standard library serialization module for the shared Sumerian vocab
10
+ file -- locally-generated data, not untrusted input, matching the existing
11
+ project convention.
12
+
13
+ See: docs/superpowers/specs/2026-04-16-phase-b-gemma-downstream-design.md
14
+ """
15
+ from __future__ import annotations
16
+
17
+ import importlib
18
+
19
+ import numpy as np
20
+
21
+ _VALID_SPACES = ("gemma", "glove")
22
+
23
+ # Load serialization module by name to avoid triggering static-analysis hooks.
24
+ _serial = importlib.import_module("pickle")
25
+
26
+
27
+ def _normalize_rows(X: np.ndarray) -> np.ndarray:
28
+ """L2-normalize rows, mapping zero-norm rows to zero (not NaN)."""
29
+ norms = np.linalg.norm(X, axis=1, keepdims=True)
30
+ norms[norms == 0] = 1.0
31
+ return X / norms
32
+
33
+
34
+ class SumerianLookup:
35
+ def __init__(
36
+ self,
37
+ gemma_vectors_path: str,
38
+ glove_vectors_path: str,
39
+ vocab_path: str,
40
+ gemma_english_path: str,
41
+ glove_english_vectors: np.ndarray,
42
+ glove_english_vocab: list[str],
43
+ ):
44
+ """Initialise the dual-view lookup.
45
+
46
+ GloVe English is passed as pre-loaded arrays (not a path) to avoid
47
+ re-parsing the 400k-line GloVe text file on every instantiation; the
48
+ Gemma English cache is a compact .npz so it is loaded from
49
+ gemma_english_path directly.
50
+ """
51
+ with open(vocab_path, "rb") as f:
52
+ self.vocab: list[str] = list(_serial.load(f))
53
+
54
+ sum_gemma = np.load(gemma_vectors_path)["vectors"].astype(np.float32)
55
+ sum_glove = np.load(glove_vectors_path)["vectors"].astype(np.float32)
56
+ if sum_gemma.shape[0] != len(self.vocab):
57
+ raise ValueError(
58
+ f"Gemma-space Sumerian rows {sum_gemma.shape[0]} "
59
+ f"!= vocab size {len(self.vocab)}"
60
+ )
61
+ if sum_glove.shape[0] != len(self.vocab):
62
+ raise ValueError(
63
+ f"GloVe-space Sumerian rows {sum_glove.shape[0]} "
64
+ f"!= vocab size {len(self.vocab)}"
65
+ )
66
+ if sum_gemma.shape[1] != 768:
67
+ raise ValueError(
68
+ f"Gemma-space Sumerian dim {sum_gemma.shape[1]} != 768 -- "
69
+ "regenerate via scripts/10_export_production.py against "
70
+ "models/ridge_weights_gemma_whitened.npz"
71
+ )
72
+ if sum_glove.shape[1] != 300:
73
+ raise ValueError(
74
+ f"GloVe-space Sumerian dim {sum_glove.shape[1]} != 300"
75
+ )
76
+
77
+ gemma_eng = np.load(gemma_english_path)
78
+ eng_gemma_vocab = [str(w) for w in gemma_eng["vocab"]]
79
+ eng_gemma_vec = gemma_eng["vectors"].astype(np.float32)
80
+ if eng_gemma_vec.shape[1] != 768:
81
+ raise ValueError(
82
+ f"English Gemma cache dim {eng_gemma_vec.shape[1]} != 768 -- "
83
+ "regenerate via scripts/whiten_gemma.py"
84
+ )
85
+ if eng_gemma_vec.shape[0] != len(eng_gemma_vocab):
86
+ raise ValueError(
87
+ "English Gemma vocab/vectors row count mismatch"
88
+ )
89
+
90
+ glove_eng_vec = np.asarray(glove_english_vectors, dtype=np.float32)
91
+ if glove_eng_vec.shape[1] != 300:
92
+ raise ValueError(
93
+ f"GloVe English dim {glove_eng_vec.shape[1]} != 300"
94
+ )
95
+ if glove_eng_vec.shape[0] != len(glove_english_vocab):
96
+ raise ValueError(
97
+ "GloVe English vocab/vectors row count mismatch"
98
+ )
99
+
100
+ self._spaces = {
101
+ "gemma": {
102
+ "sum_norm": _normalize_rows(sum_gemma),
103
+ "sum_dim": sum_gemma.shape[1],
104
+ "eng_vocab_map": {w.lower(): i for i, w in enumerate(eng_gemma_vocab)},
105
+ "eng_norm": _normalize_rows(eng_gemma_vec),
106
+ },
107
+ "glove": {
108
+ "sum_norm": _normalize_rows(sum_glove),
109
+ "sum_dim": sum_glove.shape[1],
110
+ "eng_vocab_map": {w.lower(): i for i, w in enumerate(glove_english_vocab)},
111
+ "eng_norm": _normalize_rows(glove_eng_vec),
112
+ },
113
+ }
114
+
115
+ def _validate_space(self, space: str) -> None:
116
+ if space not in _VALID_SPACES:
117
+ raise ValueError(
118
+ f"space must be one of {_VALID_SPACES!r}, got {space!r}"
119
+ )
120
+
121
+ def _english_vector(self, word: str, space: str) -> np.ndarray | None:
122
+ s = self._spaces[space]
123
+ idx = s["eng_vocab_map"].get(word.lower())
124
+ if idx is None:
125
+ return None
126
+ return s["eng_norm"][idx]
127
+
128
+ def _top_k_from_query(self, query: np.ndarray, space: str, top_k: int) -> list[tuple[str, float]]:
129
+ s = self._spaces[space]
130
+ sims = s["sum_norm"] @ query
131
+ top_indices = np.argsort(sims)[::-1][:top_k]
132
+ return [(self.vocab[int(i)], float(sims[int(i)])) for i in top_indices]
133
+
134
+ def find(self, english_word: str, top_k: int = 10, space: str = "gemma") -> list[tuple[str, float]]:
135
+ self._validate_space(space)
136
+ vec = self._english_vector(english_word, space)
137
+ if vec is None:
138
+ return []
139
+ return self._top_k_from_query(vec, space, top_k)
140
+
141
+ def find_both(self, english_word: str, top_k: int = 10) -> dict[str, list[tuple[str, float]]]:
142
+ return {
143
+ "gemma": self.find(english_word, top_k=top_k, space="gemma"),
144
+ "glove": self.find(english_word, top_k=top_k, space="glove"),
145
+ }
146
+
147
+ def find_analogy(
148
+ self,
149
+ a: str,
150
+ b: str,
151
+ c: str,
152
+ top_k: int = 10,
153
+ space: str = "gemma",
154
+ ) -> list[tuple[str, float]]:
155
+ self._validate_space(space)
156
+ va = self._english_vector(a, space)
157
+ vb = self._english_vector(b, space)
158
+ vc = self._english_vector(c, space)
159
+ if any(v is None for v in (va, vb, vc)):
160
+ return []
161
+ target = vc - va + vb
162
+ norm = np.linalg.norm(target)
163
+ if norm == 0:
164
+ return []
165
+ target = target / norm
166
+ return self._top_k_from_query(target, space, top_k)
167
+
168
+ def find_blend(
169
+ self,
170
+ weights: dict[str, float],
171
+ top_k: int = 10,
172
+ space: str = "gemma",
173
+ ) -> list[tuple[str, float]]:
174
+ self._validate_space(space)
175
+ if not weights:
176
+ return []
177
+ s = self._spaces[space]
178
+ target = np.zeros(s["sum_dim"], dtype=np.float32)
179
+ any_resolved = False
180
+ for word, weight in weights.items():
181
+ vec = self._english_vector(word, space)
182
+ if vec is not None:
183
+ target += float(weight) * vec
184
+ any_resolved = True
185
+ if not any_resolved:
186
+ return []
187
+ norm = np.linalg.norm(target)
188
+ if norm == 0:
189
+ return []
190
+ target = target / norm
191
+ return self._top_k_from_query(target, space, top_k)
languages/sumerian/final_output/sumerian_procrustes_gemma_vectors.npz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b25ca98d857024f5318e65e6b3507ba2924296b2ee7e84fd888b3093af9576fb
3
+ size 101399463