Research研究

PartialFC: Training 10 Million Identities on a Single MachinePartialFC:在单机上训练 1000 万身份

Xiang An, Xuhan Zhu, Yang Xiao, Lan Wu, Ming Zhang, Yuan Gao, Bin Qin, Debing Zhang, Ying Fu, Jiankang Deng

Shard the classifier across GPUs, keep every positive class, and sample only 10–30% of the negatives: a 10-million-identity softmax trains on one machine with accuracy on par with full softmax.

把分类层切分到多张 GPU,保留每个正类,只采样 10–30% 的负类:单机即可训练千万身份的 Softmax,精度与全量 Softmax 相当。

Decoupling classifier cost from the number of identities让分类层开销与身份数解耦

The softmax classifier of a face-recognition model stores one center per identity, so its weights and logits grow linearly with the number of identities and dominate GPU memory at the million scale. Partial FC first shards the class centers across GPUs and computes the exact softmax normalizer with scalar collectives, so no communication depends on the class count. It then keeps every positive class and samples only a fraction rr of the negative classes on each GPU, which shrinks per-GPU logits, classifier FLOPs and classifier updates to a fraction rr. With r=0.2–0.3r=0.2\text{–}0.3, Partial FC matches or exceeds full-softmax accuracy on WebFace4M/12M/42M, is markedly more robust to label noise and long-tailed identities, and trains 10M identities on a single machine.

人脸识别模型的 Softmax 分类层为每个身份保存一个类别中心,其权重与 logits 都随身份数线性增长,在百万级规模下成为显存的主要开销。Partial FC 首先把类别中心切分到多张 GPU,并用标量集合通信精确计算 Softmax 归一化项,使通信量与类别数无关;随后在每张 GPU 上保留全部正类、只采样比例为 rr 的负类,使每卡 logits、分类层 FLOPs 与参数更新都缩小为原来的 rr。当 r=0.2–0.3r=0.2\text{–}0.3 时,Partial FC 在 WebFace4M/12M/42M 上达到或超过全量 Softmax 的精度,对标签噪声和长尾身份明显更鲁棒,并可在单机上训练 1000 万身份。

Why full softmax stops scaling为什么全量 Softmax 无法继续扩展

Margin-based softmax losses such as ArcFace train a classifier W∈Rd×CW\in\mathbb R^{d\times C} with one learned center per identity, and every sample is compared with every center. Both the classifier and the B×CB\times C logit matrix therefore grow linearly with the number of classes CC. On noisy, long-tailed web data, this full inter-class interaction is not only expensive but can also be harmful:

ArcFace 等带间隔 Softmax 损失训练一个分类层 W∈Rd×CW\in\mathbb R^{d\times C},每个身份对应一个可学习的中心,每个样本都要与所有中心比较。因此分类层与 B×CB\times C 的 logits 矩阵都随类别数 CC 线性增长。在带噪声、长尾的网络数据上,这种全量类间交互不仅昂贵,还可能有害:

1. Inter-class conflict1. 类间冲突

The same person may be split across labels. Full softmax then pushes visually identical identities apart.

同一个人可能被拆成多个标签,全量 Softmax 会错误地把这些近似身份推远。

2. Passive tail updates2. 尾类被动更新

Rare classes seldom appear as positives but are repeatedly moved as negatives, so their centers can drift.

稀有类别很少作为正类出现,却不断作为负类被更新,类别中心容易漂移。

3. Logit explosion3. Logits 膨胀

Sharding W helps store centers, but the global batch still produces a massive B × C logit tensor.

即使分片存储 W,全局 batch 仍会产生巨大的 B × C logits 张量。

This page follows the expanded manuscript, which makes three contributions:

本页内容基于扩展稿,其贡献有三点:

  1. Exact class sharding. A model-parallel softmax whose communication, Θ(Nkd)+Θ(Nk)\Theta(Nkd)+\Theta(Nk), is independent of CC.精确的类别切分。一种模型并行 Softmax,其通信量 Θ(Nkd)+Θ(Nk)\Theta(Nkd)+\Theta(Nk) 与 CC 无关。
  2. Positive-preserving sampling. Each GPU keeps all of its positive classes and samples a fraction rr of its negatives, shrinking per-GPU logits and classifier FLOPs to a fraction rr.保留正类的采样。每张 GPU 保留全部正类、只采样比例为 rr 的负类,把每卡 logits 与分类层 FLOPs 缩小为原来的 rr。
  3. Evidence. Accuracy on par with full softmax, large gains under label noise, and training with 10M identities on one machine.实验验证。精度与全量 Softmax 相当,在标签噪声下大幅提升,并可在单机上训练 1000 万身份。

Exact model-parallel softmax精确的模型并行 Softmax

Consider kk GPUs, each holding a local batch of NN embeddings of dimension dd. After an AllGather, every GPU holds the global batch X∈RNk×dX\in\mathbb R^{Nk\times d}. GPU ii owns the classes Ci\mathcal C_i, with ∣Ci∣=Ci≈C/k|\mathcal C_i|=C_i\approx C/k, through a shard Wi∈Rd×CiW_i\in\mathbb R^{d\times C_i}, and computes only its block of logits Zi=XWiZ_i=XW_i. Let zn,cz_{n,c} denote the logit of sample nn for class cc; it is available only on the GPU that owns cc. The softmax normalizer needs just two statistics per sample, and both are reductions over the class partition:

