- Embedding.forward 训练时注入 randn 噪声,缩放系数 neftune_noise_alpha / sqrt(seq_len) - TrainConfig.neftune_alpha 通过 config 传递(默认 0=关闭) - TrainContextBuilder 将 config.neftune_alpha 写入 embed_tokens - --neftune_alpha CLI 参数(典型值 5.0) - label_smoothing 默认值 0.05 -> 0.0
24 lines
716 B
Python
24 lines
716 B
Python
import math
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
import torch.nn.functional as F
|
|
from torch import Tensor
|
|
|
|
|
|
class Embedding(nn.Module):
|
|
def __init__(self, vocab_size: int, embedding_dim: int):
|
|
super().__init__()
|
|
self.weight = nn.Parameter(torch.empty((vocab_size, embedding_dim)))
|
|
self.neftune_noise_alpha = 0.0
|
|
|
|
def reset_parameters(self):
|
|
nn.init.normal_(self.weight, mean=0.0, std=0.02)
|
|
|
|
def forward(self, x: Tensor) -> Tensor:
|
|
out = F.embedding(x, self.weight)
|
|
if self.training and self.neftune_noise_alpha > 0.0:
|
|
eps = self.neftune_noise_alpha / math.sqrt(out.size(1))
|
|
out = out + eps * torch.randn_like(out)
|
|
return out
|