惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
K
Kaspersky official blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Exploit Database - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Latest news
Latest news
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
T
Tor Project blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cisco Blogs
阮一峰的网络日志
阮一峰的网络日志
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园_首页
N
News and Events Feed by Topic
W
WeLiveSecurity
罗磊的独立博客
GbyAI
GbyAI
T
Troy Hunt's Blog
Y
Y Combinator Blog
Recorded Future
Recorded Future
The Cloudflare Blog
TaoSecurity Blog
TaoSecurity Blog
爱范儿
爱范儿
美团技术团队
Attack and Defense Labs
Attack and Defense Labs
C
Check Point Blog
Engineering at Meta
Engineering at Meta
Cyberwarzone
Cyberwarzone
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
D
DataBreaches.Net
Recent Announcements
Recent Announcements
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
Webroot Blog
Webroot Blog
Security Latest
Security Latest
T
Tailwind CSS Blog
V2EX - 技术
V2EX - 技术
aimingoo的专栏
aimingoo的专栏
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed

Hacker News - Newest: "LLM"

GitHub - lechmazur/position_bias: A benchmark for testing whether LLM judges keep the same preference when two lightly edited versions of the same story are shown in opposite orders. Flex routing (EU and EFTA) Dark Factories: Retooling for LLM Velocity Ask HN: What would be the impact of a LLM output injection attack? GitHub - AronDaron/dataset-generator: No-code desktop app for generating high-quality synthetic datasets to fine-tune LLMs — plan-then-execute pipeline, LLM-as-judge, HuggingFace upload. GitHub - Oaklight/llm-rosetta: Production-ready LLM API translation layer for Python — bidirectional conversion between OpenAI, Anthropic & Google formats via hub-and-spoke IR. Optional API gateway. Streaming & non-streaming. Zero core deps. Contributions welcome! GitHub - browser-use/browser-harness: Self-healing browser harness that enables LLMs to complete any task. GitHub - moeen-mahmud/remen: Remen turns thoughts into something you can return to Analyzing 156 LLM Launch Posts on Hacker News ChatGPT vs Gemini vs Claude: The Best LLM Subscription You Should Buy GitHub - salaamalykum/quran-semantic-search: High-density RAG Semantic Search Engine & Quran Corpus (GEO/SEO Architecture) GitHub - NVIDIA/TensorRT-LLM: TensorRT LLM provides users with an easy-to-use Python API to define Large Language Models (LLMs) and supports state-of-the-art optimizations to perform inference efficiently on NVIDIA GPUs. TensorRT LLM also contains components to create Python and C++ runtimes that orchestrate the inference execution in a performant way. The State of LLM Bug Bounties in 2026 Operational Readiness Criteria for Tool-Using LLM Agents Meshcore: Architecture for a Decentralized P2P LLM Inference Network How an LLM becomes more coherent as we train it GitHub - seetrex-ai/laimark GitHub - Jossifresben/BibCrit: AI-assited biblical textual criticism GitHub - wastedcode/memex: File system based wiki, maintained by Claude 99helpers.com GitHub - cliver-project/AITrigram GitHub - unbody-io/adapt: A self-evolving memory layer for AI agents. GitHub - hb20007/awesome-gen-ai-fails: A list of incidents where reliance on generative AI and LLMs resulted in harm to companies, individuals, or society GitHub - nevenkordic/localmind: Run any local LLM with persistent memory and context. CLI agent over Ollama with SQLite-backed hybrid recall. No cloud. Ask HN: What are the machine requirements for a LLM like Llama-3.1-8B? Faster LLM Inference via Sequential Monte Carlo grpo explained: group relative policy optimization for llm finetuning - cgft Stop comparing price per million tokens: the hidden LLM API costs · TensorZero Andrej Karpathy's LLM Wiki Is a Bad Idea GitHub - GG-QandV/mnemostroma: Offline RAM-first cognitive leer/coprocessor for AI agents and robotics. Solves "Context Abandonment" with 20-80ms latency using a dual-thread biomimetic memory architecture (ONNX + SQLite WAL). mempalace/agent at agent · skorotkiewicz/mempalace GitHub - Nyquest-ai/nyquest-rust-fullstack-pub: Nyquest — Semantic Compression Proxy for LLMs. 350+ rules, local LLM stage, 15-75% token savings. Full Rust stack. GitHub - TheoV823/mneme: Enforce architectural decisions in AI-assisted development. GitHub - klemenvod/TokenBrawl: A 1v1 Bomberman-style game where two LLM agents play autonomously against each other. No human plays — you watch the AIs fight. Each agent receives a text description of the board state, reasons about it, and outputs a move as JSON. The game engine executes it. Introducing the Common AI Provider: LLM and AI Agent Support for Apache Airflow Power Circuit AI: Designing Power Electronic Circuits for Motor Drives with Generative Artificial Intelligence Ask HN: How to program with IDE and LLM on CPU locally? Show HN: Agent-cache – Multi-tier LLM/tool/session caching for Valkey and Redis Bonsai 1-bit WebGPU - a Hugging Face Space by webml-community The LLM Fallacy: Misattribution in AI-Assisted Cognitive Workflows Ask HN: Simple tooling for local LLM code critique without IDE integration? Can a General LLM Diagnose a DICOM Slice? A 10-Case Public Benchmark Charts-of-Thought: Enhancing LLM Visualization Literacy (PDF, 2026) GitHub - Mesh-LLM/mesh-llm: Distributed AI/LLM for the people. Share compute privately or publicly to power your agents and chat. GitHub - seamus-brady/springdrift: A persistent runtime for long-lived LLM agents Writing an LLM from scratch, part 32k -- Interventions: training a better model locally with gradient accumulation Ask HN: Which LLM model and agentic CLI are you using for local development? GitHub - wayneColt/modelcascade: Route local. Escalate smart. Never overspend. Open-source multi-model cascade routing for autonomous agents. LLM pricing is 100x harder than you think GitHub - asakin/llm-primer: Pre-warmed Claude Code sessions in tmux. No startup wait. GitHub - EggerMarc/chat-rs: A multi-provider LLM framework for Rust. GitHub - SynapseKit/SynapseKit: Minimal, async-first Python framework for production LLM apps- 2 hard deps, no magic, no SaaS. A Claude Skill that Makes LLM Paragraphs More Bearable Does Gas Town 'steal' usage from users' LLM credits & paid services to improve itself? What's Claude Code Actually Doing? Open the Black Box with the Arthur Engine Milla Jovovich's New Open Source LLM Memory App and the Dark Code Problem Your intuition of LLM token usage might be wrong Show HN: Bloomberg Terminal for LLM ops – free and open source GitHub - 0xchamin/mcptube: Transform YouTube videos into a compounding knowledge base with transcripts, vision analysis, and agentic search. Works as an MCP server for Claude, Copilot & more. Show HN: Open KB: Open LLM Knowledge Base Your LLM is a compiler, not a runtime GitHub - sapountzis/Unslop: A Web Feed That Deserves You crates.io: Rust Package Registry Beyond Karpathy's LLM-Wiki: The Necessity of Cognitive Governance GitHub - amitshekhariitbhu/llm-internals: Learn LLM internals step by step - from tokenization to attention to inference optimization. GitHub - parallem-ai/parallem: An expressive library for running agents with the Batch API. GitHub - stfurkan/pi-llm LLM-Wiki Show HN: Formal – Formal verification for AI-generated code using Lean 4 LRTS – Regression testing for LLM prompts (open source, local-first) LLM Wiki Skill: Build a Second Brain with Claude Code and Obsidian I built an LLM Wiki and RAG solution: here's a demo for a security KB The biggest advance in AI since the LLM Predict-Rlm: The LLM Runtime That Lets Models Write Their Own Control Flow the-synthetic-library/the-synthetic-mind at main · joshferrer1/the-synthetic-library GitHub - yisding/reviewwiggum GitHub - Donnyb369/mcp-spine: Context Minifier & State Guard — Local-first MCP middleware proxy GitHub - Beledarian/wgpu-llm: A from-scratch LLM inference engine that uses wgpu (the cross-platform WebGPU implementation) to dispatch WGSL compute shaders for every math operation a Transformer needs. No CUDA. No Python. No massive framework dependencies. Just Rust, raw shaders, and your GPU. GitHub - anitiue/Hindsight: An experience-driven self-improvement framework for LLM agents — 基于经验的 LLM Agent 自我改进框架 GitHub - stef41/lmscan: 🔍 Detect AI-generated text and fingerprint which LLM wrote it. Open-source GPTZero alternative. Zero dependencies, works offline. GitHub - alainnothere/AmdPerformanceTesting: Amd Performance Testing Ask HN: Is a purely Markdown-based CRM a terrible idea? Optimized for LLM agents Context Engineering - LLM Memory and Retrieval for AI Agents | Weaviate little_helper_tui/letter.md at main · sleepyeldrazi/little_helper_tui GitHub - EvanZhouDev/umr: The Unified Model Registry for all your local AI apps. GitHub - JordanCT/VigIA-Orchestrator Your Agent Is Mine: Measuring Malicious Intermediary Attacks on the LLM Supply Chain A Taxonomy of RL Environments for LLM Agents Llama LLM Network Feture GitHub - genedeng-ca/ai-mac-migration: AI-powered Mac-to-Mac migration tool - replace Apple Migration Assistant with intelligent, selective transfer using local LLMs GitHub - lunargate-ai/gateway: High-performance self-hosted AI gateway (OpenAI-compatible) with routing, retries, and streaming GitHub - AuthBits/webmcp: A lightweight, prompt-driven MCP web research server for high-quality LLM powered information extraction. Externalization in LLM Agents: A Unified Review of Memory, Skills, Protocols and Harness Engineering Springdrift: An Auditable Persistent Runtime for LLM Agents with Case-Based Memory, Normative Safety, and Ambient Self-Perception High-Stakes Personalization: Rethinking LLM Customization for Individual Investor Decision-Making From Static Templates to Dynamic Runtime Graphs: A Survey of Workflow Optimization for LLM Agents HUOZIIME: An On-Device LLM-enhanced Input Method for Deep Personalization TIDE: Token-Informed Depth Execution for Per-Token Early Exit in LLM Inference Characterizing WebGPU Dispatch Overhead for LLM Inference Across Four GPU Vendors, Three Backends, and Three Browsers LLM Targeted Underperformance Disproportionately Impacts Vulnerable Users
How LLM Inference Works
Arpit Bhayani · 2025-11-22 · via Hacker News - Newest: "LLM"