设共有 kk 张 GPU,每张持有 NN 个 dd 维特征。AllGather 之后,每张 GPU 都拥有全局 batch X∈RNk×dX\in\mathbb R^{Nk\times d}。GPU ii 通过分片 Wi∈Rd×CiW_i\in\mathbb R^{d\times C_i} 持有类别集合 Ci\mathcal C_i(∣Ci∣=Ci≈C/k|\mathcal C_i|=C_i\approx C/k),并只计算自己的 logits 块 Zi=XWiZ_i=XW_i。记 zn,cz_{n,c} 为样本 nn 在类别 cc 上的 logit,它只存在于拥有类别 cc 的 GPU 上。Softmax 归一化项对每个样本只需要两个统计量,二者都是在类别划分上的规约:

mn=AllReduce⁡max⁡(max⁡c∈Cizn,c)\displaystyle m_n=\operatorname{AllReduce}_{\max}\Bigl(\max_{c\in\mathcal C_i} z_{n,c}\Bigr)(1)
sn=AllReduce⁡sum(∑c∈Cie zn,c−mn)\displaystyle s_n=\operatorname{AllReduce}_{\mathrm{sum}}\Bigl(\sum_{c\in\mathcal C_i} e^{\,z_{n,c}-m_n}\Bigr)(2)
pn,yn=e zn,yn−mnsn,L=−1Nk∑n=1Nklog⁡pn,yn\displaystyle p_{n,y_n}=\frac{e^{\,z_{n,y_n}-m_n}}{s_n},\qquad \mathcal L=-\frac{1}{Nk}\sum_{n=1}^{Nk}\log p_{n,y_n}(3)

No approximation is made: the loss and its gradients equal those of single-device softmax, the full Nk×CNk\times C logit matrix is never assembled, and each collective moves one scalar per sample (Figure 1).

这一过程没有任何近似:损失及其梯度与单卡 Softmax 完全相同,完整的 Nk×CNk\times C logits 矩阵从不拼出,每次集合通信每个样本只搬运一个标量(图 1)。

Why Eqs. (1)–(3) are exact为什么式 (1)–(3) 是精确的
  • Eq. (1). eze^{z} overflows fp16 near z≈11z\approx 11. One device subtracts the row maximum; with a split row, each GPU reduces its local maximum and all GPUs obtain the same mnm_n.式 (1)。fp16 下 z≈11z\approx 11 时 eze^{z} 就会溢出。单卡上减去行最大值即可;行被切开后,各 GPU 先求本地最大值再规约,得到相同的 mnm_n。
  • Eq. (2). The classes form a disjoint partition, so the global denominator is exactly the sum of the per-GPU partial sums; no GPU needs another GPU's logits.式 (2)。类别构成不相交的划分,全局分母恰好等于各 GPU 局部和之和;任何 GPU 都不需要别的 GPU 的 logits。
  • Eq. (3). Cross-entropy reads one entry per row. Only the GPU that owns yny_n holds zn,ynz_{n,y_n}, so an AllReduce over a vector that is zero elsewhere gathers the NkNk target probabilities.式 (3)。交叉熵每行只读取一个元素。只有拥有 yny_n 的 GPU 持有 zn,ynz_{n,y_n},因此对一个在其他位置为 0 的向量做 AllReduce,即可收集 NkNk 个目标概率。
  • Cost. Three collectives of NkNk scalars each, none of which depends on CC; the gradients are value-for-value those of single-device softmax.开销。三次集合通信,各搬运 NkNk 个标量,均与 CC 无关;梯度与单卡 Softmax 逐值相同。

(a) Full softmax全量 Softmax

one device holds all CC classes单卡保存全部 CC 个类别

XXNk×dNk\times d WWd×Cd\times C
Z=XWZ=XWNk×CNk\times C mn=max⁡cZncm_n=\max_c Z_{nc}sn=∑ceZnc−mns_n=\sum_c e^{Z_{nc}-m_n} P=eZ−m/sP=e^{Z-m}/sNk×CNk\times C

both WW and ZZ grow with CCWW 与 ZZ 都随 CC 增长

(b) Class-sharded forward类别切分:前向

exact global softmax, logits never gathered精确的全局 Softmax,logits 从不聚合

GPU 1 W1W_1⋯GPU kk WkW_k X1X_1N×dN\times dXkX_kN×dN\times d AllGather\mathrm{AllGather}XX: Nk×dNk\times d valuesXX:Nk×dNk\times d 个值 Z1=XW1Z_1=XW_1Nk×C/kNk\times C/k⋯Zk=XWkZ_k=XW_kNk×C/kNk\times C/k m(1)=max⁡Z1m^{(1)}=\max Z_1m(k)=max⁡Zkm^{(k)}=\max Z_k AllReducemax⁡→mn\mathrm{AllReduce}_{\max}\to m_nNkNk scalarsNkNk 个标量 s(1)=∑eZ1−ms^{(1)}=\sum e^{Z_1-m}s(k)=∑eZk−ms^{(k)}=\sum e^{Z_k-m} AllReducesum→sn\mathrm{AllReduce}_{\mathrm{sum}}\to s_nNkNk scalarsNkNk 个标量 P1=eZ1−m/sP_1=e^{Z_1-m}/sPk=eZk−m/sP_k=e^{Z_k-m}/s AllReducesum→pn,yn\mathrm{AllReduce}_{\mathrm{sum}}\to p_{n,y_n}NkNk scalars, Eq. (3)NkNk 个标量,式 (3)

