Upload folder using huggingface_hub
Browse files- README.md +28 -3
- model.py +135 -0
- sft_model_epoch_2.pt +3 -0
- tokenizer.json +0 -0
README.md
CHANGED
|
@@ -1,3 +1,28 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: mit
|
| 5 |
+
tags:
|
| 6 |
+
- pytorch
|
| 7 |
+
- causal-lm
|
| 8 |
+
- scratch
|
| 9 |
+
- custom-architecture
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Vayu-ML (94M Parameters)
|
| 13 |
+
|
| 14 |
+
Vayu-ML is a compact, custom decoder-only Transformer pre-trained from scratch on 500M tokens and aligned via Supervised Fine-Tuning (SFT) on ML/DL theoretical concepts.
|
| 15 |
+
|
| 16 |
+
## Model Architecture
|
| 17 |
+
- **Parameters**: ~94 Million
|
| 18 |
+
- **Layers**: 12
|
| 19 |
+
- **Hidden Dimension**: 768
|
| 20 |
+
- **Intermediate Dimension (SwiGLU)**: 2048
|
| 21 |
+
- **Attention Heads**: 12
|
| 22 |
+
- **Normalization**: RMSNorm
|
| 23 |
+
- **Vocabulary Size**: 32,768 (Custom BPE Tokenizer)
|
| 24 |
+
- **Context Length**: 512 tokens
|
| 25 |
+
|
| 26 |
+
## Training Details
|
| 27 |
+
- **Pretraining**: ~500M tokens in native `bfloat16` using AdamW.
|
| 28 |
+
- **Alignment**: SFT on curated ML/DL conceptual Q&A with causal response masking.
|
model.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
|
| 7 |
+
@dataclass
|
| 8 |
+
class ModelConfig:
|
| 9 |
+
vocab_size: int = 32768
|
| 10 |
+
max_seq_len: int = 512
|
| 11 |
+
hidden_dim: int = 768
|
| 12 |
+
intermediate_dim: int = 2048 # SwiGLU dimension
|
| 13 |
+
num_layers: int = 12
|
| 14 |
+
num_heads: int = 12
|
| 15 |
+
eps: float = 1e-5
|
| 16 |
+
|
| 17 |
+
class RMSNorm(nn.Module):
|
| 18 |
+
def __init__(self, dim: int, eps: float = 1e-5):
|
| 19 |
+
super().__init__()
|
| 20 |
+
self.eps = eps
|
| 21 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 22 |
+
|
| 23 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 24 |
+
variance = x.pow(2).mean(-1, keepdim=True)
|
| 25 |
+
return x * torch.rsqrt(variance + self.eps) * self.weight
|
| 26 |
+
|
| 27 |
+
def precompute_rope_cis(dim: int, max_seq_len: int, theta: float = 10000.0) -> torch.Tensor:
|
| 28 |
+
freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim))
|
| 29 |
+
t = torch.arange(max_seq_len, dtype=torch.float32)
|
| 30 |
+
freqs = torch.outer(t, freqs)
|
| 31 |
+
cis = torch.polar(torch.ones_like(freqs), freqs)
|
| 32 |
+
return cis # [max_seq_len, dim // 2]
|
| 33 |
+
|
| 34 |
+
def apply_rope(x: torch.Tensor, cis: torch.Tensor) -> torch.Tensor:
|
| 35 |
+
B, T, H, D = x.shape
|
| 36 |
+
x_complex = torch.view_as_complex(x.float().reshape(B, T, H, -1, 2))
|
| 37 |
+
cis = cis[:T].unsqueeze(0).unsqueeze(2) # Broadcast to [1, T, 1, D // 2]
|
| 38 |
+
x_rotated = torch.view_as_real(x_complex * cis).reshape(B, T, H, D)
|
| 39 |
+
return x_rotated.type_as(x)
|
| 40 |
+
|
| 41 |
+
class CausalSelfAttention(nn.Module):
|
| 42 |
+
def __init__(self, cfg: ModelConfig):
|
| 43 |
+
super().__init__()
|
| 44 |
+
self.num_heads = cfg.num_heads
|
| 45 |
+
self.head_dim = cfg.hidden_dim // cfg.num_heads
|
| 46 |
+
self.q_proj = nn.Linear(cfg.hidden_dim, cfg.hidden_dim, bias=False)
|
| 47 |
+
self.k_proj = nn.Linear(cfg.hidden_dim, cfg.hidden_dim, bias=False)
|
| 48 |
+
self.v_proj = nn.Linear(cfg.hidden_dim, cfg.hidden_dim, bias=False)
|
| 49 |
+
self.out_proj = nn.Linear(cfg.hidden_dim, cfg.hidden_dim, bias=False)
|
| 50 |
+
|
| 51 |
+
def forward(self, x: torch.Tensor, cis: torch.Tensor) -> torch.Tensor:
|
| 52 |
+
B, T, C = x.shape
|
| 53 |
+
q = self.q_proj(x).view(B, T, self.num_heads, self.head_dim)
|
| 54 |
+
k = self.k_proj(x).view(B, T, self.num_heads, self.head_dim)
|
| 55 |
+
v = self.v_proj(x).view(B, T, self.num_heads, self.head_dim)
|
| 56 |
+
|
| 57 |
+
q = apply_rope(q, cis)
|
| 58 |
+
k = apply_rope(k, cis)
|
| 59 |
+
|
| 60 |
+
q = q.transpose(1, 2) # [B, H, T, D]
|
| 61 |
+
k = k.transpose(1, 2)
|
| 62 |
+
v = v.transpose(1, 2)
|
| 63 |
+
|
| 64 |
+
# Dispatches automatically to FlashAttention or Cutlass
|
| 65 |
+
out = F.scaled_dot_product_attention(q, k, v, is_causal=True)
|
| 66 |
+
out = out.transpose(1, 2).contiguous().view(B, T, C)
|
| 67 |
+
return self.out_proj(out)
|
| 68 |
+
|
| 69 |
+
class SwiGLUMLP(nn.Module):
|
| 70 |
+
def __init__(self, cfg: ModelConfig):
|
| 71 |
+
super().__init__()
|
| 72 |
+
self.w1 = nn.Linear(cfg.hidden_dim, cfg.intermediate_dim, bias=False)
|
| 73 |
+
self.w2 = nn.Linear(cfg.hidden_dim, cfg.intermediate_dim, bias=False)
|
| 74 |
+
self.w3 = nn.Linear(cfg.intermediate_dim, cfg.hidden_dim, bias=False)
|
| 75 |
+
|
| 76 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 77 |
+
return self.w3(F.silu(self.w1(x)) * self.w2(x))
|
| 78 |
+
|
| 79 |
+
class TransformerBlock(nn.Module):
|
| 80 |
+
def __init__(self, cfg: ModelConfig):
|
| 81 |
+
super().__init__()
|
| 82 |
+
self.attn_norm = RMSNorm(cfg.hidden_dim, eps=cfg.eps)
|
| 83 |
+
self.attn = CausalSelfAttention(cfg)
|
| 84 |
+
self.mlp_norm = RMSNorm(cfg.hidden_dim, eps=cfg.eps)
|
| 85 |
+
self.mlp = SwiGLUMLP(cfg)
|
| 86 |
+
|
| 87 |
+
def forward(self, x: torch.Tensor, cis: torch.Tensor) -> torch.Tensor:
|
| 88 |
+
x = x + self.attn(self.attn_norm(x), cis)
|
| 89 |
+
x = x + self.mlp(self.mlp_norm(x))
|
| 90 |
+
return x
|
| 91 |
+
|
| 92 |
+
class DecoderLM(nn.Module):
|
| 93 |
+
def __init__(self, cfg: ModelConfig):
|
| 94 |
+
super().__init__()
|
| 95 |
+
self.cfg = cfg
|
| 96 |
+
self.tok_embeddings = nn.Embedding(cfg.vocab_size, cfg.hidden_dim)
|
| 97 |
+
self.layers = nn.ModuleList([TransformerBlock(cfg) for _ in range(cfg.num_layers)])
|
| 98 |
+
self.norm = RMSNorm(cfg.hidden_dim, eps=cfg.eps)
|
| 99 |
+
self.lm_head = nn.Linear(cfg.hidden_dim, cfg.vocab_size, bias=False)
|
| 100 |
+
|
| 101 |
+
# Weight tying
|
| 102 |
+
self.lm_head.weight = self.tok_embeddings.weight
|
| 103 |
+
|
| 104 |
+
# Precompute RoPE table
|
| 105 |
+
rope_cis = precompute_rope_cis(cfg.hidden_dim // cfg.num_heads, cfg.max_seq_len)
|
| 106 |
+
self.register_buffer("rope_cis", rope_cis, persistent=False)
|
| 107 |
+
|
| 108 |
+
self.apply(self._init_weights)
|
| 109 |
+
|
| 110 |
+
def _init_weights(self, module):
|
| 111 |
+
if isinstance(module, nn.Linear):
|
| 112 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 113 |
+
elif isinstance(module, nn.Embedding):
|
| 114 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 115 |
+
|
| 116 |
+
def forward(self, input_ids: torch.Tensor, targets: torch.Tensor | None = None):
|
| 117 |
+
B, T = input_ids.shape
|
| 118 |
+
x = self.tok_embeddings(input_ids)
|
| 119 |
+
cis = self.rope_cis[:T]
|
| 120 |
+
|
| 121 |
+
for layer in self.layers:
|
| 122 |
+
x = layer(x, cis)
|
| 123 |
+
x = self.norm(x)
|
| 124 |
+
logits = self.lm_head(x)
|
| 125 |
+
|
| 126 |
+
loss = None
|
| 127 |
+
if targets is not None:
|
| 128 |
+
loss = F.cross_entropy(logits.view(-1, self.cfg.vocab_size), targets.view(-1))
|
| 129 |
+
return logits, loss
|
| 130 |
+
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
cfg = ModelConfig()
|
| 133 |
+
m = DecoderLM(cfg)
|
| 134 |
+
total_params = sum(p.numel() for p in m.parameters())
|
| 135 |
+
print(f"Architecture loaded: {total_params / 1e6:.2f}M parameters")
|
sft_model_epoch_2.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:39cd8e11fdb16d3c64bfe671f90fce46d4e918dfc06c6c186fa3c957f90d290e
|
| 3 |
+
size 440521771
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|