When you enter a prompt into an LLM, the model converts your text into numbers, processes them, and returns a response one token at a time. In this article, we go through the journey of LLM inference and see how it works.

What are Large Language Models?

LLMs are just neural networks built on the transformer architecture. Unlike earlier architectures that processed text sequentially, transformers can analyze entire sequences in parallel, making them more efficient to train and deploy.

The fundamental building block of these models is the transformer layer, which consists of two primary components:

  1. a self-attention mechanism, and
  2. a feed-forward neural network.

LLMs stack dozens of these layers, creating deep networks capable of capturing complex patterns in language.

Transformers rely on self-attention and it evaluates how each word relates to the rest of the sequence, not just its neighbouring words.

Model size = the number of parameters in the network. A 7-billion parameter model has 7 billion floating-point numbers that store the learned knowledge from training. These parameters are organized into weight matrices that perform transformations on the input data at each layer.

Models like GPT-4, Claude, and Llama are decoder-only transformers, meaning they use only the decoder part of the original transformer architecture. This makes them autoregressive, generating one token at a time based on all previously generated tokens, which is perfect for text generation tasks.

Tokenization

Before any computation happens, the model needs to convert your text input into numbers. This process, called tokenization, breaks text into smaller units called tokens.

The most common tokenization approach in modern LLMs is Byte Pair Encoding (BPE). BPE starts with a vocabulary of individual characters and iteratively merges the most frequent pairs of adjacent tokens to create new tokens.