no collective depends on CC所有集合通信都与 CC 无关

(c) Class-sharded backward类别切分:反向

local classifier update, summed feature gradients分类层本地更新,特征梯度求和

GPU 1 W1W_1⋯GPU kk WkW_k G1=1Nk(P1−Y1)G_1=\tfrac{1}{Nk}(P_1-Y_1)Gk=1Nk(Pk−Yk)G_k=\tfrac{1}{Nk}(P_k-Y_k) ∇W1=X⊤G1\nabla W_1=X^{\top}G_1applied in place原地更新∇X(1)=G1W1⊤\nabla X^{(1)}=G_1W_1^{\top}⋯∇Wk=X⊤Gk\nabla W_k=X^{\top}G_kapplied in place原地更新∇X(k)=GkWk⊤\nabla X^{(k)}=G_kW_k^{\top} ReduceScattersum\mathrm{ReduceScatter}_{\mathrm{sum}}Nk×dNk\times d gradientsNk×dNk\times d 个梯度 ∇X1\nabla X_1N×dN\times d∇Xk\nabla X_kN×dN\times d

∇Wi\nabla W_i never leaves its GPU∇Wi\nabla W_i 从不离开所属 GPU

features and their gradients特征与特征梯度 resident classifier shard常驻分类层分片 logits, probabilities, logit gradientslogits、概率与 logit 梯度 collective communication集合通信
Figure 1. Class-sharded softmax, redrawn from the manuscript. (a) One device stores all of WW and all Nk×CNk\times C logits, so both grow with CC. (b) GPU ii keeps its shard WiW_i resident and scores the gathered batch only against it; AllReduces of the local maxima and of the exponentiated sums give the exact global normalization (Eqs. 1–2), and a third gathers the target probabilities (Eq. 3). (c) Classifier gradients are applied where they are computed; ReduceScatter sums the feature-gradient contributions and returns each GPU its N×dN\times d rows. No collective moves more than Nk×dNk\times d values.
图 1. 类别切分的 Softmax(据论文原图重绘)。(a) 单卡保存全部 WW 与 Nk×CNk\times C logits,两者都随 CC 增长。(b) GPU ii 常驻分片 WiW_i,只用它为聚合后的 batch 计算 logits;对局部最大值和指数和各做一次 AllReduce 即得到精确的全局归一化(式 1–2),第三次 AllReduce 收集目标概率(式 3)。(c) 分类层梯度在计算处原地更新;ReduceScatter 汇总特征梯度,并把各 GPU 自己的 N×dN\times d 行发回。任何集合通信都不超过 Nk×dNk\times d 个值。

Limitation. Every GPU still scores the whole global batch (Nk rows) against its C/k centers, so its logit block is Nk × C/k = N × C, independent of k. More GPUs shrink the weights but not the logits: at N = 64 and d = 512, logits take 40% of per-GPU classifier memory at 8 GPUs and 87% at 80.

局限:每张 GPU 仍要用整个全局 batch(Nk 行)与本卡的 C/k 个中心计算,logits 块大小为 Nk × C/k = N × C,与 k 无关。加卡只能缩小权重,缩不小 logits:在 N = 64、d = 512 时,8 卡下 logits 占每卡分类层显存的 40%,80 卡时升至 87%。

Figure 2. Memory analysis of exact model parallelism图 2. 精确模型并行的显存分析
✓
MW=4dC/k bytesM_W = 4dC/k \ \text{bytes}
Shrinks as 1/k. Per-GPU classifier weights — the part sharding does fix. The class dimension C is split k ways, so each rank stores a k-th of it and every GPU you add makes this term smaller.随 1/k 缩小。每卡分类器权重 —— 这是切分确实解决掉的部分。类别维度 C 被切成 k 份,每个 rank 只存其中一份,每加一张卡这一项就小一分。
✗
Mlogit=4NC bytesM_{\mathrm{logit}} = 4NC \ \text{bytes}
Independent of k. Per-GPU logits — the part sharding does not touch. Every rank scores the whole global batch Nk against its own shard C/k, so the k cancels and adding GPUs does nothing at all.与 k 无关。每卡 logits —— 这是切分完全没碰到的部分。每个 rank 都要拿整个全局 batch Nk 去打自己那份 C/k 的分,k 被约掉,加卡毫无作用。
Σ
Mlogit/MW=Nk/dM_{\mathrm{logit}}\big/M_W = Nk\big/d
The verdict. The ratio therefore grows linearly in k. At N=64, d=512 it is already 1:1 at 8 GPUs and 10:1 at 80 — past that point you are buying classifier capacity you no longer need, in order to feed logits you cannot shrink. Figure 2 plots the totals as MFC ≈ 3MW + 2Mlogit: momentum SGD keeps three copies of W, the margin softmax two of the logits.结论。于是比值随 k 线性增长。在 N=64、d=512 下,8 卡时已是 1:1,80 卡时变成 10:1 —— 越过这一点后,你是在买已经不再需要的分类器容量,去供养一个根本压不下去的 logits。图 2 按 MFC ≈ 3MW + 2Mlogit 画总量:动量 SGD 保存三份 W,带间隔 Softmax 保存两份 logits。
(a) Communication per step classifier d × C 分类器 d × C 512M logits Nk × C logits Nk × C 512M embeddings X 特征 X 262K feature grads ∇X 特征梯度 ∇X 262K softmax stats Softmax 统计量 1K target probs 目标概率 512 103 105 107 109 elements per rank, one step 单步每 rank 搬运的元素数 communicated 已通信 never gathered 从不聚合 (b) Per-GPU FC memory 1.3 8 1M 1.8 16 2M 2.8 32 4M 4.9 64 8M 5.9 80 10M 0 2 4 6 per-GPU FC memory (GB) 每卡 FC 显存 (GB) GPUs k / classes C GPU 数 k / 类别数 C 3MW 2Mlogit (c) Logits dominate 0 20 40 60 80 100 40% at k = 8 87% at k = 80 0 25 50 75 GPUs k GPU 数 k logit share of MFC (%) logits 在 MFC 中的占比 (%) Mlogit / MW = Nk / d
Figure 2. Why exact class-sharded model parallelism still hits a memory wall (N=64, d=512, 125K classes per GPU, so C grows with k). (a) Elements moved per rank in one step, on a log axis: only embeddings, softmax statistics, target probabilities and feature gradients are communicated, while the Nk×C logits and the d×C classifier never leave their owner. (b) Per-GPU FC memory splits into a constant weight term 3MW and a logit term 2Mlogit=8NC that grows linearly with k, rising from 1.3 GB at 8 GPUs to 5.9 GB at 80 GPUs. (c) The logit share of MFC therefore climbs from 40% to 87%. Adding ranks buys classifier capacity, not logit capacity.
图 2. 为什么精确的类别切分模型并行仍会撞上显存墙(N=64、d=512、每卡 12.5 万类,因此 C 随 k 增长)。(a) 单步每 rank 搬运的元素数(对数坐标):只通信特征、Softmax 统计量、目标概率和特征梯度,Nk×C logits 与 d×C 分类器始终不离开各自的 owner。(b) 每卡 FC 显存分成恒定的权重项 3MW 和随 k 线性增长的 logits 项 2Mlogit=8NC,从 8 卡的 1.3 GB 升到 80 卡的 5.9 GB。(c) 于是 logits 在 MFC 中的占比从 40% 升至 87%:加卡只买到分类器容量,买不到 logits 容量。

