【AI 核心深度 M6-001】解释 ViT 的 patch embedding 与 token 数计算。(ViT Patch Embedding Formulation and Token Count Calculation)深度数理推导与工程落地解析

所属模块:M6 · 多模态与生成模型 (Multimodal & Generative Models) | 专题分类:视觉编码器 (Vision Encoders (ViT / ConvNeXt)) | 难度等级:Easy

一、核心一句话结论 (One-Sentence Summary)

把图像切成 P×P 的 patch,线性投影为 token;token 数 = (H/P)×(W/P),故随分辨率平方增长。

ADVERTISEMENT · 赞助推荐

Vision Transformers partition 2D images into non-overlapping patches, map flattened patches to latent vectors via linear projection or 2D convolution, prepend a class token, and inject positional embeddings.

二、核心考点要义 (Key Insights)

  • 📌 patch 大小 P 决定 token 数(P 越小 token 越多、越细)
  • 📌 token 数 = HW/P²,随分辨率平方增长
  • 📌 patch embedding 可用线性投影或卷积实现

English Insights:
– Patch decomposition: maps an image $H times W times C$ into $N = frac{HW}{P^2}$ patches of spatial size $P times P$
– Linear projection: flattens patches into vectors of dimension $P^2 C$ and projects them to hidden dimension $D$ via projection matrix $E$
– Token sequence assembly: prepends a learnable [CLS] token and adds 1D learnable or 2D sinusoidal positional embeddings, yielding $N + 1$ tokens

三、核心数学原理与机理推导 (Mathematical Principles & Derivation)

$$N=frac{H}{P}timesfrac{W}{P};qquad x_i=W_pcdotmathrm{flatten}(text{patch}_i)+b$$

数学机理:patch embedding——把 H×W×3 的图像切成 P×P 的非重叠 patch(共 N=(H/P)×(W/P) 个),每个 patch 展平为 P²·3 维向量,经线性投影 W_p 映射到 d 维(模型的隐维度),得到 N 个 token。token 数——N = (H/P)×(W/P) = HW/P²,随分辨率平方增长:224×224、P=16 → N=196;448×448 → N=784(4 倍);1024×1024 → N=4096(约 21 倍)。这直接决定注意力的成本(O(N²))与后续 LLM 的上下文占用。patch 大小 P 的权衡——(a) P 小(如 8)——token 更多(更细的粒度、更强的细节能力),但成本 ∝1/P² 增长;(b) P 大(如 32)——token 少(成本低),但细节丢失;(c) 常用 P=14 或 16(与预训练配置匹配)。实现——patch embedding 通常用 stride=P 的卷积实现(等价于切分 + 线性投影,但更高效);此外常加一个可学习的 [CLS] token(用于分类)与位置编码(因为注意力对位置无先验)。注意——ViT 的 patch 划分是固定网格的,故对’非方形图像’或’任意分辨率’需特殊处理(见动态分辨率题);且 patch 边界可能切断物体(对细粒度任务不利)。与 CNN 的对比——CNN 用滑动窗口(重叠、局部),ViT 用非重叠 patch(全局注意力);前者保留更多空间连续性,后者 token 化更彻底(便于与文本 token 统一处理)。

📖 查看英文严格数学推导 (English Mathematical Derivation)

Mathematical Mechanism: 1. Patch Flattening & Projection: Given an input image $x in mathbb{R}^{H times W times C}$ and patch size $P times P$, the spatial grid is divided into: $$N = frac{H cdot W}{P^2}$$ non-overlapping patches $x_p in mathbb{R}^{N times (P^2 C)}$. A learnable linear projection $E in mathbb{R}^{(P^2 C) times D}$ maps each flattened patch to the model hidden dimension $D$: $$z_0 = [x_{text{class}} ; x_p^1 E ; x_p^2 E ; dots ; x_p^N E] + E_{text{pos}}$$ where $x_{text{class}} in mathbb{R}^{1 times D}$ is the learnable classification token (`[CLS]`), and $E_{text{pos}} in mathbb{R}^{(N+1) times D}$ is the positional embedding matrix. 2. Convolutional Implementation Equivalence: In deep learning frameworks (PyTorch), patch projection is executed as a single 2D convolution: $$text{Conv2d}(text{in_channels}=C, ; text{out_channels}=D, ; text{kernel_size}=P, ; text{stride}=P)$$ producing an output tensor $mathbb{R}^{B times D times frac{H}{P} times frac{W}{P}}$, which is flattened along spatial dimensions to $mathbb{R}^{B times N times D}$. 3. Standard Configurations: For standard $224 times 224$ images: begin{array}{l|c|c|c|c} textbf{Model} & textbf{Patch Size } P & textbf{Spatial Patches } N & textbf{Tokens } (N+1) & textbf{Dimension } D \ hline text{ViT-Base/16} & 16 times 16 & 14 times 14 = 196 & 197 & 768 \ text{ViT-Base/14} & 14 times 14 & 16 times 16 = 256 & 257 & 768 \ text{ViT-Large/14} & 14 times 14 & 16 times 16 = 256 & 257 & 1024 end{array} At higher resolution ($448 times 448$ with $P=14$), token count scales to $(448/14)^2 + 1 = 1025$.

