This video by The Half Stack Girl explains the concept of 4D AI video generation and highlights a new model called Atlas by WorldLabs.
Here is a breakdown of how 4D AI video generation works and what Atlas can do:
The 4D Challenge: A true 4D model must understand 3D space (height, width, and depth) plus time [00:11]. This is highly difficult for AI because it requires keeping a scene completely consistent across two moving variables: where the camera is located and when the frame happens [00:23].
Atlas by WorldLabs: Atlas is a spatial AI system that takes images, videos, and camera movements as inputs to generate “3D-grounded” video with precise camera control [00:36].
Inferring 3D Structure: If you give Atlas a single photo of a room, it can infer the entire 3D structure—figuring out where objects are, what is behind them, and how the geometry continues outside the frame [00:42]. This allows you to move a virtual camera around and generate videos from completely new angles.
Spatial Grounding: A key architectural detail is that every generated image is grounded at an explicit 3D position [00:58]. Because this spatial information becomes part of the AI’s context, you can give Atlas two images from different positions, and it will generate the plausible 3D space connecting them [01:04].
Ultimately, this new generation loop (model the space -> move the camera -> generate the view) gets AI closer to fully simulating environments, which has huge implications for robotics, gaming, and visual effects [01:16
This video by The Half Stack Girl explains how modern AI inference engines—specifically vLLM and SGLang—make Large Language Models (LLMs) significantly faster and more efficient without altering the actual underlying models [00:00
]. They achieve this by optimizing how the infrastructure schedules and manages “traffic” for GPUs [00:12
].
Here is a breakdown of how each engine optimizes AI performance:
vLLM and PagedAttention: Previously, AI systems wasted expensive GPU memory by reserving large chunks upfront for a conversation’s short-term memory (KV cache) before it was even needed [00:24]. vLLM solves this using “PagedAttention,” which allocates memory dynamically in small, as-needed pages [00:38]. This drastically reduces memory fragmentation, increases GPU utilization, and allows more users to share the same hardware [00:44].
SGLang and Radix Attention: SGLang tackles the issue of repeated computation, such as when multiple requests share the same system prompt, loaded document, or conversation history [00:52]. Using a technique called “radix attention,” it caches these shared prefixes so the GPU only computes the shared context once and reuses it across multiple requests [01:05]. This makes SGLang highly efficient for applications like chatbots and AI agents where context often repeats [01:13].
Ultimately, these infrastructure upgrades allow the same models to run much faster and handle more users on existing hardware [01:24
For a software, solution, or enterprise architect, documentation tools generally fall into four main categories: Diagrams-as-Code (version-controlled), Visual Canvas Tools (collaborative/presentation-ready), Specialized Architecture Modeling Tools (formal models like C4/ArchiMate), and Documentation & ADR Platforms.
Best for whiteboarding, interactive stakeholder walkthroughs, and high-fidelity presentations.
Draw.io (diagrams.net):
Strengths: Free, open-source, works offline, saves directly to Git/Google Drive/OneDrive, includes official AWS, Azure, GCP, and C4 shape libraries.
Lucidchart:
Strengths: Enterprise standard for technical architecture. Supports live data linking, automated architecture generation from AWS/GCP/Azure configs, and team collaboration.
Miro / FigJam / Mural:
Strengths: Infinite digital whiteboards. Ideal for Event Storming, Domain-Driven Design (DDD) workshops, and early-stage exploratory system architecture.
IcePanel:
Strengths: An interactive, zoomable map specifically built around the C4 model. Bridges the gap between loose whiteboards and overly rigid enterprise modeling tools.
For large enterprise teams operating under formal frameworks like TOGAF, ArchiMate, or strict UML/SysML standards.
Archi: Open-source, dedicated ArchiMate modeling tool for enterprise architects mapping business capabilities, application layers, and technology infrastructure.
Sparx Systems Enterprise Architect: Heavyweight, industrial-grade tool for deep UML, SysML, database schema, and process modeling.
Bizzdesign / LeanIX: SaaS platforms for Application Portfolio Management (APM), capability mapping, and enterprise IT governance.
4. Documentation Frameworks & Decision Records (ADRs)
Diagrams alone are rarely enough—architectural rationale needs structured documentation.
A. Architecture Decision Records (ADRs)
Document the why behind decisions (context, options considered, trade-offs, final decision).
adr-tools / Log4brains: CLI tools to maintain Markdown ADRs directly in your Git repository.
MADR (Markdown Any Architecture Decision Records): Standardized, lightweight Markdown templates for engineering teams.
B. Docs-as-Code & Knowledge Bases
arc42 Template: The industry-standard 12-section architecture documentation template (can be used in Markdown, AsciiDoc, or Confluence).
Docusaurus / MkDocs (with mkdocs-material): Static site generators that convert Markdown + Mermaid/PlantUML files into searchable architecture portals.
Backstage (by Spotify): Open-source developer portal framework that centralizes architecture documentation, API specs (OpenAPI/AsyncAPI), and service catalogs.
Quarkus is a full-stack, Kubernetes-native Java framework designed specifically for cloud environments, microservices, and serverless architectures. Created by Red Hat, it is often described as “Supersonic Subatomic Java.”
To understand Quarkus, you have to understand the problem it was built to solve.
The Problem: Java in the Cloud
Historically, Java was designed for long-running, monolithic applications. It uses dynamic class loading, reflection, and runtime proxy generation. This makes traditional Java applications (like older Spring or Jakarta EE apps) take several seconds to start up and consume hundreds of megabytes of RAM just to idle.
In modern cloud environments—where auto-scaling microservices and serverless functions (like AWS Lambda) demand instant startup times and low memory footprints—traditional Java was struggling to compete with languages like Go, Node.js, and Python.
The Solution: How Quarkus Works
Quarkus solves this by fundamentally changing when the framework does its heavy lifting. It uses a concept called “Compile-Time Boot” (or Ahead-of-Time processing).
Instead of parsing configuration files, scanning the classpath for annotations, and wiring up Dependency Injection when the application starts (which is what Spring Boot traditionally does), Quarkus does all of this when the application builds.
By the time the application is compiled, the framework has already hardcoded the exact classes and routes needed.
Core Features of Quarkus
GraalVM Native Image Support (The Superpower): Because Quarkus resolves everything at build time, it integrates flawlessly with GraalVM. GraalVM compiles your Java code into a standalone, OS-specific binary executable (an .exe on Windows, or a Linux binary). This means your app doesn’t even need a JVM installed on the server to run.
Startup time: Drops from seconds to milliseconds.
Memory footprint: Drops from 300MB+ to tens of megabytes.
“Developer Joy” (Live Coding): Quarkus features an incredible developer experience. If you run quarkus dev, it watches your code. When you save a file, it hot-reloads the changes in milliseconds. You can write code and refresh your browser without ever manually restarting the server.
Dev Services: If your code needs a PostgreSQL database, Kafka broker, or Redis cache, you do not need to install them or write docker-compose files. When you start Quarkus in dev mode, it automatically detects what you need and spins up Testcontainers (Docker containers) in the background for you.
Unifies Imperative and Reactive: You can write standard, blocking Java code (imperative) and non-blocking, event-driven code (reactive) in the exact same application, often safely routing between the two.
Quarkus vs. Spring Boot
While Spring Boot is the undisputed king of enterprise Java, Quarkus has become its most aggressive modern competitor.
Feature
Quarkus
Spring Boot
Architecture Focus
Cloud-native, Kubernetes, Serverless
General purpose, Enterprise applications
Framework Style
Build-time processing (Ahead-of-Time)
Historically Runtime processing (Just-in-Time)
Native Compilation
Built ground-up for GraalVM
Added via Spring Native (Very good, but retrofitted)
Standards
Uses standard APIs (Jakarta EE, MicroProfile, JAX-RS)
Uses custom Spring APIs (@RestController, etc.)
Ecosystem Size
Growing rapidly, strong cloud integrations
Massive, virtually infinite third-party support
When Should You Use Quarkus?
Serverless Functions: If you want to write AWS Lambdas or Google Cloud Functions in Java, Quarkus compiled to a native binary is the best way to avoid “cold start” timeouts.
High-Density Kubernetes: If you need to pack hundreds of microservices into a Kubernetes cluster, Quarkus will save you massive amounts of RAM, lowering cloud infrastructure bills.
Modernizing Java Teams: If you have a team of Java developers who know standard Java EE / Jakarta EE APIs (like JAX-RS or Hibernate) and want to transition to cloud-native microservices without learning a totally new language like Go.
When designing and implementing REST APIs in Java, architecture is typically evaluated across three dimensions:
Application / Codebase Structure (How internal code is organized)
System & Deployment Topology (How the system runs and scales across infrastructure)
Execution & Concurrency Model (How Java processes HTTP requests and I/O)
1. Codebase & Application-Level Architectures
These patterns dictate how your Java code is structured inside a repository/service.
A. Layered (Controller–Service–Repository / N-Tier)
How it works: Code is arranged horizontally by technical concern:
Controller Layer (@RestController): Handles HTTP serialization/deserialization, validation, routing, and HTTP status codes.
Service Layer (@Service): Contains business logic, orchestration, and transactional boundaries (@Transactional).
Repository/Data Layer (@Repository): Communicates with the database (Spring Data JPA, Hibernate, jOOQ).
Common Java Stack: Standard Spring Boot, Jakarta EE (JAX-RS + EJB/CDI + JPA), Micronaut.
Best for: Small to medium applications, CRUD-heavy services, fast development cycles.
B. Hexagonal Architecture (Ports and Adapters) / Clean / Onion Architecture
How it works: The business domain is placed at the center, completely independent of frameworks, databases, and HTTP transports:
Inbound Ports & Adapters: Inbound ports define what the app can do; the REST controller is an inbound adapter that translates HTTP requests to call the port.
Outbound Ports & Adapters: Outbound ports define interfaces for data persistence/external APIs; repositories are outbound adapters.
Common Java Stack: Core Java domain (POJOs, no framework annotations), with Spring Boot/Quarkus wiring adapters via Dependency Injection.
Best for: Complex domain logic, long-lived enterprise applications, and scenarios where you want high testability without spinning up Spring contexts or databases.
C. Modular Monolith Architecture
How it works: A single deployable unit structured strictly into domain-bounded modules with explicit API boundaries between modules. It prevents the “spaghetti monolith” while avoiding the operational overhead of microservices.
Best for: Teams wanting clean domain boundaries and independence without managing distributed infrastructure.
D. CQRS (Command Query Responsibility Segregation) & Event Sourcing
How it works: Splits REST write endpoints (Commands: POST, PUT, DELETE) from read endpoints (Queries: GET). The read model and write model can use different data stores optimized for queries (e.g., Elasticsearch/Redis) and transactional writes (PostgreSQL).
Common Java Stack:Axon Framework, Spring Boot + Kafka, Debezium (for Change Data Capture).
Best for: High-read/high-write asymmetry, audit-heavy domains (finance, healthcare), and systems requiring an immutable history of domain events.
2. System & Deployment-Level Architectures
These patterns define how REST services are distributed across networks and infrastructure.
Architecture
Description
Common Java Ecosystem
Best For
Monolithic REST API
Single deployable JAR/WAR serving all REST endpoints.
Spring Boot, Jakarta EE, Quarkus
Startups, small teams, lower operational complexity.
Microservices REST API
Fine-grained, independently deployable services per business capability communicating over REST/gRPC/Messaging.
Spring Cloud, Quarkus, Micronaut, Netflix Eureka/Consul, Spring Cloud Gateway
Quarkus / Micronaut with GraalVM Native Image, Spring Cloud Function
Intermittent traffic, event-driven integrations, cost minimization at low volume.
BFF (Backend-for-Frontend)
Specialized REST APIs tailored for specific frontends (Mobile, Web, Third-party) that aggregate calls to underlying services.
Spring Cloud Gateway, GraphQL/REST hybrid in Spring Boot
Applications with varied consumer requirements across mobile, desktop, and public APIs.
3. Concurrency & Execution Models in Java
The underlying I/O model significantly influences how your REST API handles traffic and scales under high loads:
A. Classic Thread-Per-Request (Blocking I/O)
Mechanism: Every incoming HTTP request is assigned a dedicated operating system (platform) thread from a pool (e.g., Tomcat/Jetty worker threads).
Java Stack: Spring MVC (spring-boot-starter-web), standard Jakarta EE Servlet container.
Pros & Cons: Simple mental model and easy debugging, but high concurrent connection counts can cause memory pressure from OS thread overhead.
B. Virtual Threads / Project Loom (Modern Blocking I/O)
Mechanism: Introduced in Java 21. Standard synchronous, blocking code runs on lightweight “virtual threads” managed by the JVM instead of OS threads. When blocking on I/O, the JVM unmounts the virtual thread, enabling millions of concurrent requests with standard imperative syntax.
Java Stack: Java 21+ with Spring Boot 3.2+ (spring.threads.virtual.enabled=true), Quarkus, Helidon 4.
Pros & Cons: Gives the performance benefits of reactive programming without rewriting code in non-blocking/reactive paradigms.
C. Reactive / Event-Loop (Non-Blocking I/O)
Mechanism: Employs an event-loop model (typically Netty) with backpressure to handle thousands of requests per thread without blocking.
Books:Design It! by Michael Keeling; 97 Things Every Programmer Should Know (design entries). Sites: GitHub repos for design katas; Exercism (design-style exercises). Articles: “System Design Primer” repo. Tools: whiteboard (Miro, Excalidraw).
Code reading & architecture reviews
System-level thinking; anti-pattern spotting
1–3 hrs
Weekly
Sources: High‑quality open-source projects (Redis, Nginx, Kubernetes). Reads: Martin Fowler essays; architecture blog posts on High Scalability. Tools: GitHub, Sourcegraph for code navigation.
Books:Refactoring by Martin Fowler; Working Effectively with Legacy Code by Michael Feathers. Tools: IDE refactor tools, unit test frameworks, mutation testing (Stryker).
Pair design / mobbing
Communication, consensus, real-world tradeoffs
1–3 hrs
When collaborating
Practices: Mob programming guides; pair design checklists. Platforms: Tuple, VS Code Live Share, Zoom + Miro. Reads: “The Art of Readable Code” (team practices).
Study canonical systems & patterns
Mental models, proven solutions
30–90 min
Weekly
Books:Design Patterns (GoF); Patterns of Enterprise Application Architecture by Fowler; Designing Data-Intensive Applications by Martin Kleppmann. Blogs: High Scalability, ACM Queue.
Build small systems end-to-end
Full lifecycle, ops, tradeoffs
Several days
Monthly project
Tutorials: Build-a-service guides (e.g., microservice from zero to prod). Platforms: Heroku, Render, Docker, Kubernetes tutorials. Courses: Cloud provider architecture labs (AWS, GCP, Azure).
How to use the resources (quick notes)
Design katas: Use the System Design Primer repo for kata prompts and sample solutions; sketch on Excalidraw, then compare with community solutions.
Code reading: Pick a small, well-documented OSS project (Redis, Nginx, Kubernetes) and read its architecture docs first, then trace code paths for a feature.
Requirements → design: Follow C4 diagrams and write an ADR for each major decision; use RFC templates from established teams as examples.
Refactoring: Work through examples in Refactoring and apply tests before/after; use mutation testing to validate test effectiveness.
Pairing: Run a 60‑minute session: 30 min design, 20 min critique, 10 min decision record; use Live Share or Miro to collaborate.
Patterns & systems: Read one chapter from Designing Data‑Intensive Applications each week and map examples to systems you know.
End-to-end builds: Follow a cloud provider’s “build and deploy” tutorial, then add observability (Prometheus/Grafana) and a simple chaos test.
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.
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.
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
Feature
Gemini 1.5 Pro
GPT-4o (“Omni”)
Max Context
2,000,000 Tokens
~128,000 Tokens
Modality Integration
Video-Native (Spatial tokens)
Audio-Native (Low-latency vocal)
Architecture
Mixture-of-Experts (MoE)
Dense or Small-MoE (Speculated)
Primary Strength
Deep Reasoning over long docs
Real-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.
The 2023 paper “Efficient Memory Management for Large Language Model Serving with PagedAttention” by Kwon et al. (UC Berkeley) introduced vLLM, a high-throughput serving engine.
The core problem it solves is KV Cache fragmentation. In standard LLM serving, memory for the Key-Value (KV) cache is allocated contiguously for the maximum possible sequence length. This leads to “Internal Fragmentation” (reserved but unused space) and “External Fragmentation” (scattered small free blocks). PagedAttention treats GPU memory like Virtual Memory in an Operating System, breaking the KV cache into non-contiguous “pages.”
1. The PagedAttention Logic
Instead of a single large tensor for a request, PagedAttention stores the KV cache in Physical Blocks. A Block Table maps the logical position of a token in a sequence to a physical slot in a GPU memory block.
While the actual high-performance kernel is written in CUDA, we can implement the Logical-to-Physical mapping in PyTorch to understand how the memory is managed.
Python
import torch
import torch.nn as nn
class PagedAttentionExecutor(nn.Module):
def __init__(self, num_heads, head_dim, block_size=16):
super().__init__()
self.num_heads = num_heads
self.head_dim = head_dim
self.block_size = block_size # Number of tokens per block
self.scale = head_dim ** -0.5
def forward(self, query, k_cache, v_cache, block_table, context_lens):
"""
query: [num_queries, num_heads, head_dim]
k_cache: [num_blocks, num_heads, head_dim, block_size]
v_cache: [num_blocks, num_heads, block_size, head_dim]
block_table: [num_queries, max_num_blocks_per_query]
context_lens: [num_queries]
"""
num_queries = query.shape[0]
output = torch.zeros_like(query)
for i in range(num_queries):
q = query[i] # [num_heads, head_dim]
cur_context_len = context_lens[i]
cur_block_table = block_table[i]
# 1. Fetch relevant blocks from the scattered physical cache
# This avoids having one giant contiguous tensor per request
num_blocks = (cur_context_len + self.block_size - 1) // self.block_size
# Logic: Collect all K and V tensors from the mapped blocks
keys = []
values = []
for j in range(num_blocks):
block_idx = cur_block_table[j]
keys.append(k_cache[block_idx]) # [num_heads, head_dim, block_size]
values.append(v_cache[block_idx]) # [num_heads, block_size, head_dim]
# Concatenate blocks into a temporary view for attention calculation
K = torch.cat(keys, dim=-1)[..., :cur_context_len] # [num_heads, head_dim, context_len]
V = torch.cat(values, dim=-2)[:, :cur_context_len, :] # [num_heads, context_len, head_dim]
# 2. Standard Attention Calculation
# energy = (Q @ K) * scale
attn_weights = torch.matmul(q.unsqueeze(1), K).squeeze(1) * self.scale
attn_weights = torch.softmax(attn_weights, dim=-1) # [num_heads, context_len]
# context = weights @ V
out = torch.matmul(attn_weights.unsqueeze(1), V).squeeze(1)
output[i] = out
return output
3. Key Component: The Block Manager
The “secret sauce” is the CPU-side Block Manager, which tracks which physical blocks on the GPU are free, occupied, or shared (for Copy-on-Write).
Python
class BlockManager:
def __init__(self, num_total_blocks):
self.free_blocks = list(range(num_total_blocks))
self.active_requests = {} # request_id -> list of block_ids
def allocate(self, request_id, num_tokens):
num_blocks_needed = (num_tokens + 15) // 16
allocated = []
for _ in range(num_blocks_needed):
allocated.append(self.free_blocks.pop(0))
self.active_requests[request_id] = allocated
return allocated
def free(self, request_id):
blocks = self.active_requests.pop(request_id)
self.free_blocks.extend(blocks)
4. Technical Specifications & Impact
Metric
Contiguous Cache
PagedAttention (vLLM)
Memory Waste
60% – 80% (Reserved)
< 4% (Only last block)
Throughput
Bottlenecked by VRAM
2x – 4x Increase
Decoding
Sequential
Highly Parallel
Complex Sampling
Difficult (Beam Search)
Efficient (Copy-on-Write)
Why it is a 2023 Milestone
PagedAttention enabled Near-Zero Memory Waste. By allowing the KV cache to be fragmented physically but contiguous logically, it allowed servers to pack significantly more requests into the same GPU. This is the technology that powers current high-speed inference APIs and allows for efficient “Beam Search” and “Parallel Sampling” by simply sharing physical blocks between different candidate sequences.