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 PyPI Downloads


Demo

Try ICM right now — 4 ways:

Method Command
pip pip install icm-llm
Docker (full stack) docker compose up -d && open http://localhost:8000
Colab (browser) Open In Colab
CLI (local) icm-demo or icm-chat or icm-server
Python (code) pip install icm-llm && python -c "from hyper_ssm.conversation_memory import InfiniteContextMemory; m = InfiniteContextMemory(); print(f'{m.memory_size_bytes}B fixed-state O(1) memory')"
# One-liner to see O(1) memory in action:
pip install icm-llm && icm-demo
# 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 (60 seconds)
pip install icm-llm

# 2. Verify O(1) memory (10 seconds)
icm-demo

# 3. Chat in the terminal
icm-chat

# 4. Start the web server
icm-server
# → 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.1.tar.gz (95.4 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.1-py3-none-any.whl (91.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: icm_llm-0.1.1.tar.gz
  • Upload date:
  • Size: 95.4 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.1.tar.gz
Algorithm Hash digest
SHA256 c11ec5f2c43a4ae44e2b48e6497f5e70875fef8c25399a7222cfe93a4e49ae7c
MD5 558e42981e862c8d88daa739273cd07a
BLAKE2b-256 6463c0bae75b6fa0513fd264aa66fb1af44aefc51853075e93e55922ad23fe27

See more details on using hashes here.

File details

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

File metadata

  • Download URL: icm_llm-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 91.9 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 91778df9249d6ab0cc352f2e234270efb47392f34f889343b0974212722ee145
MD5 e96a3200fd6d9762f446bbc01c1c29a6
BLAKE2b-256 fc886c7e582b178683ada12dab20e0769e69253a1c198a83c7efcf0829725236

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