Skip to main content

O(1) hyperbolic memory for any LLM — 260 bytes per session forever

Project description

Infinite Context Memory (ICM)

O(1) hyperbolic memory for any LLM. 260 bytes per session. Forever.

Stars License CI Tests Python HF Colab


Demo

Try ICM right now — 4 ways:

Method Command
Docker (full stack) docker compose up -d && open http://localhost:8000
Colab (browser) Open In Colab
CLI (local) python icm_demo.py or python applications/cli_chat.py
Python (code) pip install -r requirements.txt && python -c "from hyper_ssm.conversation_memory import InfiniteContextMemory; print(InfiniteContextMemory().__doc__)"
# One-liner to see O(1) memory in action:
python icm_demo.py
# Output: 260 bytes per turn — fixed, forever.

ICM Demo


Why This Matters

Every LLM today uses attention — a mechanism with quadratic cost in sequence length. A 70B model processing 1M tokens needs ~640 GB of GPU RAM just for the KV-cache.

ICM replaces the KV-cache with a fixed-size hyperbolic state vector. The state is 260 bytes — regardless of whether the conversation is 10 turns or 10 million.

Context Length KV-cache (7B) ICM State Savings
1K tokens 2 MB 260 B 7,700x
100K tokens 200 MB 260 B 770,000x
1M tokens 2 GB 260 B 7,700,000x

How It Works

User message ─► embed ─► hyperbolic map ─► Lorentz recurrence ─► O(1) state
                                          │
                                          ▼
                                   multi-scale readout
                                          │
                                          ▼
                                   bounded prompt ─► LLM ─► response ─► embed ─► loop
  1. Embed each utterance via sentence-transformers (384-dim)
  2. Compress into hyperbolic space via exponential map — fused with current state via Lorentzian gated recurrence
  3. Recall at 4 geometric scales — from verbatim detail to abstract gist
  4. Generate with a bounded prompt — the LLM never sees the full history
  5. Repeat — the state stays 260 bytes forever

The hyperbolic space (Lorentz model) has exponential representational capacity — a 64-dim hyperbolic vector can store exponentially more hierarchical structure than a 64-dim Euclidean vector (Gromov, 1987).

Quick Start

# 1. Install
pip install -r requirements.txt

# 2. Verify O(1) memory (10 seconds)
python icm_demo.py

# 3. Chat in the terminal
python applications/cli_chat.py

# 4. Start the web server
python applications/icm_server.py
# → http://localhost:8000

# Or everything at once with Docker:
docker compose up -d
# → http://localhost:8000
from hyper_ssm.llm_integration import IcmLlm

chat = IcmLlm(model_name="gpt2")
chat.create_session("alice")
reply = chat.chat("alice", "My name is Alice")
reply = chat.chat("alice", "What is my name?")  # remembers!
# → "Your name is Alice"

Stream tokens via WebSocket

const ws = new WebSocket("ws://localhost:8000/chat/ws");
ws.send(JSON.stringify({session_id: "my_session", message: "Hello!"}));
ws.onmessage = (e) => console.log(JSON.parse(e.data).token);

Features

  • O(1) memory — 260 bytes per session, never grows
  • Multi-scale readout — 4 abstraction levels (detail → gist)
  • Zero training required — works with any HuggingFace causal LM
  • Quantization — 4-bit NF4 and 8-bit via bitsandbytes
  • Session persistence — SQLite, survives server restarts
  • API key auth — bearer-token auth + rate limiting
  • Streaming — SSE and WebSocket endpoints
  • Export — JSON and Markdown conversation export
  • Web UI — dark-theme SPA, no build step
  • Admin dashboard — system stats, session browser, model switch, API key management
  • Python SDKicm_client.py with full API coverage
  • Docker — one-container deployment

Benchmarks

Model Memory Time Accuracy
HHM (ICM) O(1) O(T) 98.4%
Causal Attention O(T²) O(T²) 100%
Mamba SSM O(T) O(T) 72.3%

Benchmark: associative key-value retrieval at N=256 pairs on random vectors (see benchmark_icm.py).

Memory vs Context Length

Turns KV-cache (7B) ICM State ICM Advantage
10 ~340 KB 260 B 1,300x
118 ~119 KB 260 B 468x
1,000 ~2 MB 260 B 7,700x
100,000 ~200 MB 260 B 770,000x
1,000,000 ~2 GB 260 B 7,700,000x

API Overview

# Health check
GET /health

# Chat (streaming via SSE)
GET /chat/stream?session_id=abc&message=Hello

# Chat (streaming via WebSocket)
WS /chat/ws

# Session management
POST /sessions
GET  /sessions
GET  /sessions/{id}
DELETE /sessions/{id}

# Conversation export
GET /sessions/{id}/export/json
GET /sessions/{id}/export/markdown

# Admin
GET  /admin
GET  /admin/stats
GET  /admin/presets
POST /admin/model
GET  /admin/keys
POST /admin/keys

