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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b75d8ec
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
"""

Titans + MIRAS Demo: A Brain That Changes Itself While Thinking



This application demonstrates test-time learning using:

- Titans: Test-time training framework

- MIRAS: Associative memory with retention gate

"""

import torch
import torch.nn as nn
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr

from miras_memory import MIRASMemory
from projections import KeyProjection, ValueProjection, OutputProjection
from memory_store import MemoryStore

print("=" * 50)
print("===== Application Startup at", __import__('datetime').datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "=====")
print("=" * 50)
print()

# ========== Configuration ==========
MODEL_NAME = "distilgpt2"
HIDDEN_DIM = 768  # distilgpt2 hidden dimension
MEMORY_DIM = 256  # Memory space dimension
LEARNING_RATE = 0.01  # Increased learning rate for faster adaptation
MAX_NEW_TOKENS = 50  # Max tokens to generate
MEMORY_ALPHA = 1.0  # Increased from 0.1 - stronger memory influence
NUM_TRAIN_STEPS = 5  # Multiple gradient steps per input for better learning

# ========== Initialize Components ==========
print("๐Ÿง  Initializing Titans + MIRAS brain...")

# Load base language model (frozen)
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
model.eval()  # Frozen - no training

# Create projection layers
key_proj = KeyProjection(HIDDEN_DIM, MEMORY_DIM)
value_proj = ValueProjection(HIDDEN_DIM, MEMORY_DIM)
output_proj = OutputProjection(MEMORY_DIM, HIDDEN_DIM)  # Map memory back to hidden dim

# Create memory module
memory = MIRASMemory(memory_dim=MEMORY_DIM, init_scale=0.01)

# Load persistent memory
store = MemoryStore(save_dir="memory")
store.load(memory)

print("โœ… Brain initialized!")


