大模型-位置编码RoPE的具体实现

python 复制代码
def precompute_pos_cis(dim: int, end: int = int(32*1024), theta: float =1e6):
    """位置编码预处理

    Args:
        dim (int): 输入的维度
        end (int, optional): 最大输出Token数. Defaults to int(32*1024).
        theta (float, optional): 控制频率衰减的参数. Defaults to 1e6.
    """
    freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim)) # freq=1/e^freq,频率的多尺度和dim有关
    t = torch.arange(end ,device = freqs.device) # (end,) 表示输出序列最大长度
    freqs = torch.outer(t, freqs).float() # (end, dim//2),外积,表示每一个位置带有不同频率的旋转
    pos_cis = torch.polar(torch.ones_like(freqs), freqs) # polar(单位阵, 频率阵), 生成复数矩阵
    return pos_cis

解释:

python 复制代码
def apply_rotary_emb(xq,xk, pos_cis):
    """RoPE位置编码

    Args:
        xq (_type_): Q矩阵 (batchsize, seqlen, heads, dim//2)
        xk (_type_): K矩阵 (batchsize, seqlen, heads, dim//2)
        pos_cis (_type_): 预处理后的旋转频率矩阵 (end, dim//2)
    """
    def unite_shape(pos_cis, x):
        """对预处理后的旋转频率矩阵进行广播

        Args:
            pos_cis (_type_): 旋转频率矩阵
            x (_type_): 输入
        """
        ndim = x.ndim # 维度,4 (batchsize, seqlen, heads, dim//2)
        assert 0 <= 1 < ndim
        assert pos_cis.shape == (x.shape[1], x.shape[-1]) # (x.shape[1], x.shape[-1]) = (seqlen, dim//2)
        shape = [d if i == 1 or i == ndim - 1 else 1 for i, d in enumerate(x.shape)] # reshape x = (1, seqlen, 1, dim//2)
        return pos_cis.view(*shape) # reshape pos_cis (1, 1, end. dim//2)
    
    xq_ = torch.view_as_complex(xq.float().reshape(*xq.shape[:-1], -1, 2))
    xk_ = torch.view_as_complex(xk.float().reshape(*xk.shape[:-1], -1 ,2))
    pos_cis = unite_shape(*(pos_cis, xq_))
    xq_out = torch.view_as_real(xq_ * pos_cis).flatten(3)
    xk_out = torch.view_as_real(xk_ * pos_cis).flatten(3)
    
    return xq_out.type_as(xq), xk_out.type_as(xk)      

解释:

相关推荐
Blossom.1186 分钟前
Transformer时序预测实战:用PyTorch构建股价预测模型
运维·人工智能·pytorch·python·深度学习·自动化·transformer
weixin_4046793111 分钟前
pytorch nn.Parameter self.register_parameter() 区别
人工智能·pytorch·python·深度学习·机器学习
idkmn_15 分钟前
Agentic AI 基础概念
人工智能·python·深度学习·chatgpt·langchain
Lun3866buzha16 分钟前
【深度学习】Mask R-CNN在温室番茄成熟度检测中的应用——基于ResNet18与FPN的多级特征融合分类系统
深度学习·r语言·cnn
江上鹤.14822 分钟前
Day 41 早停策略和模型权重的保存
人工智能·深度学习·机器学习
好风凭借力,送我上青云34 分钟前
Pytorch经典卷积神经网络-----激活函数篇
人工智能·pytorch·深度学习·算法·矩阵·cnn
扫地的小何尚38 分钟前
NVIDIA CUDA-Q QEC权威指南:实时解码、GPU解码器与AI推理增强
人工智能·深度学习·算法·llm·gpu·量子计算·nvidia
人工智能培训42 分钟前
深度学习初学者指南
人工智能·深度学习·群体智能·智能体·人工智能培训·智能体搭建·深度学习培训
还不秃顶的计科生1 小时前
图像拼接匹配match
人工智能·深度学习