Text Generation
Transformers
Safetensors
PyTorch
English
recursive_language_model
transformer
recursive-language-model
mixture-of-recursion
adaptive-computation
perplexity-routing
self-supervised-perplexity-guided-adaptive-compute
custom_code
Instructions to use Girinath11/recursive-language-model-198m with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Girinath11/recursive-language-model-198m with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Girinath11/recursive-language-model-198m", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("Girinath11/recursive-language-model-198m", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Girinath11/recursive-language-model-198m with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Girinath11/recursive-language-model-198m" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Girinath11/recursive-language-model-198m", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Girinath11/recursive-language-model-198m
- SGLang
How to use Girinath11/recursive-language-model-198m with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Girinath11/recursive-language-model-198m" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Girinath11/recursive-language-model-198m", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Girinath11/recursive-language-model-198m" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Girinath11/recursive-language-model-198m", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Girinath11/recursive-language-model-198m with Docker Model Runner:
docker model run hf.co/Girinath11/recursive-language-model-198m
Update README.md
Browse files
README.md
CHANGED
|
@@ -14,6 +14,7 @@ tags:
|
|
| 14 |
datasets:
|
| 15 |
- Anthropic/hh-rlhf
|
| 16 |
- stingning/ultrachat
|
|
|
|
| 17 |
metrics:
|
| 18 |
- perplexity
|
| 19 |
library_name: transformers
|
|
@@ -21,13 +22,13 @@ pipeline_tag: text-generation
|
|
| 21 |
base_model: gpt2
|
| 22 |
---
|
| 23 |
|
| 24 |
-
#
|
| 25 |
|
| 26 |
-
A novel
|
| 27 |
|
| 28 |
## π Key Achievement
|
| 29 |
|
| 30 |
-
**Perplexity:
|
| 31 |
|
| 32 |
## π₯ Innovation
|
| 33 |
|
|
@@ -40,6 +41,7 @@ Instead of applying uniform computation to all inputs, this model features:
|
|
| 40 |
- **Perplexity-Based Router**: Neural classifier that learns sample difficulty from the model's own confidence
|
| 41 |
- **Adaptive Recursion**: Dynamically allocates 1, 3, or 5 recursive transformer passes based on input complexity
|
| 42 |
- **Self-Supervised Learning**: No manual labels - the model learns what's "hard" from its own perplexity signals
|
|
|
|
| 43 |
|
| 44 |
### How It Works
|
| 45 |
```
|
|
@@ -56,28 +58,31 @@ This enables **intelligent compute allocation** - simple inputs get fast process
|
|
| 56 |
|
| 57 |
| Model | Parameters | Perplexity | Notes |
|
| 58 |
|-------|-----------|------------|-------|
|
| 59 |
-
| **This Model** | **
|
| 60 |
-
| GPT-2 Small | 117M | ~29 |
|
| 61 |
-
| GPT-2 Medium | 345M | ~22 |
|
|
|
|
| 62 |
| Random Baseline | - | ~50,000 | Theoretical worst |
|
| 63 |
|
| 64 |
### Training Metrics
|
| 65 |
```
|
| 66 |
π Training Progression:
|
| 67 |
-
Epoch 1:
|
| 68 |
-
Epoch 2:
|
| 69 |
-
Epoch 3: 17.45 perplexity (51% total improvement)
|
| 70 |
|
| 71 |
π Loss Reduction:
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
```
|
| 74 |
|
| 75 |
### Performance Highlights
|
| 76 |
|
| 77 |
-
β
**
|
| 78 |
-
β
**Better than GPT-2
|
| 79 |
β
**Efficient inference** - adaptive computation saves resources
|
| 80 |
β
**Novel architecture** - not just fine-tuning
|
|
|
|
| 81 |
|
| 82 |
## π― Model Architecture
|
| 83 |
|
|
@@ -85,13 +90,13 @@ Start: 4.50 β Final: 2.86 (36% reduction)
|
|
| 85 |
|
| 86 |
| Component | Configuration |
|
| 87 |
|-----------|--------------|
|
| 88 |
-
| **Total Parameters** |
|
| 89 |
-
| **Vocabulary Size** | 50,
|
| 90 |
-
| **Embedding Dimension** |
|
| 91 |
-
| **Base Transformer Layers** |
|
| 92 |
-
| **Attention Heads** |
|
| 93 |
-
| **Head Dimension** |
|
| 94 |
-
| **FFN Intermediate Size** |
|
| 95 |
| **Max Sequence Length** | 512 tokens |
|
| 96 |
| **Positional Encoding** | Rotary Positional Embeddings (RoPE) |
|
| 97 |
| **Dropout Rate** | 0.1 (hidden & attention) |
|
|
@@ -106,29 +111,30 @@ Start: 4.50 β Final: 2.86 (36% reduction)
|
|
| 106 |
|
| 107 |
### Architecture Components
|
| 108 |
|
| 109 |
-
1. **Token Embedding Layer** (50,
|
| 110 |
-
- Special tokens: `<|user|>`, `<|assistant|>`, `<|endoftext|>`
|
| 111 |
|
| 112 |
-
2. **Base Transformer Stack** (
|
| 113 |
- Multi-head self-attention with RoPE
|
| 114 |
-
- Feed-forward networks (
|
| 115 |
- Pre-normalization with LayerNorm
|
| 116 |
- Residual connections
|
|
|
|
| 117 |
|
| 118 |
-
3. **Perplexity-Based Router** (~
|
| 119 |
- Attention-weighted sequence pooling
|
| 120 |
-
- 2-layer MLP classifier (
|
| 121 |
- Trained on pseudo-labels from sample perplexity
|
| 122 |
- Outputs: complexity class (0=simple, 1=medium, 2=complex)
|
| 123 |
|
| 124 |
-
4. **Recursive Refinement Layer** (~
|
| 125 |
- Transformer block applied 1-5 times adaptively
|
| 126 |
- Same architecture as base layers
|
| 127 |
- Reused weights for parameter efficiency
|
| 128 |
|
| 129 |
5. **Output Projection**
|
| 130 |
- Final LayerNorm
|
| 131 |
-
- Linear layer (
|
| 132 |
|
| 133 |
## π Quick Start
|
| 134 |
|
|
@@ -139,120 +145,82 @@ pip install transformers torch
|
|
| 139 |
|
| 140 |
### Basic Usage
|
| 141 |
```python
|
| 142 |
-
from
|
|
|
|
| 143 |
import torch
|
| 144 |
|
| 145 |
-
# Load model
|
| 146 |
-
model =
|
| 147 |
-
"Girinath11/recursive-language-model-
|
| 148 |
trust_remote_code=True
|
| 149 |
)
|
| 150 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 151 |
-
"Girinath11/recursive-language-model-
|
|
|
|
| 152 |
)
|
| 153 |
|
| 154 |
-
# Move to GPU if available
|
| 155 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 156 |
model = model.to(device)
|
| 157 |
model.eval()
|
| 158 |
|
| 159 |
print(f"β
Model loaded on {device}")
|
| 160 |
-
print(f"π Parameters: {model.
|
| 161 |
```
|
| 162 |
|
| 163 |
### Conversational Format
|
| 164 |
```python
|
| 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 |
-
# Tokenize all prompts
|
| 197 |
-
inputs = tokenizer(prompts, return_tensors="pt", padding=True).to(device)
|
| 198 |
-
|
| 199 |
-
# Generate for all at once
|
| 200 |
-
with torch.no_grad():
|
| 201 |
-
outputs = model.generate(
|
| 202 |
-
**inputs,
|
| 203 |
-
max_new_tokens=80,
|
| 204 |
-
temperature=0.8,
|
| 205 |
-
top_p=0.9,
|
| 206 |
-
do_sample=True,
|
| 207 |
-
pad_token_id=tokenizer.eos_token_id
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
# Decode all outputs
|
| 211 |
-
for i, output in enumerate(outputs):
|
| 212 |
-
text = tokenizer.decode(output, skip_special_tokens=True)
|
| 213 |
-
print(f"\nPrompt {i+1}:\n{text}\n{'-'*60}")
|
| 214 |
```
|
| 215 |
|
| 216 |
### Generation Parameters
|
| 217 |
```python
|
| 218 |
# Creative writing (high temperature)
|
| 219 |
-
|
| 220 |
-
**inputs,
|
| 221 |
-
max_new_tokens=150,
|
| 222 |
-
temperature=1.0, # More random/creative
|
| 223 |
-
top_p=0.95,
|
| 224 |
-
top_k=50,
|
| 225 |
-
do_sample=True
|
| 226 |
-
)
|
| 227 |
|
| 228 |
# Focused/deterministic (low temperature)
|
| 229 |
-
|
| 230 |
-
**inputs,
|
| 231 |
-
max_new_tokens=100,
|
| 232 |
-
temperature=0.5, # More focused
|
| 233 |
-
top_p=0.9,
|
| 234 |
-
do_sample=True
|
| 235 |
-
)
|
| 236 |
-
|
| 237 |
-
# Greedy decoding (most likely tokens)
|
| 238 |
-
greedy_output = model.generate(
|
| 239 |
-
**inputs,
|
| 240 |
-
max_new_tokens=50,
|
| 241 |
-
do_sample=False # Deterministic
|
| 242 |
-
)
|
| 243 |
```
|
| 244 |
|
| 245 |
## π Training Details
|
| 246 |
|
| 247 |
### Dataset
|
| 248 |
|
| 249 |
-
**Total Training Samples:
|
| 250 |
|
| 251 |
-
| Dataset |
|
| 252 |
-
|---------|--------
|
| 253 |
-
| **Anthropic HH-RLHF** |
|
| 254 |
-
| **UltraChat** |
|
| 255 |
-
| **
|
|
|
|
| 256 |
|
| 257 |
**Data Format:**
|
| 258 |
```
|
|
@@ -263,249 +231,151 @@ Assistant response here
|
|
| 263 |
<|endoftext|>
|
| 264 |
```
|
| 265 |
|
| 266 |
-
**Quality Control:**
|
| 267 |
-
- Minimum length: 80 characters
|
| 268 |
-
- Maximum length: 6,000 characters
|
| 269 |
-
- Minimum word count: 15 words
|
| 270 |
-
- Alphanumeric ratio: >65%
|
| 271 |
-
- Required: Proper punctuation
|
| 272 |
-
- Validation: Strict conversation structure
|
| 273 |
-
|
| 274 |
### Training Configuration
|
| 275 |
```yaml
|
| 276 |
Hardware:
|
| 277 |
-
GPU: NVIDIA Tesla T4 (15.
|
| 278 |
Platform: Kaggle
|
| 279 |
-
Framework: PyTorch + Transformers
|
| 280 |
Mixed Precision: FP16 (AMP)
|
| 281 |
|
| 282 |
Hyperparameters:
|
| 283 |
-
Batch Size:
|
| 284 |
-
Gradient Accumulation:
|
| 285 |
Effective Batch Size: 64
|
| 286 |
Max Sequence Length: 512
|
| 287 |
-
Learning Rate:
|
| 288 |
-
Optimizer: AdamW
|
| 289 |
Weight Decay: 0.01
|
| 290 |
-
LR Schedule:
|
| 291 |
-
Warmup:
|
| 292 |
Gradient Clipping: 1.0
|
| 293 |
-
Total Epochs:
|
| 294 |
-
Total Steps:
|
| 295 |
|
| 296 |
Loss Function:
|
| 297 |
Language Modeling: CrossEntropyLoss
|
| 298 |
Router Loss: CrossEntropyLoss (weight: 0.1)
|
| 299 |
Total: LM Loss + 0.1 Γ Router Loss
|
| 300 |
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
```
|
| 305 |
|
| 306 |
-
### Training Schedule
|
| 307 |
-
|
| 308 |
-
- **Steps per Epoch:** 4,639
|
| 309 |
-
- **Training Speed:** ~2.7 it/s
|
| 310 |
-
- **GPU Utilization:** ~85-90%
|
| 311 |
-
- **Memory Usage:** ~8-9 GB (peak)
|
| 312 |
-
|
| 313 |
### Training Progression
|
| 314 |
|
| 315 |
-
| Epoch | Train Loss |
|
| 316 |
-
|-------|-----------|----------
|
| 317 |
-
| **1** | 4.
|
| 318 |
-
| **2** | 3.
|
| 319 |
-
| **3** | 2.8572 | 2.8595 | **17.45** | π₯ **BEST!** |
|
| 320 |
|
| 321 |
**Key Observations:**
|
| 322 |
- Loss decreased steadily across all epochs
|
| 323 |
-
-
|
| 324 |
-
-
|
| 325 |
-
-
|
| 326 |
|
| 327 |
## π‘ Technical Innovation
|
| 328 |
|
| 329 |
### Perplexity-Based Routing (Novel Contribution)
|
| 330 |
|
| 331 |
Traditional transformers apply the same computational depth to all inputs. This model recognizes that:
|
| 332 |
-
- **Simple inputs**
|
| 333 |
-
- **Complex inputs**
|
| 334 |
|
| 335 |
-
**Key Innovation:**
|
| 336 |
```python
|
| 337 |
-
# During training:
|
| 338 |
sample_perplexity = exp(sample_loss)
|
| 339 |
|
| 340 |
if sample_perplexity < 20:
|
| 341 |
-
label = 0 # Simple
|
| 342 |
elif sample_perplexity < 50:
|
| 343 |
-
label = 1 # Medium
|
| 344 |
else:
|
| 345 |
-
label = 2 # Complex
|
| 346 |
|
| 347 |
# Router learns to predict this from input features
|
| 348 |
router_loss = CrossEntropyLoss(router_logits, label)
|
|
|
|
| 349 |
```
|
| 350 |
|
| 351 |
**Benefits:**
|
| 352 |
-
- β
**No manual labeling
|
| 353 |
-
- β
**Adapts as model learns**
|
| 354 |
-
- β
**Objective measure**
|
| 355 |
-
- β
**Efficient
|
| 356 |
|
| 357 |
-
###
|
| 358 |
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
-
|
| 363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
### Self-Supervised Curriculum Learning
|
| 366 |
|
| 367 |
-
The model implements automatic curriculum learning:
|
| 368 |
1. **Early training**: Most samples are "complex" (high perplexity)
|
| 369 |
2. **Mid training**: Distribution shifts as model learns
|
| 370 |
3. **Late training**: More samples become "simple" (low perplexity)
|
| 371 |
|
| 372 |
-
|
| 373 |
|
| 374 |
## π― Use Cases
|
| 375 |
|
| 376 |
### β
Recommended
|
| 377 |
-
|
| 378 |
-
- **Educational demos**
|
| 379 |
-
- **
|
| 380 |
-
- **
|
| 381 |
-
- **Learning** - Understanding transformer architectures
|
| 382 |
-
- **Creative writing assistance** - With human review
|
| 383 |
-
- **Code explanation** - Basic programming concepts
|
| 384 |
|
| 385 |
### β οΈ Not Recommended
|
| 386 |
-
|
| 387 |
-
- Production chatbots without human oversight
|
| 388 |
- Medical, legal, or financial advice
|
| 389 |
-
- Generating authoritative content without verification
|
| 390 |
-
- Automated content moderation
|
| 391 |
- Safety-critical systems
|
| 392 |
-
- High-stakes decision making
|
| 393 |
|
| 394 |
## β οΈ Limitations
|
| 395 |
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
1. **Context Window**: 512 tokens maximum (vs 2048+ for modern models)
|
| 399 |
-
2. **Training Data**: 37K samples - relatively small dataset
|
| 400 |
3. **Single Language**: Primarily English
|
| 401 |
-
4. **
|
| 402 |
-
5. **
|
| 403 |
-
|
| 404 |
-
### Known Issues
|
| 405 |
-
|
| 406 |
-
- **Repetition**: May repeat phrases in very long generations (>200 tokens)
|
| 407 |
-
- **Factual Accuracy**: Small knowledge base, may generate plausible but incorrect information
|
| 408 |
-
- **Context Retention**: May lose track of earlier context in long conversations
|
| 409 |
-
- **Domain Specificity**: Limited knowledge in highly specialized fields
|
| 410 |
-
|
| 411 |
-
### Generation Characteristics
|
| 412 |
-
|
| 413 |
-
- Occasionally incomplete sentences at sequence boundaries
|
| 414 |
-
- May generate generic filler phrases
|
| 415 |
-
- Temperature tuning recommended for optimal quality
|
| 416 |
-
- Best results with clear, specific prompts
|
| 417 |
-
|
| 418 |
-
## π¬ Ethical Considerations
|
| 419 |
-
|
| 420 |
-
### Bias & Fairness
|
| 421 |
-
|
| 422 |
-
This model may exhibit biases from training data:
|
| 423 |
-
|
| 424 |
-
**Potential Biases:**
|
| 425 |
-
- Geographic: Western/English content overrepresentation
|
| 426 |
-
- Demographic: Biases from web text and conversational data
|
| 427 |
-
- Temporal: Reflects content patterns up to 2024
|
| 428 |
-
- Topic: Conversational data may skew certain perspectives
|
| 429 |
-
|
| 430 |
-
**Mitigation:**
|
| 431 |
-
- Diverse training sources (Anthropic HH, UltraChat)
|
| 432 |
-
- Quality filtering applied
|
| 433 |
-
- Users should validate outputs for fairness-critical applications
|
| 434 |
-
|
| 435 |
-
### Responsible Use
|
| 436 |
-
|
| 437 |
-
**Environmental Impact:**
|
| 438 |
-
- Training: ~84 minutes on T4 GPU
|
| 439 |
-
- Estimated COβ: ~0.09 kg (single training run)
|
| 440 |
-
- Relatively low impact due to efficient training
|
| 441 |
-
|
| 442 |
-
## π Model Card
|
| 443 |
-
|
| 444 |
-
### Model Details
|
| 445 |
-
|
| 446 |
-
- **Developed by:** Girinath11
|
| 447 |
-
- **Model type:** Causal Language Model with Adaptive Recursion
|
| 448 |
-
- **Language:** English
|
| 449 |
-
- **License:** MIT
|
| 450 |
-
- **Base Model:** GPT-2 tokenizer
|
| 451 |
-
- **Training Date:** March 2026
|
| 452 |
-
- **Framework:** PyTorch + Transformers
|
| 453 |
-
|
| 454 |
-
### Intended Use
|
| 455 |
-
|
| 456 |
-
**Primary Use Cases:**
|
| 457 |
-
- Research on adaptive computation
|
| 458 |
-
- Educational demonstrations
|
| 459 |
-
- Conversational AI prototyping
|
| 460 |
-
- Understanding mixture-of-experts concepts
|
| 461 |
-
- Curriculum learning research
|
| 462 |
-
|
| 463 |
-
**Out-of-Scope:**
|
| 464 |
-
- Production deployment without fine-tuning
|
| 465 |
-
- High-stakes decision making
|
| 466 |
-
- Content requiring domain expertise
|
| 467 |
-
- Real-time applications requiring <100ms latency
|
| 468 |
-
|
| 469 |
-
### Evaluation Methodology
|
| 470 |
-
|
| 471 |
-
**Metrics:**
|
| 472 |
-
- Primary: Perplexity on held-out validation set
|
| 473 |
-
- Secondary: Training loss, generation quality
|
| 474 |
-
|
| 475 |
-
**Evaluation Data:**
|
| 476 |
-
- 2,000 samples from Anthropic HH & UltraChat
|
| 477 |
-
- Same preprocessing as training
|
| 478 |
-
- No overlap with training data
|
| 479 |
|
| 480 |
## π Repository Structure
|
| 481 |
```
|
| 482 |
-
recursive-language-model-
|
| 483 |
-
βββ config.json
|
| 484 |
-
βββ model.safetensors
|
| 485 |
-
βββ tokenizer.json
|
| 486 |
-
βββ tokenizer_config.json
|
| 487 |
-
βββ special_tokens_map.json
|
| 488 |
-
ββ
|
| 489 |
-
βββ
|
| 490 |
-
|
|
|
|
|
|
|
| 491 |
```
|
| 492 |
|
| 493 |
-
## π Links
|
| 494 |
-
|
| 495 |
-
- **Model Card:** [Hugging Face](https://huggingface.co/Girinath11/recursive-language-model-90m)
|
| 496 |
-
- **GitHub:** [Repository](https://github.com/Giri530/recursive-language-model-90m)
|
| 497 |
-
|
| 498 |
## π Citation
|
| 499 |
|
| 500 |
-
If you use this model in your research, please cite:
|
| 501 |
```bibtex
|
| 502 |
-
@misc{
|
| 503 |
author = {Girinath11},
|
| 504 |
-
title = {
|
| 505 |
year = {2026},
|
| 506 |
publisher = {Hugging Face},
|
| 507 |
-
howpublished = {\url{https://huggingface.co/Girinath11/recursive-language-model-
|
| 508 |
-
note = {
|
| 509 |
}
|
| 510 |
```
|
| 511 |
|
|
@@ -513,31 +383,25 @@ If you use this model in your research, please cite:
|
|
| 513 |
|
| 514 |
- **Anthropic** for HH-RLHF dataset
|
| 515 |
- **Tsinghua University** for UltraChat dataset
|
|
|
|
| 516 |
- **Hugging Face** for Transformers library
|
| 517 |
- **Kaggle** for free GPU access
|
| 518 |
-
- **OpenAI** for GPT-2 tokenizer
|
| 519 |
|
| 520 |
## π Version History
|
| 521 |
|
| 522 |
### v1.0 (March 2026)
|
| 523 |
- Initial release
|
| 524 |
-
-
|
| 525 |
-
- Perplexity:
|
| 526 |
-
- Trained on
|
| 527 |
-
-
|
| 528 |
-
|
| 529 |
-
## π§ Contact
|
| 530 |
-
|
| 531 |
-
For questions, issues, or collaboration:
|
| 532 |
-
- **GitHub Issues:** [Report a bug](https://github.com/Giri530/recursive-language-model-90m/issues)
|
| 533 |
-
- **Hugging Face Discussions:** [Ask a question](https://huggingface.co/Girinath11/recursive-language-model-90m/discussions)
|
| 534 |
|
| 535 |
## π License
|
| 536 |
|
| 537 |
-
MIT License
|
| 538 |
|
| 539 |
---
|
| 540 |
|
| 541 |
-
**Model Status:** β
|
| 542 |
-
|
| 543 |
-
**Last Updated:** March 2, 2026
|
|
|
|
| 14 |
datasets:
|
| 15 |
- Anthropic/hh-rlhf
|
| 16 |
- stingning/ultrachat
|
| 17 |
+
- vicgalle/alpaca-gpt4
|
| 18 |
metrics:
|
| 19 |
- perplexity
|
| 20 |
library_name: transformers
|
|
|
|
| 22 |
base_model: gpt2
|
| 23 |
---
|
| 24 |
|
| 25 |
+
# Mixture of Recursion Language Model - 198M (Adaptive Computation)
|
| 26 |
|
| 27 |
+
A novel 198M parameter conversational language model featuring **adaptive recursive computation** through perplexity-based dynamic routing. Achieves better performance than GPT-2 Medium (345M) with 57% fewer parameters.
|
| 28 |
|
| 29 |
## π Key Achievement
|
| 30 |
|
| 31 |
+
**Perplexity: 15.37** - Outperforms GPT-2 Medium (22 perplexity) despite being significantly smaller.
|
| 32 |
|
| 33 |
## π₯ Innovation
|
| 34 |
|
|
|
|
| 41 |
- **Perplexity-Based Router**: Neural classifier that learns sample difficulty from the model's own confidence
|
| 42 |
- **Adaptive Recursion**: Dynamically allocates 1, 3, or 5 recursive transformer passes based on input complexity
|
| 43 |
- **Self-Supervised Learning**: No manual labels - the model learns what's "hard" from its own perplexity signals
|
| 44 |
+
- **NaN-Safe Attention**: Uses -1e4 masking instead of -inf for stable fp16 training
|
| 45 |
|
| 46 |
### How It Works
|
| 47 |
```
|
|
|
|
| 58 |
|
| 59 |
| Model | Parameters | Perplexity | Notes |
|
| 60 |
|-------|-----------|------------|-------|
|
| 61 |
+
| **This Model** | **198M** | **15.37** | **Novel adaptive architecture** |
|
| 62 |
+
| GPT-2 Small | 117M | ~29 | Smaller baseline |
|
| 63 |
+
| GPT-2 Medium | 345M | ~22 | 1.7Γ larger |
|
| 64 |
+
| GPT-2 Large | 774M | ~18 | 3.9Γ larger |
|
| 65 |
| Random Baseline | - | ~50,000 | Theoretical worst |
|
| 66 |
|
| 67 |
### Training Metrics
|
| 68 |
```
|
| 69 |
π Training Progression:
|
| 70 |
+
Epoch 1: 21.75 perplexity (Val loss: 3.0798)
|
| 71 |
+
Epoch 2: 15.37 perplexity (Val loss: 2.7326) β BEST
|
|
|
|
| 72 |
|
| 73 |
π Loss Reduction:
|
| 74 |
+
Train loss: 4.5081 β 3.3068 (27% reduction)
|
| 75 |
+
Val loss: 3.0798 β 2.7326 (11% reduction)
|
| 76 |
+
nan_batches: 0 (stable training throughout)
|
| 77 |
```
|
| 78 |
|
| 79 |
### Performance Highlights
|
| 80 |
|
| 81 |
+
β
**15.37 perplexity** on validation set (2,000 samples)
|
| 82 |
+
β
**Better than GPT-2 Medium** with 57% fewer parameters
|
| 83 |
β
**Efficient inference** - adaptive computation saves resources
|
| 84 |
β
**Novel architecture** - not just fine-tuning
|
| 85 |
+
β
**Stable training** - 0 NaN batches across 150K steps
|
| 86 |
|
| 87 |
## π― Model Architecture
|
| 88 |
|
|
|
|
| 90 |
|
| 91 |
| Component | Configuration |
|
| 92 |
|-----------|--------------|
|
| 93 |
+
| **Total Parameters** | ~198M |
|
| 94 |
+
| **Vocabulary Size** | 50,260 tokens (GPT-2 BPE + special tokens) |
|
| 95 |
+
| **Embedding Dimension** | 768 |
|
| 96 |
+
| **Base Transformer Layers** | 16 |
|
| 97 |
+
| **Attention Heads** | 12 heads per layer |
|
| 98 |
+
| **Head Dimension** | 64 (768 Γ· 12) |
|
| 99 |
+
| **FFN Intermediate Size** | 3072 |
|
| 100 |
| **Max Sequence Length** | 512 tokens |
|
| 101 |
| **Positional Encoding** | Rotary Positional Embeddings (RoPE) |
|
| 102 |
| **Dropout Rate** | 0.1 (hidden & attention) |
|
|
|
|
| 111 |
|
| 112 |
### Architecture Components
|
| 113 |
|
| 114 |
+
1. **Token Embedding Layer** (50,260 Γ 768)
|
| 115 |
+
- Special tokens: `<|user|>`, `<|assistant|>`, `<|pad|>`, `<|endoftext|>`
|
| 116 |
|
| 117 |
+
2. **Base Transformer Stack** (16 layers)
|
| 118 |
- Multi-head self-attention with RoPE
|
| 119 |
+
- Feed-forward networks (768 β 3072 β 768)
|
| 120 |
- Pre-normalization with LayerNorm
|
| 121 |
- Residual connections
|
| 122 |
+
- NaN-safe attention masking (-1e4)
|
| 123 |
|
| 124 |
+
3. **Perplexity-Based Router** (~1.2M params)
|
| 125 |
- Attention-weighted sequence pooling
|
| 126 |
+
- 2-layer MLP classifier (768 β 384 β 3)
|
| 127 |
- Trained on pseudo-labels from sample perplexity
|
| 128 |
- Outputs: complexity class (0=simple, 1=medium, 2=complex)
|
| 129 |
|
| 130 |
+
4. **Recursive Refinement Layer** (~7M params)
|
| 131 |
- Transformer block applied 1-5 times adaptively
|
| 132 |
- Same architecture as base layers
|
| 133 |
- Reused weights for parameter efficiency
|
| 134 |
|
| 135 |
5. **Output Projection**
|
| 136 |
- Final LayerNorm
|
| 137 |
+
- Linear layer (768 β 50,260)
|
| 138 |
|
| 139 |
## π Quick Start
|
| 140 |
|
|
|
|
| 145 |
|
| 146 |
### Basic Usage
|
| 147 |
```python
|
| 148 |
+
from mixture_of_recursion import RecursiveLanguageModel
|
| 149 |
+
from transformers import AutoTokenizer
|
| 150 |
import torch
|
| 151 |
|
| 152 |
+
# Load model
|
| 153 |
+
model = RecursiveLanguageModel.from_pretrained(
|
| 154 |
+
"Girinath11/recursive-language-model-198m",
|
| 155 |
trust_remote_code=True
|
| 156 |
)
|
| 157 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 158 |
+
"Girinath11/recursive-language-model-198m",
|
| 159 |
+
trust_remote_code=True
|
| 160 |
)
|
| 161 |
|
|
|
|
| 162 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 163 |
model = model.to(device)
|
| 164 |
model.eval()
|
| 165 |
|
| 166 |
print(f"β
Model loaded on {device}")
|
| 167 |
+
print(f"π Parameters: {sum(p.numel() for p in model.parameters()):,}")
|
| 168 |
```
|
| 169 |
|
| 170 |
### Conversational Format
|
| 171 |
```python
|
| 172 |
+
def chat(question, max_new_tokens=150, temperature=0.7, top_p=0.9):
|
| 173 |
+
# Must use chat format β model trained on this
|
| 174 |
+
prompt = f"<|user|>\n{question}\n<|assistant|>\n"
|
| 175 |
+
|
| 176 |
+
inputs = tokenizer(
|
| 177 |
+
prompt,
|
| 178 |
+
return_tensors="pt",
|
| 179 |
+
add_special_tokens=False
|
| 180 |
+
).to(device)
|
| 181 |
+
|
| 182 |
+
with torch.no_grad():
|
| 183 |
+
outputs = model.generate(
|
| 184 |
+
inputs['input_ids'],
|
| 185 |
+
max_new_tokens=max_new_tokens,
|
| 186 |
+
temperature=temperature,
|
| 187 |
+
top_p=top_p,
|
| 188 |
+
do_sample=True,
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 192 |
+
|
| 193 |
+
if "<|assistant|>" in full_text:
|
| 194 |
+
return full_text.split("<|assistant|>")[-1].strip()
|
| 195 |
+
return full_text
|
| 196 |
+
|
| 197 |
+
# Example
|
| 198 |
+
print(chat("What is machine learning?"))
|
| 199 |
+
print(chat("Explain neural networks simply"))
|
| 200 |
+
print(chat("What is Python programming?"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
```
|
| 202 |
|
| 203 |
### Generation Parameters
|
| 204 |
```python
|
| 205 |
# Creative writing (high temperature)
|
| 206 |
+
response = chat("Write a story about AI", temperature=1.0, top_p=0.95)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
# Focused/deterministic (low temperature)
|
| 209 |
+
response = chat("Explain quantum computing", temperature=0.5, top_p=0.9)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
```
|
| 211 |
|
| 212 |
## π Training Details
|
| 213 |
|
| 214 |
### Dataset
|
| 215 |
|
| 216 |
+
**Total Training Samples: 150,000** (high-quality conversational data)
|
| 217 |
|
| 218 |
+
| Dataset | Samples | Percentage | Description |
|
| 219 |
+
|---------|---------|------------|-------------|
|
| 220 |
+
| **Anthropic HH-RLHF** | 80,000 | 53% | Helpful & harmless responses |
|
| 221 |
+
| **UltraChat** | 50,000 | 33% | GPT-4 generated multi-turn dialogues |
|
| 222 |
+
| **Alpaca-GPT4** | 20,000 | 14% | Instruction following data |
|
| 223 |
+
| **Validation** | 2,000 | - | Held-out evaluation samples |
|
| 224 |
|
| 225 |
**Data Format:**
|
| 226 |
```
|
|
|
|
| 231 |
<|endoftext|>
|
| 232 |
```
|
| 233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
### Training Configuration
|
| 235 |
```yaml
|
| 236 |
Hardware:
|
| 237 |
+
GPU: NVIDIA Tesla T4 (15.6 GB)
|
| 238 |
Platform: Kaggle
|
| 239 |
+
Framework: PyTorch + HuggingFace Transformers
|
| 240 |
Mixed Precision: FP16 (AMP)
|
| 241 |
|
| 242 |
Hyperparameters:
|
| 243 |
+
Batch Size: 2
|
| 244 |
+
Gradient Accumulation: 32
|
| 245 |
Effective Batch Size: 64
|
| 246 |
Max Sequence Length: 512
|
| 247 |
+
Learning Rate: 1e-4
|
| 248 |
+
Optimizer: AdamW (betas: 0.9, 0.95)
|
| 249 |
Weight Decay: 0.01
|
| 250 |
+
LR Schedule: LinearWarmup + CosineAnnealing
|
| 251 |
+
Warmup Steps: 500
|
| 252 |
Gradient Clipping: 1.0
|
| 253 |
+
Total Epochs: 2
|
| 254 |
+
Total Steps: 150,000
|
| 255 |
|
| 256 |
Loss Function:
|
| 257 |
Language Modeling: CrossEntropyLoss
|
| 258 |
Router Loss: CrossEntropyLoss (weight: 0.1)
|
| 259 |
Total: LM Loss + 0.1 Γ Router Loss
|
| 260 |
|
| 261 |
+
Key Fix:
|
| 262 |
+
Attention Mask: -1e4 (NOT -inf) β prevents NaN in fp16
|
| 263 |
+
Pad Token: Separate from EOS token
|
| 264 |
```
|
| 265 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
### Training Progression
|
| 267 |
|
| 268 |
+
| Epoch | Train Loss | Val Loss | Perplexity | NaN Batches |
|
| 269 |
+
|-------|-----------|----------|------------|-------------|
|
| 270 |
+
| **1** | 4.5081 | 3.0798 | 21.75 | 0 |
|
| 271 |
+
| **2** | 3.3068 | 2.7326 | **15.37** π₯ | 0 |
|
|
|
|
| 272 |
|
| 273 |
**Key Observations:**
|
| 274 |
- Loss decreased steadily across all epochs
|
| 275 |
+
- Zero NaN batches β completely stable training
|
| 276 |
+
- 29% perplexity improvement from Epoch 1 to 2
|
| 277 |
+
- Training time: ~9h 12m on single T4 GPU
|
| 278 |
|
| 279 |
## π‘ Technical Innovation
|
| 280 |
|
| 281 |
### Perplexity-Based Routing (Novel Contribution)
|
| 282 |
|
| 283 |
Traditional transformers apply the same computational depth to all inputs. This model recognizes that:
|
| 284 |
+
- **Simple inputs** need minimal processing
|
| 285 |
+
- **Complex inputs** benefit from deeper iterative refinement
|
| 286 |
|
| 287 |
+
**Key Innovation:** The model learns complexity from its own performance:
|
| 288 |
```python
|
| 289 |
+
# During training (self-supervised):
|
| 290 |
sample_perplexity = exp(sample_loss)
|
| 291 |
|
| 292 |
if sample_perplexity < 20:
|
| 293 |
+
label = 0 # Simple β model is confident
|
| 294 |
elif sample_perplexity < 50:
|
| 295 |
+
label = 1 # Medium β model is uncertain
|
| 296 |
else:
|
| 297 |
+
label = 2 # Complex β model struggling
|
| 298 |
|
| 299 |
# Router learns to predict this from input features
|
| 300 |
router_loss = CrossEntropyLoss(router_logits, label)
|
| 301 |
+
total_loss = lm_loss + 0.1 * router_loss
|
| 302 |
```
|
| 303 |
|
| 304 |
**Benefits:**
|
| 305 |
+
- β
**No manual labeling** β fully self-supervised
|
| 306 |
+
- β
**Adapts as model learns** β natural curriculum
|
| 307 |
+
- β
**Objective measure** β based on actual model performance
|
| 308 |
+
- β
**Efficient** β simple inputs use 1/5 the compute of complex ones
|
| 309 |
|
| 310 |
+
### NaN-Safe Attention Masking
|
| 311 |
|
| 312 |
+
Critical fix for stable fp16 training:
|
| 313 |
+
```python
|
| 314 |
+
# Unstable (causes NaN in fp16):
|
| 315 |
+
mask.fill_(float('-inf'))
|
| 316 |
+
|
| 317 |
+
# Stable β
:
|
| 318 |
+
mask.fill_(-1e4)
|
| 319 |
+
# Then clamp before softmax:
|
| 320 |
+
scores = scores.clamp(min=-1e4, max=1e4)
|
| 321 |
+
attn = F.softmax(scores, dim=-1)
|
| 322 |
+
attn = torch.nan_to_num(attn, nan=0.0)
|
| 323 |
+
```
|
| 324 |
|
| 325 |
### Self-Supervised Curriculum Learning
|
| 326 |
|
|
|
|
| 327 |
1. **Early training**: Most samples are "complex" (high perplexity)
|
| 328 |
2. **Mid training**: Distribution shifts as model learns
|
| 329 |
3. **Late training**: More samples become "simple" (low perplexity)
|
| 330 |
|
| 331 |
+
Natural curriculum without human intervention.
|
| 332 |
|
| 333 |
## π― Use Cases
|
| 334 |
|
| 335 |
### β
Recommended
|
| 336 |
+
- **Research** β Experimenting with adaptive computation
|
| 337 |
+
- **Educational demos** β Teaching language model concepts
|
| 338 |
+
- **Prototyping** β Conversational AI applications
|
| 339 |
+
- **Creative writing** β With human review
|
|
|
|
|
|
|
|
|
|
| 340 |
|
| 341 |
### β οΈ Not Recommended
|
| 342 |
+
- Production chatbots without fine-tuning
|
|
|
|
| 343 |
- Medical, legal, or financial advice
|
|
|
|
|
|
|
| 344 |
- Safety-critical systems
|
|
|
|
| 345 |
|
| 346 |
## β οΈ Limitations
|
| 347 |
|
| 348 |
+
1. **Context Window**: 512 tokens maximum
|
| 349 |
+
2. **Training Data**: 150K samples β smaller than commercial models
|
|
|
|
|
|
|
| 350 |
3. **Single Language**: Primarily English
|
| 351 |
+
4. **Repetition**: May repeat phrases in very long generations (>200 tokens)
|
| 352 |
+
5. **Factual Accuracy**: May generate plausible but incorrect information
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
## π Repository Structure
|
| 355 |
```
|
| 356 |
+
recursive-language-model-198m/
|
| 357 |
+
βββ config.json # Model configuration
|
| 358 |
+
βββ model.safetensors # Model weights (794 MB)
|
| 359 |
+
βββ tokenizer.json # Tokenizer vocabulary
|
| 360 |
+
βββ tokenizer_config.json # Tokenizer settings
|
| 361 |
+
βββ special_tokens_map.json # Special token mappings
|
| 362 |
+
βοΏ½οΏ½β vocab.json # GPT-2 vocabulary
|
| 363 |
+
βββ merges.txt # BPE merges
|
| 364 |
+
βββ mixture_of_recursion.py # Model architecture code
|
| 365 |
+
βββ model_usage.py # Usage examples
|
| 366 |
+
βββ README.md # This file
|
| 367 |
```
|
| 368 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
## π Citation
|
| 370 |
|
|
|
|
| 371 |
```bibtex
|
| 372 |
+
@misc{mixture-of-recursion-198m-2026,
|
| 373 |
author = {Girinath11},
|
| 374 |
+
title = {Mixture of Recursion Language Model with Perplexity-Based Dynamic Routing},
|
| 375 |
year = {2026},
|
| 376 |
publisher = {Hugging Face},
|
| 377 |
+
howpublished = {\url{https://huggingface.co/Girinath11/recursive-language-model-198m}},
|
| 378 |
+
note = {198M parameter model with adaptive computation achieving 15.37 perplexity}
|
| 379 |
}
|
| 380 |
```
|
| 381 |
|
|
|
|
| 383 |
|
| 384 |
- **Anthropic** for HH-RLHF dataset
|
| 385 |
- **Tsinghua University** for UltraChat dataset
|
| 386 |
+
- **Vicgalle** for Alpaca-GPT4 dataset
|
| 387 |
- **Hugging Face** for Transformers library
|
| 388 |
- **Kaggle** for free GPU access
|
|
|
|
| 389 |
|
| 390 |
## π Version History
|
| 391 |
|
| 392 |
### v1.0 (March 2026)
|
| 393 |
- Initial release
|
| 394 |
+
- 198M parameters
|
| 395 |
+
- Perplexity: 15.37
|
| 396 |
+
- Trained on 150K samples
|
| 397 |
+
- 2 epochs, 9h 12m training time
|
| 398 |
+
- NaN-safe fp16 training
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
|
| 400 |
## π License
|
| 401 |
|
| 402 |
+
MIT License β Free to use with attribution
|
| 403 |
|
| 404 |
---
|
| 405 |
|
| 406 |
+
**Model Status:** β
Ready for Research & Prototyping
|
| 407 |
+
**Last Updated:** March 2026
|
|
|