Featured image of post Infra入门——An Overview of AI Infra

Infra入门——An Overview of AI Infra

大模型学习笔记(三)

计划在这篇博客里调研并粗略地学习一下到目前为止比较有影响力的AI Infra工作(类似Survey),并慢慢补充丰富。Anyway,迈出行动的第一步最难。

Model Parameters

Parameter Estimation

1B = 1 Billion = 十亿

假设模型层数为$N$,隐藏层维度为$H$,接下来考虑一层Transformer层的参数量估算:

  • 自注意力层:(不需要考虑MHA的情况,因为多头concat起来的为度就等于隐藏层维度)需要注意的是这里包括一次注意力计算和一次线性映射,因此涉及四个可训练参数$W_Q$、$W_K$、$W_V$和$W_O$,因此注意力层的可训练参数量为$4H^2 + 4H$。
$$Attention(Q, K, V)=softmax(\frac{QK^{T}}{\sqrt[]{d_{k}} })V \newline Q=W_{Q}X+b_{Q}, K=W_{K}X+b_{K}, V=W_{V}X+b_{V}, O = W_{O}Attention_{O}+b_{O}$$
  • 前馈网络层:FFN层包括一次线性升维和一次线性降维,设计两个可训练参数$W_{1}$和$W_{2}$,可训练参数量为$(H\times 4H + 4H) + (4H \times H + H) = 8H^{2} + 5H$。
$$FFN(x) = GeLU(xW_{1}+b_{1})W_{2}+b_{2}$$
  • 残差连接和层归一化:Add & Norm层(主要是LN层)涉及两个可训练向量参数$\alpha$和$b$,即2H。
$$Y = Attention(X) + X \newline LN(Y) = \alpha \frac{Y - \mu}{\sigma} + b$$

综上,一层Transformer层由一层Attention层、一层FFN层和两层Add & Norm层组成,可训练参数量为$12H^{2} + 13H$,可以近似为$12H^{2}$。

Computation Estimation

AxB和BxC的矩阵相乘,每个输出元素需要进行$n$次乘法和$n-1$次加法,$\approx 2n FLOPs$;整个矩阵共有AxC个输出元素,因此总$FLOPs \approx 2ABC$,可以近似为$ABC$。因此估算参数时,主要关注矩阵乘或向量矩阵乘的维度即可。注意$W_{Q/K/V}$的维度是$HxH$,而$Q/K/V$的维度是$LxH$。

接下来估算计算量,由于LayerNorm、Dropout等计算量较小,暂时不考虑。设模型层数为$N$,隐藏层维度为$H$,批量大小为$B$,序列长度为$L$:

  • 自注意力层
  1. (LHxHH=LH)线性投影QKV:每个投影是$H\times H$,应用于每个token就是$BLH^{2}$,总共3个矩阵,因此$FLOPs=3BLH^{2}$

  2. (LHxHL=LL)Attention Score ($QK^{T}$):每个token对应一个$L\times L$的注意力矩阵,需要做$H$次乘加,约为$BHL^{2}$

  3. (——————)Softmax和Scaling:Softmax涉及取指、求和、逐元素除和、数值稳定的计算,这里只能估计为$xBL^{2}$,相比SDPA可忽略不计

  4. (LLxLH=LH)Attention Output与V相乘:显然是$BHL^{2}$

  5. (LHxHH=LH)输出线性层$W_{O}$:显然是$BLH^{2}$

因此,自注意力层的总FLOPs:

$$\approx (3BLH^{2})+(2BHL^{2})+(BLH^{2})=4BLH^{2}+2BHL^{2}$$
  • 前馈网络层:
  1. 升维($H$->$4H$):$FLOPs=BLH(4H)$

  2. 降维($4H$->$H$):$FLOPs=BL(4H)H$

(当然还有GeLU激活,不过是线性的计算量,可以忽略不计)因此,FFN层的总Flops:

$$\approx 8BLH^{2}$$

综上,$Total\ FLOPs \approx 12BLH^{2} + 2BHL^{2}$$。(理论上应该再乘以2)

Memory Estimation

Inference Optimization

KV Cache OPtimization

KV Cache

