Paper · 2022-07-21 · cs.CV
TinyViT: Fast Pretraining Distillation for Small Vision Transformers
TinyViT pretrains small hierarchical vision transformers on ImageNet-21k by distilling from a large teacher. The teacher's top-K soft labels and the augmentation seeds are stored once on disk, so each student trains without running the teacher. Model sizes come from progressive model contraction. Author-reported: 84.8% ImageNet-1k top-1 with 21M parameters at 224px, and 86.5% at 512px.
Knowledge distillationVision Transformer (ViT) backbonesEfficient vision backbonesvision-transformerknowledge-distillationpretrainingmodel-compressionimage-classification
arXivPDFCodeMarkdown16 min read
AI-compiled by claude-haiku-5-5 on 2026-10-18, not yet reviewed by a human. Every claim cites its source; check it when a detail matters.
In plain words
Large vision transformers do well on image benchmarks, but they are too big for phones and small devices. A small model trained on the same data usually does not catch up, because it runs out of capacity to use the extra data. This paper asks whether a small model can still benefit from a huge pretraining dataset if a big model teaches it.
The usual way to teach is to run the big "teacher" model on every training batch, which is slow and needs a lot of memory. TinyViT avoids this. The teacher is run once over the dataset, and for each augmented image it keeps only its most likely answers, plus a small random seed that lets the exact same augmentation be rebuilt later. A small student is then trained against these saved answers, with no teacher in memory.
The authors also build a family of small models by starting from a large design and shrinking it step by step, keeping the versions that meet a size and speed budget. The resulting models reach strong ImageNet accuracy for their size, and the authors report that they also transfer well to other tasks.
TL;DR
- Pretraining with distillation lets small vision transformers use large datasets better than plain pretraining does §1 tab. 2.
- Teacher outputs are stored in advance as the top-K probabilities per augmented image, together with a 4-byte seed that replays the augmentation, so the teacher is not run during student training §3.1 eq. 1 eq. 2.
- The TinyViT family (5M, 11M, 21M parameters) is derived from a large seed by progressive model contraction §3.2.
- Author-reported: TinyViT-21M reaches 84.8% ImageNet-1k top-1 at 224px after distillation on ImageNet-21k, and 86.5% at 512px tab. 4.
Problem
Pretraining on very large datasets helps large models far more than small ones: plain pretraining on ImageNet-21k gave the small DeiT-Ti, DeiT-S and Swin-T only 0.6 to 0.8 points on ImageNet-1k tab. 2. Distilling from a large teacher during pretraining adds 2.1 to 2.2 points tab. 2.
The cost is in the teacher. Running a large teacher on every batch uses most of the compute and GPU memory, which forces small batch sizes and slows student training §1 §3.1. The authors also note that a small model struggles to fit hard, noisy samples in ImageNet-21k, and that ground-truth labels in that dataset overlap (for example "chair" and "furniture") §4 app. C.
The setting assumes access to a strong pretrained teacher, a dataset of this size, and disk space for the stored labels (16 GB for ImageNet-1k with 300 epochs, 481 GB for ImageNet-21k with 90 epochs, as reported by the authors §5.2).
Method
TinyViT has two parts that are independent of each other: a fast distillation procedure §3.1 and a family of architectures §3.2.
Part 1: fast pretraining distillation §3.1.
- Before student training, pass each augmented training image through the teacher once. The augmentation is a strong pipeline (RandAugment, CutMix and others) §3.1.
- Keep the top-K values of the teacher's output and their class indices, and discard the rest. The authors store these sparse labels instead of all C logits §3.1 eq. 2.
- Store the augmentation as one seed. A pseudo-random generator, for which the paper names PCG as a common choice, turns the seed back into the same augmentation parameters during training §3.1.
- During student training, load the stored indices, values and seed, rebuild the augmented image, rebuild the sparse teacher distribution with the formula in eq. 2, and minimise the cross-entropy between the teacher distribution and the student output eq. 1.
The student loss uses no ground-truth labels. The authors explain this as a choice that avoids the overlap between ImageNet-21k labels §3.1 app. C.
Part 2: model family by progressive model contraction §3.2.
- Use a hierarchical base with four stages. The stem is two 3×3 stride-2 convolutions. Stage 1 uses MBConv blocks, and the last three stages use transformer blocks with window attention §3.2 app. A.
- Define contraction factors: stage embedding widths, stage depths, window sizes, the MBConv expansion ratio, the MLP expansion ratio and the head dimension §3.2.
- From the current model, generate smaller candidates by changing the factors. Keep the candidates that meet the parameter and throughput limits, train and validate them, and continue from the most accurate one until the target size is reached §3.2 app. A.
The authors describe step 7 as a constrained local search over the contraction factors §3.2. The paper does not give the search budget, the number of candidates per step, or the validation data beyond the description in app. A.
The factors shared by all three TinyViT sizes are listed in the paper §3.2 app. A. Each size uses its own embedding widths:
# Shared factors and per-size widths [§3.2] [app. A]
# Depths (2, 2, 6, 2); window sizes (7, 7, 14, 7); MBConv expansion 4;
# MLP expansion 4; head dimension 32.
# TinyViT-21M: embed dims (96, 192, 384, 576)
# TinyViT-11M: embed dims (64, 128, 256, 448)
# TinyViT-5M: embed dims (64, 128, 160, 320)The distillation step. The pseudocode below follows the description in §3.1 and the code path in main.py. It is written to be read, not run.
# Store teacher labels once, before pretraining [§3.1] [eq. 2]
# [code: TinyViT/save_logits.py#L103-L160]
# Input: images from ImageNet-21k; teacher T; sparsity K (K = 100)
# Output: per augmented view: a seed and K (index, probability) pairs
def store_teacher_labels(dataset, teacher, K):
for image in dataset:
# Draw one seed; it fixes the augmentation [§3.1]
seed = draw_seed() # int32, 4 bytes
view = augment(image, seed) # strong augmentation
# Teacher output after softmax, temperature 1.0 [§5.1]
probs = softmax(teacher(view)) # (C,)
# Keep only the K largest probabilities [§3.1] [eq. 2]
values, indices = top_k(probs, K) # (K,), (K,)
save(seed, indices, values)# One student step from stored labels [§3.1] [eq. 1] [eq. 2]
# [code: TinyViT/main.py#L284-L330]
# Shapes: images (B, 3, H, W); values, indices (B, K); C classes
def distill_step(student, images, seeds, values, indices):
# Rebuild the same augmented view from the stored seed [§3.1]
views = replay_augment(images, seeds) # (B, 3, H, W)
logits = student(views) # (B, C)
# Spread the leftover mass evenly over the other C - K classes [eq. 2]
rest = (1 - sum(values, axis=-1, keepdims=True)) / (C - K) # (B, 1)
teacher = broadcast(rest, (B, C)) # (B, C)
teacher = scatter(teacher, indices, values) # (B, C)
# Soft cross-entropy against the teacher; no ground-truth labels [eq. 1]
loss = mean(-sum(teacher * log_softmax(logits), axis=-1))
return lossKey equations as stated in the paper:
\mathcal{L} = CE\left(\hat{\mathbf{y}}, S(\mathcal{A}(x))\right) eq. 1
where \hat{\mathbf{y}} = T(\mathcal{A}(x)) is the teacher output for the augmented image, S is the student, and CE is cross-entropy §3.1.
\hat{y}_c = \begin{cases} \hat{y}_{\mathcal{I}(k)} & \text{if } c = \mathcal{I}(k), \\ \dfrac{1 - \sum_{k=1}^{K} \hat{y}_{\mathcal{I}(k)}}{C - K} & \text{otherwise} \end{cases} eq. 2
where \mathcal{I}(k) are the stored indices of the top-K entries §3.1.
Architecture details §3.2 app. A.
- Stem: two 3×3 convolutions, stride 2, padding 1, each followed by batch norm, with GELU between them tiny_vit.py.
- Stage 1 uses MBConv blocks with expansion ratio 4 §3.2 tiny_vit.py tiny_vit.py. Each downsampling layer is a PatchMerging block in the code: 1×1 convolution, depthwise 3×3 convolution with stride 2, then 1×1 convolution, all at the output width. The paper's table labels these layers MBConv app. A tiny_vit.py.
- Stages 2 to 4 use transformer blocks with window attention. Each block has a learned attention bias per head and relative offset tiny_vit.py. A 3×3 depthwise convolution sits between attention and the MLP, with a residual connection after each sub-block §3.2 tiny_vit.py tiny_vit.py.
- Normalisation: batch norm for convolutions and LayerNorm for linear layers. Activation: GELU §3.2.
- Head: average over tokens, then LayerNorm, then a linear classifier app. A tiny_vit.py.
Hyperparameters
Values marked "code" were not in the paper text and are read from the official repository at code_commit. Code learning rates are written per 512 images and scaled linearly by the total batch size main.py.
Pretraining on ImageNet-21k with distillation
| Name | Value | Where | Notes |
|---|---|---|---|
| epochs | 90 | §5.1 tiny_vit_21m_22k_distill.yaml | |
| batch size | 4096 | §5.1 | code: 128 per GPU, 32 GPUs in the documented command TRAINING.md |
| optimizer | AdamW | §5.1 | betas (0.9, 0.999) and eps 1e-8 are code defaults config.py |
| base learning rate | 0.002 | §5.1 | code: BASE_LR 2.5e-4 per 512 images, scaled to 2e-3 at batch 4096 tiny_vit_21m_22k_distill.yaml |
| LR schedule | cosine, 5 epochs warm-up | §5.1 tiny_vit_21m_22k_distill.yaml | warm-up start LR and minimum LR not stated; code defaults 5e-7 and 5e-6 per 512 images config.py |
| weight decay | 0.01 | §5.1 tiny_vit_21m_22k_distill.yaml | |
| gradient clipping | max norm 5 | §5.1 config.py | |
| stochastic depth | 0 for 5M and 11M, 0.1 for 21M | §5.1 tiny_vit_21m_22k_distill.yaml | |
| mixup / cutmix | disabled | §5.1 tiny_vit_21m_22k_distill.yaml | |
| teacher top-K | K = 100 (ImageNet-21k) | §5.2 | code default 100 config.py |
| distillation temperature | 1.0 | §5.1 | code applies softmax without a temperature save_logits.py |
| teacher | CLIP-ViT-L/14, fine-tuned on ImageNet-21k for 30 epochs | §5.1 | code: config file for this teacher only clip_vit_large_patch14_22k.yaml |
| RandAugment | policy not stated | §5.1 | code: rand-m9-mstd0.5-inc1 config.py |
| random erasing | probability not stated | §5.1 | code: 0.25 config.py |
| colour jitter | strength not stated | §5.1 | code: 0.4 config.py |
| image interpolation | not stated | §5.1 | code: bicubic config.py |
Fine-tuning on ImageNet-1k from the pretrained model
| Name | Value | Where | Notes |
|---|---|---|---|
| epochs | 30 | §5.1 tiny_vit_21m_22kto1k.yaml | |
| batch size | 1024 | §5.1 | |
| base learning rate | 5e-4 | §5.1 | code: BASE_LR 2.5e-4 per 512, scaled to 5e-4 at 1024 tiny_vit_21m_22kto1k.yaml |
| LR schedule | cosine, 5 epochs warm-up | §5.1 | minimum LR 1e-5 per 512 images in code tiny_vit_21m_22kto1k.yaml |
| weight decay | 1e-8 | §5.1 tiny_vit_21m_22kto1k.yaml | |
| layer-wise LR decay | 0.8 (output to input) | §5.1 tiny_vit_21m_22kto1k.yaml | |
| batch norm statistics | frozen | §5.1 | code: EVAL_BN_WHEN_TRAINING True tiny_vit_21m_22kto1k.yaml |
| mixup / cutmix | disabled | §5.1 | code sets both to 0.0 tiny_vit_21m_22kto1k.yaml |
| label smoothing | not stated | §5.1 | code default 0.1 is active because no mixup is used config.py main.py |
| stochastic depth | 0.1 | not stated in paper | tiny_vit_21m_22kto1k.yaml |
Higher-resolution fine-tuning
| Name | Value | Where | Notes |
|---|---|---|---|
| window sizes at 384px | (12, 12, 24, 12) | §5.1 | tiny_vit_21m_224to384.yaml |
| window sizes at 512px | (16, 16, 32, 16) | §5.1 | tiny_vit_21m_384to512.yaml |
| epochs | 30 | §5.1 | |
| batch size | 1024, accumulated | §5.1 | |
| base learning rate | 4e-5 | §5.1 | code: BASE_LR 2e-5 per 512 images tiny_vit_21m_224to384.yaml |
| weight decay | 1e-8 | §5.1 | |
| warm-up | 5 epochs | §5.1 | |
| bilinear interpolation of attention biases | yes | §5.1 | code uses bicubic, see Paper vs code |
Training from scratch on ImageNet-1k
| Name | Value | Where | Notes |
|---|---|---|---|
| epochs | 300 | §5.1 | code default config.py |
| batch size | 1024 | §5.1 | |
| optimizer | AdamW | §5.1 | |
| base learning rate | 0.001 | §5.1 | code default 5e-4 per 512 images, scaled to 1e-3 at 1024 config.py |
| warm-up | 20 epochs | §5.1 | code default config.py |
| weight decay | 0.05 | §5.1 | code default config.py |
| gradient clipping | max norm 5.0 | §5.1 | |
| stochastic depth | 0.0 / 0.1 / 0.2 for 5M / 11M / 21M | §5.1 | 21M value tiny_vit_21m.yaml |
| mixup / cutmix | not stated | not stated in paper | code defaults 0.8 and 1.0 apply, since the 1k config does not override them config.py config.py |
Architecture and teacher-storage hyperparameters
| Name | Value | Where | Notes |
|---|---|---|---|
| stage depths | (2, 2, 6, 2) | §3.2 app. A | tiny_vit_21m_22k_distill.yaml |
| embed dims 21M | (96, 192, 384, 576) | §3.2 app. A | tiny_vit_21m_22k_distill.yaml |
| embed dims 11M | (64, 128, 256, 448) | §3.2 app. A | tiny_vit_11m_22k_distill.yaml |
| embed dims 5M | (64, 128, 160, 320) | §3.2 app. A | tiny_vit_5m_22k_distill.yaml |
| heads per stage | dimension / 32 in every stage | §3.2 (head dimension 32, γ_H) | 21M (3, 6, 12, 18); 11M (2, 4, 8, 14); 5M (2, 4, 5, 10) tiny_vit_21m_22k_distill.yaml |
| window sizes (224px) | (7, 7, 14, 7) | §3.2 app. A | tiny_vit_21m_22k_distill.yaml |
| MBConv expansion | 4 | §3.2 app. A | tiny_vit.py |
| MLP expansion | 4 | app. A | tiny_vit.py |
| stored teacher logits, ImageNet-1k | 16 GB, K = 10, 300 epochs | §5.2 | matches the repository README README.md |
| stored teacher logits, ImageNet-21k | 481 GB, K = 100, 90 epochs | §5.2 | matches the repository README README.md |
Results
Author-reported. All TinyViT numbers are from the paper; the setup is ImageNet-21k pretraining (about 14M images, 21,841 classes §4) followed by ImageNet-1k fine-tuning or training, with 224px input unless stated §5.1 tab. 4.
Headline ImageNet-1k classification tab. 4
Selected rows. The full table has more baselines (ResNet-18, PVT-Tiny, ResT-Small, CrossViT, GLiT, Mobile-Former, ViTAS, T2T-ViT, Swin-T and others). Throughput was measured by the authors on a V100 GPU tab. 4. "Distilled" means pretraining on ImageNet-21k with the proposed distillation using CLIP-ViT-L/14 as the teacher §5.3.
| Model | Top-1 (%) | Top-5 (%) | Params (M) | MACs (G) | Throughput (img/s) | Input | Arch |
|---|---|---|---|---|---|---|---|
| MobileViT-S | 78.4 | - | 6 | 1.8 | 2,661 | 256 | Hybrid |
| TinyViT-5M | 79.1 | 94.8 | 5.4 | 1.3 | 3,060 | 224 | Hybrid |
| TinyViT-5M, distilled | 80.7 | 95.6 | 5.4 | 1.3 | 3,060 | 224 | Hybrid |
| LeViT-256 | 81.6 | - | 19 | 1.1 | 7,386 | 224 | Hybrid |
| CoaT-Lite Small | 81.9 | 95.6 | 20 | 4.0 | 1,138 | 224 | Trans |
| TinyViT-11M | 81.5 | 95.8 | 11 | 2.0 | 2,468 | 224 | Hybrid |
| TinyViT-11M, distilled | 83.2 | 96.5 | 11 | 2.0 | 2,468 | 224 | Hybrid |
| DeiT-S | 79.9 | 95.0 | 22 | 4.6 | 2,276 | 224 | Trans |
| Swin-T | 81.2 | 95.5 | 28 | 4.5 | 1,393 | 224 | Trans |
| AutoFormer-S | 81.7 | 95.7 | 23 | 5.1 | 1,341 | 224 | Trans |
| EfficientNet-B5 | 83.6 | 96.7 | 30 | 9.9 | 330 | 456 | CNN |
| TinyViT-21M | 83.1 | 96.5 | 21 | 4.3 | 1,571 | 224 | Hybrid |
| TinyViT-21M, distilled | 84.8 | 97.3 | 21 | 4.3 | 1,571 | 224 | Hybrid |
| TinyViT-21M, distilled, 384px | 86.2 | 97.8 | 21 | 13.8 | 394 | 384 | Hybrid |
| TinyViT-21M, distilled, 512px | 86.5 | 97.9 | 21 | 27.0 | 167 | 512 | Hybrid |
The text compares the distilled 21M model with Swin-B pretrained on ImageNet-21k (85.2% with 88M parameters) §1.
Pretraining distillation on existing small models tab. 2
| Model | Params (M) | Train on IN-1k | Pretrain IN-21k, no distill | Pretrain IN-21k, with distill |
|---|---|---|---|---|
| DeiT-Ti | 5 | 72.2 | 73.0 (+0.8) | 74.4 (+2.2) |
| DeiT-S | 22 | 79.9 | 80.5 (+0.6) | 82.0 (+2.1) |
| Swin-T | 28 | 81.2 | 81.9 (+0.7) | 83.4 (+2.2) |
Data cleaning and distillation on ImageNet-21k tab. 1
Top-1 on ImageNet-1k, ImageNet-Real and ImageNet-V2 after fine-tuning on ImageNet-1k tab. 1.
| # | Model | Pretraining data | IN-1k | IN-Real | IN-V2 |
|---|---|---|---|---|---|
| 0 | Swin-T | train from scratch on IN-1k | 81.2 | 86.7 | 69.7 |
| 1 | Swin-T | original IN-21k | 81.9 | 87.0 | 70.6 |
| 2 | Swin-T | cleaned IN-21k | 82.2 | 87.3 | 71.1 |
| 3 | Swin-T | original IN-21k, with distillation | 83.4 | 88.0 | 72.6 |
| 4 | TinyViT-21M | train from scratch on IN-1k | 83.1 | 88.1 | 73.1 |
| 5 | TinyViT-21M | original IN-21k | 83.8 | 88.4 | 73.8 |
| 6 | TinyViT-21M | cleaned IN-21k | 84.2 | 88.5 | 73.8 |
| 7 | TinyViT-21M | original IN-21k, with distillation | 84.8 | 88.9 | 75.1 |
Choice of teacher tab. 3
Teacher numbers in brackets are parameters and linear-probe accuracy on ImageNet-1k tab. 3. Training time and memory were measured without the fast distillation method tab. 3.
| # | Teacher | IN-1k | IN-Real | IN-V2 | GPU hours | Memory (GB) |
|---|---|---|---|---|---|---|
| 0 | none (no distillation) | 83.8 | 88.4 | 73.8 | 3,360 | 0 |
| 1 | BEiT-L (326M, 84.1) | 84.1 | 88.4 | 73.8 | 6,415 (1.9×) | 3.9 |
| 2 | Swin-L (229M, 84.4) | 84.2 | 88.6 | 73.9 | 5,804 (1.7×) | 6.8 |
| 3 | CLIP-ViT-L/14 (321M, 85.2) | 84.8 | 88.9 | 75.1 | 7,087 (2.1×) | 2.7 |
| 4 | Florence (682M, 86.2) | 84.8 | 89.0 | 74.8 | 7,942 (2.4×) | 10.7 |
Linear probe and few-shot transfer, TinyViT-21M tab. 5
Linear probe accuracy (%), followed by 5-, 20- and 50-shot 5-way accuracy (%) on four datasets tab. 5.
| Pretraining | CIFAR-10 | CIFAR-100 | Flowers | Cars | Pets |
|---|---|---|---|---|---|
| IN-1k | 91.7 | 75.2 | 80.9 | 56.3 | 86.5 |
| IN-1k, with distillation | 91.7 | 74.5 | 82.4 | 61.7 | 85.5 |
| IN-21k | 96.3 | 84.7 | 99.7 | 67.7 | 92.6 |
| IN-21k, with distillation | 96.9 | 86.6 | 99.7 | 75.1 | 93.8 |
| Pretraining | Shot | ISIC | EuroSAT | CropDisease | ChestX |
|---|---|---|---|---|---|
| IN-1k | 5 | 42.9 | 82.4 | 92.2 | 24.8 |
| IN-1k | 20 | 56.7 | 91.0 | 97.4 | 29.0 |
| IN-1k | 50 | 63.7 | 94.2 | 98.6 | 31.8 |
| IN-1k, with distillation | 5 | 43.0 | 83.0 | 94.2 | 24.4 |
| IN-1k, with distillation | 20 | 58.5 | 91.8 | 97.9 | 28.6 |
| IN-1k, with distillation | 50 | 66.2 | 94.3 | 98.9 | 31.8 |
| IN-21k | 5 | 52.5 | 87.4 | 97.4 | 24.6 |
| IN-21k | 20 | 66.5 | 93.7 | 99.1 | 29.4 |
| IN-21k | 50 | 73.4 | 95.5 | 99.5 | 33.4 |
| IN-21k, with distillation | 5 | 53.5 | 88.1 | 98.0 | 24.7 |
| IN-21k, with distillation | 20 | 67.3 | 93.9 | 99.3 | 29.5 |
| IN-21k, with distillation | 50 | 74.2 | 96.0 | 99.5 | 33.2 |
COCO object detection tab. 6
Cascade Mask R-CNN, 12 epochs, backbone only, COCO 2017 validation tab. 6.
| Backbone | Params (M) | IN-1k top-1 | AP | AP50 | AP75 | APS | APM | APL |
|---|---|---|---|---|---|---|---|---|
| Swin-T | 28 | 81.2 | 48.1 | 67.1 | 52.1 | 31.1 | 51.2 | 63.5 |
| TinyViT-21M | 21 | 83.1 | 49.6 | 68.5 | 54.2 | 32.3 | 53.2 | 64.8 |
| TinyViT-21M, distilled | 21 | 84.8 | 50.2 | 69.4 | 54.4 | 32.9 | 53.9 | 65.2 |
Scaling and teacher-logit sparsity fig. 4 fig. 5. The authors report that distillation keeps helping as the number of ImageNet-21k images seen grows, while the models tend to saturate with more epochs fig. 4 §5.2. Accuracy rises with K until it saturates, and storage grows linearly with K fig. 5 §5.2.
Analysis §4 fig. 3. TinyViT-21M reaches 53.2% training accuracy on ImageNet-21k against 57.1% for Swin-L-197M §4. The predicted class correlations of the distilled student look more like those of the teacher than those of the student trained without distillation fig. 3.
Paper vs code
Compared against the official repository at code_commit . The code is the Microsoft Cream repository, under the TinyViT folder that the paper's abstract links.
Differences:
- Stored values are probabilities, not logits. The paper calls the stored values "logits" §3.1. The code takes a softmax over the outputs and stores the top-K probabilities, which are then used directly as targets save_logits.py main.py. The sum-to-one form of eq. 2 only works with probabilities, so the code matches the formula.
- Attention-bias interpolation is bicubic, not bilinear. The paper says the attention biases are bilinear-interpolated for higher resolution §5.1. The code interpolates the bias tables with bicubic mode when loading a checkpoint whose table size does not match utils.py. The paper's description of the interpolation mode disagrees with the code.
- Teacher set. The paper uses Swin-L, BEiT-L, CLIP-ViT-L/14 and Florence as teachers §5.2. The repository only has a configuration for the CLIP teacher clip_vit_large_patch14_22k.yaml. The README calls the public teacher "CLIP-ViT-Large/16" README.md, while the config and the paper use ViT-L/14.
- Default K. The paper uses K = 10 for ImageNet-1k and K = 100 for ImageNet-21k §5.2. The default in the code is 100 config.py. The 1k run must set K explicitly; no 1k distillation config is in the repository.
- Learning rates are scaled. The paper states the effective learning rates §5.1. The code takes a base rate per 512 images and scales it linearly with the total batch main.py. The two agree at the batch sizes in the paper (4096 for pretraining, 1024 for fine-tuning), so the paper's numbers are the code's numbers after scaling. An implementer who uses a different batch size must scale the base rate.
- Mixup, cutmix and label smoothing are not in the paper for ImageNet-1k. The paper says nothing about these for from-scratch training or fine-tuning except that fine-tuning disables mixup and cutmix §5.1. The 1k config does not override them, so the code defaults apply: mixup 0.8 and cutmix 1.0 config.py. The fine-tuning configs set both to zero, and label smoothing 0.1 then applies config.py main.py.
Consistent with the paper:
- Pretraining schedule, optimiser, weight decay, clipping, stochastic depth, and the disabled mixup and cutmix in distillation tiny_vit_21m_22k_distill.yaml.
- Soft-label loss without ground truth main.py.
- Embedding widths and depths of the three sizes .
- Seed stored as a 4-byte integer save_logits.py.
- The mean-over-tokens pooling then LayerNorm and a linear head tiny_vit.py.
Pitfalls
- The stored labels fix the augmentation. A student trained on saved labels sees only the augmentations that were saved with them. Any change to the augmentation pipeline (RandAugment policy, interpolation, colour jitter, random erasing, crop or the random generator) makes the saved seeds replay a different image. The paper names PCG as a common generator §3.1; this compile did not verify which generator the code uses for the replay.
- Seeds depend on rank and epoch when labels are generated. The code derives the seed for the labelling run from the base seed, the process rank and the start epoch save_logits.py. Training replays the stored seeds as saved, so the launch configuration matters only when labels are regenerated.
- Storage is large and one-off. The paper reports 481 GB for ImageNet-21k with K = 100 over 90 epochs, and 16 GB for ImageNet-1k with K = 10 over 300 epochs §5.2. The per-epoch labels are independent and can be computed in parallel §3.1.
- The 29.8% speed-up includes the one-off labelling. The appendix gives 92.4 GPU days to store the Florence labels and 140.0 GPU days to train with them, against 330.9 GPU days for conventional distillation app. B. The 29.8% figure matches (92.4 + 140.0) compared with 330.9, a derived calculation from the paper's numbers. The per-run saving on its own is larger.
- Teacher data overlap. The main teacher, CLIP-ViT-L/14, was fine-tuned on ImageNet-21k for 30 epochs §5.1, the same data the student is pretrained on. The paper does not discuss this overlap.
- Hard-sample cleaning is an analysis step. The paper removes about 2M images (about 14%) from ImageNet-21k using a Florence top-5 filter, to test the effect of noisy labels §4. The cleaned set is not the one used for the headline TinyViT numbers.
- Ground-truth labels slightly hurt. In the authors' test, adding ground-truth to the distillation loss lowered ImageNet-1k top-1 by 0.2 points with the CLIP teacher and 0.7 points with Florence app. C. The paper gives this as its reason for label-free distillation.
- Single runs. The paper reports one number per setting and no variance across seeds in the text.
- Caption and text disagree on the detector. The text says Cascade R-CNN §5.4; the caption of the detection table says Cascade Mask R-CNN tab. 6.
- Window padding is code-only. When a feature map is not a multiple of the window size, the code pads it tiny_vit.py. The paper does not mention padding. At the stated resolutions (224, 384 and 512) every stage divides evenly into windows, so padding does not trigger.
- Higher-resolution cost. The 512px model needs 27.0 GMACs and runs at 167 images per second on the authors' V100 tab. 4; the 21M model at 224px runs at 1,571. Resolution scaling is a large cost increase, not a free gain.
- Throughput depends on the device. The authors measured throughput on one V100 GPU tab. 4. Other hardware can reorder the models.
Lineage
- Builds on: the hierarchical window-attention design of Swin, and the attention-bias and stem choices of LeViT, both cited in §3.2. The distillation idea follows Hinton-style teacher-student training §2.
- Closest related work: FKD, which stores teacher logits for CNN fine-tuning. TinyViT differs by targeting pretraining for transformers, and by storing one seed per image instead of the explicit augmentation parameters §2.
- Compared against: DeiT (including its distillation token), Swin, LeViT, MobileViT, AutoFormer, GLiT and other small models tab. 4.
Source and license
Adapted from “TinyViT: Fast Pretraining Distillation for Small Vision Transformers” by Kan Wu, Jinnian Zhang, Houwen Peng, Mengchen Liu, Bin Xiao, Jianlong Fu, Lu Yuan (arXiv:2207.10666), used under CC BY 4.0. Changes: condensed and restructured for implementation, with tables, equations and algorithms taken from the paper and its code, each cited to its source. Not affiliated with or endorsed by the authors. Code citations refer to https://github.com/microsoft/Cream at commit 4a13c4091e78 (MIT). Any quoted code is shown under that license.