Special Note: I've been struggling with an aspect of github copilot and something that I think degrades the code ouput. This repo will serve as a testing ground. Next update will be posted here.
A Python implementation that simulates the Tiny Recursion Model (TRM) recursive reasoning process using your local Ollama instance. This allows you to experiment with TRM-style reasoning without training models from scratch.
This script recreates the core TRM recursive reasoning approach:
- Initial Answer: Generate first response to the question
- Recursive Reasoning: Multiple cycles of self-reflection and analysis
- Progressive Improvement: Iteratively refine the answer based on insights
- Parameter Efficiency: Uses prompting instead of training new model weights
- Python 3.7+
- Ollama installed and running
- A compatible language model (tested with GPT-OSS 20B)
pip install requests-
Install Ollama (if not already installed):
# On macOS/Linux curl -fsSL https://ollama.ai/install.sh | sh # On Windows - download from https://ollama.ai/
-
Download a model:
ollama pull gpt-oss-20b # or any other model you prefer: llama2, mistral, codellama, etc. -
Clone/download the script:
wget https://raw.githubusercontent.com/your-repo/trm_simulation.py # or copy the code from above
-
Start Ollama (if not running):
ollama serve
-
Run the simulation:
python trm_simulation.py
Edit the test_questions list in the script or create your own:
custom_questions = [
"Your math problem here",
"Your logic puzzle here",
"Your pattern recognition task here"
]
trm = TRMSimulator()
for question in custom_questions:
result = trm.recursive_reasoning(question, K_steps=3, L_cycles=4)# Use different model
trm = TRMSimulator(model="llama2")
# Different Ollama endpoint
trm = TRMSimulator(ollama_url="http://remote-server:11434")# More intensive reasoning
result = trm.recursive_reasoning(
question="Your question",
K_steps=5, # More improvement steps
L_cycles=6 # More reasoning cycles per step
)
# Faster reasoning
result = trm.recursive_reasoning(
question="Your question",
K_steps=2, # Fewer improvement steps
L_cycles=2 # Fewer reasoning cycles per step
)π§ TRM Simulation: 2 improvement steps, 3 reasoning cycles each
β Question: A farmer has chickens and cows. There are 30 heads and 74 legs total. How many chickens and how many cows?
π― Initial Answer: Let me set up equations. Each animal has 1 head. Chickens have 2 legs, cows have 4 legs...
π Improvement Step 1/2
π Reasoning Cycle 1: Need to check if the algebra is correct...
π Reasoning Cycle 2: Should verify the solution makes sense...
π Reasoning Cycle 3: Could double-check by substitution...
β¨ Improved Answer: Let me solve this systematically. Let c = chickens, w = cows...
π Improvement Step 2/2
π Reasoning Cycle 1: The equations look correct now...
π Reasoning Cycle 2: Final answer should be verified...
π Reasoning Cycle 3: All constraints are satisfied...
β¨ Improved Answer: Final answer: 23 chickens and 7 cows...
π Final Answer after 2 recursive improvements: 23 chickens and 7 cows...
β±οΈ Total time: 45.2 seconds
The script includes several test categories you can expand:
math_questions = [
"If I have 3 apples and I give away 2, then buy 5 more, how many apples do I have?",
"A farmer has chickens and cows. There are 30 heads and 74 legs total. How many chickens and how many cows?",
"What's the derivative of x^3 + 2x^2 - 5x + 3?"
]logic_questions = [
"All birds can fly. Penguins are birds. Can penguins fly? Explain the contradiction.",
"If all A are B, and all B are C, and John is A, what can we conclude about John?",
"You have 12 balls, one weighs different. You have a balance scale. What's the minimum weighings needed?"
]pattern_questions = [
"What comes next in this sequence: 2, 6, 12, 20, 30, ?",
"Complete the pattern: ABC, BCD, CDE, ___",
"Grid pattern: [1,0,1] [0,1,0] [1,0,1] -> [0,1,0] [1,0,1] [0,1,0]. What's the rule?"
]arc_questions = [
"""Pattern recognition:
Input: [1,0,1]
[0,1,0]
[1,0,1]
Output: [0,1,0]
[1,0,1]
[0,1,0]
What transformation rule produces this output?""",
"""Color pattern:
Input: [R,B,R]
[B,R,B]
[R,B,R]
If the rule changes R->G and B->Y, what's the output?"""
]# Fast but less thorough
quick_reasoning = trm.recursive_reasoning(question, K_steps=1, L_cycles=2)
# Thorough but slower
deep_reasoning = trm.recursive_reasoning(question, K_steps=5, L_cycles=6)
# Balanced (recommended)
balanced_reasoning = trm.recursive_reasoning(question, K_steps=3, L_cycles=4)The script uses different temperatures for different phases:
- Reasoning cycles:
temperature=0.5(moderate creativity) - Answer updates:
temperature=0.3(more focused) - Initial answer:
temperature=0.7(more creative)
-
Ollama not running:
# Check if Ollama is running curl http://localhost:11434/api/tags # Start Ollama ollama serve
-
Model not found:
# List available models ollama list # Download missing model ollama pull gpt-oss-20b
-
Connection errors:
# Check different port/host trm = TRMSimulator(ollama_url="http://localhost:11434")
-
Slow responses:
- Reduce
K_stepsandL_cycles - Use smaller/faster model
- Check system resources
- Reduce
For large models or many reasoning steps:
- Monitor RAM usage
- Reduce batch processing
- Use smaller models for experimentation
| Feature | Real TRM | This Simulation |
|---|---|---|
| Training | Trains 7M parameter model | Uses existing LLM |
| Speed | Very fast inference | Slower (multiple API calls) |
| Accuracy | 45% on ARC-AGI-1 | Depends on base model |
| Cost | Requires training | Only inference costs |
| Flexibility | Fixed architecture | Easy to modify reasoning |
Feel free to:
- Add new test question categories
- Improve the reasoning prompts
- Optimize for different models
- Add evaluation metrics
- Create domain-specific versions
This simulation script is provided as-is for educational and research purposes.