coderofpears commited on
Commit
4f9392a
·
verified ·
1 Parent(s): 37a7c5c

Upload model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. model.py +9 -2
model.py CHANGED
@@ -33,8 +33,14 @@ class RMSNorm(nn.Module):
33
 
34
 
35
  class RotaryEmbedding(nn.Module):
36
- def __init__(self, head_dim, max_len=8192, base=10000.0):
 
37
  super().__init__()
 
 
 
 
 
38
  inv = 1.0 / (base ** (torch.arange(0, head_dim, 2, dtype=torch.float32) / head_dim))
39
  self.register_buffer("inv_freq", inv)
40
 
@@ -121,7 +127,8 @@ class YKDiff(nn.Module):
121
  self.tok_emb = nn.Embedding(self.vocab, d)
122
  self.mode_emb = nn.Embedding(2, d) # 0 = AR, 1 = DIFF
123
  self.time_emb = nn.Linear(1, d, bias=False) # diffusion mask ratio conditioning
124
- self.rope = RotaryEmbedding(d // cfg["n_heads"], max_len=self.max_len)
 
125
 
126
  self.blocks = nn.ModuleList([
127
  Block(d, cfg["n_heads"], cfg["d_ff"]) for _ in range(cfg["n_layers"])
 
33
 
34
 
35
  class RotaryEmbedding(nn.Module):
36
+ def __init__(self, head_dim, max_len=8192, base=10000.0,
37
+ rope_scale=1.0):
38
  super().__init__()
39
+ # NTK-aware scaling: stretch the base so the trained (short) context
40
+ # extends to longer inference windows without retraining. With
41
+ # rope_scale = context_ratio (e.g. 128k/8k = 16) the model trained at
42
+ # 8k still positions tokens correctly at 128k.
43
+ base = base * (rope_scale ** (head_dim / (head_dim - 2)))
44
  inv = 1.0 / (base ** (torch.arange(0, head_dim, 2, dtype=torch.float32) / head_dim))
45
  self.register_buffer("inv_freq", inv)
46
 
 
127
  self.tok_emb = nn.Embedding(self.vocab, d)
128
  self.mode_emb = nn.Embedding(2, d) # 0 = AR, 1 = DIFF
129
  self.time_emb = nn.Linear(1, d, bias=False) # diffusion mask ratio conditioning
130
+ self.rope = RotaryEmbedding(d // cfg["n_heads"], max_len=self.max_len,
131
+ rope_scale=cfg.get("rope_scale", 1.0))
132
 
133
  self.blocks = nn.ModuleList([
134
  Block(d, cfg["n_heads"], cfg["d_ff"]) for _ in range(cfg["n_layers"])