← Back to AI Infrastructure Mind Map
🖥️ AI InfrastructureID: activation-memory

Activation Memory Estimation

激活显存估算
🎯Core Definition
Activations are the intermediate outputs of each forward-pass layer; backprop reads them to compute gradients, so training must store or recompute them. Unlike the static param budget 16Ψ16\Psi, which depends only on parameter count, activation memory scales with batch size bb × sequence length ss × hidden dim hlh_l. Storing each layer's input activation (FP16, 2 bytes/element) needs 2⋅s⋅b⋅hl2 \cdot s \cdot b \cdot h_l bytes, so total activation memory ≈ ∑l=1L2⋅s⋅b⋅hl≈2Lsbh\sum_{l=1}^{L} 2 \cdot s \cdot b \cdot h_l \approx 2 L s b h bytes; rule of thumb: ~16 bytes per token per layer (a coarse ~8 FP16 elements per token per layer, hidden-size dependent), total ≈ 16Lsb16 L s b bytes, with the exact value from ∑l2sbhl\sum_l 2 s b h_l. Worked example: Llama-2 70B (L=80L=80, h=8192h=8192), s=4096s=4096, b=2b=2 → per layer 2×4096×2×8192≈1342 \times 4096 \times 2 \times 8192 \approx 134 MB, 80 layers ≈ 10.7 GB — about 1% of the static param budget (1120 GB), but it grows linearly with s×bs \times b and takes over under long context and large batches. Gradient checkpointing (activation recomputation): store a checkpoint every kk layers instead of every layer, and rerun the forward pass during backward to regenerate intermediates; activation memory drops from O(L)O(L) to O(L)O(\sqrt{L}) (with k≈Lk \approx \sqrt{L}) at ~1/3 extra FLOPs — trading memory for compute; selective recomputation reruns only the biggest tensors (the s×ss \times s attention score matrix, MLP intermediates) to minimize the overhead.
💡Use Cases
a complete memory budget for 70B training must add the activation term; interview favorites are deriving the activation formula, the cost of gradient checkpointing, and activation memory vs KV cache.
⚡Key Problems Solved
counting only 16Ψ16\Psi badly underestimates long-sequence/large-batch training (activations can exceed param memory); checkpointing cuts activation memory from O(L)O(L) to O(L)O(\sqrt{L}) for ~33% extra compute, complementing ZeRO's param sharding — the total budget is 16Ψ/Nd16\Psi/N_d (params) + activations (after recomputation), and together they decide whether a model fits.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Activation Memory Estimation.
Use Cases
In production engineering, what are the primary use cases for Activation Memory Estimation and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Derive the activation memory formula ∑l2⋅s⋅b⋅hl\sum_l 2 \cdot s \cdot b \cdot h_l: explain each factor (2 bytes/element FP16 × layer input hlh_l elements × ss positions × bb samples), why it is independent of parameter count, and why it grows linearly with batch×seq.
2
Worked example: Llama-2 70B (L=80L=80, h=8192h=8192), s=4096s=4096, b=2b=2 → ≈134 MB/layer, ≈10.7 GB total; put it beside the static 16Ψ=112016\Psi = 1120 GB and state what each budget scales with (params vs batch×seq) — the complementary relationship.
3
Gradient checkpointing: keep a checkpoint every kk layers and recompute the forward pass in backward — activation memory drops from O(L)O(L) to O(L)O(\sqrt{L}) at ~1/3 extra FLOPs; explain the memory-for-compute trade.
4
Which tensors dominate activation memory (the s×ss \times s attention score matrix, 4h4h MLP intermediates, dropout masks), and how selective recomputation reruns only the big ones to minimize overhead.
5
Distinguish activation memory (training) from KV cache (inference): activations are forward intermediates read back by backprop, growing with L×s×bL \times s \times b; the KV cache stores generated K/V read sequentially once, formula 2⋅2⋅L⋅H⋅d⋅s⋅b2 \cdot 2 \cdot L \cdot H \cdot d \cdot s \cdot b; why the two budgets must not be conflated.
📖 In-depth Guide:📄 gpu-hardware-and-hbm →
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Activation Memory Estimation"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardZeRO-1/2/3 & OffloadNext Card →Cluster Scheduling Ray/K8s

🔗 More AI Infrastructure Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/AI_Infra/card/activation-memory
中文速览与面试要点

激活显存估算 (Activation Memory Estimation)

定义: 激活(activations)是前向传播中每层的中间输出,反向传播要回读它们计算梯度,所以训练时要么存、要么重算。与参数静态显存 16 “只随参数量、与 batch/seq 无关”相反,激活显存正比于 批大小 b × 序列长 s × 隐藏维度 h_l。每层保存输入激活(FP16 每元素 2 字节)需 2 s b h_l 字节,累计得激活显存 ≈ _l=1^L 2 s b h_l 2 L s b h 字节;经验法则: 每层每 token 约 16 字节(≈ 每 token 存 8 个 FP16 元素的粗估口径,视隐藏维度而定),总激活 ≈ 16 L s b 字节,精确值用 _l 2 s b h_l 计算。数值例: Llama-2 70B(L=80, h=8192), s=4096, b=2 → 每层 2 4096 2 8192 134 MB,共 80 层 ≈ 10.7 GB——只有静态参数显存(1120 GB)的约 1%,但随 s b 线性放大,长上下文 + 大批次时反客为主。重计算(Gradient Checkpointing): 不存每层激活,只每 k 层存一个检查点,反向时重跑前向把中间激活算回来;激活显存从 O(L) 降到 O( L)(取 k L),FLOPs 增加约 1/3——省显存换算力;选择性重计算只重算最占显存的张量(attention 的 s s 分数矩阵、MLP 中间激活),把额外计算压到最低。 使用场景: “70B 训练到底要多大显存”的完整账本必须叠加激活项;面试追问“推导激活显存公式”“梯度检查点原理与代价”“激活显存与 KV Cache 的区别”。 解决的问题: 只算 16 会严重低估长序列/大批次训练的真实显存(激活可反超参数显存);重计算用约 33% 的额外计算把激活显存从 O(L) 压到 O( L),与 ZeRO 参数切分互补后,显存预算 = 16 /N_d(参数)+ 激活(重计算后),两者一起决定模型能否上卡。

核心要点

  • 推导激活显存公式 _l 2 s b h_l: 解释每项来源(FP16 每元素 2 字节 × 每层输入激活 h_l 个元素 × s 个位置 × b 个样本),并说明它为什么与参数量无关、随 batch×seq 线性增长。
  • 数值例: Llama-2 70B(L=80, h=8192), s=4096, b=2 → 每层 ≈ 134 MB、共 ≈ 10.7 GB;与静态 16 = 1120 GB 并排讲清两本账各自随什么缩放(参数 vs batch×seq),即“互补”关系。
  • 梯度检查点(重计算)的原理与开销: 每 k 层存一个检查点、反向重跑前向,激活显存从 O(L) 降到 O( L),FLOPs 增加约 1/3;说明为什么是“省显存换算力”。
  • 哪些张量最占激活显存(attention 的 s s 分数矩阵、MLP 中间层 4h 激活、dropout mask 等),选择性重计算如何只挑大头重算、把额外开销降到最低。
  • 激活显存(训练)与 KV Cache(推理)的区别: 前者是前向中间输出、反向必须回读、随 L s b 增长;后者是自回归生成的 K/V、顺序只读一遍、公式 2 2 L H d s b;为什么两本账不能混算。