Positive-preserving partial softmax保留正类的部分 Softmax

Partial FC keeps the exact class sharding above and changes only which classes enter each step. GPU ii builds an active set from its positive classes Pi={yn}∩CiP_i=\{y_n\}\cap\mathcal C_i and negatives NiN_i sampled uniformly without replacement from Ci∖Pi\mathcal C_i\setminus P_i:

Partial FC 沿用上文的精确类别切分,只改变每一步参与计算的类别。GPU ii 由其正类 Pi={yn}∩CiP_i=\{y_n\}\cap\mathcal C_i 与从 Ci∖Pi\mathcal C_i\setminus P_i 中无放回均匀采样的负类 NiN_i 构成激活集合:

Si=Pi∪Ni,∣Si∣=max⁡(∣Pi∣, ⌊rCi⌋)\displaystyle S_i=P_i\cup N_i,\qquad |S_i|=\max\bigl(|P_i|,\ \lfloor rC_i\rfloor\bigr)(4)
LPFC=−1Nk∑n=1Nklog⁡e zn,yn∑c∈Se zn,c,S=⋃i=1kSi\displaystyle \mathcal L_{\mathrm{PFC}}=-\frac{1}{Nk}\sum_{n=1}^{Nk}\log\frac{e^{\,z_{n,y_n}}}{\sum_{c\in S}e^{\,z_{n,c}}},\qquad S=\bigcup_{i=1}^{k}S_i(5)

Eq. (5) is evaluated with the same collectives as Eqs. (1)–(3), restricted to SS, and the ArcFace or CosFace margin is applied to the target logit zn,ynz_{n,y_n}. Because every target class belongs to SS, no sample loses its positive term; only the denominator is approximated. Per-GPU logits, classifier FLOPs and classifier updates shrink to a fraction rr, while communication is unchanged (Table 1).

式 (5) 使用与式 (1)–(3) 相同的集合通信,只是限定在 SS 上计算;ArcFace 或 CosFace 间隔作用于目标 logit zn,ynz_{n,y_n}。由于每个目标类别都属于 SS,没有样本会丢失正类项,被近似的只有分母。每卡 logits、分类层 FLOPs 与参数更新都缩小为原来的 rr,通信量不变(表 1)。

Table 1. Per-GPU cost of exact model parallelism and Partial FC (local batch NN, kk GPUs, sampling ratio rr).表 1. 精确模型并行与 Partial FC 的每卡开销(本地 batch NN,kk 张 GPU,采样率 rr)。

Per GPU每张 GPUExact model parallel精确模型并行Partial FC
Active class centers激活的类别中心C/krC/k
LogitsNCrNC
Classifier FLOPs分类层 FLOPs2NdC2NdrC
Communication通信量Θ(Nkd)+Θ(Nk)unchanged不变
Rows with loss gradients获得损失梯度的行all C/k全部 C/ksampled rC/k采样到的 rC/k
Features X (one tint per GPU)特征 X(每卡一种色阶)
Class centers W类别中心 W
Positives Pi正类 Pi
Sampled negatives Ni采样负类 Ni
Target logits目标 logits
Collective集合通信
Figure 3. One Partial FC training step (schematic: four GPUs on desktop, two on phones).图 3. Partial FC 的一个训练步(示意:桌面端四卡,手机端两卡)。 (1) Gather. Each GPU embeds its local batch; one AllGather gives every GPU the global batch XX (Nk×dNk\times d values).(1) 聚合特征。每张 GPU 计算本地 batch 的特征,一次 AllGather 后每张 GPU 都得到全局 batch XX(Nk×dNk\times d 个值)。
Figure 4. Extension: hierarchical class storage on CPU / NVMe图 4. 扩展:基于 CPU / NVMe 的分级类别存储

