Pytorch implementation of Paper/Release: Gemini: A Family of Highly Capable Multimodal Models (2023) / GPT-4o (2024)

Written by

in

The technical reports for Gemini (Google) and GPT-4o (OpenAI) represent the transition from “Modular Multimodality” (connecting a vision encoder to an LLM) to Native Multimodality.

While the exact weights are proprietary, both models utilize a Multimodal Transformer architecture where different modalities (text, images, audio, video) are converted into a unified token stream and processed by a single, massive Transformer backbone.

1. Unified Tokenization

The core “Native” secret is that images are not just “appended” as a single feature vector. Instead, they are quantized into visual tokens (using a ViT-based VQ-VAE or similar) so the LLM perceives them as just another “language.”

2. PyTorch Implementation: The Multimodal Backbone

To implement this, we need a Transformer that can handle an interleaved sequence of different token types.

Python

import torch
import torch.nn as nn

class UnifiedMultimodalTransformer(nn.Module):
    def __init__(self, vocab_size, visual_vocab_size, audio_vocab_size, d_model, n_layers):
        super().__init__()
        # Separate embedding layers for different modalities
        self.text_embed = nn.Embedding(vocab_size, d_model)
        self.vision_embed = nn.Embedding(visual_vocab_size, d_model)
        self.audio_embed = nn.Embedding(audio_vocab_size, d_model)
        
        # Shared Transformer layers
        # Gemini/GPT-4o use massive Decoder-only stacks (Pre-LN, GQA)
        self.layers = nn.ModuleList([
            GPT2Block(d_model, n_heads=16, d_ff=4*d_model, max_len=32768) 
            for _ in range(n_layers)
        ])
        
        self.final_norm = nn.LayerNorm(d_model)
        
        # Multiple output heads for generating different modalities
        self.text_head = nn.Linear(d_model, vocab_size)
        self.vision_head = nn.Linear(d_model, visual_vocab_size)
        # GPT-4o specifically focuses on native audio output (very low latency)
        self.audio_head = nn.Linear(d_model, audio_vocab_size)

    def forward(self, input_ids, modality_mask):
        """
        input_ids: Unified sequence of indices
        modality_mask: 0 for text, 1 for vision, 2 for audio
        """
        # 1. Project all inputs into the same d_model space
        embeddings = torch.zeros((*input_ids.shape, self.d_model))
        
        # Vectorized embedding selection based on modality
        embeddings[modality_mask == 0] = self.text_embed(input_ids[modality_mask == 0])
        embeddings[modality_mask == 1] = self.vision_embed(input_ids[modality_mask == 1])
        embeddings[modality_mask == 2] = self.audio_embed(input_ids[modality_mask == 2])
        
        x = embeddings
        for layer in self.layers:
            x = layer(x)
            
        x = self.final_norm(x)
        
        # Depending on the task, we sample from the appropriate head
        return self.text_head(x), self.vision_head(x), self.audio_head(x)


3. Key Technical Advances in 2023-2024

The reports highlight several critical optimizations for these “Omni” models:

  • Grouped-Query Attention (GQA): Used extensively in Gemini to maintain a massive context window (up to 1M+ tokens) while keeping the KV cache small enough for fast inference.
  • Late Fusion vs. Early Fusion: GPT-4o is “Early Fusion,” meaning it doesn’t use a separate speech-to-text model. It “hears” the audio tokens directly, allowing it to sense emotion and tone (the “Omni” capability).
  • RoPE (Rotary Positional Embeddings): Used to handle extremely long sequences without the degradation seen in absolute position embeddings.

4. Comparison: Gemini Pro vs. GPT-4o

FeatureGemini 1.5 ProGPT-4o (“Omni”)
Max Context2,000,000 Tokens~128,000 Tokens
Modality IntegrationVideo-Native (Spatial tokens)Audio-Native (Low-latency vocal)
ArchitectureMixture-of-Experts (MoE)Dense or Small-MoE (Speculated)
Primary StrengthDeep Reasoning over long docsReal-time human-like interaction

Why these are 2024 Milestones

Gemini and GPT-4o ended the era of “Text-only” LLMs. By training on a unified objective (predict the next token, whether it’s a pixel, a phoneme, or a word), these models achieved cross-modal reasoning. For example, they can watch a video of a physics experiment and explain the math behind it in text, or hear a person’s tone of voice and generate a corresponding image of how that person might be feeling.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *