Teaching a Dual Encoder Where a Cross-Encoder Looks, Layer by Layer

Image-text models come in two kinds. Dual encoders such as CLIP embed an image and a caption separately and compare them with one dot product, which makes them fast enough for web-scale retrieval. Cross-encoders let caption words attend directly to image patches, layer after layer, which makes them far better at telling "a dog chasing a cat" from "a cat chasing a dog" but too slow to score millions of pairs. The usual way to transfer cross-encoder skill into a dual encoder is knowledge distillation (training a small "student" to imitate a larger "teacher"). In practice the student imitates only the teacher's final match score or final embedding. This proposal instead has the student imitate the teacher's internal word-to-patch attention at every depth of the network. Because a dual encoder has no cross-attention of its own, we build a training-only stand-in: a similarity map between the student's intermediate caption tokens and intermediate image patches. A learned soft assignment decides which student layers should mirror which teacher layer. At test time this machinery is discarded, and the student remains a plain dual encoder.

The opportunity is that existing evidence already points to where dual encoders fail. A probing study found that CLIP's text side separates Winoground's reordered captions with over 80% accuracy, yet CLIP scores 0% on the visually difficult subset. That pattern says the word-order information is present inside the network and lost when the two sides are compressed and matched (Why is Winoground Hard?). Distilling the teacher's layer-by-layer binding of words to regions targets exactly that loss. Distilling scores alone does not.

Core Claim

Our claim is that cross-layer distillation of attention maps, with soft layer assignment, produces dual-encoder students with measurably better compositional binding than score-level or embedding-level distillation at equal teacher compute. Compositional binding means correctly matching who-does-what-to-whom between caption and image. The gain should appear on Winoground and on the swap categories of SugarCrepe, while retrieval and zero-shot classification are preserved.

A secondary, analytical claim settles a disagreement in the literature about where cross-modal alignment happens. Align-KD distills a mobile vision-language model by matching only the first-layer text-to-vision attention, and reports gains (+2.0 average, +3.8 on GQA). COMPOSITION shows that different decoder cross-attention layers draw on different encoder depths. Our learned layer-assignment matrix, together with a first-layer-only ablation, directly tests whether compositional binding is set up early or refined across depth.

Relation to Prior Work

Three papers that connect cross-modal attention with distillation supply the setting, and each leaves the internal attention untouched. LoopITR trains a dual encoder and a stacked cross-encoder jointly. The dual encoder mines hard negatives (wrong captions that look nearly right), and the cross-encoder's match scores on them are distilled back into it. Four hard negatives per example are enough. We reuse this mining loop but add an attention-level target. DCLIP distills a cross-modal teacher, which is fed YOLO object boxes, into CLIP through final-embedding cosine and contrastive losses, and it freezes the text encoder to protect zero-shot accuracy. We avoid its fixed detector vocabulary by using patch-level attention, and we test whether its reported trade-off between retrieval and generalization comes from output-only matching. Distilling Vision-Language Pretraining for Efficient Cross-Modal Retrieval likewise transfers only pairwise similarity scores, in its case into binary codes.

The method for aligning layers comes from unimodal distillation. SemCKD lets each student layer softly attend over candidate teacher layers. It shows that rigid one-to-one layer matching causes negative regularization (the extra loss hurts the student) and that soft assignment removes this. Universal-KD does the same for text classification, but it grounds layers in a fixed label space that open-vocabulary retrieval does not have. We carry soft assignment over to attention maps, where no label space is needed.

Multi-layer aggregation motivates the student's output head. COMPOSITION shows that the top encoder layer mixes syntax and semantics together, and that composing all layers repairs compositional generalization; for example, CFQ accuracy rises from 43.4% to 59.4%. Layer-wise Fusion with Modality Independence Modeling fuses modalities layer by layer. Neither paper distills, and neither addresses retrieval-scale dual encoders.

Speech Emotion Recognition via Multi-Level Cross-Modal Distillation is the closest example of multi-level cross-modal distillation, but its stage-to-stage layer pairing is fixed and its student uses only one modality.

Research Program

The teacher is a patch-level cross-encoder trained as in LoopITR: caption tokens cross-attend to ViT image patches over $L_T$ fusion layers. The student is a CLIP dual encoder with $L_S$ layers per tower. Each student layer receives a small linear projection that places its caption-token states and image-patch states in a shared space. For teacher layer $t$, the student's proxy attention is a softmax, over patches, of the projected token-patch similarities. These similarities are computed from a soft mixture of student layers with weights $\alpha_{t,\ell}$, which are learned as in SemCKD. The training objective is

$$ \mathcal{L} = \mathcal{L}_{\text{NCE}} + \lambda_s\,\mathrm{KL}\big(p^{T}\,\|\,p^{S}\big) + \lambda_a \sum_{t=1}^{L_T} \mathrm{KL}\Big(A^{T}_t \,\Big\|\, \mathrm{softmax}\big(\textstyle\sum_{\ell}\alpha_{t,\ell}\,Q^{S}_\ell {K^{S}_\ell}^{\top}/\tau\big)\Big), $$

The symbols are defined as follows. $\mathcal{L}_{\text{NCE}}$ is CLIP's contrastive loss. $p^{T}$ and $p^{S}$ are the teacher's and student's match distributions over each positive and its four mined hard negatives. $A^{T}_t$ is the teacher's head-averaged text-to-patch attention at layer $t$. $Q^{S}_\ell$ and $K^{S}_\ell$ are the student's projected caption-token and image-patch states at layer $\ell$. $\tau$ is a temperature.