Each class proxy has one permanent owner, but only the active set SiS_i enters the GPU classifier buffer. In the expanded manuscript's optional hierarchy, the complete class bank and optimizer state can live in pinned CPU memory or an NVMe-backed cache, reducing GPU-resident classifier storage from Θ(Cid)\Theta(C_i d) to Θ(rCid)\Theta(rC_i d). The released PartialFC_V2 path instead keeps each rank's complete owned shard on its GPU.

每个类别代理只有一个长期 owner,但只有激活集合 SiS_i 会进入 GPU 分类器缓冲区。在扩展稿提出的可选分级方案中,完整类别库与优化器状态可以位于锁页 CPU 内存或 NVMe 缓存中,从而把 GPU 常驻分类器存储从 Θ(Cid)\Theta(C_i d) 降至 Θ(rCid)\Theta(rC_i d)。已发布的 PartialFC_V2 路径则仍把每个 rank 拥有的完整分片放在 GPU 上。

The only subtlety is overlap between consecutive active sets: cold rows may arrive early, but repeated rows must carry the result of optimizer tt.

真正需要处理的是相邻激活集合的重叠:冷行可以提前到达,但重复行必须携带 optimizer tt 的最新结果。

Overlap-safe asynchronous hierarchical class storage pipeline A double-buffer timeline. Rows needed only by iteration t plus one are prefetched from host into slot B during iteration t. Rows shared by iterations t and t plus one are copied from slot A to slot B only after optimizer t updates them. A ready event gates Partial FC t plus one until both paths complete, while updated rows write back to host asynchronously. Build the next active set without stale rows 构建下一激活集合,同时避免旧版本行 Cold rows move immediately; overlapping rows wait for optimizer(t). 冷行立即搬运;重叠行等待 optimizer(t) 完成。 Sᵢ(t+1) = COLD ∪ OVERLAP disjoint and exhaustive 互斥且完备 PREPARE SLOT B 准备槽位 B 1 Prefetch cold rows 预取冷行 Sᵢ(t+1) ∖ Sᵢ(t) host → B · H2D 2 Patch updated overlap 补入更新后的重叠行 Sᵢ(t) ∩ Sᵢ(t+1) A → B · after optimizer(t) READY GATE 就绪门 wait for both 等待两个事件 CUDA events Slot B is readable 槽位 B 可读取 PFC(t+1) · current Sᵢ(t+1) no stale overlap rows 不存在旧版本重叠行 PIPELINED TIMELINE 流水时间线 earlier更早 later更晚 optimizer(t) done slot A槽位 A read · backward · optimizer(t) 读取 · 反向 · optimizer(t) D2H write-backD2H 写回 A remains lockedA 保持锁定 slot B槽位 B prefetch cold rows预取冷行 wait等待 patch overlap补入重叠行 B ready → PFC(t+1)
Figure 4. Overlap-safe hierarchical class storage. B becomes readable only after both the cold-row prefetch and the post-optimizer overlap patch complete; A becomes reusable only after its D2D readers and D2H write-back complete. Row versions validate host state, and CUDA events enforce these dependencies.图 4. 避免旧版本行的分级类别存储。冷行预取与 optimizer 后的重叠行补丁都完成,B 才可读取;D2D 读取与 D2H 写回都完成,A 才可复用。行版本号用于校验主存状态,CUDA event 用于强制这些依赖。

Overlap condition. The cold-row H2D copy and the overlap-row handoff must both finish before the next PFC read, otherwise the ready gate stalls. Host or NVMe staging therefore pays off only when transfers overlap computation.

重叠条件。冷行 H2D 拷贝与重叠行交接都必须在下一次 PFC 读取前完成,否则就绪门会等待。因此只有传输能与计算重叠时,主存或 NVMe 分级存储才有收益。

Sampling 10–30% of classes keeps full-softmax accuracy只采样 10–30% 的类别,精度不输全量 Softmax

Trained on WebFace4M/12M/42M and tested on the hard MFR benchmark, Partial FC stays within 0.5 points of full softmax at r = 0.1 and matches or beats it at r = 0.2–0.3. It is also far more robust when labels are noisy.

在 WebFace4M/12M/42M 上训练、在更困难的 MFR 上评测:r = 0.1 时 Partial FC 与全量 Softmax 相差不到 0.5 个点,r = 0.2–0.3 时追平或超过全量 Softmax;标签有噪声时也明显更鲁棒。

r = 0.2–0.3matches or beats full FC on WebFace4M/12M/42M在 WebFace4M/12M/42M 上追平或超过全量 FC
+36.33MFR points under 40% label flips40% 错标下 MFR 提升点数
≈10×less memory traffic per GPU than full model parallel每卡访存量比全量模型并行少约 10 倍
100Msynthetic classes scaled on 64 GPUs64 张 GPU 扩展的合成类别数

Robustness: conflict, label flips and long tails鲁棒性:类别冲突、错标与长尾

Reducing negative interaction is most valuable when labels are imperfect. PFC* additionally filters abnormal negative centers whose cosine similarity exceeds 0.4.

