fluidlayer 2.0.0__tar.gz

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.
Files changed (80) hide show
  1. fluidlayer-2.0.0/CITATION.cff +21 -0
  2. fluidlayer-2.0.0/LICENSE +21 -0
  3. fluidlayer-2.0.0/MANIFEST.in +19 -0
  4. fluidlayer-2.0.0/Makefile +45 -0
  5. fluidlayer-2.0.0/PKG-INFO +376 -0
  6. fluidlayer-2.0.0/README.md +339 -0
  7. fluidlayer-2.0.0/docs/API.md +108 -0
  8. fluidlayer-2.0.0/docs/BLOG.md +133 -0
  9. fluidlayer-2.0.0/docs/CHANGELOG.md +78 -0
  10. fluidlayer-2.0.0/docs/COMPARISON.md +309 -0
  11. fluidlayer-2.0.0/docs/DESIGN_pinned_memory.md +445 -0
  12. fluidlayer-2.0.0/docs/FRAMEWORK.md +481 -0
  13. fluidlayer-2.0.0/docs/GETTING_STARTED.md +127 -0
  14. fluidlayer-2.0.0/docs/GRAFT.md +640 -0
  15. fluidlayer-2.0.0/docs/LIMITATION.md +60 -0
  16. fluidlayer-2.0.0/docs/MILESTONES.md +205 -0
  17. fluidlayer-2.0.0/docs/NEGATIVE-RESULTS.md +689 -0
  18. fluidlayer-2.0.0/docs/POSITIONING.md +232 -0
  19. fluidlayer-2.0.0/docs/README.md +50 -0
  20. fluidlayer-2.0.0/docs/REPRODUCING.md +115 -0
  21. fluidlayer-2.0.0/docs/TEN-MILLION.md +171 -0
  22. fluidlayer-2.0.0/docs/index.html +1021 -0
  23. fluidlayer-2.0.0/examples/gate_demo.py +76 -0
  24. fluidlayer-2.0.0/examples/graft_demo.py +68 -0
  25. fluidlayer-2.0.0/examples/quickstart.py +89 -0
  26. fluidlayer-2.0.0/experiments/e10_gate.py +263 -0
  27. fluidlayer-2.0.0/experiments/e11_exact.py +308 -0
  28. fluidlayer-2.0.0/experiments/e12_router.py +252 -0
  29. fluidlayer-2.0.0/experiments/e13_knn_baseline.py +174 -0
  30. fluidlayer-2.0.0/experiments/e14_real_gpt2.py +262 -0
  31. fluidlayer-2.0.0/experiments/e1_prior_perplexity.py +454 -0
  32. fluidlayer-2.0.0/experiments/e2_scaling.py +228 -0
  33. fluidlayer-2.0.0/experiments/e4_gbalance.py +281 -0
  34. fluidlayer-2.0.0/experiments/e5_graft.py +573 -0
  35. fluidlayer-2.0.0/experiments/e6_graft.py +544 -0
  36. fluidlayer-2.0.0/experiments/e6_real_corpus.py +217 -0
  37. fluidlayer-2.0.0/experiments/e7_lossy_fix.py +265 -0
  38. fluidlayer-2.0.0/experiments/e9_ten_million.py +277 -0
  39. fluidlayer-2.0.0/experiments/gpu_graft.py +186 -0
  40. fluidlayer-2.0.0/experiments/pretrain_prior.py +304 -0
  41. fluidlayer-2.0.0/experiments/run_all.py +189 -0
  42. fluidlayer-2.0.0/fluidlayer/__init__.py +96 -0
  43. fluidlayer-2.0.0/fluidlayer/autodiff.py +472 -0
  44. fluidlayer-2.0.0/fluidlayer/blocks.py +275 -0
  45. fluidlayer-2.0.0/fluidlayer/bpe.py +173 -0
  46. fluidlayer-2.0.0/fluidlayer/data.py +92 -0
  47. fluidlayer-2.0.0/fluidlayer/diffusion.py +264 -0
  48. fluidlayer-2.0.0/fluidlayer/gpt2_model.py +129 -0
  49. fluidlayer-2.0.0/fluidlayer/gpt2_tokenizer.py +127 -0
  50. fluidlayer-2.0.0/fluidlayer/graft.py +741 -0
  51. fluidlayer-2.0.0/fluidlayer/losses.py +86 -0
  52. fluidlayer-2.0.0/fluidlayer/memory.py +525 -0
  53. fluidlayer-2.0.0/fluidlayer/model.py +141 -0
  54. fluidlayer-2.0.0/fluidlayer/optim.py +57 -0
  55. fluidlayer-2.0.0/fluidlayer/py.typed +0 -0
  56. fluidlayer-2.0.0/fluidlayer/sheaf.py +147 -0
  57. fluidlayer-2.0.0/fluidlayer/topology.py +294 -0
  58. fluidlayer-2.0.0/fluidlayer/torch_graft.py +283 -0
  59. fluidlayer-2.0.0/fluidlayer.egg-info/PKG-INFO +376 -0
  60. fluidlayer-2.0.0/fluidlayer.egg-info/SOURCES.txt +78 -0
  61. fluidlayer-2.0.0/fluidlayer.egg-info/dependency_links.txt +1 -0
  62. fluidlayer-2.0.0/fluidlayer.egg-info/requires.txt +12 -0
  63. fluidlayer-2.0.0/fluidlayer.egg-info/top_level.txt +1 -0
  64. fluidlayer-2.0.0/fluidlayer_single.py +4672 -0
  65. fluidlayer-2.0.0/pyproject.toml +76 -0
  66. fluidlayer-2.0.0/requirements.txt +1 -0
  67. fluidlayer-2.0.0/run_without_pytest.py +158 -0
  68. fluidlayer-2.0.0/selftest.py +1089 -0
  69. fluidlayer-2.0.0/setup.cfg +4 -0
  70. fluidlayer-2.0.0/tests/conftest.py +25 -0
  71. fluidlayer-2.0.0/tests/test_bpe.py +24 -0
  72. fluidlayer-2.0.0/tests/test_graft.py +122 -0
  73. fluidlayer-2.0.0/tests/test_memory.py +63 -0
  74. fluidlayer-2.0.0/tests/test_selftest.py +28 -0
  75. fluidlayer-2.0.0/tests/test_topology.py +57 -0
  76. fluidlayer-2.0.0/tests/test_torch_parity.py +79 -0
  77. fluidlayer-2.0.0/tools/_bundle_template.py +64 -0
  78. fluidlayer-2.0.0/tools/bundle_single_file.py +71 -0
  79. fluidlayer-2.0.0/tools/download_gpt2.py +48 -0
  80. fluidlayer-2.0.0/tools/fetch_pypi.py +117 -0