# Example of BPE tokenization process
# Input text: "unhappiness"

# Initial: ['u', 'n', 'h', 'a', 'p', 'p', 'i', 'n', 'e', 's', 's']
# After merges: ['un', 'happi', 'ness']

Because of BPE, common words get represented as single tokens (efficient), while rare or unknown words get broken into familiar subword pieces (flexible).

The tokenization process works by encoding your input text into UTF-8 bytes, then applying the learned merge rules to compress the byte sequence into tokens. Each token maps to an integer ID that the model can work with.

# Simplified tokenization example
text = "The AI model generates text"
tokens = tokenizer.encode(text)
# [464, 15592, 2746, 18616, 2420]

Tokenization directly impacts model performance and costs. More tokens mean more computation, higher API costs, and potentially hitting context length limits. This is why non-English text often costs more to process since these languages typically require more tokens per word when the tokenizer was primarily trained on English data.

Token Embeddings

Once text becomes tokens, the next step transforms these discrete token IDs into continuous vector representations that neural networks can process. This happens through an embedding layer, essentially a lookup table that maps each token ID to a high-dimensional vector.

For a model with a vocabulary of 50,000 tokens and an embedding dimension of 4,096, the embedding matrix has shape [50000, 4096]. Each row represents one token, and the values in that row form the embedding vector for that token.

