Excellent question. You've correctly identified the two core challenges in building multimodal transformers: representation (encoding) and fusion/alignment (the "adapter" part). Given your background, we can dive straight into the architectural patterns and technical details.
Let's break this down into the two parts you've highlighted.
Part 1: Encoding Strategies: Mapping "Sensory" Input to Vector Sequences
The fundamental goal here is to transform any modality into a sequence of vectors of a consistent dimension (d_model), which is the format a Transformer's self-attention mechanism expects. This process is often called "tokenization" or "embedding" of the modality.
1. Image Encoding
Raw pixels are not suitable for direct input. The key is to extract spatially aware features and sequence them.
2. Audio Encoding
Raw audio waveforms are 1D and extremely high-dimensional. We typically convert them into a 2D representation first.
- Spectrogram-based Patching (AST-style): This is analogous to the ViT approach for images.
- Mechanism: The raw audio waveform is converted into a Mel Spectrogram, which is a 2D representation of frequency over time. This spectrogram is now treated like a single-channel image. We can then apply the same patch-based embedding strategy: split the spectrogram into patches (e.g., 16x16 frames/frequency bins), flatten, and project them to
d_model.
- Process:
Waveform (T,) -> Spectrogram (F, T') -> Patches (N, P*P) -> Sequence of Embeddings (N, d_model)
- Example Models: Audio Spectrogram Transformer (AST), Whisper's encoder uses a similar principle but with a CNN frontend.
3. Video Encoding
Video is essentially a sequence of images with an added temporal dimension.
- Mechanism: The most straightforward method is to extend image encoding. Each frame is encoded independently using a ViT or CNN patch-based approach. This results in a sequence of frame-level embeddings.
- Process:
Video (T, H, W, 3) -> Per-frame patch embeddings (T, N, d_model)
- Temporal Modeling: The challenge is modeling time. You can either:
- Flatten: Concatenate the patch embeddings from all frames into one very long sequence:
(T*N, d_model).
- Factorized Attention: Use spatial self-attention within each frame, followed by temporal self-attention across frames. This is more computationally efficient.
- Example Models: TimeSformer, VideoMAE.
4. Text Encoding
This is the native modality for Transformers.
- Mechanism:
- Tokenization: A subword tokenizer (like BPE or WordPiece) splits the text into tokens.
- Embedding: Each token ID is mapped to a dense vector via a learnable embedding matrix.
- Positional Encoding: Positional embeddings are added to give the model information about word order.
- Result: A sequence of vectors
(Seq_Len, d_model).
Part 2: Modality Alignment and "Adapter" Methods
Once you have sequences of vectors for each modality, you can't just concatenate them. They live in different "semantic spaces." The projection layers you mentioned are the first step, but the alignment strategy is what truly enables cross-modal understanding. The term "adapter" here can mean a general alignment module or the specific PEFT technique.
1. Unified Projection and Early Fusion
This is the simplest and a very common method.
- Mechanism: As discussed in the encoding step, each modality's raw embedding (e.g., patch vectors, token embeddings) is passed through its own projection layer (a simple FFN) to map it to the shared
d_model dimension.
Image Patches (N, d_img) -> Projection_Img -> (N, d_model)
Text Tokens (L, d_txt) -> Projection_Txt -> (L, d_model)
- The "Magic": These projection layers are trained end-to-end with the rest of the model. The loss function (e.g., from a contrastive or captioning task) forces the model to learn projections that place semantically related concepts close to each other in this shared latent space. For example, the projected vector for an image patch of a cat's face will be pushed closer to the projected vector for the text token "cat".
- Fusion: After projection, the sequences can be concatenated and fed into a single, deep Transformer encoder.
[CLS_img, patch_1, ..., patch_N, SEP, CLS_txt, token_1, ..., token_L]
The self-attention mechanism will then calculate attention scores between image patches and text tokens from the very first layer, enabling deep fusion.
- Example Models: VisualBERT, ViLBERT.
2. Cross-Attention Mechanisms
This is a more explicit and powerful form of alignment, often seen in more recent architectures. Instead of just mixing tokens, one modality actively "queries" the other.
- Mechanism: It uses the standard cross-attention block from the original Transformer encoder-decoder. One modality provides the
Query (Q) vectors, and the other provides the Key (K) and Value (V) vectors.
- Application (e.g., Flamingo):
- A pre-trained Vision Encoder processes the image into a sequence of patch embeddings.
- A special module called a Perceiver Resampler uses a small set of learnable latent queries to cross-attend to the dense image patch embeddings. This distills the rich visual information into a fixed, small number of "visual tokens".
- These few visual tokens are then interleaved with the text tokens and fed into a frozen Large Language Model (LLM). Gated cross-attention layers are inserted into the LLM, allowing text tokens to attend to these visual tokens (but not vice-versa, to preserve the LLM's structure).
- Benefit: This decouples the vision and language models, allowing for the use of powerful, pre-trained, and frozen models. The alignment is learned in the lightweight cross-attention layers.
3. Parameter-Efficient "Adapters" (PEFT)
This is a specific, modern interpretation of "adapter" that focuses on efficiency. The goal is to make a large, pre-trained unimodal (e.g., text) model multimodal without retraining its billions of parameters.
- Mechanism: The core idea is to freeze the entire pre-trained model and insert a few small, trainable modules ("adapters").
- Example (LLaMA-Adapter):
- A Vision Encoder processes the image.
- A small adapter module (a few layers of FFNs and attention) takes the global
[CLS] image feature as input.
- This adapter generates a set of "adaptation prompt" vectors (e.g., 10 vectors of
d_model dimension).
- These learned prompt vectors are prepended to the text token embeddings before they are fed into the frozen LLM.
- How it Works: The adapter learns to encode the visual information into a soft prompt that "guides" the frozen LLM's attention and generation process to be conditioned on the image, without ever changing the LLM's weights.
4. Dual-Encoder Architecture and Contrastive Alignment
This architecture doesn't fuse the modalities at the token level but aligns them at the global level.
- Mechanism (CLIP):
- An Image Encoder (e.g., ViT) processes an image and outputs a single global embedding vector
I_e (from the [CLS] token).
- A Text Encoder (e.g., Transformer) processes a caption and outputs a single global embedding vector
T_e (also from a [CLS] token).
- Both encoders are trained from scratch on a massive dataset of (image, text) pairs.
- Alignment: The training objective is a contrastive loss. For a batch of
N pairs, you compute the cosine similarity of all N x N possible pairings. The model is trained to maximize the similarity for the N correct pairs (on the diagonal of the similarity matrix) and minimize it for all N^2 - N incorrect pairs.
- Result: This forces the two encoders to project semantically similar images and texts into the same region of a shared, multi-modal embedding space. This is a very powerful form of alignment learned implicitly through the contrastive objective.
Summary of Architectural Patterns
| Method | Key Idea | How Alignment Happens | Example Models |
|---|
| Early Fusion Encoder | Interleave all modalities' tokens in one big sequence. | Projections + shared self-attention from layer 1. | VisualBERT, ViLBERT |
| Dual Encoder | Encode modalities separately, align global features. | Contrastive loss on final [CLS] embeddings. | CLIP, ALIGN |
| Encoder-Decoder | Encode source modality, decode target modality. | Cross-attention in the decoder. | Image Captioning Models |
| Frozen LLM + Adapters | Keep LLM frozen, add lightweight modules. | Cross-attention layers or learned soft prompts. | Flamingo, BLIP-2, LLaMA-Adapter |
The field is rapidly moving towards the "Frozen LLM + Adapters" approach, as it allows us to leverage the immense power of existing LLMs for multimodal tasks in a very parameter- and compute-efficient way.