Transformers
English
Japanese
Spanish
recursive_transformer
Drjkedwards commited on
Commit
e183a48
·
verified ·
1 Parent(s): b35f709

Create README.md

Browse files

```markdown
# ERS — Enhanced Reconsideration System

**A production-ready Python library turning stateless AI agents into stateful ones** by providing recursive reconsideration, knowledge-graph backed memory, and robust tensor/embedding workflows (PMLL-inspired).

**Linked to the Kaggle PyTorch model:**
[Recursive Transformer Model (RTM) / ERS PyTorch Implementation](https://www.kaggle.com/models/josefedwards/recursive-transformer-model/pyTorch)

---

## Table of Contents

- [Overview](#overview)
- [Key Features](#key-features)
- [Architecture & Core Components](#architecture--core-components)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Configuration & Persistence](#configuration--persistence)
- [API Summary](#api-summary)
- [Development & Testing](#development--testing)
- [Roadmap](#roadmap)
- [Contributing](#contributing)
- [License](#license)
- [Acknowledgements](#acknowledgements)

---

## Overview

ERS (Enhanced Reconsideration System) is a memory-management and reconsideration framework for AI systems. It combines:

- Persistent memory slots and deferred reconsideration queues
- Embedding & tensor processing via a PMLL-like lattice and multi-petal attention
- Knowledge-graph integration (Graphiti) and ID-based memory store (Mem0)
- Recursive loops for multi-pass validation (reconsideration)
- Consensus, temporal decay, and contradiction detection for self-correction
- State persistence (JSON + safetensors) for continuity across runs

Designed for production use, ERS focuses on async operation, thread-safety, and modular integration with existing LLM-based stacks.

This library is the official runtime for the **Recursive Transformer Model (RTM)** described in the TechRxiv preprint (Edwards, 2025).

---

## Key Features

- **Stateful transformation** of stateless models: load/save memory, queues, and lattice state
- **ERSPromise**: chainable async promises connected to MemoryBlocks for deferred resolution
- **Deferred queue + MemoryLine**: priority & circular buffer memory structures with thread locks
- **Temporal decay, consensus voting, and contradiction detection** (mathematical heuristics)
- **PMLL Lattice + X-Graph routing + AttentionFlower**: dynamic tensor routing and embedding refinement
- **Graphiti & Mem0 integration** for episode addition, querying, and local/global rewrites
- **LangChain compatibility** for conversational KG memory and agent-based grounding
- **Safe persistence**: JSON for structural state and safetensors for lattice/tensor checkpoints
- **Blockchain-style SHA-256 hashing** for integrity of memory-block chains

---

## Architecture & Core Components

### High-level components
- **MemoryBlock** — content unit with IDs, confidence, timestamps, embedding, and hash. Serializable.
- **ERSPromise** — async future wrapper linking memory blocks in chainable promises.
- **MemoryLine** — slotted circular buffer for active memory slots and operations (push/pull/move).
- **PMLLLattice** — tensor processing core; supports hooks, attention modules, checkpointing via safetensors.
- **XGraphMemory** — routing/compression layer for tensor paths.
- **AttentionFlower** — multi-petal attention module for richer embedding transformations.
- **PMLL** — coordinator for Graphiti, Mem0, and lattice flows (episode addition, querying, rewriting).
- **EnhancedReconsiderationSystem** — orchestrator tying KG, memory, lattice, promises, queues, and loops.

### Core algorithms & behaviors
- `temporal_decay(mem, now)` — decays confidence using adaptive rates (configurable)
- `find_related(embedding)` — KG + Mem0 nearest-neighbour style retrieval for context & consensus
- `compute_consensus(candidates)` — weighted voting and agreement scores
- `detect_contradiction(block, neighbours)` — semantic/temporal/entity contradiction scoring
- `reconsider_memory(block)` — main reconsideration flow: decay → consensus → contradiction → optional rewrite & re-grounding → embedding re-process
- `defer_memory(block, score)` — enqueue for later reconsideration with score-based scheduling
- `recursive_loop_check()` — iterates slots and queue using PMLL-inspired while-loops for multi-pass reconsideration
- `chain_promises(p1, p2, ...)` — links promises and concatenates embedding tails for downstream inference

---

## Installation

**Requirements**
- Python 3.8+
- Core libs: `asyncio`, `numpy`, `collections`, `threading`, `hashlib`, `json`, `datetime`, `typing`
- ML libs: `torch`, `sentence-transformers`
- Persistence: `safetensors`
- KG & memory: `mem0-ai`, `graphiti-core`
- LangChain and community connectors for agent-based grounding

```bash
pip install numpy sentence-transformers torch safetensors mem0-ai graphiti-core langchain langchain-community langchain-openai
```

> **Note:** Replace `langchain-openai` with the provider package that matches your LLM (e.g., `langchain-anthropic`, `langchain-google-genai`, etc.).

---

## Quick Start

```python
import asyncio
from ERS import EnhancedReconsiderationSystem, MemoryBlock, ERSPromise