token_id = 464  # Token ID for "The"
embedding_vector = embedding_matrix[token_id]
# Result: a vector of 4096 floating-point numbers

These embedding vectors capture semantic meaning learned during training. Words with similar meanings have embedding vectors that point in similar directions in this high-dimensional space.

Transformers do not inherently understand the order of tokens. To address this, we add positional encodings to the embeddings, providing information about where each token sits in the sequence. Modern approaches use learned positional embeddings or relative position encodings like Rotary Position Embeddings (RoPE).

The Transformer Architecture

The transformer processes embedding vectors through its layers. Each transformer layer applies two main operations: multi-head self-attention and feed-forward networks.

The self-attention mechanism computes three matrices for each token: Query (Q), Key (K), and Value (V). These come from multiplying the input embeddings by three learned weight matrices.

# Self-attention computation
Q = input @ W_query   # Shape: [batch, seq_len, dim]
K = input @ W_key     # Shape: [batch, seq_len, dim]
V = input @ W_value   # Shape: [batch, seq_len, dim]

The weight matrices W_query, W_key, and W_value are learned during training. They are randomly initialized and then adjusted through backpropagation to extract the most useful patterns from the embeddings.

The attention mechanism then computes how much each token should attend to every other token. This happens through a scaled dot-product attention calculation:

# Attention scores
scores = (Q @ K.transpose()) / sqrt(dim)
attention_weights = softmax(scores)
output = attention_weights @ V

The scaling factor (square root of dimension) prevents the dot products from becoming too large, which would cause the softmax function to saturate and produce extremely small gradients during training.

Multi-head attention runs this process multiple times in parallel with different learned projection matrices. A model might use 32 attention heads, each learning to focus on different aspects of the relationships between tokens. The outputs from all heads get concatenated and projected back to the model dimension.

After attention, the output passes through a feed-forward network, which consists of two linear transformations with a non-linear activation function in between. This typically expands the dimensionality by 4x before projecting back down.

# Feed-forward network
hidden = activation(input @ W1 + b1)   # Expand to 4x dimension
output = hidden @ W2 + b2              # Project back down

Inference Phases - Prefill and Decode

The prefill phase happens when you first submit a prompt. The model processes all input tokens in parallel, computing the Query, Key, and Value matrices for each token simultaneously. This phase is compute-bound, meaning the GPU’s computational throughput determines performance.

During prefill, the attention mechanism performs matrix-matrix multiplication, which GPUs excel at. All tokens can see all other tokens (in the input), and the model computes attention scores for every pair of positions in one batch operation.

# Prefill phase computation
input_tokens = [token_1, token_2, ..., token_n]
# Process all tokens at once
for layer in model.layers:
    Q, K, V = compute_qkv(input_tokens)
    attention_output = attention(Q, K, V)
    layer_output = feedforward(attention_output)

The prefill phase produces the first output token and builds the KV cache, which we will discuss shortly. Time to First Token (TTFT) measures how long this phase takes, directly impacting user experience since this is the wait time before seeing any output.

The decode phase begins after the first token generates. The model produces tokens one at a time, autoregressively. Each new token gets computed based on all previous tokens, but only the latest token needs fresh Q, K, V computations.

This phase is memory-bound, not compute-bound. The GPU spends most of its time loading data from memory rather than performing calculations. Each iteration involves a matrix-vector operation instead of matrix-matrix, providing far less computational work to saturate the GPU.

# Decode phase computation
current_token = first_generated_token
while not done:
    # Only compute for the new token
    q_new = compute_query(current_token)

    # Retrieve cached K, V from previous tokens
    k_cached, v_cached = retrieve_cache()

    # Compute attention with cached values
    attention_output = attention(q_new, k_cached, v_cached)

    next_token = generate_token(attention_output)
    current_token = next_token

Inter-Token Latency (ITL) measures the time between consecutive token generations in the decode phase. This metric determines how fast text streams to the user after generation begins.

The KV Cache

The KV cache represents one of the most important optimizations in transformer inference. Without it, generating 100 tokens would require recomputing attention for all previous tokens 100 times, wasting enormous computational resources.

During autoregressive generation, the Key and Value matrices for previously processed tokens never change. Only the Query matrix for the new token needs computation. By caching the K and V matrices from all previous tokens, we avoid recomputing them.

A pseudocode to make sense of it goes like this.