# ========== Chat Function ==========
def chat(message, history):
    """

    Main chat function for gr.ChatInterface.

    

    Args:

        message: str - user's current message

        history: list of dicts with 'role' and 'content' keys

        

    Returns:

        str - assistant's response with memory stats

    """
    if not message.strip():
        return "Please enter a message."
    
    # === Step 1: Extract hidden states from input ===
    inputs = tokenizer(message, return_tensors="pt", padding=True)
    
    with torch.no_grad():
        outputs = model(
            **inputs,
            output_hidden_states=True
        )
    
    # Get last hidden state of the last token
    h_last = outputs.hidden_states[-1][:, -1, :]  # (1, hidden_dim)
    
    # === Step 2: Test-time memory learning with LANGUAGE MODELING loss ===
    # Key insight: Train memory to help predict next tokens, not just map kโ†’v
    
    # Get ALL hidden states (not just last token) for training
    all_hidden = outputs.hidden_states[-1]  # (1, seq_len, hidden_dim)
    seq_len = all_hidden.shape[1]
    
    if seq_len > 1:
        # We have context - train on predicting each next token
        # Run multiple training steps for faster learning
        for train_step in range(NUM_TRAIN_STEPS):
            with torch.enable_grad():
                total_lm_loss = 0.0
                
                # For each position (except last), predict next token
                for pos in range(seq_len - 1):
                    h_pos = all_hidden[:, pos, :]  # Hidden at position pos
                    
                    # Project to memory space
                    k = key_proj(h_pos)
                    
                    # Query memory and augment hidden state
                    memory_out = memory(k)
                    h_augmented = h_pos + MEMORY_ALPHA * output_proj(memory_out)
                    
                    # Compute logits for next token
                    logits = model.lm_head(h_augmented)  # (1, vocab_size)
                    
                    # Target is the actual next token
                    target = inputs['input_ids'][:, pos + 1]
                    
                    # Cross-entropy loss
                    lm_loss = nn.functional.cross_entropy(logits, target)
                    total_lm_loss = total_lm_loss + lm_loss
                
                # Average loss over positions
                memory_loss = total_lm_loss / (seq_len - 1)
                
                # Get retention factor
                retention = memory.retention_gate(memory_loss)
                effective_lr = LEARNING_RATE * retention
                
                # Backprop and update
                memory_loss.backward()
                
                with torch.no_grad():
                    # Update memory
                    if memory.W.grad is not None:
                        memory.W -= effective_lr * memory.W.grad
                        memory.W.grad.zero_()
                    
                    # Update output projection
                    if output_proj.projection.weight.grad is not None:
                        output_proj.projection.weight -= effective_lr * output_proj.projection.weight.grad
                        output_proj.projection.weight.grad.zero_()
        
        # Update stats after all training steps (use final loss)
        with torch.no_grad():
            memory.update_stats(memory_loss)
    else:
        # Single token - just compute MSE for stats
        with torch.no_grad():
            k = key_proj(h_last)
            v = value_proj(h_last)
            memory_pred = memory(k)
            memory_loss = ((memory_pred - v) ** 2).mean()
            retention = 1.0
            memory.update_stats(memory_loss)
    
    # === Step 3: Memory-augmented generation ===
    # Token-by-token generation where memory influences hidden states
    # Key insight: h' = h + alpha * output_proj(memory(k))
    
    generated_ids = inputs['input_ids'].clone()
    
    with torch.no_grad():
        for _ in range(MAX_NEW_TOKENS):
            # Forward pass to get hidden states
            outputs = model(generated_ids, output_hidden_states=True)
            h_last = outputs.hidden_states[-1][:, -1, :]  # (1, hidden_dim)
            
            # Query memory with projected key
            k_gen = key_proj(h_last)
            memory_out = memory.query(k_gen)  # (1, memory_dim)
            
            # Augment hidden state: h' = h + alpha * output_proj(memory(k))
            h_augmented = h_last + MEMORY_ALPHA * output_proj(memory_out)
            
            # Compute logits with augmented hidden state
            logits = model.lm_head(h_augmented)  # (1, vocab_size)
            
            # Temperature sampling
            logits = logits / 0.8
            probs = torch.softmax(logits, dim=-1)
            next_token = torch.multinomial(probs, num_samples=1)
            
            # Stop on EOS
            if next_token.item() == tokenizer.eos_token_id:
                break
            
            # Append to sequence
            generated_ids = torch.cat([generated_ids, next_token], dim=1)
    
    response = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
    
    # Remove the input prompt from response
    if response.startswith(message):
        response = response[len(message):].strip()
    
    if not response:
        response = "..."
    
    # === Step 4: Save memory ===
    store.save(memory)
    
    # === Step 5: Format output with memory stats ===
    stats = memory.get_stats()
    
    memory_info = (
        f"\n\n---\n"
        f"**๐Ÿง  Memory Update**\n"
        f"- Loss: {memory_loss.item():.4f} (lower = better prediction)\n"
        f"- Retention: {retention:.2f}x (surprise factor)\n"
        f"- Total Updates: {stats['updates']}\n"
        f"- Avg Loss: {stats['avg_loss']:.4f}"
    )
    
    return response + memory_info


# ========== Gradio Interface ==========
print("๐Ÿš€ Launching Gradio interface...")