KV (Key-Value) Cache是一种在自回归模型(如Decoder of Transformer)中常用的推理加速技术,通过在推理的注意力机制计算过程中缓存已计算过的$Key$和$Value$,减少重复的$K$、$V$与权重矩阵的projection计算。

$$Attention(Q, K, V)=softmax(\frac{QK^{T}}{\sqrt[]{d_{k}} })V$$

为什么可以缓存$K$和$V$?由于Casual Mask机制,当模型推理时当前token不需要与之后的token进行Attention计算,因此在计算第$t$个token的$Attention_{t}$时,只需要$Q_{0:t}$、$K_{0:t}$和$V_{0:t}$。而Decoder中的$Q$需要token在embedding后通过$W_q$投影,但$K_{0:t-1}$与$V_{0:t-1}$来自Encoder中,且在计算$Attention_{0:t-1}$时已被计算过,因此可以通过缓存已被计算过的历史$K$与$V$来节省这部分计算。

接下来参考知乎@看图学的公式推导,

计算第一个token时的Attention:

$$ Attention(Q, K, V) = softmax(\frac{Q_{1}K_{1}^{T}}{\sqrt[]{d}})V_{1} $$

计算第二个token时的Attention(矩阵第二行对应$Attention_{2}$),$softmax(\frac{Q_{1}K_{2}}{\sqrt d})$项被mask掉了:

$$ Attention(Q, K, V) = softmax(\frac{Q_{2}[K_{1}, K_{2}]^{T}}{\sqrt[]{d}})[V_{1}, V_{2}] \newline = \begin{pmatrix} softmax(\frac{Q_{1}K_{1}^{T}}{\sqrt d}) & softmax(-\infty )\\ softmax(\frac{Q_{2}K_{1}^{T}}{\sqrt d}) & softmax(\frac{Q_{2}K_{2}^{T}}{\sqrt d}) \end{pmatrix}[V_{1}, V_{2}] \newline =\begin{pmatrix} softmax(\frac{Q_{1}K_{1}^{T}}{\sqrt d})V_{1} + 0 \times V_{2} \\ softmax(\frac{Q_{2}K_{1}^{T}}{\sqrt d})V_{1} + softmax(\frac{Q_{2}K_{2}^{T}}{\sqrt d})V_{2} \end{pmatrix} $$

以此类推,Attention矩阵的严格上三角部分都被mask掉了,因此计算第$t$个token的$Attention_{t}$时与$Q_{1:t-1}$无关

$$ Attention_{1} = softmax(\frac{Q_{1}K_{1}^{T}}{\sqrt[]{d}})V_{1} \newline Attention_{2} = softmax(\frac{Q_{1}[K_{1}, K_{2}]^{T}}{\sqrt[]{d}})[V_{1}, V_{2}] \newline ... \newline Attention_{t} = softmax(\frac{Q_{t}K_{1:t}^{T}}{\sqrt[]{d}})V_{1:t} $$

源码实现参考Huggingface的GPT2推理实现,KV Cache的逻辑核心思路如下:

  • 对于Cross Attention,$Q$来自decoder的当前token,$KV$来自encoder的全部输出。因此$KV$通常不变,只需生成一次并缓存。

  • 对于Self Attention,$QKV$都来自decoder的当前token,因为decoder需要看过去所有的token,因此前面token的$KV$都需要缓存

