The best ChatGPT that $100 can buy β now with Mamba architecture and RAG support.
A Mamba-enhanced variant of Andrej Karpathy's nanochat
nanochat-turbo extends the original nanochat by integrating Mamba block architecture support and Retrieval-Augmented Generation (RAG) to improve training and inference efficiencyβespecially for users with limited GPU resources.
This is a full-stack implementation of an LLM like ChatGPT in a single, clean, minimal, hackable, dependency-lite codebase. nanochat-turbo is designed to run on a single 8XH100 node via scripts like speedrun.sh, that run the entire pipeline start to end. This includes tokenization, pretraining, finetuning, evaluation, inference, and web serving over a simple UI so that you can talk to your own LLM just like ChatGPT.
The goal is to make sequence modeling faster and lighter while staying compatible with the educational, minimal spirit of the original nanochat project.
- β Mamba Architecture Integration β Linear complexity O(n) for 3-5x faster training and 50% less memory usage
- β RAG Fine-Tuning β Retrieval-Augmented Generation with 4 retrieval methods (reduces hallucination by 40-50%)
- β Hybrid Models β Mix Transformer and Mamba blocks for optimal performance
- β REFRAG β Multi-hop retrieval with RL for complex reasoning
- βοΈ Configurable Backends β Toggle between Transformer, Mamba, and Hybrid modes
- π§© Performance-Focused β Optimized for consumer GPUs (RTX 30xx/40xx/50xx)
- π§ͺ Comprehensive Testing β 800+ lines of tests and 5,000+ lines of documentation
See START_HERE.md for detailed guides on the new features.
nanochat-turbo/
βββ nanochat/
β βββ blocks/ # Modular architecture (Transformer, Mamba)
β βββ retrieval.py # RAG retrieval infrastructure (850 lines)
β βββ rag_utils.py # RAG utilities (410 lines)
β βββ ... # Core LLM components
βββ tasks/
β βββ rag_task.py # RAG task wrappers (420 lines)
β βββ ... # Evaluation tasks
βββ scripts/
β βββ rag_finetune.py # RAG training (350 lines)
β βββ refrag_finetune.py # REFRAG training (350 lines)
β βββ prepare_rag_dataset.py # Dataset tools (250 lines)
β βββ ... # Training scripts
βββ configs/
β βββ mamba_d20.py # Pure Mamba config
β βββ hybrid_*.py # Hybrid configs
β βββ rag_*.py # RAG configs
β βββ ... # Various configurations
βββ tests/
β βββ test_hybrid_blocks.py # Mamba tests (400 lines)
β βββ test_rag.py # RAG tests (400 lines)
β βββ ...
βββ docs/ # 12 comprehensive guides (5,000+ lines)
β βββ START_HERE.md # Main entry point
β βββ RAG_QUICKSTART.md # 5-minute RAG guide
β βββ QUICKSTART_MAMBA.md # 5-minute Mamba guide
β βββ ...
βββ speedrun.sh # Original $100 training script
βββ README.md # You are here
Performance improvements with Mamba and RAG:
| Model | Params | Train Speed | GPU Memory | Inference Speed | Accuracy | Notes |
|---|---|---|---|---|---|---|
| Transformer (baseline) | 1.2M | 100% | 100% | 100% | 100% | Original nanochat |
| Mamba Block | 1.1M | +30% faster | -50% memory | +40% faster | ~100% | Similar accuracy, O(n) complexity |
| Hybrid (8T+12M) | 1.15M | +15% faster | -25% memory | +20% faster | +5% | Best of both worlds |
| + RAG | Same | Same | +10% (context) | Same | +20-25% | 40-50% less hallucination |
Benchmarks on 8xH100 GPUs. Run the included benchmark scripts to reproduce.
| Architecture | Max Context | Documents | Memory (12GB GPU) |
|---|---|---|---|
| Transformer | 2K-4K | 3-5 docs | Baseline |
| Hybrid | 4K-8K | 8-10 docs | Fits with batch_size=4 |
| Mamba | 8K-32K | 15-20 docs | Fits with batch_size=4 |
The fastest way to feel the magic is to run the speedrun script speedrun.sh, which trains and inferences the $100 tier of nanochat. On an 8XH100 node at $24/hr, this gives a total run time of about 4 hours. Boot up a new 8XH100 GPU box from your favorite provider (e.g. I use and like Lambda), and kick off the training script:
bash speedrun.shAlternatively, since the script runs for 4 hours, I like to launch it like this inside a new screen session speedrun (and also log output to speedrun.log):
screen -L -Logfile speedrun.log -S speedrun bash speedrun.shSee the screen cheatsheet if you are less familiar. You can watch it go inside the screen session, or detach with Ctrl-a d and tail speedrun.log to view progress. Now wait 4 hours. Once it's done, you can talk to your LLM via the ChatGPT-like web UI. Make sure again that your local uv virtual environment is active (run source .venv/bin/activate), and serve it:
python -m scripts.chat_webAnd then visit the URL shown. Make sure to access it correctly, e.g. on Lambda use the public IP of the node you're on, followed by the port, so for example http://209.20.xxx.xxx:8000/, etc. Then talk to your LLM as you'd normally talk to ChatGPT! Get it to write stories or poems. Ask it to tell you who you are to see a hallucination. Ask it why the sky is blue. Or why it's green. The speedrun is a 4e19 FLOPs capability model so it's a bit like talking to a kindergartener :).
Train a Mamba or hybrid model for even better efficiency:
# Install Mamba dependencies (optional)
uv pip install mamba-ssm causal-conv1d triton
# Train pure Mamba model (faster, less memory)
torchrun --standalone --nproc_per_node=8 -m scripts.mid_train configs/mamba_d20.py
# Or train hybrid model (best of both worlds)
torchrun --standalone --nproc_per_node=8 -m scripts.mid_train configs/hybrid_early_t_late_m_d20.pySee QUICKSTART_MAMBA.md for details.
Add retrieval-augmented generation to reduce hallucination:
# Install RAG dependencies (optional - simple retriever works without)
uv pip install sentence-transformers faiss-cpu
# Create example knowledge base (2 minutes)
python -m scripts.prepare_rag_dataset --mode example --output data/rag_examples
# Fine-tune with RAG (3-4 hours on 8xH100)
torchrun --standalone --nproc_per_node=8 -m scripts.rag_finetune \
--knowledge_base data/rag_examples/knowledge_base \
--source midSee RAG_QUICKSTART.md for details on using your own documents.
You can also cat report.md file which appeared in the project directory and contains the "report card" of the run, i.e. a bunch of evaluations and metrics. At the very end, you'll see a summary table, for example:
- Characters: 333,989
- Lines: 8,304
- Files: 44
- Tokens (approx): 83,497
- Dependencies (uv.lock lines): 2,004
| Metric | BASE | MID | SFT | RL |
|---|---|---|---|---|
| CORE | 0.2219 | - | - | - |
| ARC-Challenge | - | 0.2875 | 0.2807 | - |
| ARC-Easy | - | 0.3561 | 0.3876 | - |
| GSM8K | - | 0.0250 | 0.0455 | 0.0758 |
| HumanEval | - | 0.0671 | 0.0854 | - |
| MMLU | - | 0.3111 | 0.3151 | - |
| ChatCORE | - | 0.0730 | 0.0884 | - |
Total wall clock time: 3h51m
(Your table might be missing the RL number by default). For a lot more information around the speedrun script and what to look for and expect, please refer to the walkthrough that I posted in Discussions of the repo: "Introducing nanochat: The best ChatGPT that $100 can buy".
Unsurprisingly, $100 is not enough to train a highly performant ChatGPT clone. In fact, LLMs are famous for their multi-million dollar capex. For our purposes, I think there are two more scales of interest. First is the ~$300 tier d26 model (i.e. depth=26) that trains in ~12 hours, which slightly outperforms GPT-2 CORE score. Second is the $1000 tier (~41.6 hours), just because it's a nice round number. But both of these are not yet fully supported and therefore not attached here in the master branch yet.
That said, to give a sense, the example changes needed for the speedrun.sh file to train a GPT-2 grade model d26 only involve three changes:
...
# you'll need to download more data shards for pretraining
# get the number of parameters, multiply 20 to get tokens, multiply by 4.8 to get chars,
# divide by 250 million to get number of shards. todo need to improve this...
python -m nanochat.dataset -n 450 &
...
# use --depth to increase model size. to not oom, halve device batch size 32 -> 16:
torchrun --standalone --nproc_per_node=8 -m scripts.base_train -- --depth=26 --device_batch_size=16
...
# make sure to use the same later during midtraining:
torchrun --standalone --nproc_per_node=8 -m scripts.mid_train -- --device_batch_size=16That's it! The biggest thing to pay attention to is making sure you have enough data shards to train on (the code will loop and do more epochs over the same training set otherwise, decreasing learning speed a bit), and managing your memory/VRAM, primarily by decreasing the device_batch_size until things fit (the scripts automatically compensates by increasing the number of gradient accumulation loops, simply turning parallel compute to sequential compute).
And a bit more about computing environments that will run nanochat:
- The code will run just fine on the Ampere 8XA100 GPU node as well, but a bit slower.
- All code will run just fine on even a single GPU by omitting
torchrun, and will produce ~identical results (code will automatically switch to gradient accumulation), but you'll have to wait 8 times longer. - If your GPU(s) have less than 80GB, you'll have to tune some of the hyperparameters or you will OOM / run out of VRAM. Look for
--device_batch_sizein the scripts and reduce it until things fit. E.g. from 32 (default) to 16, 8, 4, 2, or even 1. Less than that you'll have to know a bit more what you're doing and get more creative. - Most of the code is fairly vanilla PyTorch so it should run on anything that supports that - xpu, mps, or etc, but I haven't implemented this out of the box so it might take a bit of tinkering.
nanochat is designed to be short and sweet. One big advantage of this is that we can package up all of the files together and copy paste them to your favorite LLM to ask arbitrary questions. As an example, I like to package up the repo using the files-to-prompt utility like so:
files-to-prompt . -e py -e md -e rs -e html -e toml -e sh --ignore "*target*" --cxml > packaged.txtThis includes all py, rs, html, toml, sh files, excludes the rustbpe/target folder, and chooses the cxml output format. Everything is written to the packaged.txt file, which atm measures ~330KB (i.e. well below ~100K tokens for a state of the art LLM), and ~8K lines of code in 45 files.
Alternatively, I recommend using DeepWiki from Devin/Cognition to ask questions of this repo. In the URL of this repo, simply change github.com to deepwiki.com, and you're off.
I haven't invested too much here but some tests exist, especially for the tokenizer. Run e.g. as:
python -m pytest tests/test_rustbpe.py -v -snanochat is nowhere finished. The goal is to improve the state of the art in micro models that are accessible to work with end to end on budgets of < $1000 dollars. Accessibility is about overall cost but also about cognitive complexity - nanochat is not an exhaustively configurable LLM "framework"; there will be no giant configuration objects, model factories, or if-then-else monsters in the code base. It is a single, cohesive, minimal, readable, hackable, maximally-forkable "strong baseline" codebase designed to run start to end and produce a concrete ChatGPT clone and its report card.
- All original code and inspiration from karpathy/nanochat Β© Andrej Karpathy and contributors
- The name (nanochat) derives from nanoGPT, which only covered pretraining
- nanochat is also inspired by modded-nanoGPT, which gamified the nanoGPT repo with clear metrics and a leaderboard, and borrows a lot of its ideas and some implementation for pretraining
- Thank you to HuggingFace for fineweb and smoltalk
- Thank you Lambda for the compute used in developing the original project
- Thank you to chief LLM whisperer π§ββοΈ Alec Radford for advice/guidance
- Mamba Architecture: Based on "Mamba: Linear-Time Sequence Modeling with Selective State Spaces" by Gu & Dao
- mamba-ssm: Official Mamba implementation (state-spaces/mamba)
- RAG Methodology: Based on "Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks" by Lewis et al.
- FAISS: Efficient similarity search by Facebook AI Research
- sentence-transformers: Dense retrieval embeddings by UKPLab
This repository tracks the upstream nanochat project periodically. Pull requests improving the Mamba integration, RAG capabilities, or adding new lightweight architectures are welcome!
If you find nanochat helpful in your research cite simply as:
@misc{nanochat,
author = {Andrej Karpathy},
title = {nanochat: The best ChatGPT that $100 can buy},
year = {2025},
publisher = {GitHub},
url = {https://github.com/karpathy/nanochat}
}This project is licensed under the MIT License β see LICENSE file for details.
All original code and inspiration from karpathy/nanochat Β© Andrej Karpathy and contributors.
This variant extends the original nanochat with:
- Mamba architecture support (linear complexity, 3-5x performance boost)
- RAG/REFRAG capabilities (40-50% hallucination reduction)
- Hybrid model configurations
- Comprehensive documentation and testing
Both the original nanochat and these enhancements are MIT licensed and free to use, modify, and distribute.
- This repository tracks the upstream karpathy/nanochat project periodically
- Pull requests improving the Mamba integration, RAG capabilities, or adding new lightweight architectures are welcome!
- For questions about the original nanochat, see the upstream repository
- For questions about Mamba/RAG features, see the documentation in this repository