demo = gr.ChatInterface(
    fn=chat,
    title="๐Ÿง  The Brain That Learns While Thinking",
    description="""

    # A Living System That Updates Its Weights During Inference

    

    **The Novel Thing**: Standard LLMs freeze their weights after training. This system performs gradient descent *while you chat*.

    

    ---

    

    ## ๐Ÿš€ The Revolutionary Difference

    

    **Standard LLMs (ChatGPT, Claude, etc.)**: Think โ†’ Predict โ†’ **Forget**  

    **Titans + MIRAS**: Think โ†’ Predict โ†’ **Update** โ†’ **Remember** โ†’ Think Differently

    

    ---

    

    ### ๐Ÿ’ก What Makes This Different?

    

    | Feature | ChatGPT/Claude/GPT-4 | This Demo (Titans+MIRAS) |

    |---------|---------------------|--------------------------|

    | **Weights during chat** | ๐Ÿ”’ Frozen forever | โœ… Update with every message |

    | **Learning** | โŒ Simulated (in-context only) | โœ… Real (gradient descent) |

    | **Memory** | ๐Ÿ“ Token context only | ๐Ÿง  Neural parameters |

    | **Persistence** | โŒ Forgets when context ends | โœ… Saves to disk |

    | **Adaptation** | ๐ŸŽญ Acts like it learned | ๐Ÿ”ฌ Actually learns |

    

    ---

    

    ### ๐ŸŽฏ What You're Witnessing

    

    **This is NOT a better chatbot** - it's a **learning demonstrator**.

    

    1. **The text responses are random** - that's expected! We're using a small, frozen model (distilgpt2)

    2. **The MAGIC is in the numbers below** - watch the "Loss" decrease when you repeat inputs!

    3. **Every message physically changes the brain** - the memory weights update via gradient descent

    4. **Refresh the page** - the update count continues (memory persists!)

    

    ---

    

    ### ๐Ÿงช How It Works (The Technical Truth)

    

    ```

    Your Message

        โ†“

    [distilgpt2: FROZEN] โ† Not learning, just generating

        โ†“

    Hidden States (768-dim)

        โ†“

    [Projections] โ†’ Memory Space (256-dim)

        โ†“

    [MIRAS Memory: LEARNING!] โ† This is what updates!

        โ†“

    Loss = How surprised the memory is

        โ†“

    Gradient Descent โ†’ Memory weights change

        โ†“

    Saved to disk โ†’ Persists forever

    ```

    

    **Key Insight**: We're training the **memory**, not the text generator!

    

    ---

    

    ### ๐Ÿ”ฌ The Science: Why This Matters

    

    **Standard LLMs**:

    - Weights frozen after training (costs millions)

    - "Learning" is just pattern matching in context

    - Forget everything when context ends

    - Same model for everyone

    

    **Titans + MIRAS**:

    - Weights update during inference (free!)

    - Real optimization via gradient descent

    - Memory persists across sessions

    - Personalizes to each user

    

    **This is test-time learning** - the future of adaptive AI.

    

    ---

    

    ### ๐Ÿ“Š What the Stats Mean

    

    - **Loss**: How surprised the memory is (lower = more familiar)

    - **Retention**: Learning rate multiplier (2.0x = very surprising, 0.5x = familiar)

    - **Updates**: Total number of memory updates (persists across sessions!)

    - **Avg Loss**: Overall learning progress

    

    ---

    

    ### ๐ŸŽฎ Try This Experiment

    

    1. **Send "hello world" 5 times** โ†’ Watch loss decrease!

    2. **Send something completely different** โ†’ Loss spikes!

    3. **Refresh the page and send another message** โ†’ Update count continues!

    

    **That decreasing loss is proof the neural weights are changing!**

    

    ---

    

    ### ๐ŸŒŸ The Bottom Line

    

    **ChatGPT**: A frozen calculator that *simulates* adaptation  

    **This Demo**: A living system that *performs* adaptation

    

    You're not chatting with a model.  

    **You're watching a brain rewire itself in real-time.** ๐Ÿง โšก

    

    ---

    

    ### ๐Ÿงช How to Test This (Interactive Experiments)

    

    **Don't just chatโ€”run experiments to see the learning happen!**

    

    #### Experiment 1: Watch Loss Decrease (Proof of Learning)

    ```

    1. Send "hello world" 

    2. Send "hello world" again

    3. Send "hello world" again

    4. Send "hello world" again

    5. Send "hello world" again

    ```

    **What to watch**: Loss should decrease each time (7.5 โ†’ 6.0 โ†’ 5.0 โ†’ 4.0)  

    **Why it matters**: This proves the memory is learning the pattern!

    

    #### Experiment 2: Trigger Surprise (Spike the Loss)

    ```

    1. Send "hello world" 5 times (loss decreases)

    2. Then send: "Supercalifragilisticexpialidocious quantum entanglement"

    ```

    **What to watch**: Loss should spike back up (4.0 โ†’ 9.0+)  

    **Why it matters**: The memory detects noveltyโ€”it knows this is different!

    

    #### Experiment 3: Test Persistence (Memory Survives)

    ```

    1. Note the "Updates" count (e.g., 15)

    2. Refresh this page completely

    3. Send any message

    4. Check if Updates = 16 (not reset to 1!)

    ```

    **What to watch**: Update count should continue, not reset  

    **Why it matters**: Memory persists to diskโ€”it's not just in RAM!

    

    ---

    

    ### ๐Ÿ“Š What Each Stat Means (Decoder Ring)

    

    **Loss** (e.g., 7.48 โ†’ 6.61 โ†’ 5.23)

    - **What it is**: Prediction error (how surprised the memory is)

    - **Lower = Better**: Memory is familiar with this pattern

    - **Higher = Novel**: Memory hasn't seen this before

    - **Why it matters**: Decreasing loss = learning is happening!

    

    **Retention** (e.g., 2.00x)

    - **What it is**: Learning rate multiplier based on surprise

    - **2.0x = Very surprising**: Memory learns aggressively

    - **0.5x = Very familiar**: Memory learns slowly (you won't see this yet)

    - **Why it matters**: The brain learns more from surprising events (like humans!)

    

    **Updates** (e.g., 1 โ†’ 2 โ†’ 3 โ†’ 4...)

    - **What it is**: Total number of memory updates

    - **Persists across sessions**: Survives page refreshes

    - **Never resets**: Keeps counting forever

    - **Why it matters**: Proof that memory is persistent, not ephemeral!

    

    **Avg Loss** (e.g., 7.26)

    - **What it is**: Running average of all losses

    - **Trends downward**: As memory learns recurring patterns

    - **Reflects overall learning**: Lower = memory is getting smarter

    - **Why it matters**: Shows long-term learning progress!

    

    ---

    

    ### โš ๏ธ What to Ignore (Important!)

    

    **The text responses are random and bad** - this is expected!

    - We're NOT training the text generator (distilgpt2 is frozen)

    - The responses don't matterโ€”they're a side effect

    - **Focus on the numbers below**, not the text above

    - The magic is in the decreasing loss, not the generated text

    

    **Why?** Because we're demonstrating **memory learning**, not text generation.  

    Standard LLMs train the text generator. This trains the memory. Different goals.

    

    ---

    

    ### ๐ŸŽฏ What Success Looks Like

    

    โœ… **You're seeing it work if**:

    - Loss decreases when you repeat inputs

    - Loss spikes when you send something new

    - Update count increments with each message

    - Update count persists after page refresh

    - Retention is 2.0x (everything is surprising to fresh memory)

    

    โŒ **You're NOT seeing it work if**:

    - Loss stays constant (not learning)

    - Updates reset to 1 after refresh (not persisting)

    - No stats appear below responses

    

    ---

    

    ### ๐Ÿ”ฌ Why This Matters (The Big Picture)

    

    **Standard LLMs**: Frozen weights โ†’ No learning during use  

    **This Demo**: Live weights โ†’ Learning with every message

    

    That decreasing loss you see? **That's gradient descent happening during inference.**  

    That's the revolution. That's what ChatGPT doesn't do.

    

    You're not just using a model. **You're watching it change.**

    

    ---

    

    *Built with Titans (test-time training) + MIRAS (associative memory)*  

    *Papers: [Titans](https://arxiv.org/abs/2501.00663) | [MIRAS](https://arxiv.org/abs/2504.13173)*

    

    **๐Ÿ“– [Read the full essay: "When Models Learn While Thinking"](https://huggingface.co/spaces/Pavantej/titans-miras-demo/blob/main/ESSAY.md)**

    """,
    examples=[
        "hello world",
        "hello world",  # Repeat to show learning!
        "Tell me about test-time learning",
        "What is 2+2?",
        "my name is [your name]",
    ],
    cache_examples=False,
    theme="soft",
)

demo.launch()