Instructions to use tak633b/memt-4b-research-poc with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use tak633b/memt-4b-research-poc with MLX:
# Make sure mlx-lm is installed # pip install --upgrade mlx-lm # if on a CUDA device, also pip install mlx[cuda] # Generate text with mlx-lm from mlx_lm import load, generate model, tokenizer = load("tak633b/memt-4b-research-poc") prompt = "Once upon a time in" text = generate(model, tokenizer, prompt=prompt, verbose=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- MLX LM
How to use tak633b/memt-4b-research-poc with MLX LM:
Generate or start a chat session
# Install MLX LM uv tool install mlx-lm # Generate some text mlx_lm.generate --model "tak633b/memt-4b-research-poc" --prompt "Once upon a time"
- Atomic Chat
MemT-4B Research PoC: Dynamic Parametric Memory Adapters for Gemma 4 E4B & Qwen3.5-4B
Unofficial Research Prototype / 非公式・研究検証用プロトタイプ
This repository is an independent small-model research experiment inspired by MemTensor/MemT-1.0 and its core idea of memory-native LLMs whose internal memory state updates dynamically as conversations unfold. We implemented MemT-style gated-delta memory adapters on two 4B-class open models — Gemma 4 E4B (
google/gemma-4-E4B-it) and Qwen3.5-4B (Qwen/Qwen3.5-4B) — with both PyTorch (chunked WY/UT) and Apple Silicon MLX (gated_delta_kernel) runtimes to test what is possible (and where the boundaries are) with lightweight post-training alone (2,000 synthetic Japanese conversations, no continued pre-training).This project is not affiliated with or endorsed by MemTensor, Google, or Alibaba/Qwen. Only independently trained MemT adapter weights (
.safetensors) and code are distributed here; base model weights are not redistributed.
What Is Included
| Adapter File | Base Model | Memory Params | Write Layers | Read-Only Layers | Memory Heads | Precision | Size |
|---|---|---|---|---|---|---|---|
weights/gemma4-e4b-memt-v8.safetensors |
google/gemma-4-E4B-it |
146.5M | [5, 11, 17, 23] |
[29, 35, 41] (KV-shared) |
2 (GQA 8→2, d=512) |
fp32 |
559 MB |
weights/qwen3.5-4b-memt-v8.safetensors |
Qwen/Qwen3.5-4B |
188.8M | [3, 7, 11, 15, 19, 23, 27, 31] |
none | 4 (GQA 16→4, d=256) |
fp32 |
720 MB |
Each .safetensors file loads directly in both PyTorch and Apple Silicon MLX (no duplicate .pt/.npz files required).
Repository Files
memt_block.py— Standalone PyTorch MemT memory block with exact 64-token chunked WY/UT updates, Gemma 4MEMT_SHARED_READsupport, early-exitcommit(), and multimodal tower pruning.memt_mlx.py— Standalone Apple Silicon MLX runtime usingmlx_lm's Metalgated_delta_kernel(includes automaticstrict=Falsefallback for stockgoogle/gemma-4-E4B-it's unused KV-shared weights).quickstart_pytorch.py/quickstart_mlx.py— Minimal ~30-line demos: commit conversation history → discard text → answer purely from memory state.make_data.py/train_poc.py/scoring.py/eval_poc.py/eval_locomo_mlx.py— Full synthetic Japanese data generator, teacher top-64 KL distillation training script, and evaluation scripts (including official LoCoMo).weights/config.json— Structured metadata for both adapters.
Key Architectural & Empirical Findings
- Gemma 4 KV-Shared Upper Layers (
MEMT_SHARED_READ=1): Gemma 4 E4B shares K/V projections across its upper layers (24..41). Placing MemT blocks only on the 4 lower full-attention layers ([5, 11, 17, 23]) yielded only 7.5% short-recall accuracy because the upper half of the network could not read from memory. Adding lightweight read-only MemT read heads on the upper full-attention layers ([29, 35, 41]) reading from layer 23's memory state jumped accuracy to 96.0% while saving 141 MB of unused write parameters. (Note: In a separate ablation, enabling independent writes on[29, 35, 41]initialized from layer 23's write weights collapsed at 2K scale because upper-layer hidden representations were misaligned with layer 23's feature space.) - Gemma 4 E4B vs. Qwen3.5-4B at 4B Scale:
- Gemma 4 E4B wins decisively on Japanese short recall (96.0% vs 82.5%), unseen fact types (68.3% vs 51.7%), hard no-lexical-overlap holdout templates (50–83% vs 33–42%), and non-memory preservation (82.5% vs 70.0%).
- Qwen3.5-4B (8 full-attention write layers across the full depth, 4 KV heads) edges ahead on long-horizon distraction (
dist100/dist400) and zero-shot cross-lingual English LoCoMo (F10.0726 → 0.1503, temporal14.3×lift).
- Cross-Lingual Zero-Shot Transfer:
Despite training on 100% synthetic Japanese data (2,000 items), both adapters show clear 2.1×–2.6× Token-F1 gains over
noneon 16K–25K-token English conversations from LoCoMo, confirming that the learned gated-delta memory read/write mechanism transfers across languages — though absolute accuracy remains far below full-scale continued pre-training + English post-training.
Evaluation Results (bf16 on Apple Silicon MLX)
All evaluations were run identically in bf16 on stock base models (google/gemma-4-E4B-it and Qwen/Qwen3.5-4B), comparing:
none: Question only (no conversation history, empty memory)memory(MemT): History committed viacommit(), then text history completely discarded before answeringfull: Full text history kept in the prompt context
1. Japanese Memory & Generalization Suites (6 Benchmarks)
| Benchmark | Split / Metric | Gemma 4 E4B (none / MemT / full) |
Qwen3.5-4B (none / MemT / full) |
|---|---|---|---|
Short Recall (eval_short, 200q) |
Seen fact types | 0.0% / 96.0% / 100.0% | 0.0% / 82.5% / 99.0% |
Long Context (eval_long, 30q each) |
dist20 (~500 tok) |
0 / 27 / 30 | 0 / 24 / 30 |
dist100 (~2.5K tok) |
0 / 14 / 29 | 0 / 15 / 30 | |
dist400 (~10K tok) |
0 / 1 / 30 | 0 / 3 / 30 | |
many15 (15 facts at once) |
0 / 7 / 30 | 1 / 4 / 30 | |
para (paraphrased) |
0 / 28 / 30 | 0 / 23 / 30 | |
nolex (zero lexical overlap) |
0 / 21 / 10 | 0 / 23 / 19 | |
Unseen Fact Types (eval_kinds) |
kinds (60q, unseen types) |
0 / 41 (68.3%) / 52 | 0 / 31 (51.7%) / 57 |
kinds_absent (30q, unanswerable) |
17 / 7 (23.3%) / 19 | 10 / 5 (16.7%) / 20 | |
Non-Memory Tasks (eval_pollution, 40q) |
General instruction retention | 87.5% / 82.5% / 87.5% | 75.0% / 70.0% / 82.5% |
| Holdout A (unseen nolex + absent) | hold_nolex (/12) / absent (/6) |
0, 6 / 6, 0 / 10, 5 | 0, 3 / 4, 0 / 11, 3 |
| Holdout B (unseen nolex + absent) | hold_nolex (/12) / absent (/6) |
0, 5 / 10, 1 / 7, 5 | 1, 2 / 5, 0 / 9, 3 |
2. Zero-Shot Cross-Lingual Evaluation: Official LoCoMo (English)
Evaluated on the first 3 conversations of snap-research/locomo (locomo10.json, 16K–25K tokens per conversation, 385 questions across Categories 1–4; excluding adversarial Category 5, matching MemT-1.0 Appendix D.1). Reported in official LoCoMo Normalized Token F1 (and normalized substring recall Sub).
Note: Both adapters were trained exclusively on 2,000 synthetic Japanese items without any English memory data or continued pre-training.
| LoCoMo Category | N | Gemma 4 E4B (none → MemT) |
Qwen3.5-4B (none → MemT) |
|---|---|---|---|
| Overall (Cat 1–4) | 385 | F1 0.0434 → 0.1109 (2.55×) / Sub 1.4% → 4.6% |
F1 0.0726 → 0.1503 (2.07×) / Sub 2.1% → 7.1% |
| Single-hop (Cat 4) | 200 | F1 0.0651 → 0.1228 |
F1 0.1021 → 0.1294 |
| Temporal (Cat 2) | 90 | F1 0.0038 → 0.0964 (25×) |
F1 0.0161 → 0.2297 (14×) |
| Multi-hop (Cat 1) | 74 | F1 0.0369 → 0.0773 |
F1 0.0565 → 0.1043 (Sub 3.2% → 12.4%) |
| Open-domain (Cat 3) | 21 | F1 0.0304 → 0.1774 |
F1 0.0908 → 0.1716 |
Honest Limitations & Intended Use
These adapters are strictly for research and experimentation — NOT for production use.
- Confident Hallucination on Unanswerable Questions: When asked about facts absent from memory (
kinds_absent,hold_absent), both 4B models frequently confabulate plausible-looking entities instead of abstaining (0–23%refusal vs.55–85%without memory). At 2K post-training scale, noisy readout activations bias the model toward guessing. - Capacity & Long-Horizon Decay: Accuracy drops sharply around 10K distraction tokens (
dist400: 1–3/30) and 15 simultaneous facts (many15: 4–7/30). For reliable factual recall in practical applications, external structured memory / hybrid RAG (BM25 + dense embeddings) still decisively outperforms small-scale parametric memory. - Training Scale: Only 2,000 synthetic Japanese teacher-distilled conversations (forward KL on top-64 teacher logits, initialized from an earlier
v4checkpoint) were used, with no continued pre-training (CPT) stage.
Quickstart
PyTorch (CUDA)
pip install torch transformers accelerate safetensors
# Gemma 4 E4B (MEMT_SHARED_READ=1 is required for KV-shared upper layers)
MEMT_SHARED_READ=1 python quickstart_pytorch.py \
google/gemma-4-E4B-it weights/gemma4-e4b-memt-v8.safetensors
# Qwen3.5-4B
python quickstart_pytorch.py \
Qwen/Qwen3.5-4B weights/qwen3.5-4b-memt-v8.safetensors
Apple Silicon (MLX)
pip install mlx mlx-lm
# Gemma 4 E4B
python quickstart_mlx.py \
google/gemma-4-E4B-it weights/gemma4-e4b-memt-v8.safetensors
# Qwen3.5-4B
python quickstart_mlx.py \
Qwen/Qwen3.5-4B weights/qwen3.5-4b-memt-v8.safetensors
Training Details
- Objective: Forward KL distillation against the frozen base model's top-64 full-context logits (
T=1.0,lr=1e-4, AdamWwd=0.01, cosine schedule,grad_clip=1.0, batch size 8, ~200 steps on a single NVIDIA L4 24GB GPU). - Data (
make_data.py --v 8 --n 2000 --seed 1): 100% synthetic Japanese conversations with randomly composed names/locations/codes/dates, mixing direct recall, no-lexical-overlap recall, unanswerable (absent) questions, and memory-unrelated general instructions (free). Filtered teacher counts: 1,632 / 2,000 for Gemma 4 E4B; 1,593 / 2,000 for Qwen3.5-4B. - Initialization: Continued from
v4adapters (which were initialized from backbone Q/K/V projections and MemT paper Appendix B.2 gate initialization).
日本語概要
本リポジトリは、MemTensor / MemT-1.0 の「会話を通じてモデル自身の記憶状態が動的に更新される(Memory-Native LLM)」という仕組みにインスパイアされ、4B クラスの小型オープンモデル(Gemma 4 E4B / Qwen3.5-4B)でどの程度機能し、どこに限界があるかを検証する研究用テスト(非公式 PoC) として作成しました。
- 配布するもの:独自に学習した MemT 記憶アダプタ重み(
.safetensors)と、PyTorch(チャンク化 WY/UT)+ Apple Silicon MLX(Metalgated_delta_kernel)の推論・学習コード一式のみです(ベースモデルの重みは再配布していません)。 - 主な発見:
- Gemma 4 特有の KV 共有上位層への対応(
MEMT_SHARED_READ=1):下位 4 層だけに記憶を置くと短期正解率は 7.5% に留まりましたが、KV 共有する上位 full-attention 層(29・35・41層)に読み出し専用ヘッドを追加することで 96.0% まで向上しました。 - 2モデルの特性差:日本語の短期想起・未見タスクへの汎化・非汚染性では Gemma 4 E4B が優勢(短期 96.0% / 未見型 68.3% / 非汚染 82.5%)。一方、長文の雑談耐性や日本語のみ学習からの 英語 LoCoMo ゼロショット転移(16K〜25K トークン)では、全層にわたり 8 層の独立書き込みを持つ Qwen3.5-4B が上回りました(LoCoMo F1
0.0726 → 0.1503)。 - 小型・小規模学習の明確な限界:2,000 件の事後学習のみ(CPT なし)では、「聞いていない質問」に対して根拠なく答えを作ってしまう傾向(断り率の低下)や、10K トークン・15 事実同時保持での急落が残ります。実務用途では依然として外部記憶+RAG が現実的であり、本アダプタは研究・実験用途として公開しています。
- Gemma 4 特有の KV 共有上位層への対応(
Citation & Credits
- Inspired by MemTensor / MemT-1.0 (Memory-Native Models /
MemTensor/MemT-1.0-Nano). - Base models:
google/gemma-4-E4B-it(subject to Gemma Terms of Use) andQwen/Qwen3.5-4B(Apache-2.0). - LoCoMo benchmark: Maharana et al., Evaluating Very Long-Term Conversational Memory of LLM Agents (
snap-research/locomo).
Quantized