当标签不完美时,减少负类交互的价值最明显。PFC* 还会过滤余弦相似度高于 0.4 的异常负类中心。

Table 2. MFR-All (TAR@FAR = 1e−6) under inter-class conflict, label flips and long-tailed identities.表 2. 类间冲突、标签翻转与长尾身份下的 MFR-All(TAR@FAR = 1e−6)。

Stress test压力测试FCPartialFCPFC*Best gain最佳提升
Inter-class conflict类间冲突79.9391.2091.68+11.75
40% label flips40% 标签翻转43.8778.5380.20+36.33
Long-tail identities长尾身份分布87.4491.96-+4.52

MFR-All from paper Tables 3–5. Long-tail PartialFC uses r=0.2; conflict PFC/PFC* entries use r=0.1 and r=0.2 respectively.

MFR-All 取自论文表 3–5。长尾实验使用 r=0.2;冲突实验的 PFC / PFC* 分别取 r=0.1 / 0.2。

Table 3. Sampling-ratio ablation (ResNet50)表 3. 采样率消融(ResNet50)

Complete ResNet50 sampling-rate ablation. MFR columns use TAR@FAR=1e-6; Mask uses FAR=1e-4. Extremely low rates fail, while 0.1-0.3 generally recovers full FC.

完整 ResNet50 采样率消融。MFR 各列使用 TAR@FAR=1e-6,Mask 使用 FAR=1e-4。极低采样率明显退化,0.1-0.3 通常可恢复全量 FC。

Configuration配置AllAfricanCaucasianSouth AsianEast AsianMask
WF4M + FC-1.086.2583.3591.1188.1465.7972.05
WF4M + PFC-0.0474.1171.4381.7976.2552.2454.21
WF4M + PFC-0.185.7683.8291.0087.9066.0471.13
WF4M + PFC-0.386.8584.8691.5788.5767.5272.28
WF12M + FC-1.091.7090.7294.9493.4475.1080.47
WF12M + PFC-0.01387.8587.0792.3290.7068.2872.98
WF12M + PFC-0.191.2490.8094.6793.1874.9779.73
WF12M + PFC-0.291.7891.0995.0093.5375.9079.92
WF12M + PFC-0.391.8291.1495.0093.6175.5580.08
WF42M + FC-1.093.8693.3396.2095.2479.4683.90
WF42M + PFC-0.00891.2790.3495.1693.0476.9381.24
WF42M + PFC-0.193.9593.4896.3795.5180.0383.79
WF42M + PFC-0.294.0493.6796.3895.4980.0784.32
WF42M + PFC-0.394.0393.6896.3895.5279.7684.46

Source: paper Table 1. The optimum is broad rather than a single fragile setting.

来源:论文表 1。最优区间较宽,并非依赖单一脆弱超参数。

Table 4. Eight training sets with and without PFC表 4. 八个训练集上有无 PFC 的对比

Partial FC remains close to full FC across datasets, but does not improve every metric. MFR-All is TAR@FAR=1e-6; IJB-C is TAR@FAR=1e-4; the other columns are verification accuracy.

Partial FC 在不同数据集上总体接近全量 FC,但并非每个指标都提升。MFR-All 为 TAR@FAR=1e-6,IJB-C 为 TAR@FAR=1e-4,其余为验证准确率。

Training set训练集MFR-AllAgeDBCFP-FPLFWIJB-C
CASIA36.7999.4595.2194.9087.22
CASIA + PFC37.1199.3795.4394.6084.97
VGGFace38.5899.5597.4195.0891.22
VGGFace + PFC40.6799.6898.5395.4092.49
GlintAsian62.6699.5893.1995.4091.50
GlintAsian + PFC63.1599.6593.0395.2391.14
MS1MV277.7099.8398.0898.0896.14
MS1MV2 + PFC77.7499.7898.0798.0296.08
MegaFaceMS1M78.3799.7597.5697.4095.35
MegaFaceMS1M + PFC78.7799.8097.8797.7395.40
MS1MV382.5299.8098.5398.2796.58
MS1MV3 + PFC81.6899.8098.4498.1796.43
Glint360K86.7999.8299.1498.4597.13
Glint360K + PFC87.0899.8299.1498.4597.02
WebFace12M90.5799.8099.2098.1097.12
WebFace12M + PFC89.9599.8299.1498.1297.01

Source: active benchmark table in the expanded manuscript.

来源:扩展稿当前启用的基准表。

Table 5. Datasets and training settings表 5. 数据集与训练配置

Training covers CASIA, MS1MV2, Glint360K (17M images, 360K identities), and WebFace4M/12M/42M (200K/600K/2M identities). Evaluation uses LFW, CFP-FP, AgeDB-30, CPLFW, CALFW, MegaFace, IJB-B, IJB-C and ICCV21-MFR. WebFace42M is strongly long-tailed: 44.57% of identities have fewer than ten images.

训练数据包括 CASIA、MS1MV2、Glint360K(1700 万图像、36 万身份),以及 WebFace4M/12M/42M(20 万/60 万/200 万身份)。评测覆盖 LFW、CFP-FP、AgeDB-30、CPLFW、CALFW、MegaFace、IJB-B、IJB-C 与 ICCV21-MFR。WebFace42M 长尾显著:44.57% 的身份少于 10 张图像。