看源码好难——

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
    def forward(
        self,
        hidden_states: Optional[tuple[torch.FloatTensor]],
        past_key_value: Optional[Cache] = None,
        cache_position: Optional[torch.LongTensor] = None,
        attention_mask: Optional[torch.FloatTensor] = None,
        head_mask: Optional[torch.FloatTensor] = None,
        encoder_hidden_states: Optional[torch.Tensor] = None,
        encoder_attention_mask: Optional[torch.FloatTensor] = None,
        output_attentions: Optional[bool] = False,
        **kwargs,
    ) -> tuple[Union[torch.Tensor, tuple[torch.Tensor]], ...]:
        # 判断是否是Cross Attention
        is_cross_attention = encoder_hidden_states is not None
        # Cross Attention使用cross_attention_cache
        # Self Attention使用self_attention_cache
        # 用is_updated表示当前层的KV是否已缓存 (用于Cross Attention)
        if past_key_value is not None:
            if isinstance(past_key_value, EncoderDecoderCache):
                is_updated = past_key_value.is_updated.get(self.layer_idx)
                if is_cross_attention:
                    curr_past_key_value = past_key_value.cross_attention_cache
                else:
                    curr_past_key_value = past_key_value.self_attention_cache
            else:
                curr_past_key_value = past_key_value


        if is_cross_attention:
            # Cross Attention
            query_states = self.q_attn(hidden_states)
            attention_mask = encoder_attention_mask
            # 尝试获取KV Cache
            if past_key_value is not None and is_updated:
                key_states = curr_past_key_value.layers[self.layer_idx].keys
                value_states = curr_past_key_value.layers[self.layer_idx].values
            else:
                key_states, value_states = self.c_attn(encoder_hidden_states).split(self.split_size, dim=2)
                # 变换成MHA的shape
                shape_kv = (*key_states.shape[:-1], -1, self.head_dim)
                key_states = key_states.view(shape_kv).transpose(1, 2)
                value_states = value_states.view(shape_kv).transpose(1, 2)
        else:
            # Self Attention
            query_states, key_states, value_states = self.c_attn(hidden_states).split(self.split_size, dim=2)
            shape_kv = (*key_states.shape[:-1], -1, self.head_dim)
            key_states = key_states.view(shape_kv).transpose(1, 2)
            value_states = value_states.view(shape_kv).transpose(1, 2)


        shape_q = (*query_states.shape[:-1], -1, self.head_dim)
        query_states = query_states.view(shape_q).transpose(1, 2)

        # 更新缓存: 启动KV Cache,且是Self Attention,或Cross Attention没有缓存过的情况
        if (past_key_value is not None and not is_cross_attention) or (
            past_key_value is not None and is_cross_attention and not is_updated
        ):
            cache_position = cache_position if not is_cross_attention else None
            key_states, value_states = curr_past_key_value.update(
                key_states, value_states, self.layer_idx, {"cache_position": cache_position}
            )
            if is_cross_attention:
                past_key_value.is_updated[self.layer_idx] = True

        # 判断是否是因果注意力 (Casual)
        is_causal = attention_mask is None and query_states.shape[-2] > 1 and not is_cross_attention

        # 选择注意力实现方式
        # [eager/flash_attention_2/sdpa/triton/xformers]
        using_eager = self.config._attn_implementation == "eager"
        attention_interface: Callable = eager_attention_forward
        if self.config._attn_implementation != "eager":
            attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]

        # 选择精度提升(upcast)和重排(reorder)
        if using_eager and self.reorder_and_upcast_attn:
            attn_output, attn_weights = self._upcast_and_reordered_attn(
                query_states, key_states, value_states, attention_mask, head_mask
            )
        else:
            # 调用注意力计算函数
            attn_output, attn_weights = attention_interface(
                self,
                query_states,
                key_states,
                value_states,
                attention_mask,
                head_mask=head_mask,
                dropout=self.attn_dropout.p if self.training else 0.0,
                is_causal=is_causal,
                **kwargs,
            )

        # 将Attention结果用线性层c_proj投影回原始维度
        attn_output = attn_output.reshape(*attn_output.shape[:-2], -1).contiguous()
        attn_output = self.c_proj(attn_output)
        attn_output = self.resid_dropout(attn_output)


        return attn_output, attn_weights

同时,KV Cache在减少重复$KV$计算的同时会引入大量的Memory开销,可以粗略计算一下KV Cache的显存占用:

$$ Memory = 2 \times batch\_size \times seq\_len \times num\_layers \times num\_heads \times head\_dims \times dtype\_size $$

MQA

Multi-Query Attention (MQA)是Google在2019年于《Fast Transformer Decoding: One Write-Head is All You Need》提出的一种高效注意力机制,旨在减少推理过程中的计算和内存开销。与传统MHA不同,MQA保留多个Query头,但所有注意力头共享同一组Key和Value,这种结构显著减少了KV Cache的Memory开销,同时保持了MHA相近的性能表现。

下面是基于PyTorch的一个简单实现(还没实现Casual Mask):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import torch
import torch.nn as nn
import torch.nn.functional as F

