← Back to Leaderboard
Vlucidrains

lucidrains/vit-pytorch

PyTorch Vision Transformers for classification, pretraining, and architecture experiments

View on GitHub ↗
AI FrameworkQuickstart ↓
Engineering Health (Methodology)
A+ · 89/100
Maintenance Activity
● Updated 8d ago
Latest Release
1.17.8
Commercial License Check
MIT · Commercial Friendly

Automated SPDX check for reference only — not legal advice. Disclaimer

Maintenance35/35
Fork Adoption23/25
Release Cadence11/20
License Clarity20/20
CLI
Total Stars
★ 25,603
24h Growth
+1
7d Growth
+13
Forks
3,487

GitHub Star History

Full-history star trajectory from GitHub official history. Switch ranges or hover any point for exact counts.

lucidrains/vit-pytorch Star History (2020-10-02 - 2026-12-04) ALL-TIME STAR HISTORY (2020-10 – 2026-12) +25.6k in 2254d · avg +11/day 27k 13.5k 0 2020-10-02: ★ 12021-02-20: ★ 2,4562021-07-10: ★ 4,5932021-11-27: ★ 6,2932022-04-16: ★ 8,8812022-09-03: ★ 10,7072023-01-21: ★ 12,0922023-06-10: ★ 13,7542023-10-28: ★ 15,1222024-03-16: ★ 16,8542024-08-03: ★ 18,6232024-12-21: ★ 20,6632025-05-10: ★ 22,3652025-09-27: ★ 23,6732026-02-14: ★ 24,7522026-07-04: ★ 25,2822026-12-04: ★ 25,603 ★ 25.6k 2020-10 2023-11 2026-12

Evidence-Backed Technical Brief

AI Deep Review

Architecture & 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.

Classifier baseline: ViT supports CLS or mean pooling; SimpleViT uses 2D sinusoidal positions, global average pooling, and a linear head.
Mixed-resolution batching: NaViT packs patch sequences with attention masks and supports automatic grouping under a configurable sequence-length budget.
Nested-tensor variant: vit_pytorch.na_vit_nested_tensor.NaViT reduces explicit masking and padding and specifies PyTorch 2.5.
Masked pretraining: SimMIM projects masked-token features into pixel space and computes L1 reconstruction loss.
Teacher-student training: DistillWrapper supports hard or soft targets; DistillableViT.to_vit() returns a standard ViT after training.
Architecture comparisons: CrossViT exchanges information across patch scales; CCT and CvT offer convolutional approaches to tokenization or feature processing.
Best For

PyTorch teams comparing ViT architectures, experimenting with mixed-resolution batching, or integrating masked pretraining or distillation into an existing training loop.

Skip If

You primarily need a pretrained model catalog, a complete training-to-serving pipeline, or turnkey inference with a validated hardware profile.

Reviewed on 2026-12-04 · Grounded in GitHub README, root file tree & releasesMethodology

Quick Start & Installation

CLI
$ pip install vit-pytorch

Comparison vs. Top Voice, Vision & Video Alternatives

Full comparison →
RepositoryStars7d TrendStackLicenseCore Positioning
vit-pytorch ★25.6k+13PythonMITPyTorch Vision Transformers for classification, pretraining, and architecture ex
pytorch-grad-cam13k+4PythonMITPyTorch CAM SDK for visual attribution across CNNs and vision transformers
pytorch-image-models37.3k+12PythonApache-2.0PyTorch vision SDK for swapping, training, and deploying image backbones
mlx-audio8.4k+32PythonMITMLX 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.

Raw .md ↗
curl -sL https://repoforai.com/repo/lucidrains/vit-pytorch.md

Embed Star History Chart & Badges in README.md

Full-history Star History SVGs and velocity cards on GitHub Camo: drop-in replacement for OSSInsight and Star History widgets.

lucidrains/vit-pytorch Star History on RepoForAI

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?

Instantiate 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?

There is no fixed VRAM requirement. Measure peak memory with the intended image sizes, patch size, width, depth, precision, and batch size, including backward and optimizer steps for training. Smaller patches create more tokens at the same resolution; global attention therefore gets substantially more expensive. For NaViT, profile packed sequence lengths and the actual attention path rather than using image count alone as a sizing proxy.

Can we self-host this in a commercial product?

The repository uses the MIT license, which permits commercial use, modification, and redistribution subject to retaining the required copyright and license notices. The model code can run in a local PyTorch process without a hosted API. Check the terms of any external checkpoints, teacher weights, and datasets separately before redistributing or deploying them.

When should I choose this over Ross Wightman's pretrained-model collection?

Prefer 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.

Maintaining lucidrains/vit-pytorch? Pin to Homepage + competitor /alternatives pages with a custom Cloud/Demo CTA.Feature lucidrains/vit-pytorch on Homepage & Alternatives ($19)
WEEKLY BREAKOUT DIGEST

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

Saved in your browser · Export Markdown RFC tables or jump into /vs compare