Runs实验GPUBatch批量Optimizer / LR优化器 / LRSchedule训练周期
CASIA / MS1MV2 / Glint360K8× RTX2080Ti512 totalSGD / 0.132K / 180K / 600K iters
WebFace42M CNN32128 / GPUSGD / 0.420 epochs, 2 warm-up
WebFace42M ViT-384 / GPUAdamW / 10-340 epochs, 4 warm-up

Normalized margins: feature scale s=64; CosFace margin 0.4; ArcFace angular margin 0.5.

归一化间隔:特征缩放 s=64;CosFace margin 0.4;ArcFace 角度 margin 0.5。

Figure 5. Efficiency analysis: GEMM shapes and memory traffic图 5. 效率分析:GEMM 形状与访存量

Model-parallel Partial FC communicates Θ(Nkd)+Θ(Nk)\Theta(Nkd)+\Theta(Nk) values per step, independent of CC; sampled data parallelism must synchronize Θ(rCd)\Theta(rCd) replicated proxy gradients. Even at identical FLOPs, the two also differ in how much memory each GPU has to stream:

模型并行 Partial FC 每步通信 Θ(Nkd)+Θ(Nk)\Theta(Nkd)+\Theta(Nk) 个值,与 CC 无关;采样数据并行则必须同步 Θ(rCd)\Theta(rCd) 个复制代理的梯度。即使 FLOPs 完全相同,两者每张 GPU 需要搬运的数据量也不同:

Per-rank GEMM shapes每个 rank 的 GEMM 形状

Per-rank GEMM shapes drawn to scale Three GEMM diagrams on a shared cell grid. Every block is built from discrete square cells: block width counts the classes held on the rank and block height counts the batch rows. Full model parallel is twenty cells wide, sampled data parallel sixteen cells wide but only one cell tall, and MP-PFC two cells wide by eight cells tall. Full MP Wᵀ · 512 × 125K X · 1024 × 512 Z · 1024 × 125K Full batch, C/k = 125K centers per rank. 每 rank 仍存 C/k = 125K 个中心。 The logit block dominates memory. logit 块主导显存开销。 Sampled DP Wᵀ · 512 × 100K X · 128 × 512 Z · 128 × 100K dashed = rows a gathered batch would fill 虚线 = 全局 batch 本可填满的行 Local batch only; rC = 100K replicated. 只有本地 batch;rC = 100K 逐卡复制。 Short and wide: almost no reuse per center. 又矮又宽:中心几乎无法复用。 MP-PFC Wᵀ · 512 × 12.5K Z · 1024 × 12.5K dashed = Full MP width, 10× wider 虚线 = Full MP 的宽度,宽 10 倍 X · 1024 × 512 Shard × sample: rC/k = 12.5K per rank. 分片 × 采样:每 rank 只剩 rC/k = 12.5K。 Tall and narrow: full reuse per center. 又高又窄:中心被全 batch 复用。 X features X 特征 Wᵀ classifier on this rank Wᵀ 本 rank 的分类器 Z logits Z logits capacity not used 未被利用的容量 k=8 · N=128 per rank · gathered 1024 · C=1M · r=0.1 · d=512

First-order traffic per rank每个 rank 的一阶访存量

Full MP
192.5M
Sampled DP
64.1M
MP-PFC
19.7M
050M100M150M192.5M
feature readweight readlogit write

Why identical FLOPs do not run at identical speed为什么相同的 FLOPs 跑不出相同的速度

Sampled DP and MP-PFC issue the same arithmetic, F=2NdrCF=2NdrC. Counting elements is not yet an answer either, because a kernel runs at whichever of compute or bandwidth saturates first. The quantity that decides which one is the ratio between them: how much arithmetic the GEMM performs per byte it is forced to pull from memory. For the classifier read, that ratio is set entirely by the shape.

采样数据并行与 MP-PFC 发射的算术量完全相同,都是 F=2NdrCF=2NdrC。而只数元素同样不算回答问题,因为 kernel 的速度取决于算力和带宽哪一个先饱和。决定这一点的是两者之比:GEMM 每从显存拉取一个字节,能做多少次算术。对分类器的读取而言,这个比值完全由形状决定。

Every classifier weight that a rank loads participates in exactly one multiply-accumulate per batch row it is multiplied against. So the reuse each loaded center earns is the number of rows in the GEMM. Sampled DP multiplies against the local batch and gets NN; MP-PFC multiplies against the gathered batch and gets NkNk. Same weights, same flops, k×k\times the reuse.

一个 rank 载入的每个分类器权重,与多少个 batch 行相乘,就参与多少次乘加。因此每个被载入的中心所获得的复用次数就等于 GEMM 的行数。采样数据并行只与本地 batch 相乘,得到 NN;MP-PFC 与聚合后的全局 batch 相乘,得到 NkNk。同样的权重、同样的浮点运算,复用高出 kk 倍。

Per-rank GEMM每 rank 的 GEMM Centers held持有的中心数 Rows multiplied参与相乘的行数 Reuse per center每中心复用 Classifier traffic分类器访存
Full MPC/kC/kNkNkNkNkCd/kCd/k
Sampled DPrCrCNNNNrCdrCd
MP-PFCrC/krC/kNkNkNkNkrCd/krCd/k

This is why the short, wide block is the bad one. A GEMM whose row count is small has few rows to amortize each loaded weight over, so it spends most of its time streaming the classifier past the tensor cores rather than multiplying with it — a bandwidth-bound kernel, no matter that its flop count is small. The tall, narrow block performs the same multiplications while touching a fraction of the weights, and amortizes each one over the full gathered batch, which is what moves it back into the compute-bound regime.