class KVCache:
    def __init__(self):
        self.cache_k = None
        self.cache_v = None
    
    def update(self, new_k, new_v):
        if self.cache_k is None:
            self.cache_k = new_k
            self.cache_v = new_v
        else:
            # Concatenate new K, V with cached values
            self.cache_k = concat([self.cache_k, new_k], dim=1)
            self.cache_v = concat([self.cache_v, new_v], dim=1)
    
    def get(self):
        return self.cache_k, self.cache_v

For each transformer layer and each attention head, the model maintains separate KV caches. When generating the nth token, the cache stores K and V matrices for all n-1 previous tokens.

The speedup from KV caching can be dramatic. Empirical tests show that generating 1000 tokens with KV caching takes about ~10 seconds, while without caching the same task takes ~50 seconds, nearly a 5x difference.

However, the KV cache comes with a memory cost. The cache grows linearly with sequence length. For a 13-billion parameter model like LLaMA-2, each output token requires approximately 1 MB of cache storage. A 4,000 token context needs about 4 GB just for the cache, comparable to the model size itself.

This memory pressure becomes severe with long contexts or large batch sizes. Modern systems employ several strategies to manage KV cache memory: quantizing the cache to lower precision (4-bit or 2-bit keys and values), using sliding window attention that only retains recent tokens, or implementing attention approximations that reduce cache requirements.

When I first experimented with running a model myself, I blamed the GPU for slow responses before noticing that the KV cache kept spilling out of GPU memory.

Every time a user typed a long prompt, latency skyrocketed. A single fix - reducing precision from FP16 to INT8 - cut our response times by more than half.

Matrix Multiplication

Matrix multiplication forms the computational heart of transformer inference. Every layer performs multiple matrix multiplications: computing Q, K, V from inputs, applying attention, and running the feed-forward network.

On GPUs, efficient matrix multiplication employs a tiling strategy. The large matrix operation gets divided into smaller tiles that fit in shared memory, reducing expensive global memory accesses.

Each thread block computes one output tile, stepping through the K dimension in tiles. This maximizes data reuse: once data loads into shared memory, all threads in the block can access it without additional global memory traffic.

Tensor Cores further accelerate this by performing entire small matrix multiplications in hardware. The programming model exposes 16x16x16 operations, but hardware executes them as multiple 4x4x4 operations automatically.

# Using Tensor Core operations
for each 16x16 output tile:
    for each 16x16 input tile along K dimension:
        # This becomes a single Tensor Core instruction
        output_tile += tensor_core_mma_16x16x16(A_tile, B_tile)

Precision and Quantization in Inference

LLM inference often operates at reduced precision compared to training. While training typically uses FP32 or BF16 precision, inference can use FP16, INT8, or even INT4 with minimal quality loss.

FP16 (16-bit floating point) cuts memory usage and bandwidth requirements in half compared to FP32. Tensor Cores achieve maximum throughput at FP16, making it the default precision for many inference deployments.

# Precision formats
FP32: 1 sign bit, 8 exponent bits, 23 mantissa bits
FP16: 1 sign bit, 5 exponent bits, 10 mantissa bits  
BF16: 1 sign bit, 8 exponent bits, 7 mantissa bits
INT8: 8 bits for integer representation
INT4: 4 bits for integer representation

Quantization converts model weights and activations to lower precision formats. This requires careful calibration to maintain model quality. Post-training quantization analyzes activation distributions on representative data to determine optimal scaling factors.

A 7-billion parameter model at FP16 precision requires approximately 14 GB of memory (7B parameters × 2 bytes per parameter). Quantizing to INT4 reduces this to 3.5 GB, enabling inference on consumer hardware.

Quantization techniques like GPTQ and AWQ apply different scaling factors per channel or per group, preserving more information from the original weights. Some methods quantize weights but keep activations at higher precision, balancing quality and performance.

End-to-end Inference Flow

Step 1: Tokenization. Your prompt “Explain how transformers work” gets converted to token IDs by the tokenizer. The BPE algorithm splits this into subword units, producing something like [Explain, how, transform, ers, work].

prompt = "Explain how transformers work"
token_ids = tokenizer.encode(prompt)
# [22163, 703, 4659, 364, 990]

Step 2: Embedding lookup. Each token ID indexes into the embedding matrix, retrieving its corresponding embedding vector. If the model has 4096 dimensions, each token becomes a vector of 4096 floating-point numbers.

embeddings = embedding_matrix[token_ids]
# Shape: [5, 4096]

Step 3: Add positional encodings. The model adds positional information to the embeddings so the attention mechanism knows the order of tokens.