class MultiQueryAttention(nn.Module):
    def __init__(self, embed_dim, num_heads):
        super().__init__()

        self.embed_dim = embed_dim
        self.num_heads = num_heads
        self.head_dim = embed_dim // num_heads

        # 多个Query头
        self.q_proj = nn.Linear(embed_dim, embed_dim)
        # 共享的Key和Value
        self.k_proj = nn.Linear(embed_dim, self.head_dim)
        self.v_proj = nn.Linear(embed_dim, self.head_dim)

        self.out_proj = nn.Linear(embed_dim, embed_dim)

    def forward(self, x, mask=None):
        B, T, C = x.size()

        # Q: (B, T, num_heads, head_dim)
        q = self.q_proj(x).view(B, T, self.num_heads, self.head_dim).transpose(1, 2) # (B, num_heads, T, head_dim)

        # K, V: shared (B, T, head_dim)->(B, 1, T, head_dim)
        k = self.k_proj(x).unsqueeze(1)
        v = self.v_proj(x).unsqueeze(1)

        # Attention Score: (B, num_heads, T, T)
        attn_scores = torch.matmul(q, k.transpose(-2, -1)) / (self.head_dim ** 0.5)
        if mask is not None:
            attn_scores = attn_scores.masked_fill(mask == 0, float('-inf'))
        attn_weights = F.softmax(attn_scores, dim=-1)

        # Attention output: (B, num_heads, T, head_dim)
        attn_output = torch.matmul(attn_weights, v)

        # 合并头->(B, T, embed_dim)
        attn_output = attn_output.transpose(1, 2).contiguous().view(B, T, C)

        return self.out_proj(attn_output)

GQA

Grouped-Query Attention (GQA)Google与在023年于GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints提出的一种介于MHA和MQA之间的注意力机制,让多个Query头共享同一组Key和Value,旨在保留部分表达能力的同时大幅减少计算和内存开销。

Overview of grouped-query method

源码上,只在Huggingface的仓库里找到了sdpa_attention_paged_forward的实现,看上去挺GQA的。

核心思路是:

  • 先用repeat_kv将KV head复制num_attention_heads // num_key_value_heads次(从(B, num_key_value_heads, L, D)(B, num_attention_heads, L, D)

  • 支持KV Cache的SDPA

Preliminaries (FlashAttention)

FlashAttention由Tri Dao等在2022年于《FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness》提出,并在2023年于《FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning 》提出v2版本,2024年于《FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision》提出v3版本。

Online Softmax

Naive Softmax涉及两次read(遍历求sum和逐元素除sum(exp))和一次write(结果写回),数学公式如下:

$$softmax(x_{i}) = \frac{e^{x_{i}}}{\Sigma _{j=1}^{n}e^{x_{j}}}$$

如果$x$太大,$e^x$会上溢,而safe softmax解决了这一问题。

Safe Softmax涉及三次read(还需要遍历一次减去max)和一次write,目的是为了避免数值溢出,数学公式如下:

$$softmax(x_{i}) = \frac{e^{x_{i} - max(x)}}{\Sigma _{j=1}^{n}e^{x_{j} - max(x)}}$$

但是需要多次遍历数据,性能较差,而online softmax解决了这一问题。

Online Softmax只需要两次read(遍历一次x并维护最大值归一化因子)和一次write,核心思路如下:

  • 在线维护变量($m_t$,当前前$t$个元素的最大值;$d_t$,当前前$t$个元素的归一化因子)

  • 初始化

    $m_0=-\infty, d_0=0$

  • 遍历并维护变量

  1. 更新最大值:

    $m_t=max(m_{t-1}, x_t)$

  2. 更新归一化因子(递推)【难点】:

    $d_t=d_{t-1}\cdot e^{m_{t-1}-m_t}+e^{x_t - m_t}(=\Sigma_{j=1}^{t-1}e^{x_j-m_{t-1}}\cdot e^{m_{t-1}-m_t}+e^{x_t - m_t})$

这里的公式推导非常巧妙,应用了同底指数相乘等于两个指数幂相加,论文的推导如下,$d_{t}$代表前$t$个数与最大值(局部,即$m_t$)之差的指数和

