Markdown 写作示例:代码、公式与拓扑图

这是一篇 Markdown 格式的文章(文件扩展名 .md)。本文展示博客支持的各类排版能力,你可以复制这篇文章作为新文章的起点。

行内代码如 ncclAllReduce,块级代码自动带复制按钮:

#include <nccl.h>

int main(int argc, char** argv) {
    ncclUniqueId id;
    ncclGetUniqueId(&id);

    ncclComm_t comm;
    ncclCommInitRank(&comm, 8, id, 0);

    float* sendbuf = nullptr;  // 设备指针
    float* recvbuf = nullptr;
    ncclAllReduce((const void*)sendbuf, (void*)recvbuf, 1 << 20,
                  ncclFloat32, ncclSum, comm, nullptr);

    ncclCommDestroy(comm);
    return 0;
}
# 启动 8 卡 AllReduce 带宽测试
all_reduce_perf -b 8 -e 1G -f 2 -g 8

集合通信离不开公式。行内公式:Ring AllReduce 的带宽代价为 $b_w = \frac{2(n-1)}{n}$,当 GPU 数 $n \to \infty$ 时趋近于 $2$。

写作提示:公式中含下标 _ 时,请写作 \_(反斜杠转义),否则 Markdown 可能把成对的下划线解析成斜体导致公式损坏。上标 ^ 无此问题。

块级公式(Ring AllReduce 在 $\alpha$-$\beta$ 模型下的完成时间):

Ring AllReduce 完成时间:$$ T_{\text{ring}}(S, n) = \underbrace{2(n-1) \cdot \frac{S/n}{B}}_{\text{数据传输}} + \underbrace{2(n-1) \cdot \alpha}_{\text{同步延迟}} = \frac{2(n-1)}{n} \cdot \frac{S}{B} + 2(n-1)\alpha $$

多步算法(两步 AllReduce = ReduceScatter + AllGather):

$$ \text{AllReduce} = \text{ReduceScatter}\left(\frac{n-1}{n} S\right) + \text{AllGather}\left(\frac{n-1}{n} S\right) $$

graph LR
    GPU0 --> GPU1
    GPU1 --> GPU2
    GPU2 --> GPU3
    GPU3 --> GPU0
    style GPU0 fill:#e1f5fe
    style GPU1 fill:#e1f5fe
    style GPU2 fill:#fff3e0
    style GPU3 fill:#fff3e0

上图是一个 4 卡 NVLink Ring 拓扑:同一方向的卡(同色)共享一条链路带宽。

集合原语数据量环算法带宽代价 $\alpha_w$典型场景
AllReduce$S$$2(n-1)/n$梯度同步
AllGather$S$$(n-1)/n$TP 前向激活汇聚
ReduceScatter$S$$(n-1)/n$梯度分片
Broadcast$S$$1$参数/状态广播
AllToAll$S$$\lceil n/2 \rceil (n-1)/n$MoE 专家路由

Markdown 中也可以直接写 HTML(本站已开启 unsafe 渲染):

提示:这是一个用原生 <div> 写的提示框,适合放重要结论或注意事项。

论文结论:在 $n$ 卡互联中,Ring AllReduce 是带宽最优的 AllReduce 算法,其渐近带宽代价 $2\times$ 理论下界。

  • 无序列表项:hugo new posts/my-post.md 新建文章
  • 斜体 draft 设为 true 则不发布
  • 删除线 旧算法

写完新文章后,本地 hugo server 预览,git push 即自动发布。详见仓库 README.md。