Self-contained HF upload with config.py
Browse files- config.json +1 -1
- config.py +3 -0
- modeling_nexus_small.py +103 -159
config.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
| 16 |
"transformers_version": "4.51.3",
|
| 17 |
"vocab_size": 50304,
|
| 18 |
"auto_map": {
|
| 19 |
-
"AutoConfig": "
|
| 20 |
"AutoModel": "modeling_nexus_small.NexusForCausalLM",
|
| 21 |
"AutoModelForCausalLM": "modeling_nexus_small.NexusForCausalLM"
|
| 22 |
}
|
|
|
|
| 16 |
"transformers_version": "4.51.3",
|
| 17 |
"vocab_size": 50304,
|
| 18 |
"auto_map": {
|
| 19 |
+
"AutoConfig": "modeling_nexus_small.NexusSmAllConfig",
|
| 20 |
"AutoModel": "modeling_nexus_small.NexusForCausalLM",
|
| 21 |
"AutoModelForCausalLM": "modeling_nexus_small.NexusForCausalLM"
|
| 22 |
}
|
config.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoConfig
|
| 2 |
+
from modeling_nexus_small import NexusSmAllConfig
|
| 3 |
+
AutoConfig.register('nexus_small', NexusSmAllConfig)
|
modeling_nexus_small.py
CHANGED
|
@@ -4,15 +4,41 @@ from typing import Optional
|
|
| 4 |
import torch
|
| 5 |
import torch.nn as nn
|
| 6 |
import torch.nn.functional as F
|
| 7 |
-
from transformers import PreTrainedModel, AutoConfig, AutoModel, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
try:
|
| 10 |
-
from configuration_nexus_small import NexusConfig, NexusSmAllConfig
|
| 11 |
-
except ImportError:
|
| 12 |
-
try:
|
| 13 |
-
from config import NexusConfig, NexusSmAllConfig
|
| 14 |
-
except ImportError:
|
| 15 |
-
from .config import NexusConfig, NexusSmAllConfig
|
| 16 |
|
| 17 |
class RMSNorm(nn.Module):
|
| 18 |
def __init__(self, dim: int, eps: float = 1e-6):
|
|
@@ -24,49 +50,47 @@ class RMSNorm(nn.Module):
|
|
| 24 |
rms = torch.rsqrt(x.float().pow(2).mean(-1, keepdim=True) + self.eps)
|
| 25 |
return (x.float() * rms * self.weight.float()).type_as(x)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
freqs = torch.outer(t, freqs)
|
| 32 |
return torch.polar(torch.ones_like(freqs), freqs)
|
| 33 |
|
|
|
|
| 34 |
class RotaryEmbedding(nn.Module):
|
| 35 |
-
def __init__(self,
|
| 36 |
super().__init__()
|
| 37 |
-
self.freqs_cis = precompute_freqs_cis(
|
| 38 |
|
| 39 |
-
def forward(self, x
|
| 40 |
_, seq_len, _, head_dim = x.shape
|
| 41 |
-
freqs_cis = self.freqs_cis[start_pos:start_pos+seq_len, :head_dim//2].to(x.device)
|
| 42 |
-
freqs_cis = freqs_cis.view(1, seq_len, 1, head_dim//2)
|
| 43 |
-
|
| 44 |
x_shaped = x.float().reshape(*x.shape[:-1], -1, 2)
|
| 45 |
x_complex = torch.complex(x_shaped[..., 0], x_shaped[..., 1])
|
| 46 |
-
|
| 47 |
x_rotated = x_complex * freqs_cis
|
| 48 |
-
|
| 49 |
x_out = torch.stack([x_rotated.real, x_rotated.imag], dim=-1).reshape_as(x_shaped)
|
| 50 |
-
|
| 51 |
return x_out.reshape_as(x).type_as(x)
|
| 52 |
|
|
|
|
| 53 |
class Attention(nn.Module):
|
| 54 |
-
def __init__(self,
|
| 55 |
super().__init__()
|
| 56 |
-
self.num_heads =
|
| 57 |
-
self.num_kv_heads =
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
self.
|
| 62 |
-
|
| 63 |
-
self.
|
| 64 |
-
self.
|
| 65 |
-
self.
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def forward(self, x: torch.Tensor, start_pos: int = 0, mask: Optional[torch.Tensor] = None):
|
| 70 |
bsz, seqlen, _ = x.shape
|
| 71 |
|
| 72 |
q = self.wq(x).view(bsz, seqlen, self.num_heads, self.head_dim).transpose(1, 2)
|
|
@@ -90,175 +114,95 @@ class Attention(nn.Module):
|
|
| 90 |
|
| 91 |
attn_weights = F.softmax(attn_weights.float(), dim=-1).type_as(q)
|
| 92 |
attn_output = torch.matmul(attn_weights, v)
|
| 93 |
-
|
| 94 |
attn_output = attn_output.transpose(1, 2).contiguous().view(bsz, seqlen, -1)
|
| 95 |
return self.wo(attn_output)
|
| 96 |
|
|
|
|
| 97 |
class FeedForward(nn.Module):
|
| 98 |
-
def __init__(self,
|
| 99 |
super().__init__()
|
| 100 |
-
hidden_dim = int(2 *
|
| 101 |
-
hidden_dim =
|
| 102 |
|
| 103 |
-
self.w1 = nn.Linear(
|
| 104 |
-
self.w2 = nn.Linear(hidden_dim,
|
| 105 |
-
self.w3 = nn.Linear(
|
| 106 |
|
| 107 |
def forward(self, x):
|
| 108 |
return self.w2(F.silu(self.w1(x)) * self.w3(x))
|
| 109 |
|
|
|
|
| 110 |
class TransformerBlock(nn.Module):
|
| 111 |
-
def __init__(self,
|
| 112 |
super().__init__()
|
| 113 |
-
self.attention = Attention(
|
| 114 |
-
self.feed_forward = FeedForward(
|
| 115 |
-
self.attention_norm = RMSNorm(
|
| 116 |
-
self.ff_norm = RMSNorm(
|
| 117 |
|
| 118 |
-
def forward(self, x
|
| 119 |
h = x + self.attention(self.attention_norm(x), start_pos, mask)
|
| 120 |
out = h + self.feed_forward(self.ff_norm(h))
|
| 121 |
return out
|
| 122 |
|
| 123 |
-
class NexusPreTrainedModel(PreTrainedModel):
|
| 124 |
-
config_class = NexusSmAllConfig
|
| 125 |
-
base_model_prefix = "nexus_small"
|
| 126 |
-
supports_gradient_checkpointing = False
|
| 127 |
-
_no_split_modules = ["TransformerBlock"]
|
| 128 |
-
|
| 129 |
-
def __init__(self, config: NexusSmAllConfig):
|
| 130 |
-
super().__init__(config)
|
| 131 |
-
self._nexus_config = NexusConfig(
|
| 132 |
-
vocab_size=config.vocab_size,
|
| 133 |
-
max_seq_len=config.max_seq_len,
|
| 134 |
-
dim=config.dim,
|
| 135 |
-
num_layers=config.num_layers,
|
| 136 |
-
num_heads=config.num_heads,
|
| 137 |
-
num_kv_heads=config.num_kv_heads,
|
| 138 |
-
multiple_of=config.multiple_of,
|
| 139 |
-
ff_dim=config.ff_dim,
|
| 140 |
-
norm_eps=config.norm_eps,
|
| 141 |
-
rope_theta=config.rope_theta,
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
def _init_weights(self, module):
|
| 145 |
-
if isinstance(module, nn.Linear):
|
| 146 |
-
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 147 |
-
|
| 148 |
|
| 149 |
class Nexus(nn.Module):
|
| 150 |
-
def __init__(self, config
|
| 151 |
super().__init__()
|
| 152 |
self.config = config
|
| 153 |
|
| 154 |
self.token_embeddings = nn.Embedding(config.vocab_size, config.dim)
|
| 155 |
-
self.layers = nn.ModuleList([
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
self.norm = RMSNorm(config.dim, config.norm_eps)
|
| 157 |
self.output = nn.Linear(config.dim, config.vocab_size, bias=False)
|
| 158 |
-
|
| 159 |
self.token_embeddings.weight = self.output.weight
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
def _init_weights(self, module):
|
| 164 |
-
if isinstance(module, nn.Linear):
|
| 165 |
-
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 166 |
-
|
| 167 |
-
def forward(self, input_ids: torch.Tensor, start_pos: int = 0):
|
| 168 |
_, seqlen = input_ids.shape
|
| 169 |
|
| 170 |
-
mask = torch.full(
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
mask = torch.triu(mask, diagonal=start_pos + 1).type_as(input_ids)
|
| 173 |
|
| 174 |
x = self.token_embeddings(input_ids)
|
| 175 |
-
|
| 176 |
for layer in self.layers:
|
| 177 |
x = layer(x, start_pos, mask)
|
| 178 |
-
|
| 179 |
x = self.norm(x)
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
seq_len = input_ids.shape[1]
|
| 191 |
-
if seq_len > self.config.max_seq_len:
|
| 192 |
-
input_ids = input_ids[:, -self.config.max_seq_len:]
|
| 193 |
-
|
| 194 |
-
with torch.no_grad():
|
| 195 |
-
logits = self(input_ids, 0)
|
| 196 |
-
logits = logits[:, -1, :] / temperature
|
| 197 |
-
|
| 198 |
-
if top_k > 0:
|
| 199 |
-
top_k_values, _ = torch.topk(logits, top_k)
|
| 200 |
-
min_top_k = top_k_values[:, -1].unsqueeze(-1)
|
| 201 |
-
logits = torch.where(logits < min_top_k,
|
| 202 |
-
torch.full_like(logits, float('-inf')), logits)
|
| 203 |
-
|
| 204 |
-
if top_p > 0:
|
| 205 |
-
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
|
| 206 |
-
cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
|
| 207 |
-
sorted_indices_to_remove = cumulative_probs > top_p
|
| 208 |
-
sorted_indices_to_remove[:, 0] = False
|
| 209 |
-
indices_to_remove = torch.zeros_like(logits, dtype=torch.bool)
|
| 210 |
-
indices_to_remove = indices_to_remove.scatter(1, sorted_indices,
|
| 211 |
-
sorted_indices_to_remove)
|
| 212 |
-
logits = torch.where(indices_to_remove,
|
| 213 |
-
torch.full_like(logits, float('-inf')), logits)
|
| 214 |
-
|
| 215 |
-
probs = F.softmax(logits, dim=-1)
|
| 216 |
-
next_token = torch.multinomial(probs, num_samples=1)
|
| 217 |
-
|
| 218 |
-
input_ids = torch.cat([input_ids, next_token], dim=-1)
|
| 219 |
-
generated.append(next_token.item())
|
| 220 |
-
|
| 221 |
-
return generated, input_ids
|
| 222 |
-
|
| 223 |
-
class NexusForCausalLM(NexusPreTrainedModel):
|
| 224 |
-
def __init__(self, config: NexusSmAllConfig):
|
| 225 |
super().__init__(config)
|
| 226 |
-
self.model = Nexus(
|
| 227 |
self.lm_head = nn.Linear(config.dim, config.vocab_size, bias=False)
|
| 228 |
with torch.no_grad():
|
| 229 |
self.lm_head.weight = self.model.output.weight
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
def forward(self, input_ids, attention_mask=None, **kwargs):
|
| 232 |
return self.model(input_ids)
|
| 233 |
|
| 234 |
-
def generate(self, input_ids, max_new_tokens=128, temperature=0.7, top_k=50, top_p=0.9, **kwargs):
|
| 235 |
-
return self.model.generate(input_ids, max_new_tokens, temperature, top_k, top_p)
|
| 236 |
-
|
| 237 |
-
@classmethod
|
| 238 |
-
def _from_config(cls, config, **kwargs):
|
| 239 |
-
return cls(config)
|
| 240 |
-
|
| 241 |
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 242 |
return {"input_ids": input_ids}
|
| 243 |
|
| 244 |
|
| 245 |
-
AutoConfig.register("nexus_small", NexusSmAllConfig)
|
| 246 |
AutoModel.register(NexusSmAllConfig, NexusForCausalLM)
|
| 247 |
AutoModelForCausalLM.register(NexusSmAllConfig, NexusForCausalLM)
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
def create_nexus_model():
|
| 251 |
-
try:
|
| 252 |
-
from config import nexus_config
|
| 253 |
-
except ImportError:
|
| 254 |
-
from .config import nexus_config
|
| 255 |
-
config = nexus_config
|
| 256 |
-
|
| 257 |
-
model = Nexus(config)
|
| 258 |
-
total_params = sum(p.numel() for p in model.parameters())
|
| 259 |
-
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 260 |
-
|
| 261 |
-
print(f"[Nexus SmAll] Model created with {total_params/1e6:.1f}M parameters "
|
| 262 |
-
f"({trainable_params/1e6:.1f}M trainable)")
|
| 263 |
-
|
| 264 |
-
return model, config
|
|
|
|
| 4 |
import torch
|
| 5 |
import torch.nn as nn
|
| 6 |
import torch.nn.functional as F
|
| 7 |
+
from transformers import PreTrainedModel, PretrainedConfig, AutoConfig, AutoModel, AutoModelForCausalLM
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class NexusSmAllConfig(PretrainedConfig):
|
| 11 |
+
model_type = "nexus_small"
|
| 12 |
+
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
vocab_size=50304,
|
| 16 |
+
max_seq_len=512,
|
| 17 |
+
dim=768,
|
| 18 |
+
num_layers=10,
|
| 19 |
+
num_heads=12,
|
| 20 |
+
num_kv_heads=4,
|
| 21 |
+
multiple_of=256,
|
| 22 |
+
ff_dim=2048,
|
| 23 |
+
norm_eps=1e-6,
|
| 24 |
+
rope_theta=500000.0,
|
| 25 |
+
**kwargs,
|
| 26 |
+
):
|
| 27 |
+
super().__init__(**kwargs)
|
| 28 |
+
self.vocab_size = vocab_size
|
| 29 |
+
self.max_seq_len = max_seq_len
|
| 30 |
+
self.dim = dim
|
| 31 |
+
self.num_layers = num_layers
|
| 32 |
+
self.num_heads = num_heads
|
| 33 |
+
self.num_kv_heads = num_kv_heads
|
| 34 |
+
self.multiple_of = multiple_of
|
| 35 |
+
self.ff_dim = ff_dim
|
| 36 |
+
self.norm_eps = norm_eps
|
| 37 |
+
self.rope_theta = rope_theta
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
AutoConfig.register("nexus_small", NexusSmAllConfig)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
class RMSNorm(nn.Module):
|
| 44 |
def __init__(self, dim: int, eps: float = 1e-6):
|
|
|
|
| 50 |
rms = torch.rsqrt(x.float().pow(2).mean(-1, keepdim=True) + self.eps)
|
| 51 |
return (x.float() * rms * self.weight.float()).type_as(x)
|
| 52 |
|
| 53 |
+
|
| 54 |
+
def precompute_freqs_cis(max_seq_len, dim, num_heads, rope_theta):
|
| 55 |
+
head_dim = dim // num_heads
|
| 56 |
+
freqs = 1.0 / (rope_theta ** (torch.arange(0, head_dim, 2).float() / head_dim))
|
| 57 |
+
t = torch.arange(max_seq_len)
|
| 58 |
freqs = torch.outer(t, freqs)
|
| 59 |
return torch.polar(torch.ones_like(freqs), freqs)
|
| 60 |
|
| 61 |
+
|
| 62 |
class RotaryEmbedding(nn.Module):
|
| 63 |
+
def __init__(self, max_seq_len, dim, num_heads, rope_theta):
|
| 64 |
super().__init__()
|
| 65 |
+
self.freqs_cis = precompute_freqs_cis(max_seq_len, dim, num_heads, rope_theta)
|
| 66 |
|
| 67 |
+
def forward(self, x, start_pos=0):
|
| 68 |
_, seq_len, _, head_dim = x.shape
|
| 69 |
+
freqs_cis = self.freqs_cis[start_pos : start_pos + seq_len, : head_dim // 2].to(x.device)
|
| 70 |
+
freqs_cis = freqs_cis.view(1, seq_len, 1, head_dim // 2)
|
| 71 |
+
|
| 72 |
x_shaped = x.float().reshape(*x.shape[:-1], -1, 2)
|
| 73 |
x_complex = torch.complex(x_shaped[..., 0], x_shaped[..., 1])
|
|
|
|
| 74 |
x_rotated = x_complex * freqs_cis
|
|
|
|
| 75 |
x_out = torch.stack([x_rotated.real, x_rotated.imag], dim=-1).reshape_as(x_shaped)
|
|
|
|
| 76 |
return x_out.reshape_as(x).type_as(x)
|
| 77 |
|
| 78 |
+
|
| 79 |
class Attention(nn.Module):
|
| 80 |
+
def __init__(self, dim, num_heads, num_kv_heads, max_seq_len, rope_theta):
|
| 81 |
super().__init__()
|
| 82 |
+
self.num_heads = num_heads
|
| 83 |
+
self.num_kv_heads = num_kv_heads or num_heads
|
| 84 |
+
self.head_dim = dim // num_heads
|
| 85 |
+
self.num_kv_groups = num_heads // self.num_kv_heads
|
| 86 |
+
|
| 87 |
+
self.wq = nn.Linear(dim, dim, bias=False)
|
| 88 |
+
self.wk = nn.Linear(dim, self.head_dim * self.num_kv_heads, bias=False)
|
| 89 |
+
self.wv = nn.Linear(dim, self.head_dim * self.num_kv_heads, bias=False)
|
| 90 |
+
self.wo = nn.Linear(dim, dim, bias=False)
|
| 91 |
+
self.rotary = RotaryEmbedding(max_seq_len, dim, num_heads, rope_theta)
|
| 92 |
+
|
| 93 |
+
def forward(self, x, start_pos=0, mask=None):
|
|
|
|
|
|
|
| 94 |
bsz, seqlen, _ = x.shape
|
| 95 |
|
| 96 |
q = self.wq(x).view(bsz, seqlen, self.num_heads, self.head_dim).transpose(1, 2)
|
|
|
|
| 114 |
|
| 115 |
attn_weights = F.softmax(attn_weights.float(), dim=-1).type_as(q)
|
| 116 |
attn_output = torch.matmul(attn_weights, v)
|
|
|
|
| 117 |
attn_output = attn_output.transpose(1, 2).contiguous().view(bsz, seqlen, -1)
|
| 118 |
return self.wo(attn_output)
|
| 119 |
|
| 120 |
+
|
| 121 |
class FeedForward(nn.Module):
|
| 122 |
+
def __init__(self, dim, ff_dim, multiple_of):
|
| 123 |
super().__init__()
|
| 124 |
+
hidden_dim = int(2 * ff_dim / 3)
|
| 125 |
+
hidden_dim = multiple_of * ((hidden_dim + multiple_of - 1) // multiple_of)
|
| 126 |
|
| 127 |
+
self.w1 = nn.Linear(dim, hidden_dim, bias=False)
|
| 128 |
+
self.w2 = nn.Linear(hidden_dim, dim, bias=False)
|
| 129 |
+
self.w3 = nn.Linear(dim, hidden_dim, bias=False)
|
| 130 |
|
| 131 |
def forward(self, x):
|
| 132 |
return self.w2(F.silu(self.w1(x)) * self.w3(x))
|
| 133 |
|
| 134 |
+
|
| 135 |
class TransformerBlock(nn.Module):
|
| 136 |
+
def __init__(self, dim, num_heads, num_kv_heads, ff_dim, multiple_of, norm_eps, max_seq_len, rope_theta):
|
| 137 |
super().__init__()
|
| 138 |
+
self.attention = Attention(dim, num_heads, num_kv_heads, max_seq_len, rope_theta)
|
| 139 |
+
self.feed_forward = FeedForward(dim, ff_dim, multiple_of)
|
| 140 |
+
self.attention_norm = RMSNorm(dim, norm_eps)
|
| 141 |
+
self.ff_norm = RMSNorm(dim, norm_eps)
|
| 142 |
|
| 143 |
+
def forward(self, x, start_pos=0, mask=None):
|
| 144 |
h = x + self.attention(self.attention_norm(x), start_pos, mask)
|
| 145 |
out = h + self.feed_forward(self.ff_norm(h))
|
| 146 |
return out
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
class Nexus(nn.Module):
|
| 150 |
+
def __init__(self, config):
|
| 151 |
super().__init__()
|
| 152 |
self.config = config
|
| 153 |
|
| 154 |
self.token_embeddings = nn.Embedding(config.vocab_size, config.dim)
|
| 155 |
+
self.layers = nn.ModuleList([
|
| 156 |
+
TransformerBlock(
|
| 157 |
+
config.dim, config.num_heads, config.num_kv_heads,
|
| 158 |
+
config.ff_dim, config.multiple_of, config.norm_eps,
|
| 159 |
+
config.max_seq_len, config.rope_theta,
|
| 160 |
+
)
|
| 161 |
+
for _ in range(config.num_layers)
|
| 162 |
+
])
|
| 163 |
self.norm = RMSNorm(config.dim, config.norm_eps)
|
| 164 |
self.output = nn.Linear(config.dim, config.vocab_size, bias=False)
|
|
|
|
| 165 |
self.token_embeddings.weight = self.output.weight
|
| 166 |
|
| 167 |
+
def forward(self, input_ids, start_pos=0):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
_, seqlen = input_ids.shape
|
| 169 |
|
| 170 |
+
mask = torch.full(
|
| 171 |
+
(1, 1, seqlen, start_pos + seqlen), float("-inf"),
|
| 172 |
+
dtype=torch.float32, device=input_ids.device,
|
| 173 |
+
)
|
| 174 |
mask = torch.triu(mask, diagonal=start_pos + 1).type_as(input_ids)
|
| 175 |
|
| 176 |
x = self.token_embeddings(input_ids)
|
|
|
|
| 177 |
for layer in self.layers:
|
| 178 |
x = layer(x, start_pos, mask)
|
|
|
|
| 179 |
x = self.norm(x)
|
| 180 |
+
return self.output(x)
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
class NexusForCausalLM(PreTrainedModel):
|
| 184 |
+
config_class = NexusSmAllConfig
|
| 185 |
+
base_model_prefix = "nexus_small"
|
| 186 |
+
supports_gradient_checkpointing = False
|
| 187 |
+
_no_split_modules = ["TransformerBlock"]
|
| 188 |
+
|
| 189 |
+
def __init__(self, config):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
super().__init__(config)
|
| 191 |
+
self.model = Nexus(config)
|
| 192 |
self.lm_head = nn.Linear(config.dim, config.vocab_size, bias=False)
|
| 193 |
with torch.no_grad():
|
| 194 |
self.lm_head.weight = self.model.output.weight
|
| 195 |
|
| 196 |
+
def _init_weights(self, module):
|
| 197 |
+
if isinstance(module, nn.Linear):
|
| 198 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 199 |
+
|
| 200 |
def forward(self, input_ids, attention_mask=None, **kwargs):
|
| 201 |
return self.model(input_ids)
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 204 |
return {"input_ids": input_ids}
|
| 205 |
|
| 206 |
|
|
|
|
| 207 |
AutoModel.register(NexusSmAllConfig, NexusForCausalLM)
|
| 208 |
AutoModelForCausalLM.register(NexusSmAllConfig, NexusForCausalLM)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|