async def main():
ers = EnhancedReconsiderationSystem() # loads saved state if present

await ers.add_memory("Paris is the capital of France")
mem1 = MemoryBlock("Paris is the capital of France")

await ers.add_memory("Paris is the largest city in France")
mem2 = MemoryBlock("Paris is the largest city in France")

ers.chain_promises(ERSPromise(mem1), ERSPromise(mem2))

ers.defer_memory(mem1)
ers.defer_memory(mem2)

await ers.reconsider_deferred()
await ers.recursive_loop_check()
await ers.close() # saves state/backups

asyncio.run(main())
```

---

## Configuration & Persistence

- **State files**:
- `ers_state.json` — stores queues, memory-line slots, head pointers, and non-tensor data
- `lattice_state.safetensors` — contains lattice tensors and checkpoints
- **KG**: Graphiti (Neo4j recommended). Configure connection URIs and credentials in the `PMLL/PMLLConfig` class or via environment variables.
- **Mem0**: Configure IDs and storage backends according to Mem0 docs.
- Checkpointing is safetensor-based for speed and safety in production.

---

## API Summary

(Condensed; see code docstrings for full details)

**`EnhancedReconsiderationSystem`**
- `add_memory(text, metadata=None)` → adds `MemoryBlock`, processes embeddings, adds to KG/Mem0
- `defer_memory(memory_block, score=None)` → defers for reconsideration
- `reconsider_deferred()` → processes the deferred queue (async)
- `recursive_loop_check()` → performs multi-pass slot reconsideration (async)
- `chain_promises(*promises)` → link promise chain
- `close()` → saves JSON + safetensors

**`MemoryBlock`**
- `to_dict()`, `from_dict()` — serialization helpers
- Fields: `id`, `text`, `embedding`, `confidence`, `created_at`, `updated_at`, `sha256_hash`, `kg_id`, `mem0_id`

**`ERSPromise`**
- `resolve()`, `then(next_promise)` — promise chaining helpers

---

## Development & Testing

- Use virtual environments and pin dependencies when deploying.
- Tests should mock KG & Mem0 interfaces or run a local Neo4j + Mem0 test instance.
- Lattice operations can be checkpointed with safetensors for regression testing.

---

## Roadmap

- Replace stub callbacks with LLM-guided embeddings & reasoning (gRPC/HTTP options)
- Visualize KG evolution and ERS revision history (temporal graph animation)
- Define “axiom nodes” and lucidity flags for system self-awareness
- Add telos/ethical filter layer for narrative steering and safety

---

## Contributing

Contributions are welcome. Please:

1. Fork the repo
2. Create feature branches
3. Add tests and update docs
4. Open PRs with clear descriptions and rationale

If you contribute embedding backends, reasoning modules, or new ERS heuristics, document them and keep the double-loop semantics in mind.

---

## License

**MIT** — see [LICENSE](https://github.com/drqedwards/ERS/blob/main/LICENSE) file.

---

## Acknowledgements

Inspired by the "Enhanced Reconsideration" white paper and the work of cognitive-inspired memory & KG systems.
Authored by **Dr. Q (Josef Kurk Edwards / drQedwards)**.

---

**Related Resources**
- [TechRxiv Paper (Recursive Transformer Model)](https://www.techrxiv.org/users/856117/articles/1345789)
- [Kaggle PyTorch Model](https://www.kaggle.com/models/josefedwards/recursive-transformer-model/pyTorch)
- [PMLL Archive](https://github.com/drqedwards/PMLL_archive)

---

*Last updated: March 2026*
*Repository stars: 2 • Forks: 0*
```

**Copy and paste the above into a `README.md` file** — it is ready for GitHub, Kaggle model pages, or any documentation site. It incorporates the latest content from the official ERS repository while staying fully aligned with the Recursive Transformer Model (RTM) implementation. Let me know if you want a version tailored specifically for Hugging Face, a notebook-style README, or any custom sections added!