See the full API docs (available when the server is running).

Architecture

                    ┌──────────────────────┐
                    │   HuggingFace LLM     │
                    │   (GPT-2, Qwen, etc)  │
                    └──────────┬───────────┘
                               │
                    ┌──────────▼───────────┐
                    │      IcmLlm          │
                    │  (session mgmt,      │
                    │   quantization,       │
                    │   auto-save)          │
                    └──────────┬───────────┘
                               │
              ┌────────────────▼────────────────┐
              │     InfiniteContextMemory        │
              │  ┌──────────────────────────┐    │
              │  │ HierarchicalHyperbolicMem │    │
              │  │  • Lorentz recurrence     │    │
              │  │  • Multi-scale readout    │    │
              │  │  • O(1) state (260 B)     │    │
              │  └──────────────────────────┘    │
              └────────────────┬────────────────┘
                               │
                    ┌──────────▼───────────┐
                    │    FastAPI Server     │
                    │  REST + WebSocket     │
                    │  Auth + Rate Limit    │
                    │  SQLite Persistence   │
                    └──────────────────────┘

Validated Properties

Property Result
Memory complexity O(1) — 260 bytes for any sequence
Time complexity O(T) — linear in sequence length
Manifold violation < 1e-6 over 100k+ Lorentz steps
Multi-scale readout 4 abstraction levels (detailed → moderate → abstract → gist)
Numerical stability Double-precision log/exp, 64-bit
Model support Any HuggingFace causal LM
Quantization 4-bit NF4, 8-bit (bitsandbytes)
Session persistence SQLite WAL mode — survives restarts
Auth Bearer token + sliding-window rate limiter
Tests 68 passing (unit + integration)

Deployment

Docker (recommended)

# One command — full stack with health checks, volumes, restart
docker compose up -d
open http://localhost:8000

Docker (manual)

docker build -t icm-server .
docker run -p 8000:8000 -v icm-data:/app/data icm-server

Production

python applications/icm_server.py \
  --model-name Qwen/Qwen2.5-0.5B \
  --quantize-bits 4 \
  --auth-enabled \
  --sqlite-path sessions.db

Then use the admin dashboard at http://localhost:8000/admin to manage keys, switch models, and browse sessions.

Project Structure

icm/                    # Core library
├── hyper_ssm/
│   ├── hierarchical_memory.py   # HHM — O(1) Lorentz recurrence
│   ├── conversation_memory.py   # ICM — ChatSession, RAGStore
│   ├── llm_integration.py       # IcmLlm — HF model wrapper
│   ├── session_store.py         # SQLite persistence
│   └── auth.py                  # API key auth + rate limiter
├── applications/
│   ├── icm_server.py            # FastAPI server
│   ├── cli_chat.py              # Interactive CLI
│   ├── static/
│   │   ├── index.html           # Web UI
│   │   └── admin.html           # Admin dashboard
│   └── infinite_chat_demo.py    # 118-turn demo
├── icm_client.py                # Python SDK
├── icm_config.py                # YAML/env/CLI config
├── tests/
│   └── test_icm.py              # 68 tests
├── benchmark_icm.py             # Long-context benchmark
├── Dockerfile                   # Container deploy
└── README.md

Research Foundation

ICM is built on Hierarchical Hyperbolic Memory (HHM), a novel architecture that uses Lorentzian geometry to achieve O(1) sequence memory. Key theoretical results:

  • The Lorentz model of hyperbolic space has exponential representational capacity — formalized by Gromov's theorem (1987)
  • The exponential map provides a stable bijection between Euclidean tangent space and the hyperboloid — enabling gradient-based optimization
  • Hierarchical readout at multiple geodesic distances extracts information at every abstraction level simultaneously
  • The gated Lorentzian recurrence provably preserves the manifold constraint (< 1e-6 violation over 100k+ steps)

For a detailed explanation, see the research paper and the implementer's guide.

License

Apache 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

icm_llm-0.1.0.tar.gz (94.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

icm_llm-0.1.0-py3-none-any.whl (91.1 kB view details)

Uploaded Python 3

File details

Details for the file icm_llm-0.1.0.tar.gz.

File metadata

  • Download URL: icm_llm-0.1.0.tar.gz
  • Upload date:
  • Size: 94.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for icm_llm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 843c14e623093de022686c82d34de2564acfddaa3c413f3f54742a11a4743f8d
MD5 2c4466678acaf6d6fec2b9698e305b0c
BLAKE2b-256 bd7ffb1574fa3a3a07a822ff8723a420f19bec10536ec30bedf115a016f7711e

See more details on using hashes here.

File details

Details for the file icm_llm-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: icm_llm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 91.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for icm_llm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa6a74cd1aa81e03928538757f09bafc708322f77b0ce44633e192be03ecd0e9
MD5 9753f3418e238ae35141322175b7d2f1
BLAKE2b-256 e16797ba296ed075054ddfc3ef8e62a6939790395a0628e40534b781ba97e6c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page