@@ -0,0 +1,21 @@
1
+ cff-version: 1.2.0
2
+ message: "If you use this software, please cite it as below."
3
+ title: "FluidLayer: a research memory layer for language models using cellular-sheaf diffusion"
4
+ abstract: >-
5
+ A NumPy research implementation of a sheaf-diffusion sequence architecture
6
+ with a bounded, majority-tracking exact-token store and an optional
7
+ PyTorch adapter. Includes machine-checked tests and documented measured
8
+ limitations. It is a research prototype, not a production-ready service.
9
+ type: software
10
+ version: "2.0.0"
11
+ license: MIT
12
+ keywords:
13
+ - large language models
14
+ - memory
15
+ - cellular sheaves
16
+ - persistent homology
17
+ - KV cache
18
+ authors:
19
+ - family-names: "Dhinwa"
20
+ given-names: "Kapil"
21
+ repository-code: "https://github.com/kapildhinwa2010-dev/fluidtech"
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 FluidLayer contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,19 @@
1
+ include LICENSE
2
+ include CITATION.cff
3
+ include README.md
4
+ include requirements.txt
5
+ include Makefile
6
+ include selftest.py
7
+ include run_without_pytest.py
8
+ include fluidlayer_single.py
9
+ recursive-include fluidlayer *.py
10
+ include fluidlayer/py.typed
11
+ recursive-include docs *.md *.html
12
+ recursive-include examples *.py
13
+ recursive-include experiments *.py
14
+ recursive-include tests *.py
15
+ recursive-include tools *.py
16
+ include tools/_bundle_template.py
17
+ prune results
18
+ global-exclude __pycache__
19
+ global-exclude *.pyc
@@ -0,0 +1,45 @@
1
+ # FluidLayer -- a constant-size exact memory tier for LLMs.
2
+ #
3
+ # make test self-verification and unit tests
4
+ # make quick a fast smoke run of every experiment
5
+ # make full every experiment, at full size (slow: ~40 min, CPU)
6
+ # make bundle rebuild the single-file bundle
7
+ # make lint ruff + a compile check
8
+ # make release build the sdist and the wheel
9
+
10
+ PY ?= python3
11
+ RESULTS := results
12
+
13
+ .PHONY: help test quick full bundle lint clean release artifacts help
14
+
15
+ help:
16
+ @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
17
+ awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-12s\033[0m %s\n", $$1, $$2}'
18
+
19
+ test: ## run the self-verification checks
20
+ $(PY) selftest.py
21
+ $(PY) fluidlayer_single.py --selftest
22
+ $(PY) -m pytest -q tests
23
+
24
+ quick: ## a fast smoke run of every experiment
25
+ $(PY) experiments/run_all.py --quick
26
+
27
+ full: ## every experiment at full size
28
+ $(PY) experiments/run_all.py --full
29
+
30
+ bundle: ## rebuild the single-file bundle
31
+ $(PY) tools/bundle_single_file.py
32
+
33
+ lint: ## compile check + ruff (if installed)
34
+ $(PY) -m compileall -q fluidlayer experiments tools tests examples selftest.py run_without_pytest.py
35
+ $(PY) -m ruff check fluidlayer experiments tools tests examples selftest.py run_without_pytest.py
36
+
37
+ artifacts: ## rebuild the downloadable prior artifact
38
+ $(PY) experiments/pretrain_prior.py --synthetic --tokens 200000
39
+
40
+ release: ## build the sdist and the wheel
41
+ $(PY) -m build
42
+
43
+ clean: ## remove build artefacts
44
+ rm -rf build dist *.egg-info .pytest_cache .ruff_cache
45
+ find . -name __pycache__ -type d -exec rm -rf {} + 2>/dev/null || true
@@ -0,0 +1,376 @@
1
+ Metadata-Version: 2.4
2
+ Name: fluidlayer
3
+ Version: 2.0.0
4
+ Summary: A research memory layer for language models using cellular-sheaf diffusion
5
+ Author: Kapil Dhinwa
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/kapildhinwa2010-dev/fluidtech
8
+ Project-URL: Repository, https://github.com/kapildhinwa2010-dev/fluidtech
9
+ Project-URL: Issues, https://github.com/kapildhinwa2010-dev/fluidtech/issues
10
+ Project-URL: Changelog, https://github.com/kapildhinwa2010-dev/fluidtech/blob/main/docs/CHANGELOG.md
11
+ Keywords: llm,memory,long-context,kv-cache,retrieval,cellular-sheaves,persistent-homology
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
22
+ Classifier: Typing :: Typed
23
+ Requires-Python: >=3.9
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ Requires-Dist: numpy<3,>=1.23
27
+ Provides-Extra: dev
28
+ Requires-Dist: pytest>=7; extra == "dev"
29
+ Requires-Dist: ruff>=0.5; extra == "dev"
30
+ Requires-Dist: build; extra == "dev"
31
+ Provides-Extra: gpu
32
+ Requires-Dist: torch>=2.1; extra == "gpu"
33
+ Requires-Dist: transformers>=4.40; extra == "gpu"
34
+ Requires-Dist: accelerate>=0.30; extra == "gpu"
35
+ Requires-Dist: safetensors>=0.4; extra == "gpu"
36
+ Dynamic: license-file
37
+
38
+ # FluidLayer
39
+
40
+ **A research memory layer for LLMs — attachable with one residual-stream insertion.**
41
+
42
+ `16 KB` research-scale fluid state as a compact long-range channel ·<br>
43
+ exact recall of retained token IDs from outside the attention window ·<br>
44
+ pure NumPy core, with optional PyTorch support ·<br>
45
+ eleven measured negative results, published on purpose
46
+
47
+ [![CI](https://github.com/kapildhinwa2010-dev/fluidtech/actions/workflows/ci.yml/badge.svg)](https://github.com/kapildhinwa2010-dev/fluidtech/actions/workflows/ci.yml)
48
+ ![Python 3.9+](https://img.shields.io/badge/python-3.9%2B-blue.svg)
49
+ ![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)
50
+ ![Self-verification](https://img.shields.io/badge/self--verification-50%2F50-brightgreen.svg)
51
+
52
+ ---
53
+
54
+ ## The problem, in one number
55
+
56
+ A 70B model on 2×H100 80GB cannot hold a single 128K-token session.
57
+
58
+ The weights take **131.5 GB**; the KV cache for 128K tokens needs another **40 GB**, and only **28.5 GB** is left.
59
+ This is a memory-budget illustration, not a claim that this prototype
60
+ replaces a KV cache or is a drop-in serving optimization.
61
+
62
+ The usual answer is to quantise it, compress it, or make the model smaller.
63
+ This project asks a different question:
64
+
65
+ > What if a compact, constant-size memory channel sat beside attention and
66
+ > could be addressed by content instead of position?
67
+
68
+ ---
69
+
70
+ ## What this is
71
+
72
+ A research memory layer designed to attach to a causal LM:
73
+
74
+ ```text
75
+ h' = h + W_e · E(h) + W_l · S(h)
76
+ ^^^^^^ ^^^^^
77
+ exact tier lossy tier (the constant-size fluid state)
78
+ ```
79
+
80
+ - The **exact tier** is a bounded, content-keyed store of token IDs.
81
+ **20 bytes per packed slot.** A recalled value is an `int32` — bit-exact, not approximate.
82
+ The attachable store tracks majority continuations and evicts the
83
+ least-corroborated entries. Bit-exactness describes the stored token ID,
84
+ not its truth or provenance. The Python reference's memory estimate
85
+ excludes Python object overhead.
86
+
87
+ - The **lossy tier** is a sheaf-diffusion aggregate over `M` anchors — `M × r` floats, constant size, whatever you stream through it.
88
+
89
+ The two tiers exist because a diffusion smears. If the exact value of a token lives only inside the fluid, it is mixed with its neighbours and is provably no longer exact.
90
+
91
+ So:
92
+
93
+ > **The fluid answers WHERE. The register answers WHAT.**
94
+
95
+ ---
96
+
97
+ ## The three numbers
98
+
99
+ | Claim | Measured result |
100
+ |---|---|
101
+ | Attaching the layer changes nothing | The logits are **bit-identical** (max diff `0.000e+00`). The output projections start at zero, so the branch is a **provable no-op** until someone trains it. |
102
+ | It adds a capability the model did not have | Cross-window verbatim recall: **`0.050 → 0.800`** (chance `0.125`) — **16× the base model** — for a fact planted 400 tokens outside the attention window. |
103
+ | The stored token ID is exact | A retained token ID is returned bit-exactly. This does not verify whether the token is true, safe, or authenticated. |
104
+ | The model gets better | Gated retrieval improves the base model’s perplexity by **`−0.176 nats`** (`ppl 13.61 → 11.42`) — the model’s own confidence decides when the memory speaks. |
105
+ | It beats its closest prior art in the regime it targets | Against a **kNN-LM** (the standard way to use a datastore), with a **16 KB** store the gated layer wins by **7.4×** on loss (`−0.1134 vs −0.0154`). With a 1 MB store the kNN-LM wins by 6.4×, so the claim is precise: **the gate is the contribution under a hard memory budget.** |
106
+ | The fluid tier earns its place | Used as the **router** (the geometry as an address space), it rescues recall when the exact store is too small (`0.100 → 0.625`) and fixes hard contexts where the base model has no idea (`+0.053 → −0.071 nats`) — at **0.0005%** of a layer’s compute. |
107
+ | The diffusion tier delivers none of it | With the lossy tier alone: **`0.050`**, i.e. the base model’s own number. The exact tier delivers **all** of it. |
108
+
109
+ Measured on a frozen base model, in [`results/e6_graft.json`](results/e6_graft.json).
110
+ The recipe is in [`docs/GRAFT.md`](docs/GRAFT.md).
111
+
112
+ ---
113
+
114
+ ## Try it, in 60 seconds
115
+
116
+ ```bash
117
+ git clone https://github.com/kapildhinwa2010-dev/fluidtech.git
118
+ cd fluidtech
119
+ pip install -r requirements.txt
120
+
121
+ python selftest.py # 50 machine-checked claims
122
+ python examples/quickstart.py # the tour
123
+ python examples/graft_demo.py # the capability, end to end
124
+ python examples/gate_demo.py # the gate: recall AND a better perplexity
125
+ python experiments/e11_exact.py # the exact memory (the full benchmark)
126
+ ```
127
+
128
+ To install the published package instead of cloning the repository:
129
+
130
+ ```bash
131
+ python -m pip install fluidlayer
132
+ ```
133
+
134
+ Expected output:
135
+
136
+ ```text
137
+ base model : 1/20 correct (it never saw the fact)
138
+ base + the layer : 20/20 correct (the layer retrieved it)
139
+ ```
140
+
141
+ ---
142
+
143
+ ## Using it
144
+
145
+ ```python
146
+ import torch
147
+ from fluidlayer.torch_graft import SAABranchTorch
148
+
149
+ # Assume `model` is a loaded causal LM and `windows` contains 1-D token-ID tensors.
150
+ embedding = model.get_input_embeddings()
151
+ branch = SAABranchTorch(
152
+ M=64,
153
+ r=8,
154
+ d=embedding.weight.shape[1],
155
+ emb=embedding,
156
+ ctx_len=1,
157
+ read_gain=300.0,
158
+ transform="pc1",
159
+ use_exact=True,
160
+ use_lossy=False,
161
+ ).to(embedding.weight.device)
162
+
163
+ branch.reset() # a new session
164
+
165
+ with torch.no_grad():
166
+ for input_ids in windows: # one 1-D token-ID window at a time
167
+ content = embedding(input_ids)
168
+ residual = content
169
+ h, info = branch(residual, content, input_ids)
170
+ logits = model(inputs_embeds=h.unsqueeze(0)).logits[0]
171
+ logits = branch.gated_logits(logits, info)
172
+ ```
173
+
174
+ - **4 KB** of fluid state for `M=64, r=8`, plus a logical **20 B per populated context or anchor slot**; the Python reference has additional object overhead.
175
+ - The base model is frozen the whole time.
176
+ - The output projections start at zero, so attachment initially preserves
177
+ the model output. Enabling retrieval is a separate, calibrated operation.
178
+ - This sketch omits model-specific positional embeddings, attention masks,
179
+ session lifecycle, and gain calibration. See the [integration notes](docs/GETTING_STARTED.md#attach-the-optional-pytorch-adapter).
180
+ - The NumPy reference implementation is [`fluidlayer/graft.py`](fluidlayer/graft.py), and the Torch one mirrors it exactly.
181
+
182
+ Start with the [documentation guide](docs/README.md), then read
183
+ [Getting started](docs/GETTING_STARTED.md) and the [API reference](docs/API.md).
184
+
185
+ ---
186
+
187
+ ## What it does not do
188
+
189
+ Being explicit, because this is where grafts usually oversell:
190
+
191
+ - It does **not** make the model better at language — it makes it better at remembering what it has already seen.
192
+ - It does **not** extend the attention window — the window is still 128 tokens; the layer can quote from outside it, not reason over it.
193
+ - It does **not** compose, infer, or answer questions about what it holds.
194
+ - The diffusion tier has **no measurable upside** on real text and used to damage the model it was attached to (`+0.81 nats`) until its training signal was fixed — keep it off.
195
+
196
+ The full list, with numbers: [`docs/LIMITATION.md`](docs/LIMITATION.md)
197
+
198
+ ---
199
+
200
+ ## At ten million tokens
201
+
202
+ Give a model 10M tokens with the layer attached, and:
203
+
204
+ - The state is still **16 KB**.
205
+ The same tokens as a KV cache would require **1.31 TB** for an 8B model, **3.28 TB** for a 70B one.
206
+ - Filling it costs **80 seconds** and **no model forward at all** — the exact tier is model-free.
207
+ - With distinctive keys it recalls **6/6** facts planted from 100K to 9M tokens deep.
208
+ With realistic 12-gram keys and a 16 KB budget it recalls **0/6** — its reach is the last ~10,000 tokens.
209
+ - The perplexity delta is **`−0.0057 nats`** at the right injection gain, and **`+1.08 nats`** at the gain that maximises recall — the two knobs are three orders of magnitude apart.
210
+ The gate closes that gap: scaling the override by the model’s own uncertainty keeps recall at **`1.000`** and improves perplexity to **`11.42`**.
211
+
212
+ Details: [`docs/TEN-MILLION.md`](docs/TEN-MILLION.md)
213
+
214
+ ---
215
+
216
+ ## The negative results
217
+
218
+ This is the part that makes the rest believable.
219
+ Eleven results came out negative or against expectation, and all eleven are measured, reproducible, and published.
220
+
221
+ | What we expected | What happened |
222
+ |---|---|
223
+ | The **topological** selection criterion picks the right facts | It does **not** — `0.000` precision vs `0.970` for plain geometric affinity. |
224
+ | `‖L·1‖∞` detects the frozen state | It does **not** — the frustration index does; the old claim is now a test case. |
225
+ | `λ₂` predicts long-context quality | Barely — Pearson `0.20`. |
226
+ | A transformer loses at some scale | It **wins** at every scale we could train. |
227
+ | A domain prior beats a frequency table | It helps (`−0.006 nats`) but a **unigram table wins 6× more**. |
228
+ | The diffusion tier is neutral on a frozen model | It **degrades** the model by **`+0.81 nats`** (fixed → `+0.0000`, but no upside either). |
229
+ | Training the layer helps | It **hurts** — the training-free identity map is better (`0.800 vs 0.650`). |
230
+
231
+ Read it: [`docs/NEGATIVE-RESULTS.md`](docs/NEGATIVE-RESULTS.md)
232
+
233
+ ---
234
+
235
+ ## The repository
236
+
237
+ ```text
238
+ fluidlayer/ the package (pure NumPy; torch only in torch_graft.py)
239
+ graft.py the attachable memory layer
240
+ memory.py the frozen register (20 B per fact, bit-exact)
241
+ torch_graft.py the same layer in PyTorch, for a real LLM
242
+ gpt2_model.py a GPT-2 forward pass in PyTorch (no transformers needed)
243
+ gpt2_tokenizer.py the GPT-2 byte-level BPE, in pure Python
244
+ sheaf.py topology.py diffusion.py blocks.py autodiff.py ...
245
+
246
+ experiments/ every measurement; each writes a JSON into results/
247
+ e14_real_gpt2.py the graft on a REAL pretrained GPT-2 (124M)
248
+ e13_knn_baseline.py the layer vs. the closest prior art (kNN-LM)
249
+
250
+ tools/ the artifact checker, bundler, checkpoint downloader,
251
+ and PyPI fetcher
252
+ fluidlayer/bpe.py the byte-level BPE tokenizer
253
+
254
+ tests/ the unit tests (pytest, or run them without it)
255
+
256
+ examples/ quickstart.py, graft_demo.py, gate_demo.py
257
+
258
+ docs/ the design docs, the API, the limits, the theory report
259
+
260
+ results/ every number in the README, as JSON
261
+ ```
262
+
263
+ ---
264
+
265
+ ## Documentation
266
+
267
+ Browse the [documentation guide](docs/README.md) for a topic-based index.
268
+ The [getting-started tutorial](docs/GETTING_STARTED.md) and
269
+ [API reference](docs/API.md) are the recommended entry points. Research
270
+ claims and their limits are explained in the
271
+ [graft report](docs/GRAFT.md), [negative results](docs/NEGATIVE-RESULTS.md),
272
+ and [limitations](docs/LIMITATION.md).
273
+
274
+ ---
275
+
276
+ ## Status — and it now includes a REAL pretrained LLM
277
+
278
+ GPT-2 (124M parameters, the real 50,257-token BPE tokenizer), run on CPU with the memory layer attached:
279
+
280
+ | Test | Result |
281
+ |---|---|
282
+ | Attaching the layer | The logits are **bit-identical** (max diff `0.000e+00`). |
283
+ | A fact planted **2,947 tokens** before the query | The base GPT-2 predicts `464`; **the layer predicts the fact**. |
284
+ | The base model’s perplexity | `4.0289` (`ppl 56.20`) → **`4.0019` (`ppl 54.70`)** with the layer. |
285
+
286
+ And one result that narrows the claim honestly:
287
+
288
+ On the real model, the classic kNN-LM interpolation beats the gated override by **8×** on perplexity — the opposite of the 131k-parameter model, where the gate won by **7.4×**.
289
+ The gate wins where the base model is weak; interpolation wins where it is strong.
290
+ The capability works on both.
291
+
292
+ The checkpoint, the forward pass and the tokenizer are all in this repository:
293
+
294
+ - [`fluidlayer/gpt2_model.py`](fluidlayer/gpt2_model.py)
295
+ - [`fluidlayer/gpt2_tokenizer.py`](fluidlayer/gpt2_tokenizer.py)
296
+
297
+ No `transformers` dependency, and the generation is verified coherent.
298
+
299
+ The Torch twin is now tested against the NumPy reference in [`tests/test_torch_parity.py`](tests/test_torch_parity.py): the two implementations must agree on the router, the store and the gated override.
300
+
301
+ That test found **four real port bugs**:
302
+
303
+ 1. A different route scale.
304
+ 2. The wrong routing content — the residual stream instead of the position-free embedding.
305
+ 3. A different confidence formula.
306
+ 4. A read/write order that broke the snapshot semantics.
307
+
308
+ Four bugs that would have shipped silently.
309
+
310
+ The checks cover the tested behaviors; they do not prove every research
311
+ claim or validate production-scale operation.
312
+
313
+ ---
314
+
315
+ ## A research prototype, honestly labelled
316
+
317
+ Everything above was measured on CPU, at small scale, on a character-level or 512-token subword corpus, with a **131,392-parameter model**.
318
+
319
+ The subword repeat is the closest thing to a realistic tokenizer in this repository.
320
+ The GPU run, [`experiments/gpu_graft.py`](experiments/gpu_graft.py), is written and has not been executed here.
321
+
322
+ ---
323
+
324
+ ## Reproducing
325
+
326
+ ```bash
327
+ python selftest.py # 50 checks
328
+ python experiments/run_all.py --full # every measurement (~40 min, CPU)
329
+ ```
330
+
331
+ Everything is pure NumPy, CPU-only, and needs no network.
332
+
333
+ The details and expected outputs: [`docs/REPRODUCING.md`](docs/REPRODUCING.md)
334
+
335
+ ---
336
+
337
+ ---
338
+
339
+ ## 🤝 Contributions & corrections
340
+
341
+ Suggestions, bug reports, and corrections are welcome, especially measurement
342
+ disagreements. Please open an issue or submit a pull request.
343
+
344
+ If a number in the docs does not match what you get, that is a bug in the code or in the documentation, and it is the most valuable report you can file.
345
+
346
+ See [`CONTRIBUTING.md`](CONTRIBUTING.md).
347
+
348
+ ---
349
+
350
+ ## License
351
+
352
+ MIT — see [`LICENSE`](LICENSE).
353
+
354
+ If you use this, please cite it: [`CITATION.cff`](CITATION.cff).
355
+
356
+ ---
357
+
358
+ ## Confession
359
+
360
+ The project began as:
361
+
362
+ > “Replace attention with a sheaf diffusion.”
363
+
364
+ The measurements have said three times that this is not the way.
365
+
366
+ What is left is smaller and much more defensible:
367
+
368
+ A research memory layer with an operator-level stability bound, a bounded in-memory register that returns retained token IDs bit-exactly, 50 machine-checked claims, and published measurements of its limitations. The register is not tamper-evident, encrypted, or tenant-isolated by default.
369
+
370
+ ## Origin and acknowledgments
371
+
372
+ The project began with the author's idea of flowing information through a
373
+ topological structure. The implementation and repository organization were
374
+ developed with AI assistance. The measurements, limitations, and research
375
+ decisions are documented alongside the code so the work can be evaluated and
376
+ reproduced.