A modality is one kind of input, text, images or sound. A contrastive model is trained on pairs of matching items from two modalities, say a picture and its caption, and learns to place the two members of a pair close together and everything else far apart. The place it puts them is a shared space, where every item becomes a list of numbers of one fixed length, and closeness is measured between those lists.
The model turns an item into its list with an encoder. An encoder is a neural network that reads one input and outputs the numbers that stand for it. The numbers inside the network that training adjusts are its parameters, and a model's size is quoted as the count of them. CLIP, which pairs images with text, and CLAP, which pairs audio with text, each give every modality its own encoder. At their scale that is fine, and separate encoders are the default design.
The encoders here are transformers. A transformer does not read its input whole. The input is first cut into tokens, short pieces such as a fragment of a word or a small square of an image, and each token becomes a list of numbers. The transformer is then a stack of identical layers, and in each layer every token looks at every other token, decides which ones matter to it, and updates its own numbers. The number of layers is the depth.
At a few million parameters, which is where a model has to be to live on a phone or a similarly small device, it is not obvious that the default holds. If you have 4.2M transformer parameters to spend on text and images, do you build two 2.1M encoders, one per modality, or one 4.2M encoder that both modalities go through? The first choice buys specialisation, since each stack only ever sees one kind of input. The second buys depth, since every input passes through twice as many layers.
Shared encoders are known to help at large scale, but nobody had checked whether that survives when the transformer is the bottleneck. We trained 16 contrastive models on Localized Narratives, a dataset with naturally aligned text, images and spoken descriptions, with transformer budgets from 2.1M to 6.3M and everything else held fixed.
One shared encoder beat two or three separate ones at the same parameter count on every pair we could measure. A shared encoder with half the parameters came close to the separate pair. Adding a third modality helped the weakest pair at the expense of the strongest.
The comparison
The data is the COCO subset of Localized Narratives. COCO is a large collection of everyday photos, and in Localized Narratives an annotator describes each photo aloud and the speech is transcribed, so each sample is an image, a spoken narration and a text transcription that all describe the same thing. Keeping samples with audio under 30 seconds and carving a test split from the original validation set gives 92,987 training samples over 82,683 unique images, 5,088 validation samples and a 1,000-sample test set over 874 unique images.
In the test set 12.5% of images carry annotations from more than one narrator. Each triple is treated as a distinct sample with a single correct match, so a retrieval that returns the same image under a different narrator's description is not credited.
A unit is two transformer layers at width 256 with 8 attention heads and a feed-forward width of 1024, and it has 2.1M parameters. Width is the length of the list of numbers each token carries through the stack, here 256. Each layer has two halves. The first is attention, where every token gathers information from the other tokens, and it is run 8 times side by side with different weights, each run being one head. The second is a feed-forward block, a small two-layer network applied to each token on its own, which widens the 256 numbers to 1024 and narrows them back.
Four smaller choices inside each layer are standard and were held fixed. Attention on its own does not know where in the sequence a token sits, so RoPE marks position by rotating each token's numbers by an angle that grows with its place in the sequence. RMSNorm rescales each token's numbers before every layer so their overall size is always the same. The feed-forward block uses a GELU gate, a smooth switch that lets each of its 1024 internal numbers through in proportion to how positive it is. Dropout 0.2 zeroes a random fifth of the numbers at each training step so the model cannot lean on any one of them.
Shared models route every modality through the same units. Separate models give each modality its own unit, so a separate bimodal model at 2U and a shared model at 2U both have 4.2M transformer parameters. The only difference is whether each modality's tokens see two layers of their own or four layers that everything shares.
Turning raw input into tokens is modality-specific in both designs, because it has to be. Text is cut with the BERT WordPiece tokenizer, which splits each word into pieces from a fixed list, so a rare word becomes several pieces and a common one stays whole, and a text keeps up to 256 pieces. Images are cut into a 14 by 14 grid of 16-pixel squares, which gives 196 tokens. Audio is sampled 16,000 times a second and cut into short windows every 320 samples, and each window is summarised by 64 numbers saying how much energy sits in each of 64 frequency bands. That grid of numbers is a spectrogram. It is padded with silence to 30 seconds and cut into 240 patches.
One extra token is added at the front of every sequence. It starts as a learned list of numbers that is the same for every input, and because it attends to every other token on the way up the stack, its output at the top is taken as the summary of the whole input. That summary goes through a projection head, a single matrix that maps it to 512 numbers, and the heads are also modality-specific. All comparison between modalities happens on those 512 numbers.
Matching the sequence lengths matters. Attention cost grows with the square of the sequence, so if one modality arrived with far more tokens it would get more compute per example inside a shared stack and the comparison would be about tokens rather than parameters. With 196 to 256 tokens across the three modalities, the one thing that varies is where the transformer parameters go.
The loss
A little maths now, though I have kept it to two equations. Training uses the symmetric contrastive loss of CLIP with a learnable temperature, and it helps to see what it does as geometry. A batch is the set of examples the model sees in one training step. Take a batch of matched pairs and run each side through its encoder and projection head, so every item is 512 numbers. Those numbers are its embedding. Scale each embedding so its length is one, and each is now a point on the surface of a sphere of radius one in 512 dimensions. Form the matrix of cosine similarities between every item on side and every item on side . The inner product of two lists of numbers multiplies them entry by entry and adds the results. For two lists of length one it is the cosine of the angle between them, one when they point the same way, zero at right angles and minus one when opposite, and that is cosine similarity. The matched pairs sit on the diagonal.
Treat each row as scores over the candidates on the other side and divide by a temperature . The softmax turns a row of scores into probabilities by raising e to each score and dividing by the total, so they are all positive and sum to one, and a smaller temperature lets the largest score take more of the total. The loss asks that probability to sit on the diagonal entry,
Each term pulls a matched pair together on the sphere and pushes the item away from the other items in the batch, and the temperature sets how sharply. Doing it in both directions makes the loss symmetric in the two modalities. In a shared model the same transformer weights have to carry text tokens and image patches to points that land next to each other on this sphere, and the only modality-specific freedom is in the embedding and the projection at either end.
For three modalities we average the three pairwise losses,
so that one set of shared parameters is pulled by three objectives at once, which is the tension the third-modality experiment is about.
The training recipe needs a few words defined. An epoch is one pass over the whole training set. The gradient of the loss says, for each weight, how much the loss would rise if that weight moved up a little. The optimiser is the rule that turns the gradient into an actual change to the weights, and AdamW is the usual choice for transformers. The learning rate is the size of that change. Weight decay pulls every weight a little toward zero on each step, and gradient clipping caps how large a single step can be. A checkpoint is a saved copy of the weights, and the validation set is data held back from training to check them against.
All models train for 20 epochs with the AdamW optimiser at a learning rate of . The learning rate rises linearly over the first 10% of training and then falls along a cosine curve, weight decay is 0.1, the batch is 256 and gradients are clipped at 1.0. We keep the checkpoint with the lowest validation loss. All 16 models train one after another on one A100, a data-centre GPU, in about 11 hours per seed.
Retrieval is the test. Take one item, say a caption, as the query, rank all 1,000 items on the other side by cosine similarity to it, and see where the correct match lands. It runs in both directions. Recall at 1, 5 and 10 is the fraction of queries whose correct match lands in the top 1, 5 or 10. Median rank is the position the correct match typically lands at, so lower is better. Every number is a mean and standard deviation over three seeds, where a seed fixes the random choices in training and three seeds are three independent runs. The ordering of models was the same in every individual seed.
Matched capacity
| model | params | text→image R@1 | image→text | text→audio | audio→text |
|---|---|---|---|---|---|
| shared 1U | 2.1M | 11.0 | 10.8 | 31.5 | 32.3 |
| shared 2U | 4.2M | 16.4 | 15.4 | 47.2 | 45.6 |
| separate 2U | 4.2M | 11.3 | 11.7 | 37.7 | 36.7 |
| shared 3U | 6.3M | 17.7 | 16.9 | 51.7 | 50.5 |
| shared 3U, trimodal | 6.3M | 13.2 | 12.4 | 52.9 | 49.1 |
| separate 3U, trimodal | 6.3M | 7.7 | 7.6 | 33.6 | 30.6 |
At 4.2M the shared model is five points ahead on text and image and ten on text and audio, with median ranks of 7.7 and 7.8 against 13.0 and 13.7. The gap widens at Recall at 5, where shared reaches 43.1% on text to image against 32.6%. At 6.3M with three modalities the gap is wider still, 13.2 against 7.7 on text to image and 52.9 against 33.6 on text to audio.
Depth over specialisation is the explanation we favour. At matched parameters the shared model is twice as deep for every modality, and in this regime depth is worth more than having layers that only ever see one kind of input.
Image to audio stays below 1% Recall at 1 for everything, with median ranks above 400. In this dataset the audio is someone describing the picture rather than the sound of the scene, so there is no perceptual overlap between pixels and waveform, and the only route between them is through meaning.
The same comparison can be read as a way to shrink a model. The shared 1U model has 2.1M transformer parameters, half of the separate 2U pair, and it is within a point of that pair on text and image, 11.0 and 10.8 against 11.3 and 11.7, with median ranks of 12.7 and 13.0 against 13.0 and 13.7. On text and audio it is six points behind, 31.5 and 32.3 against 37.7 and 36.7. In the trimodal setting the shared 1U model at 2.1M reaches 6.9% both ways on text and image against 7.7% and 7.6% for the separate 3U model at 6.3M.
So merging the encoders halves or thirds the transformer and keeps most of the retrieval. The transformer is not the whole model. With embeddings and heads the totals are 10.4M for shared 1U against 12.5M for separate 2U in the bimodal case, and 10.6M against 14.8M for trimodal. But the transformer is the part the design decision controls.
A third modality
Adding text to an image and audio model acts as a bridge. Image to audio median rank falls from 429 to 298 at 1U, from 450 to 199 at 2U and from 435 to 188 at 3U. Text is aligned to both, so the shared encoder learns image and audio geometry through it, although absolute Recall at 1 for the pair stays at or below 1%.
The strong pairs pay for it. At 1U text to image Recall at 1 drops from 11.0 to 6.9 and median rank goes from 12.7 to 20.2. At 2U Recall at 1 drops from 16.4 to 11.9 and median rank goes from 7.7 to 11.7, and the same pattern holds at 3U.
We ran three analyses to find out why, and each needs a measurement defined first. The first is the generalisation gap, the validation loss minus the training loss at the best checkpoint. A large gap means the model fits its training data much better than new data. Comparing gaps separates a model that learns a better representation from one that merely overfits less.
At matched 2U capacity the shared and separate bimodal gaps are the same, 0.81 against 0.83 for text and image, 0.46 against 0.44 for text and audio and −0.01 against −0.01 for image and audio. In the trimodal setting at 3U the shared gap, 0.59, is 40% larger than the separate gap, 0.42, yet the shared model retrieves better on every pair. So the shared encoder generalises worse and retrieves better, and its advantage comes from the representation rather than from overfitting less.
The second measurement is three numbers about where the embeddings sit on the sphere, computed on the test set. Alignment is the mean squared distance between matched cross-modal pairs, and smaller is closer. Uniformity, from Wang and Isola, takes random pairs of embeddings, computes e to the minus twice their squared distance for each pair, averages, and takes the log. Pairs that sit close push that average up, so the number is more negative when the embeddings are spread evenly over the sphere. Anisotropy, from Ethayarajh, is the mean cosine between random embeddings, near zero when they fill the space and near one when they have all bunched around one direction, a narrow cone.
Text and image models have anisotropy of 0.05 to 0.11, uniformity of −3.06 to −3.34 and alignment of 0.85 to 0.89. Image and audio models have anisotropy of 0.95 to 0.99, uniformity of −0.04 to −0.16 and alignment of 1.58 to 1.85 at every depth. The image and audio embeddings sit in a cone so narrow that two random embeddings have a cosine above 0.95, and no depth or architecture in this study fixed that.
The third measurement is the cosine between the gradients of the separate loss terms on the shared transformer parameters. Each gradient is a direction in the space of weights, the direction in which that loss rises fastest, and a training step moves against it. Two losses whose gradients point the same way, cosine near one, ask for the same change to the shared weights. A cosine near minus one means they ask for opposite changes. For the shared 3U trimodal model the text-image and text-audio losses have a mild negative cosine of −0.09, so they compete a little for shared capacity. The text-image and image-audio losses are positively aligned at +0.22, and text-audio against image-audio at +0.17.
That rules out the simplest explanation. Text to image does not get worse because the image-audio loss pulls against it, since those two agree. It gets worse because fixed parameters are being asked to satisfy three objectives at once, and the mild tension between the two text-anchored losses is enough to pull the strong pairs down under that constraint. The positive cosines with the image-audio loss are also why that pair improves, since nothing on the shared weights is pulling against it.
What it means
When the transformer is the bottleneck, spending the parameters on depth that every modality shares beat specialising them in every configuration we trained, and it did so without overfitting less, so the gain is in the representation. A shared encoder is also a lever for shrinking a model, since one unit shared across modalities kept most of what two separate units achieved.
A third modality is a trade with a predictable sign. It helps the pair that had no direct route and costs the pairs that were already strong, and the gradient geometry on the shared weights says the cost comes from splitting capacity rather than from the new loss fighting the old ones.
What it does not say
- One dataset, and one where the audio is speech about the image rather than the scene's sound, so image to audio was always going to go through meaning.
- Small models only, from 2.1M to 6.3M transformer parameters, with no claim about larger scales.
- All layers shared uniformly, with no test of sharing only the lower layers, which is the question You et al. studied at larger scale.
- Transformer parameters and sequence lengths are controlled. How training unfolds, and what each modality's tokenizer and patching build in before the transformer sees anything, are not.
- Retrieval only. We did not test sorting images into named categories from text labels, or fitting a simple classifier on top of the frozen embeddings.
What stands is that when transformer parameters are the bottleneck, spending them on depth that every modality shares is a better default than specialising them, and that a third modality should be added knowing which pairs it will help and which it will cost.
References
- Radford et al., Learning transferable visual models from natural language supervision, ICML, 2021.
- Elizalde et al., CLAP: learning audio concepts from natural language supervision, ICASSP, 2023.
- You et al., Learning visual representation from modality-shared contrastive language-image pre-training, ECCV, 2022.
- Pont-Tuset et al., Connecting vision and language with localized narratives, ECCV, 2020.
- Wang and Isola, Understanding contrastive representation learning through alignment and uniformity on the hypersphere, ICML, 2020.
- Ethayarajh, How contextual are contextualized word representations?, EMNLP, 2019.
- Dosovitskiy et al., An image is worth 16x16 words, 2020.