赞
踩
准备学习llama2模型的代码
- class ModelArgs:
- dim: int = 4096
- n_layers: int = 32
- n_heads: int = 32
- n_kv_heads: Optional[int] = None
- vocab_size: int = -1 # defined later by tokenizer
- multiple_of: int = 256 # make SwiGLU hidden layer size multiple of large power of 2
- ffn_dim_multiplier: Optional[float] = None
- norm_eps: float = 1e-5
-
- max_batch_size: int = 32
- max_seq_len: int = 2048
此段代码的作用是定义模型中需要用到的各种超参数,维度,层数,头数,kv头数,词典大小,ffn_dim_multiplier是隐藏维度的可选乘法因子,multiple_of是确保隐藏维度是这个值的倍数,norm_eps为标准化时防止0出现在分母上的一个极小参数,,还有批次大小,序列长度大小
- class RMSNorm(torch.nn.Module):
- def __init__(self, dim: int, eps: float = 1e-6):
- super().__init__()
- self.eps = eps
- self.weight = nn.Parameter(torch.ones(dim))
-
- def _norm(self, x):
- return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
-
- def forward(self, x):
- output = self._norm(x.float()).type_as(x)
- return output * self.weight
定义RMS正则化标准,eps为防止分母出现0的极小数,使用nn.Parameter创建一个可学习的参数,维度为dim,值全为1。
torch.rsqrt()可以计算torch中逐个元素的平方根的倒数,tensor.mean(-1, keepdim=True)可以对输入张量(本函数中为最后一维)求平均。
- def precompute_freqs_cis(dim: int, end: int, theta: float = 10000.0):
- freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim))
- t = torch.arange(end, device=freqs.device) # type: ignore
- freqs = torch.outer(t, freqs).float() # type: ignore
- freqs_cis = torch.polar(torch.ones_like(freqs), freqs) # complex64
- return freqs_cis
此段代码用于预计算频率,用于实现Rotary Positional Embedding,freqs是一个频率向量,(torch.arange(start,end,step)方法可以指定创建范围内的一维向量),torch.outer计算了一个频率矩阵,torch.polar将频率矩阵转化为复数形式,得到了预计算的频率项freqs_cis
- def reshape_for_broadcast(freqs_cis: torch.Tensor, x: torch.Tensor):
- ndim = x.ndim
- assert 0 <= 1 < ndim
- assert freqs_cis.shape == (x.shape[1], x.shape[-1])
- shape = [d if i == 1 or i == ndim - 1 else 1 for i, d in enumerate(x.shape)]
- return freqs_cis.view(*shape)
调整freqs_cis张量形状与x进行广播,torch.ndim可以获取torch的维度(是几维向量),遍历x向量的第i个维度和第i个维度的大小,创建一个新形状shape,除了第二维和最后一维其余与x唯独一样,剩下的两维与freqs_cis一样,使用view方法调整freqs_cis的形状
- def apply_rotary_emb(
- xq: torch.Tensor,
- xk: torch.Tensor,
- freqs_cis: torch.Tensor,
- ) -> Tuple[torch.Tensor, torch.Tensor]:
- 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))
- freqs_cis = reshape_for_broadcast(freqs_cis, xq_)
- xq_out = torch.view_as_real(xq_ * freqs_cis).flatten(3)
- xk_out = torch.view_as_real(xk_ * freqs_cis).flatten(3)
- return xq_out.type_as(xq), xk_out.type_as(xk)
通过torch.view_as_complex函数将xq,xk变成复数形式,将freqs_cis广播为xq形式,将位置编码以复数相乘的方法加入xq,xk中,最后再返回实数形式
- def repeat_kv(x: torch.Tensor, n_rep: int) -> torch.Tensor:
- bs, slen, n_kv_heads, head_dim = x.shape
- if n_rep == 1:
- return x
- return (
- x[:, :, :, None, :]
- .expand(bs, slen, n_kv_heads, n_rep, head_dim)
- .reshape(bs, slen, n_kv_heads * n_rep, head_dim)
- )
计算张量时,经常需要对齐张量,本函数在倒数第二维度增加一维,扩张成x*n_rep
- class Attention(nn.Module):
- def __init__(self, args: ModelArgs):
- super().__init__()
- #不定义键值头的话就默认是本地注意力头的数量
- self.n_kv_heads = args.n_heads if args.n_kv_heads is None else args.n_kv_heads
- #定义模型并行大小
- model_parallel_size = fs_init.get_model_parallel_world_size()
- #本地注意力头(其实是一个显卡上分配的注意力头)
- self.n_local_heads = args.n_heads // model_parallel_size
- #键值头
- self.n_local_kv_heads = self.n_kv_heads // model_parallel_size
- #用于上一个函数中的张量对齐的数
- self.n_rep = self.n_local_heads // self.n_local_kv_heads
- self.head_dim = args.dim // args.n_heads
-
- self.wq = ColumnParallelLinear(
- args.dim,
- args.n_heads * self.head_dim,
- bias=False,
- gather_output=False,
- init_method=lambda x: x,
- )
- self.wk = ColumnParallelLinear(
- args.dim,
- self.n_kv_heads * self.head_dim,
- bias=False,
- gather_output=False,
- init_method=lambda x: x,
- )
- self.wv = ColumnParallelLinear(
- args.dim,
- self.n_kv_heads * self.head_dim,
- bias=False,
- gather_output=False,
- init_method=lambda x: x,
- )
- self.wo = RowParallelLinear(
- args.n_heads * self.head_dim,
- args.dim,
- bias=False,
- input_is_parallel=True,
- init_method=lambda x: x,
- )
-
- self.cache_k = torch.zeros(
- (
- args.max_batch_size,
- args.max_seq_len,
- self.n_local_kv_heads,
- self.head_dim,
- )
- ).cuda()
- self.cache_v = torch.zeros(
- (
- args.max_batch_size,
- args.max_seq_len,
- self.n_local_kv_heads,
- self.head_dim,
- )
- ).cuda()
-
- def forward(
- self,
- x: torch.Tensor,
- start_pos: int,
- freqs_cis: torch.Tensor,
- mask: Optional[torch.Tensor],
- ):
- bsz, seqlen, _ = x.shape
- xq, xk, xv = self.wq(x), self.wk(x), self.wv(x)
-
- xq = xq.view(bsz, seqlen, self.n_local_heads, self.head_dim)
- xk = xk.view(bsz, seqlen, self.n_local_kv_heads, self.head_dim)
- xv = xv.view(bsz, seqlen, self.n_local_kv_heads, self.head_dim)
-
- xq, xk = apply_rotary_emb(xq, xk, freqs_cis=freqs_cis)
-
- self.cache_k = self.cache_k.to(xq)
- self.cache_v = self.cache_v.to(xq)
-
- self.cache_k[:bsz, start_pos : start_pos + seqlen] = xk
- self.cache_v[:bsz, start_pos : start_pos + seqlen] = xv
-
- keys = self.cache_k[:bsz, : start_pos + seqlen]
- values = self.cache_v[:bsz, : start_pos + seqlen]
-
- # repeat k/v heads if n_kv_heads < n_heads
- keys = repeat_kv(keys, self.n_rep) # (bs, cache_len + seqlen, n_local_heads, head_dim)
- values = repeat_kv(values, self.n_rep) # (bs, cache_len + seqlen, n_local_heads, head_dim)
-
- xq = xq.transpose(1, 2) # (bs, n_local_heads, seqlen, head_dim)
- keys = keys.transpose(1, 2) # (bs, n_local_heads, cache_len + seqlen, head_dim)
- values = values.transpose(1, 2) # (bs, n_local_heads, cache_len + seqlen, head_dim)
- scores = torch.matmul(xq, keys.transpose(2, 3)) / math.sqrt(self.head_dim)
- if mask is not None:
- scores = scores + mask # (bs, n_local_heads, seqlen, cache_len + seqlen)
- scores = F.softmax(scores.float(), dim=-1).type_as(xq)
- output = torch.matmul(scores, values) # (bs, n_local_heads, seqlen, head_dim)
- output = output.transpose(1, 2).contiguous().view(bsz, seqlen, -1)
- return self.wo(output)
Attention类的定义,在init函数中将本地头和键值头根据模型并行数进行划分,定义了四个线形层wq,wk,wv,wo分别用于查询,键,值,输出。
在forward方法中,先获取x的shape,接着通过线性层计算查询,键,值(其实就是把x中的batch_size和seqlen提取出来,将x变为(bsz, seqlen, self.n_local_heads, self.head_dim)的形状),接着将键,值应用于旋转编码,放入缓存,再将k和v从缓存中取出,并对齐(此处主要是消除并行的影响),之后进行k,q,v的计算。
个人理解:这里注意力层整体的输入维度和输出维度即是x的维度(batch_size,seqlen,-1)
- class FeedForward(nn.Module):
- def __init__(
- self,
- dim: int,
- hidden_dim: int,
- multiple_of: int,
- ffn_dim_multiplier: Optional[float],
- ):
- super().__init__()
- hidden_dim = int(2 * hidden_dim / 3)
- # custom dim factor multiplier
- if ffn_dim_multiplier is not None:
- hidden_dim = int(ffn_dim_multiplier * hidden_dim)
- hidden_dim = multiple_of * ((hidden_dim + multiple_of - 1) // multiple_of)
-
- self.w1 = ColumnParallelLinear(
- dim, hidden_dim, bias=False, gather_output=False, init_method=lambda x: x
- )
- self.w2 = RowParallelLinear(
- hidden_dim, dim, bias=False, input_is_parallel=True, init_method=lambda x: x
- )
- self.w3 = ColumnParallelLinear(
- dim, hidden_dim, bias=False, gather_output=False, init_method=lambda x: x
- )
-
- def forward(self, x):
- return self.w2(F.silu(self.w1(x)) * self.w3(x))
前馈网络层用于对注意力层的输出进行进一步的处理,每个transformer块中都有一个注意力层和前馈层,是一个全连接的神经网络,在每个位置上独立处理输入
- class TransformerBlock(nn.Module):
- def __init__(self, layer_id: int, args: ModelArgs):
- super().__init__()
- self.n_heads = args.n_heads
- self.dim = args.dim
- self.head_dim = args.dim // args.n_heads
- self.attention = Attention(args)
- self.feed_forward = FeedForward(
- dim=args.dim,
- hidden_dim=4 * args.dim,
- multiple_of=args.multiple_of,
- ffn_dim_multiplier=args.ffn_dim_multiplier,
- )
- self.layer_id = layer_id
- self.attention_norm = RMSNorm(args.dim, eps=args.norm_eps)
- self.ffn_norm = RMSNorm(args.dim, eps=args.norm_eps)
-
- def forward(
- self,
- x: torch.Tensor,
- start_pos: int,
- freqs_cis: torch.Tensor,
- mask: Optional[torch.Tensor],
- ):
- h = x + self.attention.forward(
- self.attention_norm(x), start_pos, freqs_cis, mask
- )
- out = h + self.feed_forward.forward(self.ffn_norm(h))
- return out
transformer模块,包含注意力层和前馈层,每个层之前都需要正则化
- class Transformer(nn.Module):
- def __init__(self, params: ModelArgs):
- super().__init__()
- self.params = params
- self.vocab_size = params.vocab_size
- self.n_layers = params.n_layers
-
- self.tok_embeddings = ParallelEmbedding(
- params.vocab_size, params.dim, init_method=lambda x: x
- )
-
- self.layers = torch.nn.ModuleList()
- for layer_id in range(params.n_layers):
- self.layers.append(TransformerBlock(layer_id, params))
-
- self.norm = RMSNorm(params.dim, eps=params.norm_eps)
- self.output = ColumnParallelLinear(
- params.dim, params.vocab_size, bias=False, init_method=lambda x: x
- )
-
- self.freqs_cis = precompute_freqs_cis(
- # Note that self.params.max_seq_len is multiplied by 2 because the token limit for the Llama 2 generation of models is 4096.
- # Adding this multiplier instead of using 4096 directly allows for dynamism of token lengths while training or fine-tuning.
- self.params.dim // self.params.n_heads, self.params.max_seq_len * 2
- )
- @torch.inference_mode()
- def forward(self, tokens: torch.Tensor, start_pos: int):
- _bsz, seqlen = tokens.shape
- h = self.tok_embeddings(tokens)
- self.freqs_cis = self.freqs_cis.to(h.device)
- freqs_cis = self.freqs_cis[start_pos : start_pos + seqlen]
-
- mask = None
- if seqlen > 1:
- mask = torch.full(
- (seqlen, seqlen), float("-inf"), device=tokens.device
- )
-
- mask = torch.triu(mask, diagonal=1)
-
- # When performing key-value caching, we compute the attention scores
- # only for the new sequence. Thus, the matrix of scores is of size
- # (seqlen, cache_len + seqlen), and the only masked entries are (i, j) for
- # j > cache_len + i, since row i corresponds to token cache_len + i.
- mask = torch.hstack([
- torch.zeros((seqlen, start_pos), device=tokens.device),
- mask
- ]).type_as(h)
-
- for layer in self.layers:
- h = layer(h, start_pos, freqs_cis, mask)
- h = self.norm(h)
- output = self.output(h).float()
- return output
在forward函数中,对输入的 token 索引进行嵌入操作,通过freqs_cis提取频率信息,创建注意力掩码,遍历transformer层,正则化,输出。
将对角线以下的全0矩阵和以上的全mask矩阵进行水平拼接,并与输入数据h的数据类型保持一致
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。