Vision transformers split an image into patches ("tokens") and let every token attend to every other one. The cost of this grows with the square of the number of tokens, so a common way to speed them up is to drop unimportant tokens partway through the network. Existing ways of choosing which tokens to drop keep the ones the network is currently attending to, or the ones that best preserve the overall spread of token features. We argue that both rules break when the test images differ from the training images, for example when they are blurred, noisy, or rendered as sketches. Under such shifts the corrupted texture and background dominate both the attention and the feature distribution. The pruner then spends its budget on exactly the tokens that carry the shift.
We propose a different rule. Keep the tokens whose features would stay the same if the image were perturbed. Self-supervised contrastive learning trains a network to give two randomly altered copies of the same image ("views") matching representations. That training signal already tells us, patch by patch, which features are stable and which are nuisance. We distill it into a small scoring head that reads one view in one forward pass and predicts each token's stability. At test time the network keeps only the top-scoring tokens after an early layer, with no gradient updates. The claim is that this pruner loses less accuracy under shift than attention-based or distribution-matching pruning at the same compute. At high corruption severity it may even beat the unpruned model, because the dropped tokens are the ones carrying the corruption.
Core Claim
The paper would establish one thing: at a fixed token budget, choosing tokens by predicted cross-view invariance preserves out-of-distribution accuracy better than choosing them by attention or by distribution matching, while matching them in distribution. The advantage should grow with shift severity. It should hold for corruption families that were never used as augmentations during training.
Two lines of theory motivate the claim. Toward Understanding the Feature Learning Process of Self-supervised Contrastive Learning shows that augmentations break the correlation of dense spurious noise between positive pairs. This forces the encoder onto sparse invariant features. Contrastive learning, multi-view redundancy, and linear models shows that when views share label-relevant signal but differ in independent nuisance, the contrastive objective recovers the shared part. Both results concern the image-level representation. Our step is to apply the same logic per token and use it as a selection rule, which neither paper does.
Two empirical findings motivate the pruning half. Towards domain generalized pruning by scoring out-of-distribution importance shows that standard filter pruning on PACS can take a model with 96% cross-domain accuracy and make it "plummet" at 30–50% pruning, even when in-domain accuracy survives. It also shows that domain-generalization pretraining such as CORAL or Mixup does not prevent this. Their fix scores CNN filters by variance of risk across labeled source domains. We instead score tokens, dynamically per input, without domain labels.
OTPrune supplies the main contrast. It selects visual tokens so that their distribution matches the full token set in 2-Wasserstein distance, a measure of how far one point cloud must move to match another. It gives every token equal weight. It was not tested under corruption shifts. If shifted images are dominated by nuisance tokens, matching their distribution faithfully preserves the nuisance, and our experiments are designed to show whether that happens.
Research Program
Scoring target. We start from a ViT-S/16 trained with a MoCo-style contrastive objective on ImageNet, using a momentum encoder as in Contrastive Test-Time Adaptation. For two views of one image, the crop geometry tells us which patch in view 2 covers each patch $i$ in view 1. At a chosen layer $l$ we compute the invariance target
$$s_i = \cos\big(h_l^{(1)}(i),\, h_l^{(2)}(\pi(i))\big),$$
where $h_l^{(v)}(i)$ is the layer-$l$ feature of patch $i$ in view $v$ and $\pi(i)$ is its corresponding patch in the other view. A two-layer head $g$ reads only the view-1 features $h_l^{(1)}(i)$ and is trained, with the backbone gradient stopped, to regress $s_i$. The contrastive pretraining is left unchanged, so the scorer is a cheap add-on rather than a new training recipe.
This design replaces the obvious but self-defeating idea of running several augmented views at test time and measuring agreement. That would multiply compute, which is the thing we are trying to cut.
Pruning at inference. For each test image we run layers 1 through $l$ on all $N$ tokens and score them with $g$. We keep the top $\rho N$ tokens plus the class token, gather them into a dense tensor, and run the remaining layers on that tensor. Gathering into a dense tensor sidesteps the memory-latency penalty of scattered sparse patterns noted by Fast Attention Over Long Sequences With Dynamic Sparse Flash Attention. For a depth-$L$ network, attention cost in the pruned layers falls from $(L-l)N^2$ to $(L-l)\rho^2N^2$, because every remaining token still attends to every other remaining token. The feed-forward cost falls by $\rho$. We report measured wall-clock time alongside FLOPs.
Guarding against leakage. The augmentations that define $s_i$ must not simply rehearse the test corruptions. We split the 15 ImageNet-C corruption families into two groups. The first group has photometric counterparts in the augmentation set, such as blur and color distortion. The second group, such as pixelation, JPEG compression, frost, and elastic distortions, is excluded from augmentation. The central result must hold on the second group. We also evaluate on style shifts (PACS, DomainNet-126), which no augmentation imitates.
Analysis. Beyond accuracy, we measure two things for each method. The first is what fraction of kept tokens fall on the object versus the background or the corrupted region. The second is how selection changes with corruption severity. The theory predicts that invariance scores for background and noise tokens fall as severity rises, while attention mass on them rises.
Baselines
The naive reference is the unpruned contrastive ViT evaluated directly on the shifted data. We add a second reference: random token dropping at the same keep ratio $\rho$. Any selection rule must clear random dropping to show it is selecting anything at all.
The most direct competitor is class-token attention pruning from the same contrastively pretrained backbone. This drops the tokens that receive the least attention from the class token at layer $l$. It is the strongest threat because contrastively trained ViTs are known informally to attend to objects. If their attention already concentrates on invariant content, our head adds nothing. OTPrune reports that early-layer attention is noisy and unreliable, and that training-free attention pruning can trail trained selection by up to 30 points (LongAttnComp, Speculative Prefill). Both findings come from language and multimodal models, however, not from shifted image classification.
The strongest existing selection method is OTPrune's greedy log-determinant selection, run on the same layer-$l$ features with the same $\rho$. OTPrune reports the best average rank across 11 multimodal benchmarks at about 15% of original FLOPs, with $p=0.028$ against DivPrune. We found no reported result on Lacuna for OTPrune under corruption or domain shift. We also include diversity-based selection in the style of DivPrune.
For all selection baselines, the backbone, the pruning layer $l$, and $\rho$ are shared. Hyperparameters are chosen on clean ImageNet validation only, with the same grid for every method. Beating these baselines means higher mean accuracy on the held-out ImageNet-C families at $\rho \in \{0.3, 0.5\}$. The margin must exceed seed variance across three runs, and clean accuracy must stay within about half a point of the baselines.
The test-time adaptation reference is AdaContrast, which updates the weights with contrastive learning and refined pseudo-labels on the target stream. On VisDA-C it reports 86.8% accuracy and 0.65% expected calibration error, against 83.0% and 2.97% for SHOT. On DomainNet-126 it reports 67.8%. Those numbers come from convolutional backbones, so we rerun AdaContrast on our ViT.
We do not expect a gradient-free pruner to beat weight adaptation on accuracy. The comparison is accuracy per unit of test-time compute. We also run AdaContrast on top of our pruned model to see whether the two combine. Test-Time Training (Test-Time Training with Self-Supervision for Generalization under Distribution Shifts) is a second adaptation reference. On CIFAR-10-C level-5 pixelation it reduced error from roughly 50% to nearly 20%.
The key ablation isolates the learned invariance target. We keep the same head architecture but train it to predict three alternative targets: feature norm, class-token attention, or a label-supervised importance score. If a head that predicts attention performs as well as one that predicts $s_i$, the contribution is the extra head, not invariance. A second ablation computes $s_i$ exactly from two test-time views at double the compute. This upper-bounds how much the distillation loses.
Why not just…?
Why not just prune by class-token attention in a contrastive ViT? This is the real threat. We do not dismiss it. The class-attention baseline and the attention-target ablation settle it. Our reason to expect a gap is mechanistic. Attention is computed from the shifted input itself, so a corruption that raises the magnitude of high-frequency texture features can attract attention. The invariance head is trained to give low scores to tokens whose features move under perturbation, whatever their magnitude. If the gap on held-out corruptions is within noise, we will report that contrastive pretraining alone already yields shift-robust attention pruning. That is a useful, smaller finding.
Why not just adapt the model at test time with entropy minimization or AdaContrast and skip pruning? Adaptation and pruning answer different questions. Adaptation requires backward passes over all $N$ tokens for every test batch, and TTT's authors note this latency. Entropy minimization also causes the miscalibration documented in the AdaContrast paper. Our method adds no test-time gradients. The accuracy-per-FLOP curves and the combined pruned-plus-AdaContrast run will show whether pruning first makes adaptation cheaper without hurting it.
Why not just train the ViT with domain-generalization methods and then use any pruner? Cai et al. found that CORAL- and Mixup-pretrained CNNs often lost more cross-domain accuracy after pruning than plain empirical-risk models. That is direct evidence against this shortcut, though for filters rather than tokens. We test it on tokens by running OTPrune and attention pruning on a ViT fine-tuned with Mixup, and comparing the results on PACS.
Why not just run full attention with efficient kernels? At 196 tokens per image, full attention is affordable. The case for pruning is stronger at high resolution, where token counts reach the thousands. We therefore also report results at 384 and 512 pixels. If invariance pruning improves accuracy under severe shift even at 224 pixels, the argument no longer rests on speed alone.
Proposed Experiments
The primary benchmark is ImageNet-C at severities 1–5, with the family split described above, reporting mean accuracy and expected calibration error per group. CIFAR-10-C with a ViT-Tiny gives a smaller-scale replication. It also allows a direct comparison with TTT's pixelation result. PACS in leave-one-domain-out evaluation and DomainNet-126 test style shift, and VisDA-C tests synthetic-to-real shift. For each, we sweep $\rho$ from 0.2 to 0.7 and $l$ from 3 to 6, and plot accuracy against measured latency.
Three outcomes would be decisive. The first is whether invariance pruning beats class-attention and OTPrune selection on the held-out corruption families at matched $\rho$, with a margin that widens from severity 1 to severity 5. If it does not, the core claim fails. The second is whether, at severity 5, invariance pruning at $\rho=0.5$ equals or exceeds the unpruned model. This would echo LongAttnComp's finding that compressing context can beat full context (76.40% versus 74.37% on InfiniteBench Code-Debug), here in images and under shift. The third is the object-versus-background analysis. It should show that the kept tokens move toward the object as severity rises under our method and away from it under attention pruning. If accuracy gains appear without this shift in which tokens are kept, the mechanism we propose is not what is producing them.
References
- Toward Understanding the Feature Learning Process of Self-supervised Contrastive Learning
- Contrastive learning, multi-view redundancy, and linear models
- Towards domain generalized pruning by scoring out-of-distribution importance
- OTPrune: Distribution-Aligned Visual Token Pruning via Optimal Transport
- Contrastive Test-Time Adaptation
- Fast Attention Over Long Sequences With Dynamic Sparse Flash Attention
- LongAttnComp: Cross-Family Context Compression for Long-Context Reasoning
- Test-Time Training with Self-Supervision for Generalization under Distribution Shifts