[!NOTE] nano-scGPT is now part of nanomirrors, clean PyTorch implementations of state-of-the-art bio cell models behind one unified API and framework.
The simplest, fastest repository for scGPT inference, (soon) finetuning and training, with minimal dependencies. It reimplements the original scGPT from scratch. nano_scgpt/model.py is pure PyTorch in ~270 lines of code, and nano_scgpt/scGPT_tokenizer.py turns raw scRNA data into model input. Small enough to read in one sitting and hack on.
Embeddings are numerically equivalent to the original scGPT.
Cell modeling is potentially the most exciting and under-indexed AI/ML area. The hope is to make state-of-the-art cell models more accessible to run, understand, and tinker with.
I reproduced the original scGPT on perturbation response prediction and identified a potential gene sampling flaw in the original scGPT tutorial. For each training batch, when the number of genes n exceeds max_length (default 1536), the tutorial randomly samples genes, which means the actual perturbed gene gets dropped with probability (n - max_length) / n. With n ~5000 highly variable genes and max_length ~1536, this drops the perturbation signal in ~ 70% of training examples, so the model is effectively trained to treat perturbed cells as unperturbed most of the time.
I fixed this by always keeping the perturbed gene(s) and sampling the rest of the gene set per training example. You can toggle this sampling fix via --keep_genes_per_cell.
Interestingly, across most metrics and datasets, the fix leaves performance roughly flat or slightly lower than the original sampling. One of the biggest gain we see is on Replogle K562/Pearson Delta (all genes), which improves from 0.275 to 0.316.
Results are computed from 5 independent runs each with different data splits and seeds.
nano-scGPT produces embeddings numerically equivalent to the original
while running 1.38x faster, mostly from a clean forward pass plus torch.compile.
| Model | Total (65,847 cells) | Per cell | Throughput | Speedup |
|---|---|---|---|---|
| nano-scGPT | 77.7 s | 1.18 ms | 847 cells/s | 1.38× |
| scGPT | 107.0 s | 1.63 ms | 615 cells/s | 1.00× |
benchmarked under: single A100, batch size 256, AMP autocast, 10 batch warmup, Tabula Sapiens lung (65,847 cells).
git clone https://github.com/Danqi7/nano-scGPT.git
cd nano-scGPTuv sync
source .venv/bin/activatepython -m venv .venv && source .venv/bin/activate
pip install -e .# scGPT Embedding example
import numpy as np
from nano_scgpt.scGPT_tokenizer import scGPTTokenizer
from nano_scgpt.model import scGPTModel
model = scGPTModel.from_pretrained("scGPT_human") # weights pulled from HF hub
model.eval()
tokenizer = scGPTTokenizer.from_pretrained("scGPT_human")
genes = ['DUX4L30', 'CTB-52I2.4', 'USP17L16P', 'RPL7P23']
exprs = np.array([[1.0, 0.0, 23.0, 6.0], [0.0, 0.0, 3.0, 7.0]])
encoded = tokenizer.encode(exprs, genes)
embeddings = model.encode(encoded["gene_ids"], encoded["exprs"], encoded["padding_mask"]) # [N, D_embd]# Example: Tabula Sapiens lung data (downloaded automatically)
python tasks/embedding.py
# Or on your own local file
python tasks/embedding.py \
--input <path to local .h5ad file> \
--output <path to save embeddings>
# Or from a remote URL
python tasks/embedding.py \
--input_url <URL to a remote .h5ad file> \
--output <path to save embeddings># Example: finetune/evaluate on adamson dataset (downloaded automatically)
python tasks/finetune_perturbation.py \
--data adamson \
--mode <train/eval> \
--load_splits \
--pre_normalized \
--keep_genes_per_cell # skip this if want og scGPT sampling
# Or on your custom dataset
python tasks/finetune_perturbation.py \
--data_file <path to local .h5ad file> \
--gene_symbol_col <column name in adata.var that contains gene symbols> \
--condition_col <column name in adata.obs that contains condition names> \
--mode train \
--pre_normalized \
--load_splits \
--keep_genes_per_cell
# Example: Predict purturbation results using trained model
python tasks/finetune_perturbation.py \
--data adamson \
--mode predict \
--perturbation 'AARS+ctrl' \
--load_splits \
--pre_normalized \
--keep_genes_per_cellIf using custom data, prepare your AnnData such that the condition column in
adata.obs encodes perturbations in the following format:
| Type | Format | Example |
|---|---|---|
| Control | ctrl |
ctrl |
| Single | gene+ctrl |
KLF1+ctrl |
| Double | gene1+gene2 |
KLF1+FOXA1 |
So control-conditioned cells use ctrl, single-gene perturbations use gene+ctrl, and double-gene perturbations use gene1+gene2. Matching is case-sensitive.
The built-in datasets (adamson, norman, replogle_k562_essential, replogle_rpe1_essential) already follow this convention.
- Finetuning for perturbation response prediction
- Training from scratch
Let me know what tasks or even models you'd like to see next!
- This repository reimplements scGPT from scratch. All credit for the original model and method goes to the authors (Cui et al., Nature Methods, 2024). See the original repo and paper.
- nano-scGPT is inspired by Andrej Karpathy's nanoGPT and Chris Hayduk's minAlphaFold2.
- The perturbation data split is adapted from the GEAR codebase.
MIT. See LICENSE