这正是"又矮又宽"那一块糟糕的原因。行数很小的 GEMM 没有足够的行去摊薄每个载入的权重,于是大部分时间都花在把分类器流过 tensor core,而不是拿它做乘法 —— 这是一个带宽受限的 kernel,哪怕它的浮点运算量本身很小。而"又高又窄"那一块,在只触碰一小部分权重的前提下完成了同样多的乘法,并把每个权重摊薄到整个全局 batch 上,这才把它推回算力受限的区间。

Shrinking the shard has a second, discontinuous effect. A GEMM tiles its output, and the weight tiles get revisited as the kernel walks down the rows; whether those revisits are served by cache or by HBM depends on whether the whole shard fits in L2. Sharding and sampling shrink the resident classifier by rkrk, which is what can carry it across that threshold — and crossing it changes the cost of reuse by an order of magnitude, not a few percent.

缩小分片还有第二个、且不连续的收益。GEMM 会对输出做分块,kernel 沿行方向推进时会反复访问权重分块;这些重访是由缓存还是由 HBM 承担,取决于整个分片能否放进 L2。分片与采样把常驻分类器缩小了 rkrk 倍,这个倍数足以让它跨过那道门槛 —— 而跨过与否,带来的是数量级的差异,不是百分之几。

One scoping note: all of the above concerns the forward GEMM alone. The logit block is where full model parallelism actually hurts, because it is written by the GEMM, read back for the softmax normalization and read once more in backward. Sampling shrinks that block by 1/r1/r, and it is the only term in the whole pipeline that scales with the product of batch and class count.

一点范围说明:以上讨论都只涉及前向 GEMM。全量模型并行真正吃亏的地方在 logit 矩阵 —— 它由 GEMM 写出、为 Softmax 归一化读回、反向时再读一次。采样把这一块缩小了 1/r1/r,而它是整条流水线中唯一随 batch 与类别数之积增长的项。

Figure 5. Per-rank GEMM shapes and first-order memory traffic, re-drawn from the manuscript analysis. Sampled DP and MP-PFC have the same leading FLOPs, 2NdrC, and produce NrC logits per rank; MP-PFC wins through data movement, because sharding and sampling reduce classifier traffic from Cd to rCd/k while the gathered batch reuses each active center.图 5. 每个 rank 的 GEMM 形状与一阶访存量,根据论文分析重新绘制。采样数据并行与 MP-PFC 的主导 FLOPs 都是 2NdrC,每个 rank 都产生 NrC 个 logits;MP-PFC 的优势来自数据移动:分片与采样把分类器访存从 Cd 降到 rCd/k,同时全局 batch 复用每个激活中心。
TDP ≈ Nd + rCd + NrC TMP ≈ Nkd + rCd/k + NrC FDP = FMP = 2NdrC

Minimal training loop最小训练循环

The implementation needs one global feature gather, local class sampling, distributed softmax statistics, and sparse optimizer updates.

实现只需要全局特征聚合、本地类别采样、分布式 softmax 统计,以及稀疏的优化器更新。

Listing 1. One model-parallel Partial FC training step (PyTorch-style pseudocode).代码 1. 模型并行 Partial FC 的一个训练步(PyTorch 风格伪代码)。

# Model-parallel Partial FC training step
X, y = differentiable_all_gather(local_X, local_y)
S_i = local_positives(y) | sample_local_negatives(r)
Z_i = normalize(X) @ normalize(W_i[S_i]).T
loss = distributed_sampled_margin_softmax(Z_i, remap(y, S_i))
loss.backward()
# sampled classifier rows update locally; feature gradients reduce-scatter
optimizer.step(sampled_rows=S_i)

Conclusion and limitations结论与局限

Partial FC decouples the cost of the classifier from the number of identities in two steps: exact class sharding makes communication independent of CC, and positive-preserving sampling shrinks per-GPU logits and classifier computation to a fraction rr. Moderate ratios keep full-softmax accuracy while improving robustness to noisy and long-tailed labels.

Partial FC 分两步把分类层的开销与身份数解耦:精确的类别切分使通信与 CC 无关,保留正类的采样把每卡 logits 与分类层计算缩小为原来的 rr。适中的采样率在保持全量 Softmax 精度的同时,提升了对噪声与长尾标签的鲁棒性。

Limitations. Renormalizing over the active set makes the sampled gradient a biased estimate of the full-softmax gradient, and very low ratios (r ≤ 0.04 in Table 3) clearly lose accuracy. Host or NVMe staging of the class bank pays off only when transfers overlap computation.

局限。在激活集合上重新归一化,使采样梯度成为全量 Softmax 梯度的有偏估计;采样率过低(表 3 中 r ≤ 0.04)会明显掉点。类别库的主存或 NVMe 分级存储只有在传输能与计算重叠时才有收益。

References参考文献

  1. [1]
    Killing Two Birds with One Stone: Efficient and Robust Training of Face Recognition CNNs by Partial FC An, Deng, Guo, Feng, Zhu, Yang, Liu · CVPR 2022 · arXiv:2203.15565
  2. [2]
    Partial FC: Training 10 Million Identities on a Single Machine An, Zhu, Gao, Xiao, Zhao, Feng, Wu, Qin, Zhang, Zhang, Fu · ICCV Workshops 2021 · arXiv:2010.05222