The attention term is computed on matched pairs and on hard negatives alike. On a hard negative such as a swapped caption, the teacher's attention shows which words fail to land on the right regions, and we expect this to be the most informative signal.

At inference the student's embedding is a learned, input-independent mixture of pooled layer states plus the original top-layer path. Its cost matches CLIP's up to one weighted sum. Following DCLIP, we keep the original top-layer projection available as an anchor and report both readouts. The deliverables are the trained student, the per-benchmark comparison, and the $\alpha$ matrix. The $\alpha$ matrix serves as a map of which student depths best reproduce each teacher fusion depth.

Baselines

The naive reference is off-the-shelf CLIP at the same backbone, evaluated zero-shot. The file reports no Winoground group score for CLIP, only its 0% success on the visually difficult subset (Why is Winoground Hard?). For context, the original benchmark reports that strong cross-modal models score barely at chance: VinVL reaches 14.50% and FLAVA 14.25% against a 16.67% chance level, while humans reach 85.5% (Winoground). We will compute CLIP's own score ourselves.

The strongest existing competitor for the central question is LoopITR-style score distillation over mined hard negatives. It already transfers cross-encoder judgments on exactly the near-miss pairs that compositional tests probe, and it reports MS-COCO text-retrieval Recall@1 of 67.6 (LoopITR). We run it with our own teacher, the same data, the same four negatives, and the same number of teacher forward passes and optimizer steps. This is cheap because our method uses the same teacher passes and only reads off attention as well as scores. Both methods get an identical hyperparameter search budget.

We also run DCLIP-style final-embedding distillation with a frozen text tower as the second strongest competitor, because it reports the cleanest retention figure: about 94% of CLIP's ImageNet top-1, with 15–35 point Recall@1 gains (DCLIP).

Four ablations isolate the key ingredients. The first restricts attention distillation to the first teacher layer, matching Align-KD. The second fixes $\alpha$ to a rigid proportional-depth pairing, the matching SemCKD warns against. The third trains the multi-layer readout with contrastive loss and score distillation but without the attention term. The fourth uses the attention term with a top-layer-only readout.

Beating the strongest baseline means a statistically reliable improvement in Winoground group score and SugarCrepe swap accuracy over score distillation, at equal compute, across three seeds. It also requires COCO and Flickr30k Recall@1 within one point of that baseline and ImageNet zero-shot retention at or above DCLIP's roughly 94%.

Why not just…?

Why not just distill match scores on hard negatives, as LoopITR does? This is the real threat. A scalar score on a swapped caption already says "these differ." If score distillation matches our method on Winoground and SugarCrepe, the attention term is unnecessary and we will report that. Our reason to expect a gap is that one scalar per pair says the match failed without saying which word-region binding failed. Universal-KD and the speech-emotion study both found that intermediate targets beat output-only distillation, though only in unimodal or one-modality-student settings. The equal-compute head-to-head comparison settles the question.

Why not just fine-tune with harder negative captions, as CLIC or NegCLIP do? Data augmentation is cheap and does help: CLIC reports +9% on SugarCrepe++ image-to-text and +1.3 to +2.2 on COCO retrieval (Advancing Compositional Awareness in CLIP with Efficient Fine-Tuning). But SugarCrepe showed that NegCLIP's apparent gains largely came from text-only grammar artifacts and collapsed on cleaned negatives (SugarCrepe). Augmentation also does not compete with our method, since our hard negatives can include CLIC-style rewrites. We therefore run CLIC-style fine-tuning alone and combined with our losses. If the combination adds nothing over augmentation alone, the distillation is not earning its cost.

Why not just pool all layers of CLIP, with no teacher at all? Uniform layer averaging improves transferability estimation (Choose Your Transformer), and the probing result suggests the needed information is already inside CLIP. A reweighted readout might be enough on its own. The third ablation above tests exactly this. SemCKD and COMPOSITION suggest that uniform mixing blurs depth-specific roles, but neither tested vision-language retrieval, so this remains an open empirical question rather than a settled one.

Why not just distill first-layer attention, as Align-KD does? It is simpler and reported gains. However, it was evaluated on question answering, not on minimal-pair compositionality. The first-layer ablation, read alongside the learned $\alpha$ matrix, shows directly whether deeper teacher layers contribute.

Proposed Experiments

Training uses MS-COCO and Flickr30k pairs with Karpathy splits, with ViT-B/32 and ViT-B/16 students. Evaluation covers four groups of metrics. The first is Winoground text, image, and group scores, with the visually difficult subset reported separately. The second is SugarCrepe by category, emphasizing the swap categories where GPT-4V reaches 83.13% against 90.95% overall, plus SugarCrepe++. The third is COCO and Flickr30k Recall@1/5/10. The fourth is zero-shot ImageNet-1K, CIFAR-10, and CIFAR-100, reported as retention relative to the starting CLIP.

The first decisive check is the equal-compute comparison against score distillation on Winoground and SugarCrepe swap accuracy. The second is whether the attention term still helps when CLIC-style negatives are present. The third is whether the learned $\alpha$ concentrates mass beyond the first teacher layer, and whether removing those layers erases the gain; this tests Align-KD's first-layer hypothesis against COMPOSITION's depth-specific picture. The fourth is whether zero-shot retention stays high without DCLIP's early stopping, which tests whether DCLIP's retrieval-generalization trade-off is an artifact of output-only matching.

A negative result on the first check, with the attention term failing to beat score distillation, would itself be informative. It would mean a single pooled dual-encoder vector cannot store the binding information even when it is supplied during training, and the limit would then lie in the dual-encoder interface rather than in the training signal.