$$d_t = d_{t-1}\times e^{m_{t-1}-m_t} + e^{x_t-m_t} \newline =(\Sigma_{j=1}^{t-1}e^{x_j-m_{t-1}}) \times e^{m_{t-1}-m_t} + e^{x_t-m_t} \newline = \Sigma_{j=1}^{t-1}e^{x_j-m_t}+e^{x_t-m_t} \newline = \Sigma_{j=1}^{t}e^{x_j-m_t}$$

可以这么理解:每次更新归一化因子时,都乘以了$e^{m_{t-1} - m_{t}}$,那么最后这个因子会是$e^{0-m_{global}}$,正是分母$e^{x_{t}-m_{global}}$的一部分,如此巧妙地将全局最大值保留到了遍历结束,而且在递推中的每一步都纠正了之前的局部最大值

online softmax的伪代码如下,实现上还是比较简单的:

Pseudocode of Online Softmax

参考@TaurusMoon的实现写了C++的online softmax kernel:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
using namespace std;

template<typename T>
void OnlineSoftmax(T* dst, const T* src, int n) {
    T m = -numeric_limits<T>::infinity();
    T d = 0.0f;

    for (int i = 0; i < n; +i) {
        T m_update = max(m, src[i]);
        d = d * exp(m - m_update) + exp(src[i] - m_update);
        m = m_update;
    }

    for (int i = 0; i < n; ++i) {
        dst[i] = exp(src[i] - m) / d;
    }
}

GPU Memory Architecture

  • 显存/高带宽内存(HBM, High Bandwidth Memory)是封装在GPU Core外的DRAM(动态存储,需要周期性刷新),通过超宽总线连接GPU Core,大容量的同时延迟也相对较大。

  • 静态内存(SRAM, Static Random Access Memory)**是封装在GPU Core内部的SRAM(静态存储),如Register、Shared Memory、L1/L2 Cache。

Memory/Bandwidth Architcture of A100

Operator

算子主要可以分为两类:

  • 计算受限型:如GEMM等

  • 内存受限型:主要是element-wise类(如Activation、Dropout、Maskibg等)和reduction类(如Sum、Softmax、LayerNorm等)

FlashAttention

FlashAttention-v1

Transformer的核心计算是Attention,朴素的Attention计算步骤如下,其中一般$N \gg d$,复杂度是$N^2$,在长序列频繁读写(5read & 3 write)大矩阵时非常依赖HBM:

Standard Attention Implementation

FlashAttention的核心思路就是提高Attention算子的SRAM利用率(将输入的QKV矩阵从HBM加载到SRAM中计算),减少HBM访存。

  • Tiling

常规的row-wise softmax不适合分块的算法,因此这里需要使用online softmax,在分块后的范围内,片上计算max和rowsum,并在通信后计算全局的max和scale factor。

  • Recomputation

在反向传播的优化,计算梯度需要用到QK计算的attention score ($S$)和softmax后的attention score ($P$)。FlashAttention通过存储Attention的输出结果($O$)和归一化统计量$(m, l)$来快速计算$S$和$P$,避免了用$QKV$的重复计算。

  • Kernel Fusion

很常见的优化,减少了多余的HBM写回和重新加载。

PyTorch vs. FlashAttention on GPT-2

总结一下,FlashAttention可以让计算提速2-4倍,节约10倍以上内存(主要是边存变算,不用存储复杂度为$N^2$的$QKV$,转而存储复杂度为$Nd$的输出结果和统计量)。

Training Optimization

Parallel Computting (on Data)

DP

数据并行(DP, Data Parallel):模型副本在每个GPU上各自独立地前向传播,梯度会聚合(AllReduce)到主GPU进行参数更新。缺点是非跨进程,只支持单机多卡;梯度聚合会发生在主设备,导致通信瓶颈和负载不均衡。

实现上较为简单:

1
2
model = ...()
model = torch.nn.DataParallel(model, device_ids=[0, 1, 2, 3])

不过PyTorch建议多卡并行的时候使用DPP,即使只有一个节点(DP的性能较DDP更差,因为主卡负载很不均衡,单进程多线程环境下设计GIL竞争,且可扩展性不如DDP),源码实现见这里,更加底层的Operator在torch.nn.parallel.scatter_gather/_functions/comm下(如scatter、gather等)。

