lucidrains/vit-pytorch
PyTorch Vision Transformers for classification, pretraining, and architecture experiments
Automated SPDX check for reference only — not legal advice. Disclaimer
GitHub Star History
Full-history star trajectory from GitHub official history. Switch ranges or hover any point for exact counts.
Evidence-Backed Technical Brief
AI Deep ReviewArchitecture & Core Mechanics
vit-pytorch is an in-process Python library built around PyTorch model classes. The base ViT projects image patches into tokens, adds positional information, runs a Transformer encoder, and uses either the CLS token or mean pooling to produce classification logits. SimpleViT instead uses fixed 2D sinusoidal positions, global average pooling without a CLS token, and a linear classifier.
The extension points are Python imports and model composition rather than a plugin protocol. Variants live in modules under vit_pytorch/, such as na_vit and cross_vit, and training methods such as SimMIM wrap an encoder. The tree includes examples/, tests/, and train_vit_decorr.py; the package is built with setuptools and wheel. The documented integration path is direct tensor input and PyTorch autograd, not a separate model service.
Key Capabilities & Differentiators
NaViT handles mixed-resolution images by packing their patch sequences and masking attention across image boundaries. Callers can group images manually or pass a flat list with group_images=True and set group_max_seq_len. The implementation also exposes token dropout and uses factorized 2D positions and query-key normalization. A separate nested-tensor variant reduces explicit masking and padding and specifies PyTorch 2.5.
The training wrappers cover different tasks: SimMIM linearly projects features at masked positions back into pixel space and returns an L1 reconstruction loss, while DistillWrapper supports hard or soft teacher targets and exposes temperature and loss weighting. The architecture variants also change substantive parts of the model: DeepViT mixes attention heads after softmax, CaiT defers CLS-to-patch attention to later layers, CrossViT couples two patch scales, and CvT uses convolution for embedding, downsampling, and attention projections.
Trade-offs & Limitations
The library is a good fit for changing model internals or running controlled comparisons, but examples that produce logits on random tensors do not supply trained weights or a comparable training recipe. If the priority is getting a pretrained classifier into a baseline quickly, Ross Wightman's PyTorch model collection is the more direct starting point. For experiments here, budget for preprocessing, comparable training settings, and variant-specific input and output checks.
Standard global attention scales quadratically with token count, so increasing resolution or reducing patch size can drive up compute and memory even at the same image batch size. NaViT gives more control over mixed-resolution inputs, but adds data-loader and sequence-budget decisions. Verify the cost of the selected packed or nested-tensor path on representative batches. MIT licensing simplifies reuse of the repository code; it does not determine the terms for external checkpoints or datasets.
Hardware & Deployment Guide
Install with pip install vit-pytorch and import the desired class into a Python process. Package metadata declares Python 3.8 or newer, but that floor is not a compatibility guarantee for every PyTorch build or variant; nested-tensor NaViT explicitly calls for PyTorch 2.5. The base tensor forward pass does not call an external service. For offline runs, stage packages, datasets, and any external weights in advance. Size hardware against the chosen model and input workload, and measure training separately from inference.
The supplied release list records v1.17.8 on 2026-02-11, fixing propagation of use_flash_attn and a duplicate mask reshape in ViViT. The supplied pyproject.toml has version 1.26.16, so the manifest and release list identify different versions. Pin the exact package or checkout you intend to run, then smoke-test the selected model's input shapes and attention path on the target PyTorch build.
ViT supports CLS or mean pooling; SimpleViT uses 2D sinusoidal positions, global average pooling, and a linear head.vit_pytorch.na_vit_nested_tensor.NaViT reduces explicit masking and padding and specifies PyTorch 2.5.SimMIM projects masked-token features into pixel space and computes L1 reconstruction loss.DistillWrapper supports hard or soft targets; DistillableViT.to_vit() returns a standard ViT after training.PyTorch teams comparing ViT architectures, experimenting with mixed-resolution batching, or integrating masked pretraining or distillation into an existing training loop.
You primarily need a pretrained model catalog, a complete training-to-serving pipeline, or turnkey inference with a validated hardware profile.
Quick Start & Installation
$ pip install vit-pytorchComparison vs. Top Voice, Vision & Video Alternatives
Full comparison →| Repository | Stars | 7d Trend | Stack | License | Core Positioning |
|---|---|---|---|---|---|
| vit-pytorch ★ | 25.6k | +13 | Python | MIT | PyTorch Vision Transformers for classification, pretraining, and architecture ex |
| pytorch-grad-cam | 13k | +4 | Python | MIT | PyTorch CAM SDK for visual attribution across CNNs and vision transformers |
| pytorch-image-models | 37.3k | +12 | Python | Apache-2.0 | PyTorch vision SDK for swapping, training, and deploying image backbones |
| mlx-audio | 8.4k | +32 | Python | MIT | MLX inference SDK for local speech and audio pipelines on Apple Silicon |
AI Context Briefing (for Claude / Cursor / Windsurf)
Standardized architecture summary & quickstart commands ready to paste into coding agents.
curl -sL https://repoforai.com/repo/lucidrains/vit-pytorch.md
Active README embeds unlock Hot-Tier 6h Telemetry Refresh automatically (zero pay-to-rank).
Frequently Asked Questions about vit-pytorch
How do I integrate the base model into an existing training loop?
ViT or SimpleViT as a PyTorch model, pass a batch of image tensors shaped [B, C, H, W], and use the [B, num_classes] logits in your classification loss. Match image dimensions to the selected model's patch constraints and handle preprocessing, optimization, and checkpointing in your application. Masked pretraining is a separate path: SimMIM wraps a ViT encoder and returns a loss rather than class logits.How should I estimate GPU and VRAM requirements?
Can we self-host this in a commercial product?
When should I choose this over Ross Wightman's pretrained-model collection?
vit-pytorch when the experimental variable is the implementation itself: tokenization, attention, pooling, distillation, or image packing. If the goal is to fine-tune or evaluate an existing pretrained classifier quickly, start with a pretrained-model library such as Ross Wightman's PyTorch model collection. Match the architecture, preprocessing, and checkpoint format before assuming weights will transfer.Upstream README & Documentation
This profile focuses on independent engineering analysis and live telemetry. Read the full upstream README, changelog, and examples directly on GitHub.
Top 10 Breakout Open-Source AI Repos, Every Thursday
Signal-only digest of fast-growing AI repos, SPDX license alerts, and architecture picks. Zero spam.
Zero spam · 1-click unsubscribe · Privacy Policy