四、工业级落地权衡与工程考量 (Industrial Trade-offs)

深度剖析与工程权衡:① ‘token 数 ∝ HW/P²’是所有成本分析的起点——它决定了注意力的 O(N²) 成本、LLM 的上下文占用、以及 KV cache 的规模;故’图像分辨率的成本是平方级的’。② ‘P=14 vs 16’的实践——CLIP-ViT-L/14 用 P=14(224/14=16,N=256);SigLIP 常用 P=16。P 与预训练一致很重要(否则需重新适配)。③ ‘[CLS] token’的用途——在分类任务中作为全局表示;在 VLM 中通常不用(因为要保留所有 patch token 供 LLM 使用),但保留位置以兼容预训练。④ ‘patch 切断物体’的问题——非重叠网格可能把物体切成两半(对细粒度识别、OCR 不利);故有’重叠 patch’、’可变 patch’、以及动态分辨率(按原生分辨率切分)的改进。⑤ ‘与 LLM 的 token 对齐’——ViT 的 patch token 与文本 token 都是 d 维向量,故可拼接后送入 LLM(这是 VLM 的基础);但视觉 token 数通常远多于文本(一张 448² 图 = 784 token),故需token 压缩(见连接器架构与视觉 token 压缩题)。⑥ 面试要点——被问’ViT 的 token 数怎么算’,应给出’N=HW/P²‘并指出’成本随分辨率平方增长‘;能说明’patch 非重叠会切断物体’与’视觉 token 远多于文本’是深度理解的标志。

⚙️ 查看英文落地权衡分析 (English Systems & Trade-offs)

Deep Dive & Engineering Trade-offs: ① Patch Size vs Attention Scaling: Reducing patch size from $P=16$ to $P=14$ increases spatial token count by $(16/14)^2 approx 1.31times$. Because standard self-attention complexity scales quadratically $mathcal{O}(N^2 D)$, this 31% token increase inflates attention FLOPs and KV memory by $approx 1.71times$, but captures finer spatial details vital for small-object detection and text OCR. ② Convolutional Kernel Optimization: Using `nn.Conv2d` with stride equal to kernel size leverages highly optimized cuDNN Winograd/implicit GEMM algorithms, avoiding explicit tensor slicing, memory copies, and reshape overhead. ③ Positional Embedding Interpolation: When changing input resolution during fine-tuning (e.g., $224 to 448$), pre-trained positional embeddings $E_{text{pos}}$ cannot be indexed directly; they must undergo 2D bicubic grid interpolation to preserve geometric inductive biases. ④ Global Average Pooling (GAP) vs `[CLS]`: Modern vision architectures (CLIP-ViT, DINOv2) frequently complement or replace `[CLS]` with Multi-Head Attention Pooling (MAP) or GAP over all $N$ patch tokens, which eliminates spatial bias towards the arbitrary class token position. ⑤ Interview Strategy: Formulate $N = frac{HW}{P^2}$, derive the Conv2d stride/kernel equivalence, compute token counts for standard resolutions ($224$ and $448$), and articulate the quadratic attention trade-off governed by patch size.

五、常见面试避坑陷阱 (Common Pitfalls & Traps)

  • ⚠️ 忽略 token 数随分辨率平方增长
  • ⚠️ patch 大小与预训练配置不一致

English Pitfalls:
– Overlooking the quadratic scaling of attention memory and compute when reducing patch size from $P=16$ to $P=14$
– Applying naive 1D linear interpolation to positional embeddings instead of 2D bicubic spatial grid interpolation during resolution upscaling
– Forgetting to account for the [CLS] token offset when slicing or reshaping spatial feature maps for downstream vision heads

六、高频深度面试追问与预测 (Follow-Up Questions)

  1. P 从 16 降到 8 有什么影响?
  2. How does 2D bicubic interpolation mathematically remap positional embeddings when adapting a pre-trained 224×224 ViT to 448×448 inputs?
  3. ViT 为什么需要位置编码?
  4. Why does a 2D convolution with kernel size P and stride P compute an identical linear transformation to patch flattening plus matrix multiplication?

七、知识图谱对齐 (Knowledge Graph Anchor)

  • 🔗 关联底层卡片:Vision Transformer (ViT) 架构与图像 Patch 线性投影机制 (Vision Transformers (ViT) & Patch Projection Mechanics)
  • 🗺️ 知识图谱模块:深度学习架构导图

🔬 算法科学家与机器学习深度考察全量题库 (Science Depth)

本题收录于 TalentMe 算法科学家深度考察真题库 (Science Depth)。全库共 856 道硬核考点,深度覆盖数学统计、经典ML、深度学习、Transformer、大语言模型、多模态、推荐系统与 MLOps。支持 Jev 面经智能匹配、一键离线单文件 HTML 手册导出并直连 Obsidian 本地记忆。

👉 前往 TalentMe 交互式研读本题 (M6-001) →


Discover more from AirSOTA – Air School Of Thoughts AtoZ

Subscribe to get the latest posts sent to your email.