positions = [0, 1, 2, 3, 4]
positional_embeddings = positional_encoding[positions]
input_embeddings = embeddings + positional_embeddings

Step 4: Prefill phase. The input embeddings flow through each transformer layer. For a 32-layer model, this happens 32 times.

hidden_states = input_embeddings
for layer in model.layers:
    # Multi-head self-attention
    Q = hidden_states @ W_query
    K = hidden_states @ W_key
    V = hidden_states @ W_value
    
    attention_scores = (Q @ K.T) / sqrt(dim)
    attention_probs = softmax(attention_scores)
    attention_output = attention_probs @ V
    
    # Store K, V in cache for this layer
    kv_cache[layer].update(K, V)
    
    # Residual connection and layer norm
    hidden_states = layer_norm(hidden_states + attention_output)
    
    # Feed-forward network
    ffn_output = feed_forward(hidden_states)
    
    # Residual connection and layer norm
    hidden_states = layer_norm(hidden_states + ffn_output)

Step 5: Generate first token. After the final layer, the hidden states get projected to vocabulary size through a linear layer, then softmax converts these logits to probabilities over all possible next tokens.

Step 6: Decode phase. Now we generate tokens one at a time. For each new token, we only compute fresh Q, K, V for that token, retrieving cached values for all previous tokens.

Step 7: Detokenization. Finally, the sequence of token IDs gets converted back to text using the tokenizer’s vocabulary.

output_text = tokenizer.decode(generated_tokens)

This entire process repeats for every token generated, with the KV cache growing at each step. The decode phase continues until the model generates a stop token or reaches a maximum length limit.

Inference Serving Frameworks

Production LLM inference relies on specialized serving frameworks that handle batching, memory management, and optimization automatically.

vLLM implements PagedAttention for efficient KV cache management and continuous batching for high throughput. It achieves 2-4x higher throughput than naive implementations on the same hardware.

TensorRT-LLM from NVIDIA provides highly optimized kernels specific to NVIDIA GPUs, achieving near-theoretical peak performance. It includes techniques like in-flight batching and FP8 quantization support.

# Simplified vLLM usage
from vllm import LLM

llm = LLM(
    model="meta-llama/Llama-2-7b-hf",
    tensor_parallel_size=2,  # Use 2 GPUs
    dtype="float16"
)

outputs = llm.generate(
    prompts=["Explain transformers", "What is AI?"],
    sampling_params=SamplingParams(max_tokens=100)
)

Text Generation Inference (TGI) from Hugging Face offers broad model support and features like continuous batching and token streaming. It provides a production-ready HTTP API for deploying models.

Each framework makes different tradeoffs between ease of use, performance, and model support. Choosing the right one depends on your specific requirements, hardware, and model architecture.

Performance metrics and monitoring

Understanding and monitoring inference performance requires tracking several key metrics.

Time to First Token (TTFT) measures prefill phase latency. This directly impacts user experience since users wait this long before seeing any output. Optimizing TTFT means efficient prompt processing, often through batch prefill or speculative decoding techniques.

Inter-Token Latency (ITL) measures the time between consecutive tokens during decode. Low ITL creates smooth streaming experiences. This metric depends heavily on memory bandwidth and KV cache efficiency.

Throughput, measured in tokens per second, indicates overall system capacity. High throughput means serving more users concurrently. Batching strategies significantly impact throughput.

# Performance monitoring
start_time = time.now()
first_token = model.generate_first_token(prompt)
ttft = time.now() - start_time

token_times = []
for i in range(num_tokens):
    token_start = time.now()
    token = model.generate_next_token()
    token_times.append(time.now() - token_start)

itl = mean(token_times)
throughput = num_tokens / sum(token_times)

GPU utilization indicates how effectively the hardware is being used. Low utilization during decode suggests memory bottlenecks. Monitoring tools like nvidia-smi show GPU usage, memory consumption, and power draw in real-time.

Memory pressure, especially KV cache size, affects maximum context length and batch size. Tracking cache memory helps prevent out-of-memory errors and guides quantization decisions.

LLM inference transforms text prompts into responses through a process involving tokenization, transformer layers with self-attention mechanisms, and autoregressive token generation.

There are two stages in practice: the model first handles your full prompt in parallel, then switches to generating tokens one by one, which shifts the bottleneck from math to memory access.

Key optimizations include KV caching to avoid redundant computation, batching to improve GPU utilization, and quantization to reduce memory pressure.

Thanks for reading. I hope the breakdown made the inner workings of inference a little clearer.