Pytorch implementation of paper: Gemini 1.5: Unlocking multimodal understanding across millions of tokens of context (2024)

Written by

in

The 2024 technical report “Gemini 1.5: Unlocking multimodal understanding across millions of tokens of context” by Google DeepMind introduced a highly efficient Sparse Mixture-of-Experts (MoE) architecture.

Its defining achievement is the massive context window—initially 1 million and later expanded to 2 million+ tokens—with near-perfect retrieval. While Google has not released the specific proprietary code, the report points to several key architectural pillars: Sparse MoE, native multimodality, and advanced attention scaling (likely Ring Attention).


1. Sparse Mixture-of-Experts (MoE)

Unlike the dense Transformer blocks in Gemini 1.0 Ultra, Gemini 1.5 Pro uses a Sparse MoE. This allows the model to have a massive total parameter count while only activating a small fraction for each token, significantly reducing compute costs.

Python

import torch
import torch.nn as nn

class GeminiMoELayer(nn.Module):
    def __init__(self, d_model, num_experts=8, top_k=2):
        super().__init__()
        self.router = nn.Linear(d_model, num_experts)
        self.experts = nn.ModuleList([
            # Standard Gated Linear Unit (GLU) as used in Gemini/PaLM
            nn.Sequential(
                nn.Linear(d_model, d_model * 4),
                nn.GELU(),
                nn.Linear(d_model * 4, d_model)
            ) for _ in range(num_experts)
        ])
        self.top_k = top_k

    def forward(self, x):
        # x: [batch, seq, d_model]
        batch, seq, d_model = x.shape
        x_flat = x.view(-1, d_model)
        
        # Routing logic
        logits = self.router(x_flat)
        weights, indices = torch.topk(logits, self.top_k, dim=-1)
        weights = torch.softmax(weights, dim=-1)
        
        output = torch.zeros_like(x_flat)
        for i, expert in enumerate(self.experts):
            # Mask for tokens assigned to this expert
            mask = (indices == i).any(dim=-1)
            if mask.any():
                # Extract corresponding weight for this expert
                expert_weight = weights[indices == i].unsqueeze(-1)
                output[mask] += expert_weight * expert(x_flat[mask])
                
        return output.view(batch, seq, d_model)


2. Scaling to Millions: Ring Attention

To handle millions of tokens without running out of GPU memory (VRAM), Gemini 1.5 likely employs Ring Attention. Standard attention is $O(N^2)$ in memory; Ring Attention distributes the sequence across multiple GPUs in a ring, overlapping communication with computation to achieve linear memory scaling relative to the number of devices.

Python

# Conceptual logic for Blockwise/Ring Attention interaction
def blockwise_masked_attention(q_block, k_block, v_block, mask_block):
    """
    Computes attention for a small block of the sequence.
    In Ring Attention, k_block and v_block are passed from 
    one GPU to the next in a ring topology.
    """
    attn_weights = torch.matmul(q_block, k_block.transpose(-1, -2))
    attn_weights = attn_weights / (q_block.size(-1) ** 0.5)
    
    if mask_block is not None:
        attn_weights += mask_block
        
    probs = torch.softmax(attn_weights, dim=-1)
    return torch.matmul(probs, v_block)


3. Native Multimodal Inputs

Gemini 1.5 is “multimodal from the ground up.” It tokenizes video at a specific frame rate (e.g., 1 frame per second) and treats those visual tokens as part of the primary sequence.

ModalityTokenization MethodContext Capacity (1.5 Pro)
TextSentencePiece / Byte-levelUp to 2,000,000+ Tokens
VideoInterleaved Frames (Visual Tokens)~3 hours (at 1 fps)
AudioUSM / Discrete Audio Tokens~22 hours

4. Technical Specifications

  • Architecture: Sparse Mixture-of-Experts (MoE) Transformer.
  • Optimization: Likely uses FlashAttention-2 within each block and Ring Attention across nodes.
  • Recall Performance: Near-perfect “needle-in-a-haystack” retrieval (>99%) across the entire context window.
  • Efficiency: Achieves Gemini 1.0 Ultra-level quality with significantly lower training compute and inference latency.

Why it is a 2024 Milestone

Gemini 1.5 changed the paradigm of Retrieval-Augmented Generation (RAG). While smaller models require RAG to “find” information in external databases, Gemini 1.5 can simply take the entire database (e.g., an entire codebase or 1,500-page PDF) into its “active memory” (context window). This allows for much more complex reasoning across distant parts of a massive dataset that RAG chunks might miss.

Comments

Leave a Reply

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