Upload modeling_nexus_small.py with huggingface_hub
Browse files- modeling_nexus_small.py +12 -38
modeling_nexus_small.py
CHANGED
|
@@ -4,42 +4,12 @@ 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,
|
| 8 |
|
| 9 |
try:
|
| 10 |
-
from config import NexusConfig
|
| 11 |
except ImportError:
|
| 12 |
-
from .config import NexusConfig
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
class NexusConfigHF(PretrainedConfig):
|
| 16 |
-
model_type = "nexus_small"
|
| 17 |
-
|
| 18 |
-
def __init__(
|
| 19 |
-
self,
|
| 20 |
-
vocab_size=50304,
|
| 21 |
-
max_seq_len=512,
|
| 22 |
-
dim=768,
|
| 23 |
-
num_layers=10,
|
| 24 |
-
num_heads=12,
|
| 25 |
-
num_kv_heads=4,
|
| 26 |
-
multiple_of=256,
|
| 27 |
-
ff_dim=2048,
|
| 28 |
-
norm_eps=1e-6,
|
| 29 |
-
rope_theta=500000.0,
|
| 30 |
-
**kwargs,
|
| 31 |
-
):
|
| 32 |
-
super().__init__(**kwargs)
|
| 33 |
-
self.vocab_size = vocab_size
|
| 34 |
-
self.max_seq_len = max_seq_len
|
| 35 |
-
self.dim = dim
|
| 36 |
-
self.num_layers = num_layers
|
| 37 |
-
self.num_heads = num_heads
|
| 38 |
-
self.num_kv_heads = num_kv_heads
|
| 39 |
-
self.multiple_of = multiple_of
|
| 40 |
-
self.ff_dim = ff_dim
|
| 41 |
-
self.norm_eps = norm_eps
|
| 42 |
-
self.rope_theta = rope_theta
|
| 43 |
|
| 44 |
class RMSNorm(nn.Module):
|
| 45 |
def __init__(self, dim: int, eps: float = 1e-6):
|
|
@@ -148,12 +118,12 @@ class TransformerBlock(nn.Module):
|
|
| 148 |
return out
|
| 149 |
|
| 150 |
class NexusPreTrainedModel(PreTrainedModel):
|
| 151 |
-
config_class =
|
| 152 |
base_model_prefix = "nexus_small"
|
| 153 |
supports_gradient_checkpointing = False
|
| 154 |
_no_split_modules = ["TransformerBlock"]
|
| 155 |
|
| 156 |
-
def __init__(self, config:
|
| 157 |
super().__init__(config)
|
| 158 |
self._nexus_config = NexusConfig(
|
| 159 |
vocab_size=config.vocab_size,
|
|
@@ -248,9 +218,12 @@ class Nexus(nn.Module):
|
|
| 248 |
return generated, input_ids
|
| 249 |
|
| 250 |
class NexusForCausalLM(NexusPreTrainedModel):
|
| 251 |
-
def __init__(self, config:
|
| 252 |
super().__init__(config)
|
| 253 |
self.model = Nexus(self._nexus_config)
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
def forward(self, input_ids, attention_mask=None, **kwargs):
|
| 256 |
return self.model(input_ids)
|
|
@@ -266,8 +239,9 @@ class NexusForCausalLM(NexusPreTrainedModel):
|
|
| 266 |
return {"input_ids": input_ids}
|
| 267 |
|
| 268 |
|
| 269 |
-
AutoConfig.register("nexus_small",
|
| 270 |
-
AutoModel.register(
|
|
|
|
| 271 |
|
| 272 |
|
| 273 |
def create_nexus_model():
|
|
|
|
| 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 config import NexusConfig, NexusSmAllConfig
|
| 11 |
except ImportError:
|
| 12 |
+
from .config import NexusConfig, NexusSmAllConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
class RMSNorm(nn.Module):
|
| 15 |
def __init__(self, dim: int, eps: float = 1e-6):
|
|
|
|
| 118 |
return out
|
| 119 |
|
| 120 |
class NexusPreTrainedModel(PreTrainedModel):
|
| 121 |
+
config_class = NexusSmAllConfig
|
| 122 |
base_model_prefix = "nexus_small"
|
| 123 |
supports_gradient_checkpointing = False
|
| 124 |
_no_split_modules = ["TransformerBlock"]
|
| 125 |
|
| 126 |
+
def __init__(self, config: NexusSmAllConfig):
|
| 127 |
super().__init__(config)
|
| 128 |
self._nexus_config = NexusConfig(
|
| 129 |
vocab_size=config.vocab_size,
|
|
|
|
| 218 |
return generated, input_ids
|
| 219 |
|
| 220 |
class NexusForCausalLM(NexusPreTrainedModel):
|
| 221 |
+
def __init__(self, config: NexusSmAllConfig):
|
| 222 |
super().__init__(config)
|
| 223 |
self.model = Nexus(self._nexus_config)
|
| 224 |
+
self.lm_head = nn.Linear(config.dim, config.vocab_size, bias=False)
|
| 225 |
+
with torch.no_grad():
|
| 226 |
+
self.lm_head.weight = self.model.output.weight
|
| 227 |
|
| 228 |
def forward(self, input_ids, attention_mask=None, **kwargs):
|
| 229 |
return self.model(input_ids)
|
|
|
|
| 239 |
return {"input_ids": input_ids}
|
| 240 |
|
| 241 |
|
| 242 |
+
AutoConfig.register("nexus_small", NexusSmAllConfig)
|
| 243 |
+
AutoModel.register(NexusSmAllConfig, NexusForCausalLM)
|
| 244 |
+
AutoModelForCausalLM.register(NexusSmAllConfig, NexusForCausalLM)
|
| 245 |
|
| 246 |
|
| 247 |
def create_nexus_model():
|