DDP

分布式数据并行(DDP, Distributed Data Parallel):每个进程对应一个GPU,每个GPU上都有模型副本,梯度通过AllReduce同步,每个金层都参与参数更新(每个GPU独立进行前向、计算loss、计算梯度,并在AllReduce后通过平均梯度更新)。

实现上可以通过手动设置并行(多个terminal设置RANK、WORLD_SIZE、MASTER_ADDR、MASTER_PORT等环境变量并启动脚本)或用torchrun自动管理环境变量(torchrun --nproc_per_node=... <script>):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import os
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP

os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '...'

dist.init_process_group(backend='nccl', rank=rank, world_size=world_size)
model = ...().to(rank)
model = DDP(model, device_ids=[rank])
...
dist.destroy_process_group()

源码参考这里。DDP可以使用高效的通信后端(如NCCL),没有主从瓶颈(支持单机多卡/多机多卡),还是非常实用的。

FSDP

全分片数据并行(FSDP, Fully Sharded Data Parallel):模型权重按参数维度切分到多个GPU上(shard),前向传播时重新聚合参数(gather),反向传播后再切分(reshard),大幅减少显存占用,主要通过torch.nn.distributed.fsdp.FullyShardedDataParallel来实现,源码参考这里

本质上FSDP还是数据并行,知识参数分别有点模型并行的味道

Parallel Computting (on Model)

Existing parallelism for distributed training (sorry我没找到图片来源)

TP

张量并行(TP, Tensor Parallel):是一种层内并行(Intra-Layer Parallelism)策略,将模型中的一个层(如MLP层、Attention层)的内部计算划分到多个设备上,多个设备共同完成该层前向和反向传播。这么做可以突破显存的限制,但是会对延迟较敏感。

  • Column-wise Parallelism (切列,维度完整)
$$W = [W_{1}, W_{2}] \newline Y_{i}=XW_{i}^{T}\ (on\ each\ GPU) \newline Y = Y_{1} + Y_{2}\ (AllReduce)$$
  • Row-wise Parallelism (切行)
$$W=\begin{bmatrix}W_{1} \\W_{2}\end{bmatrix} \newline Y_{i}=XW_{i}^{T}\ (on\ each \ GPU) \newline Y=concat(Y_{1}, Y_{2})\ (AllGather)$$

PP

流水线并行(PP, Pipeline Parallel):是一种层间并行(Inter-Layer Parallelism)策略,将模型按顺序划分为多个stage,不同GPU执行不同的stage,多个micro-batch以流水线方式通过模型。

支持极大模型(层数多),显存需求分布在各stage,且跨GPU通信压力小;但是存在pipeline bubble(起始阶段GPU空闲,影响吞吐)

Quantization

Precision Formats

TF32 strikes a balance that delivers performance with range and accuracy

IEEE 754标准中浮点数由三部分组成:S符号位、E指数位、M尾数位,接下来介绍各种精度格式:

  • FP32 标准的IEEE 754单精度浮点格式,1位符号位+8位指数位+23位位数(下文用[S, E, M]来表示),精度较高,适用于所有主流硬件(CPU、GPU、TPU等)

  • TP32 NVIDIA在Ampere架构引入的混合格式,[1, 8, 10],截断了尾数位(减少乘加复杂度),支持Tensor Core优化,精度介于FP32和FP16之间,常在训练时作为FP32替换

  • FP16 16-bit半精度浮点数,[1, 5, 10]

  • BF16 Google TPU推出的Brain Float 16,[1, 8, 7],常用于混合精度训练

  • FP8 [1, 4, 3]或[1, 5, 2],需要Hopper架构GPU支持

  • INT8 8-bit整型

  • FP4 [1, 2, 1]

Reference

大模型推理加速:看图学KV Cache

LM(20):漫谈 KV Cache 优化方法,深度理解 StreamingLLM

Fast Transformer Decoding: One Write-Head is All You Need

GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints

PyTorch 分布式概览

FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness

FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning

FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision

Online normalizer calculation for softmax

一心二用的Online Softmax

使用 Hugo 构建
主题 StackJimmy 设计