nanoVLM is the simplest way to get started with training your very own Vision Language Model (VLM) using pure PyTorch. It is lightweight toolkit which allows you to launch a VLM training on a free tier colab notebook.
We were inspired by Andrej Karpathy’s nanoGPT, and provide a similar project for the vision domain.
According to recent reports, at its heart, nanoVLM is a toolkit that helps you build and train a model that can understand both images and text, and then generate text based on that. The beauty of nanoVLM lies in its simplicity. The entire codebase is intentionally kept minimal and readable, making it perfect for beginners or anyone who wants to peek under the hood of VLMs without getting overwhelmed. (Related: How to train a newly introduced language model from scr...)
In this blog post, we cover the core ideas behind the project and provide a simple way to interact with the repository. We not only go into the details of the project but also encapsulate all of it so that you can promptly get started. (Related: Simple considerations for simple people building fancy ...)
You can start training a Vision Language Model using our nanoVLM toolkit by following these steps:.
Here is a Colab notebook that will help you launch a training run with no local setup required!
It has been revealed that as the name suggests, a Vision Language Model (VLM) is a multi-modal model that processes two modalities: vision and text. These models typically take images and/or text as input and generate text as output.
Generating text (output) conditioned on the understanding of images and texts (inputs) is a powerful paradigm. It enables a wide range of applications, from image captioning and object detection to answering questions about visual content (as shown in the table below). One thing to note is that nanoVLM focuses only on Visual Question Answering as the training objective.
If you are interested in learning more about VLMs, we strongly recommend reading our latest blog on the topic: Vision Language Models (Better, Faster, Stronger).
"Talk is cheap, show me the code" - Linus Torvalds.
In this section, we’ll guide you through the codebase. It’s helpful to keep a tab open for reference as you follow along.
In a recent development, below is the folder structure of our repository. We have removed helper files for brevity.
We model nanoVLM after two well known and widely used architectures. Our vision backbone (models/vision_transformer.py) is the standard vision transformer, more specifically Google’s SigLIP vision encoder. Our language backbone follows the Llama 3 architecture.
According to recent reports, the vision and text modalities are aligned using a Modality Projection module. This module takes the image embeddings produced by the vision backbone as input, and transforms them into embeddings compatible with the text embeddings from the embedding layer of the language model. These embeddings are then concatenated and fed into the language decoder. The Modality Projection module consists of a pixel shuffle operation followed by a linear layer.
Pixel shuffle reduces the number of image tokens, which helps reduce computational cost and speeds up training, especially for transformer-based language decoders which are sensitive to input length. The figure below demonstrates the concept.
Industry observers note that all the files are very lightweight and well documented. We highly encourage you to check them out individually to get a better understanding of the implementation details (models/xxx.py).
While training, we use the following pre-trained backbone weights:.
One could also swap out the backbones with other variants of SigLIP/SigLIP 2 (for the vision backbone) and SmolLM2 (for the language backbone).
Now that we are familiar with the architecture, let's shift gears and talk about how to train your own Vision Language Model using train.py.
You can kick off training with:.
This script is your one-stop shop for the entire training pipeline, including:.
Before anything else, the script loads two configuration classes from models/config.py:.
Recent findings suggest that at the heart of the data pipeline is the get_dataloaders function. It:.
A helpful flag here is data_cutoff_idx, useful for debugging on small subsets.
The model is built via the VisionLanguageModel class. If you're resuming from a checkpoint, it’s as easy as:.
Otherwise, you get a freshly initialized model with optionally preloaded backbones for both vision and language.
Because the modality projector (MP) is freshly initialized while the backbones are pre-trained, the optimizer is split into two parameter groups, each with its own learning rate:.
This balance ensures the MP learns promptly while preserving knowledge in the vision and language backbones.
This part is fairly standard but thoughtfully structured:.
Every 250 steps (configurable), the model is evaluated on the validation and MMStar test datasets. If accuracy improves, the model is checkpointed.
If log_wandb is enabled, training stats like batch_loss, val_loss, accuracy, and tokens_per_second are logged to Weights & Biases for real-time tracking.
Runs are auto-named using metadata like sample size, batch size, epoch count, learning rates, and the date, all handled by the helper get_run_name.
Use the following to push the trained model to the Hub for others to find and test:.
You can easily push them using:.
It has been revealed that using nanoVLM as the toolkit, we have trained a model and published it to Hub. We have used the google/siglip-base-patch16-224 and HuggingFaceTB/SmolLM2-135M as backbones. The model was trained this for ~6h on a single H100 GPU on ~1.7M samples of the cauldron.
This model isn't intended to compete with SoTA models, but rather to demystify the components and training process of VLMs.
Sources indicate that let’s run inference on the trained model using the generate.py script. You can run the generation script using the following command:.
This will use the default arguments and run the query “What is this?” on the image assets/image.png.You can use this script on your own images and prompts like so:.
If you want to visualize the heart of the script, it is just these lines:.
We create the model and set it to eval. Initialize the tokenizer, which tokenizes the text prompt, and the image processor, which is used to process the images. The next step is to process the inputs and run model.generate to generate the output text. Finally, decode the output using batch_decode.
If you want to run inference on the trained model in a UI interface, here is the Hugging Face Space for you to interact with the model.
In this blog post, we walked through what VLMs are, explored the architecture choices that power nanoVLM, and unpacked the training and inference workflows in detail.
Recent findings suggest that by keeping the codebase lightweight and readable, nanoVLM aims to serve as both a learning tool and a foundation you can build upon. Whether you’re looking to understand how multi-modal inputs are aligned, or you want to train a VLM on your own dataset, this repository gives you a head start.
If you try it out, build on top of it, or just have questions we’d love to hear from you. Happy tinkering!
I’ve been working on a fork of Nano-vLLM called Nano-vLLM-v1, which re-engineers the core architecture to closely reproduce the vLLM v1 scheduler and introduces Chunked Prefill for better performance.
The goal was to build a lightweight, readable, yet highly efficient inference engine that stays true to the original vLLM design while being easy to understand and extend.
Check it out here: https://github.com/slwang-ustc/nano-vllm-v1/tree/main.
In a recent development, I’d love for the community to try it out, give feedback, or contribute! The code is designed to be readable and modular, making it easy to experiment with upcoming features or optimizations.
If you’re interested in lightweight, high-performance LLM inference without the complexity, give it a star ⭐ and let me know what you think!