Below is a “tool-box” style overview of how practitioners typically convert audio and visual information into Transformer-friendly token sequences, and how those sequences can be mixed (early, mid, or late) with language tokens so that a single model can do → , → , or fully interleaved multimodal reasoning.
Feel free to jump to the sections you need:
- Core recipe: turning any signal into “tokens”
- Vision encoders
2.1 Static images
2.2 Multi-view / stereo / 3-D volumes
2.3 Short videos & spatio-temporal clips
2.4 Domain-specific (medical, point-cloud, remote-sensing, comics, etc.)
- Audio encoders
3.1 Waveform-level
3.2 Time-frequency patches (log-Mel, CQT, etc.)
3.3 Event-/segment-level
- Mixing modalities inside a Transformer
4.1 Token stream layouts
4.2 Positional encodings for space, time & “view”
4.3 Attention-matrix tricks (sparse, factorized, gated, adapters)
- Mini-cheat-sheet & pseudo-code
───────────────────────────────────────
- CORE RECIPE – From signal → Transformer tokens
───────────────────────────────────────
Step | How it looks
-----------------|--------------------------------------------------
Frame / patching | Split the signal along its natural axes (2-D XY for images, T×XY for video, T for audio) into small chunks with overlap or stride.
Projection | Flatten each chunk & feed through a linear layer (or conv) to d-model dimension. This is the “patch embedding”.
Special tokens | Add
[CLS], modality markers ([IMG], [AUD], [VIEW_2], …) or content-aware sentinels (for masked modeling).
Extra metadata | Concatenate or add-into embedding: scale, frame-rate, camera-ID, mic array ID, etc.
Positional code | Add or concatenate (a) spatial code (x, y), (b) temporal code (t), (c) view code (v).
Result: a 1-D tensor (n_tokens, d_model) – identical shape to a text token list.
───────────────────────────────────────
2. VISION ENCODERS
───────────────────────────────────────
2.1 Static Images (ViT style, CLIP-like)
• Regular grid, patch = 16×16 or 14×14 RGB → flatten(3×P×P) → Linear(d_model).
• Learnable 2-D absolute pos or decomposed (x + y). Rotary works too.
• Optional: Finer tokens near salient objects (Dynamic ViT / VQ-KD).
2.2 Multi-view / Stereo / 3-D
Approaches:
a) Early fuse: Concatenate views along “batch” dimension and add view_id embedding.
b) Late fuse: Encode each view separately, pool (max/attention), then feed pooled tokens into a uni-modal language stream.
c) Volumetric patches: Treat depth or slices as another axis (X,Y,Z) and use 3-D patches (P×P×P) → tokens, then 3-D relative pos (x,y,z).
Useful tricks
• “Camera token” (NeRF-like) contains extrinsics; added as key/value in cross-attn.
• Cylindrical/ spherical pos enc. for 360° panoramas.
2.3 Short Videos (≤10 s)
Sampling: FPS=8–30. Clip length L.
Tokenisation variants
a) Factorised ViViT: spatial patches first (ViT) → tokens; then temporal Transformer on [CLS] of each frame.
b) Tubelet / 3-D patch: Patch = P×P×τ, produces (T/τ)(H/P)(W/P) tokens; add (x,y,t) relative pos or RoPE-3D.
c) Masked autoenc style (VideoMAE): mask 90% tokens, reconstruct in decoder ⇒ stronger representation.
2.4 Domain-specific
Medical MRI / CT 3-D volumes: tubelet in x,y,z.
Field cameras (LiDAR fusion): encode point cloud with PointNet-Patch or Voxel-ViT → tokens.
Comics / docs: patch + OCR tokens interleaved; use “layout” embedding for bounding-box.
───────────────────────────────────────
3. AUDIO ENCODERS
───────────────────────────────────────
3.1 Raw waveform
• 1-D conv front-end (like Wav2Vec2). Strided 5 ms windows → 512-d vector every 20 ms. Each vector = 1 token.
• Positional: rotary or relative along time only.
3.2 Time-frequency patches (most common)
• STFT → log-Mel or HTK Mel (f_bins≈64–128, hop=10 ms).
• Treat the spectrogram like an “image”: patch = f_patch × t_patch (e.g., 16 freq × 16 frames).
• Linear→d_model, add (freq, time) pos.
Benefits: inherits ViT weight initialisation, stable for music/speech.
3.3 Event / segment tokens
• If long clip (podcast, meeting): run VAD or HuBERT to cut into “speech segments” → feed [SEG_START] emb + pooled acoustic descriptor. Reduces 10^5 tokens → 10^3.
Special audio markers
[SPEAKER_i], [MUSIC], [NOISE] – helpful for conditioned generation (e.g., “describe the music then continue the dialogue”).
───────────────────────────────────────
4. MIXING MODALITIES INSIDE A TRANSFORMER
───────────────────────────────────────
4.1 Token-stream layouts
Layout A – Unified sequence (early-fusion)
[CLS][IMG_1..n][AUD_1..m][TEXT_1..k]
- modality-type embedding (
E_type ∈ {vision,audio,text}).
Layout B – Dual encoders + cross-attention (Perceiver IO, Flamingo)
Vision & audio go through frozen or lightweight encoders → produce latents.
Text decoder attends over latents with gated x-attn (“Perceiver Resampler”).
Layout C – Alternating blocks (Kosmos-2)
Layer 1-4: intra-modal (separate); Layer 5-N: shared full attention.
4.2 Positional encodings
Absolute 1-D: good for ≤512 tokens, language-like.
Decomposed: E_pos = E_x + E_y + E_t (+E_view) keeps parameter count small.
RoPE-2D/3D: rotate each axis separately, then sum – preserves extrapolation.
Relative / ALiBi: handy for variable-length video / audio.
4.3 Attention-matrix adaptations
• Block-sparse: allow Vision↔Vision & Lang↔Lang fully; Vision↔Lang only via [CLS] or special “bridge” tokens. Cuts O(N²).
• FiLM / Gated cross-attn: K,V from vision, Q from text (≤1 k tokens), learned gating scalar controls info flow (Flamingo).
• MoE experts per modality: Router top-2 gating, keeps compute constant.
• Low-rank adapters (LoRA) per modality: fine-tune multimodal model cheaply.
───────────────────────────────────────
5. MINI CHEAT-SHEET (pseudo-code)
───────────────────────────────────────
def encode_image(img, patch=16, d=768):
# img: (H,W,3)
patches = img_unfold(img, patch) # (N, 3*P*P)
tokens = linear(patches, d) # (N,d)
pos = img_2d_pos(N, H//patch, W//patch, d)
return tokens + pos, modality_emb("vision")
def encode_audio(wav, sr=16_000, patch_t=16, patch_f=16, d=768):
spec = log_mel(wav, sr) # (F,T)
patches = spec_unfold(spec, patch_f, patch_t) # (M, patch_f*patch_t)
tokens = linear(patches, d)
pos = spec_2d_pos(M, F//pf, T//pt, d)
return tokens + pos, modality_emb("audio")
def pack_and_run(vision_toks, audio_toks, text_ids):
text_emb = tok_embedding(text_ids) + text_pos(len(text_ids))
seq = concat([CLS, vision_toks, audio_toks, text_emb])
type_ids = [mod_emb("cls")] + vision_type + audio_type + lang_type
seq = seq + type_ids
out = transformer(seq)
return out
───────────────────────────────────────
OPTIONAL TIPS & TRICKS
───────────────────────────────────────
• Mixed precision & chunked attention are crucial when unified token counts reach 10–50 k.
• “Any-to-any” generation often uses an autoregressive decoder with modality tokens:
<BOS> [IMG_PATCHes] [AUD_PATCHes] … target_text_tokens <EOS>
For audio-out, treat waveform frames as discrete (EnCodec, SoundStream) or regress in a diffusion head.
• Aligning modalities:
‑ Contrastive pre-training (CLAP, CLIP) first, then supervised fine-tuning.
‑ Masked token modeling (BEIT, MAE, wav2vec) per modality before fusion.
• Evaluation:
Multimodal retrieval (R@1), VQA, AudioCaps, MSR-VTT QA, image-to-speech and speech-to-image generation to verify “any-to-any”.
───────────────────────────────────────
Key Papers / Codebases
───────────────────────────────────────
Vision: ViT, BEIT-3, VideoMAE, EVA, PaLI-3
Audio: AST, Wav2Vec2, AudioMAE, EnCodec
Multimodal: Flamingo, Gato, Kosmos-2, LLaVA-1.5, Perceiver IO, SeamlessM4T, PAE (Per-axis rotary encoding)
Hope this gives you a solid map of the design space. Ping me if you’d like deeper dives (e.g., diffusion decoders for audio-out, or how to compress 30-min video+audio into 4 k tokens).