Files changed (1) hide show
  1. README.md +211 -0
README.md ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ **# Model Card for Recursive Transformer Model (RTM) / ERS PyTorch Implementation**
2
+
3
+ <!-- Provide a quick summary of what the model is/does. -->
4
+
5
+ This is the official PyTorch implementation of the **Recursive Transformer Model (RTM)**, a novel architecture that augments standard Transformer-based systems with **recursive memory reconsideration**, **temporal decay mechanisms**, and **Persistent Memory Logic Loops (PMLL)**. It addresses "nostalgic incorrectness" (the tendency of stateless AI to retain outdated or contradictory beliefs) by maintaining coherent, self-correcting state across inference sessions. The production-grade reference implementation is the **Enhanced Reconsideration System (ERS)** library, which includes PyTorch components for embeddings, lattice-based tensor routing, multi-petal attention, and knowledge-graph integration.<grok:render card_id="0ccb01" card_type="citation_card" type="render_inline_citation"><argument name="citation_id">1</argument></grok:render><grok:render card_id="f06887" card_type="citation_card" type="render_inline_citation"><argument name="citation_id">20</argument></grok:render>
6
+
7
+ The Kaggle-hosted PyTorch model provides the core RTM/ERS runtime (including `PMLLLattice`, `MemoryBlock`, temporal decay, consensus, and contradiction detection) for integration with any LLM/transformer stack. It is **not** a standalone pretrained language model but a stateful memory layer/framework.
8
+
9
+ ---
10
+
11
+ ## Model Details
12
+
13
+ ### Model Description
14
+
15
+ The Recursive Transformer Model (RTM) extends the classic Transformer architecture with:
16
+ - **Adaptive temporal decay** on memory confidence.
17
+ - **Multi-dimensional consensus** via embedding-space geometry and knowledge graphs.
18
+ - **Vector-based contradiction detection** with integrated rewrite capabilities.
19
+ - **Persistent Memory Logic Loops (PMLL)**: a lattice-based DAG for compressed, low-rank tensor routing and recursive passes over memory slots.
20
+
21
+ Key innovations solve the stateless limitation of standard transformers by enabling iterative, multi-pass reconsideration of beliefs during inference. The Enhanced Reconsideration System (ERS) is the complete, production-ready Python/PyTorch reference implementation.<grok:render card_id="c29ff8" card_type="citation_card" type="render_inline_citation"><argument name="citation_id">17</argument></grok:render><grok:render card_id="7edf74" card_type="citation_card" type="render_inline_citation"><argument name="citation_id">43</argument></grok:render>
22
+
23
+ - **Developed by:** Dr. Josef “Q.” Edwards (Josef Kurk Edwards / josefedwards / drQedwards), University of Colorado Boulder
24
+ - **Funded by [optional]:** U.S. Department of Defense (funder identifier 100000005)
25
+ - **Shared by [optional]:** Josef Edwards (via Kaggle and GitHub)
26
+ - **Model type:** Recursive Transformer extension / stateful memory framework (PMLL + ERS)
27
+ - **Language(s) (NLP):** Language-agnostic (works with any text/embedding-based input; primarily demonstrated on English factual/knowledge-base tasks)
28
+ - **License:** MIT (see ERS repository)
29
+ - **Finetuned from model [optional]:** Not finetuned; augments any base Transformer (integrates with sentence-transformers, LangChain, etc.)
30
+
31
+ ### Model Sources [optional]
32
+
33
+ - **Repository:** [Kaggle Model](https://www.kaggle.com/models/josefedwards/recursive-transformer-model/pyTorch) • [GitHub ERS (primary implementation)](https://github.com/drqedwards/ERS) • [GitHub PMLL_archive](https://github.com/drqedwards/PMLL_archive)
34
+ - **Paper:** Edwards, J. K. (2025). *The Recursive Transformer Model: Architecture, Theory, and Implementation with Persistent Memory Logic Loops*. TechRxiv. DOI: 10.36227/techrxiv.176118936.69886233/v1 (October 23, 2025)<grok:render card_id="a04172" card_type="citation_card" type="render_inline_citation"><argument name="citation_id">20</argument></grok:render>
35
+ - **Demo [optional]:** See ERS README quick-start example (async memory reconsideration loop)
36
+
37
+ ## Uses
38
+
39
+ ### Direct Use
40
+
41
+ Use as a drop-in memory layer for any Transformer/LLM pipeline:
42
+ - Add factual or conversational memories.
43
+ - Run recursive reconsideration loops (temporal decay → consensus → contradiction detection → optional rewrite).
44
+ - Persist state across sessions via JSON + safetensors.
45
+
46
+ Ideal for agents, chatbots, or knowledge-intensive applications that require long-term coherence.
47
+
48
+ ### Downstream Use [optional]
49
+
50
+ - Integrate with LangChain agents or any LLM stack via Graphiti/Mem0 knowledge graphs.
51
+ - Extend base models (e.g., Llama, Mistral) with stateful recursive passes.
52
+ - Use in production AI systems needing self-correction and belief updating.
53
+
54
+ ### Out-of-Scope Use
55
+
56
+ - Not intended as a standalone generative LLM.
57
+ - Not suitable for real-time low-latency inference without hardware acceleration (multiple recursive passes add compute).
58
+ - Avoid use in safety-critical systems without additional ethical/guardrail layers (rewrites can be LLM-guided).
59
+
60
+ ## Bias, Risks, and Limitations
61
+
62
+ - **Technical limitations:** Recursive loops increase inference-time compute; performance depends on embedding quality and KG backend (Neo4j recommended for Graphiti).
63
+ - **Sociotechnical risks:** Automated memory rewrites could propagate or amplify biases present in the underlying LLM or knowledge graph. Contradiction detection relies on embedding geometry and may miss subtle nuances.
64
+ - **Nostalgic incorrectness mitigation:** The core goal is to *reduce* outdated beliefs, but incorrect source data or poor consensus thresholds can still lead to erroneous updates.
65
+
66
+ ### Recommendations
67
+
68
+ Users should:
69
+ - Monitor rewrite logs and confidence deltas.
70
+ - Use high-quality, verified knowledge graphs.
71
+ - Apply domain-specific safety policies before committing rewrites.
72
+ - Test with synthetic contradictory memory scenarios to validate behavior.
73
+
74
+ ## How to Get Started with the Model
75
+
76
+ ```python
77
+ # Via Kaggle (PyTorch model) or direct from ERS GitHub
78
+ # Install dependencies (from ERS README)
79
+ # pip install torch sentence-transformers safetensors mem0-ai graphiti-core langchain langchain-community
80
+
81
+ import asyncio
82
+ from ERS import EnhancedReconsiderationSystem, MemoryBlock, ERSPromise # or load from Kaggle PyTorch weights
83
+
84
+ async def main():
85
+ ers = EnhancedReconsiderationSystem() # loads saved state if present
86
+
87
+ await ers.add_memory("Paris is the capital of France")
88
+ await ers.add_memory("Paris is the largest city in France") # contradictory example
89
+
90
+ await ers.reconsider_deferred()
91
+ await ers.recursive_loop_check() # performs RTM-style multi-pass reconsideration
92
+ await ers.close()
93
+
94
+ asyncio.run(main())
95
+ ```
96
+
97
+ Full usage and configuration in the [ERS GitHub README](https://github.com/drqedwards/ERS). The Kaggle PyTorch model loads the core `PMLLLattice` and related tensors.
98
+
99
+ ## Training Details
100
+
101
+ ### Training Data
102
+
103
+ None (this is an architectural extension/framework, not a pretrained LLM). It operates on top of any Transformer embeddings (e.g., via `sentence-transformers`). Memory content is user-provided or agent-generated.
104
+
105
+ ### Training Procedure
106
+
107
+ #### Preprocessing [optional]
108
+
109
+ Memory blocks are created with embeddings (via sentence-transformers), timestamps, confidence scores, and SHA-256 hashes. Optional KG indexing via Graphiti/Mem0.
110
+
111
+ #### Training Hyperparameters
112
+
113
+ - **Training regime:** Not applicable (no end-to-end training). Runtime inference uses PyTorch (fp32/bf16 supported via torch).
114
+ - Configuration options (RTM integration): `passes: 2`, `early_stop_cosine_delta: 0.002`, `max_rewrites_per_slot: 1`, `decay_alpha: 0.95`, adaptive λ decay rates, similarity threshold τ_sim, etc. (fully configurable in ERS).<grok:render card_id="355dea" card_type="citation_card" type="render_inline_citation"><argument name="citation_id">43</argument></grok:render>
115
+
116
+ #### Speeds, Sizes, Times [optional]
117
+
118
+ Real-time performance demonstrated in ERS (production-grade). Exact throughput depends on hardware, number of recursive passes, and KG backend. Lattice uses low-rank compression for scalability.
119
+
120
+ ## Evaluation
121
+
122
+ ### Testing Data, Factors & Metrics
123
+
124
+ No public benchmark datasets or quantitative results published in the preprint. Evaluation is qualitative/conceptual via synthetic contradictory memory scenarios (e.g., Paris facts example) and convergence metrics (confidence delta, rewrite count, cosine similarity shifts).
125
+
126
+ #### Factors
127
+
128
+ - Memory age, source quality, domain volatility, embedding similarity.
129
+
130
+ #### Metrics
131
+
132
+ - Nostalgic Incorrectness (NI) metric defined in paper.
133
+ - Consensus score, contradiction score, confidence update delta.
134
+
135
+ ### Results
136
+
137
+ [More Information Needed] — Paper focuses on theoretical framework and architectural feasibility rather than large-scale empirical benchmarks. ERS demonstrates real-time recursive reconsideration.
138
+
139
+ #### Summary
140
+
141
+ The model successfully maintains coherent state and resolves contradictions in controlled memory scenarios.
142
+
143
+ ## Model Examination [optional]
144
+
145
+ Interpretability is built-in: per-pass logs of embedding shifts, confidence changes, rewrite proposals, and KG updates. Visualize memory graph evolution (planned roadmap feature).
146
+
147
+ ## Environmental Impact
148
+
149
+ Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
150
+
151
+ - **Hardware Type:** [More Information Needed] (tested on standard CPU/GPU with PyTorch)
152
+ - **Hours used:** [More Information Needed]
153
+ - **Cloud Provider:** [More Information Needed]
154
+ - **Compute Region:** [More Information Needed]
155
+ - **Carbon Emitted:** [More Information Needed]
156
+
157
+ ## Technical Specifications [optional]
158
+
159
+ ### Model Architecture and Objective
160
+
161
+ - Base: Transformer stack with augmented embedding layer and reconsideration head.
162
+ - Key equations: temporal decay \( \text{conf}_i(t) = \text{conf}_i(0) \cdot e^{-\lambda_i (t - t_i)} \cdot \dots \), consensus scoring, integrated confidence update, PMLL lattice (DAG with quantization and low-rank compression).
163
+ - Objective: Stateful, self-correcting memory across sessions.
164
+
165
+ ### Compute Infrastructure
166
+
167
+ #### Hardware
168
+
169
+ Standard PyTorch-compatible (CPU/GPU).
170
+
171
+ #### Software
172
+
173
+ Python 3.8+, PyTorch, sentence-transformers, safetensors, mem0-ai, graphiti-core, LangChain.
174
+
175
+ ## Citation [optional]
176
+
177
+ **BibTeX:**
178
+ ```bibtex
179
+ @article{edwards2025recursive,
180
+ author = {Edwards, Josef Kurk},
181
+ title = {The Recursive Transformer Model: Architecture, Theory, and Implementation with Persistent Memory Logic Loops},
182
+ journal = {TechRxiv},
183
+ year = {2025},
184
+ month = {October},
185
+ doi = {10.36227/techrxiv.176118936.69886233/v1},
186
+ url = {https://www.techrxiv.org/users/856117/articles/1345789}
187
+ }
188
+ ```
189
+
190
+ **APA:**
191
+ Edwards, J. K. (2025). The Recursive Transformer Model: Architecture, Theory, and Implementation with Persistent Memory Logic Loops. TechRxiv. https://doi.org/10.36227/techrxiv.176118936.69886233/v1
192
+
193
+ ## Glossary [optional]
194
+
195
+ - **PMLL**: Persistent Memory Logic Loop — lattice-based memory compression and routing.
196
+ - **ERS**: Enhanced Reconsideration System — production Python/PyTorch library.
197
+ - **Nostalgic Incorrectness**: Retention of outdated/conflicting beliefs in stateless models.
198
+
199
+ ## More Information [optional]
200
+
201
+ - Full paper and math: TechRxiv preprint.
202
+ - Live implementation: [ERS GitHub](https://github.com/drqedwards/ERS).
203
+ - Related work: Hybrid TRM-RTM model, PMLL P=NP proof paper (separate preprint).
204
+
205
+ ## Model Card Authors [optional]
206
+
207
+ Compiled by Dr Q based on public sources from Josef Edwards / Dr. Q.
208
+
209
+ ## Model Card Contact
210
+
211
+ Josef Edwards (Kaggle: josefedwards, GitHub: drqedwards, Email: joed6834@colorado.edu) or open an issue on the ERS repository.