Upload folder using huggingface_hub
Browse files- config.json +16 -0
- model.safetensors +3 -0
- modeling_hybrid.py +60 -0
- tokenizer.json +0 -0
- tokenizer_config.json +13 -0
config.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"CustomHybridModel"
|
| 4 |
+
],
|
| 5 |
+
"block_size": 128,
|
| 6 |
+
"dtype": "float32",
|
| 7 |
+
"model_type": "techcodex_hybrid_transformer",
|
| 8 |
+
"n_embd": 256,
|
| 9 |
+
"n_head": 4,
|
| 10 |
+
"transformers_version": "5.15.1",
|
| 11 |
+
"vocab_size": 50257,
|
| 12 |
+
"auto_map": {
|
| 13 |
+
"AutoConfig": "modeling_hybrid.TechcodeXConfig",
|
| 14 |
+
"AutoModel": "modeling_hybrid.TechcodeXModel"
|
| 15 |
+
}
|
| 16 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7228ea9c41ccf82b04994c707a689cb8e5df01f9d79538b6989b9c1dd77b1ba3
|
| 3 |
+
size 107999948
|
modeling_hybrid.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torch.nn import functional as F
|
| 4 |
+
from transformers import PretrainedConfig, PreTrainedModel
|
| 5 |
+
|
| 6 |
+
class TechcodeXConfig(PretrainedConfig):
|
| 7 |
+
model_type = "techcodex_hybrid_transformer"
|
| 8 |
+
def __init__(self, vocab_size=50257, n_embd=256, n_head=4, block_size=128, **kwargs):
|
| 9 |
+
super().__init__(**kwargs)
|
| 10 |
+
self.vocab_size = vocab_size
|
| 11 |
+
self.n_embd = n_embd
|
| 12 |
+
self.n_head = n_head
|
| 13 |
+
self.block_size = block_size
|
| 14 |
+
|
| 15 |
+
class ProprietaryRecurrentLayer(nn.Module):
|
| 16 |
+
def __init__(self, n_embd):
|
| 17 |
+
super().__init__()
|
| 18 |
+
self.hidden_dim = n_embd
|
| 19 |
+
self.gate_mix = nn.Linear(n_embd * 2, n_embd)
|
| 20 |
+
self.gate_state = nn.Linear(n_embd * 2, n_embd)
|
| 21 |
+
self.ln = nn.LayerNorm(n_embd)
|
| 22 |
+
|
| 23 |
+
def forward(self, x):
|
| 24 |
+
B, T, C = x.shape
|
| 25 |
+
hidden = torch.zeros(B, self.hidden_dim, device=x.device)
|
| 26 |
+
outputs = []
|
| 27 |
+
for t in range(T):
|
| 28 |
+
current_token = x[:, t, :]
|
| 29 |
+
combined = torch.cat([current_token, hidden], dim=-1)
|
| 30 |
+
mix = torch.sigmoid(self.gate_mix(combined))
|
| 31 |
+
new_state = torch.tanh(self.gate_state(combined))
|
| 32 |
+
hidden = (mix * hidden) + ((1.0 - mix) * new_state)
|
| 33 |
+
outputs.append(hidden.unsqueeze(1))
|
| 34 |
+
return self.ln(torch.cat(outputs, dim=1))
|
| 35 |
+
|
| 36 |
+
class TechcodeXModel(PreTrainedModel):
|
| 37 |
+
config_class = TechcodeXConfig
|
| 38 |
+
def __init__(self, config):
|
| 39 |
+
super().__init__(config)
|
| 40 |
+
self.config = config
|
| 41 |
+
self.token_embedding = nn.Embedding(config.vocab_size, config.n_embd)
|
| 42 |
+
self.position_embedding = nn.Embedding(config.block_size, config.n_embd)
|
| 43 |
+
self.attn_layer = nn.TransformerEncoderLayer(
|
| 44 |
+
d_model=config.n_embd, nhead=config.n_head, dim_feedforward=config.n_embd*4, batch_first=True
|
| 45 |
+
)
|
| 46 |
+
self.recurrent_layer = ProprietaryRecurrentLayer(config.n_embd)
|
| 47 |
+
self.bridge = nn.Linear(config.n_embd * 2, config.n_embd)
|
| 48 |
+
self.ln_final = nn.LayerNorm(config.n_embd)
|
| 49 |
+
self.lm_head = nn.Linear(config.n_embd, config.vocab_size)
|
| 50 |
+
self.post_init()
|
| 51 |
+
|
| 52 |
+
def forward(self, input_ids, labels=None, **kwargs):
|
| 53 |
+
B, T = input_ids.shape
|
| 54 |
+
positions = torch.arange(0, T, device=input_ids.device).unsqueeze(0)
|
| 55 |
+
x = self.token_embedding(input_ids) + self.position_embedding(positions)
|
| 56 |
+
path_a = self.attn_layer(x)
|
| 57 |
+
path_b = self.recurrent_layer(x)
|
| 58 |
+
x = self.bridge(torch.cat([path_a, path_b], dim=-1))
|
| 59 |
+
logits = self.lm_head(self.ln_final(x))
|
| 60 |
+
return {"logits": logits}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"backend": "tokenizers",
|
| 4 |
+
"bos_token": "<|endoftext|>",
|
| 5 |
+
"eos_token": "<|endoftext|>",
|
| 6 |
+
"errors": "replace",
|
| 7 |
+
"is_local": false,
|
| 8 |
+
"local_files_only": false,
|
| 9 |
+
"model_max_length": 1024,
|
| 10 |
+
"pad_token": null,
|
| 11 |
+
"tokenizer_class": "GPT2Tokenizer",
|
| 12 |
+
"unk_token": "<|endoftext|>"
|
| 13 |
+
}
|