Upload folder using huggingface_hub
Browse files- checkpoint.pt +3 -0
- model.py +121 -0
checkpoint.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6eaec05a3d5c700662793e0d6e951050debd2a8bd44b9d24da8acb0afbf7402e
|
| 3 |
+
size 194273765
|
model.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
CSE 251B — Example model.py
|
| 3 |
+
|
| 4 |
+
This is a minimal example showing the required interface for your submission.
|
| 5 |
+
Your model.py must contain a load_model() function with this signature.
|
| 6 |
+
|
| 7 |
+
This example implements a tiny 2-layer Transformer (~1M params) for demonstration.
|
| 8 |
+
It will have terrible perplexity — it's just to show the required structure.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
import torch.nn as nn
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# --- Your model definition (replace with your architecture) ---
|
| 16 |
+
|
| 17 |
+
class TinyGPT(nn.Module):
|
| 18 |
+
"""A minimal GPT for demonstration purposes."""
|
| 19 |
+
|
| 20 |
+
def __init__(self, vocab_size=50257, n_embd=128, n_head=4, n_layer=6, block_size=1024):
|
| 21 |
+
super().__init__()
|
| 22 |
+
self.block_size = block_size
|
| 23 |
+
self.token_emb = nn.Embedding(vocab_size, n_embd)
|
| 24 |
+
self.pos_emb = nn.Embedding(block_size, n_embd)
|
| 25 |
+
self.blocks = nn.ModuleList([
|
| 26 |
+
nn.TransformerEncoderLayer(
|
| 27 |
+
d_model=n_embd, nhead=n_head,
|
| 28 |
+
dim_feedforward=4 * n_embd, dropout=0.1,
|
| 29 |
+
activation="gelu", batch_first=True,
|
| 30 |
+
)
|
| 31 |
+
for _ in range(n_layer)
|
| 32 |
+
])
|
| 33 |
+
self.ln_f = nn.LayerNorm(n_embd)
|
| 34 |
+
self.lm_head = nn.Linear(n_embd, vocab_size, bias=False)
|
| 35 |
+
|
| 36 |
+
def forward(self, input_ids):
|
| 37 |
+
"""
|
| 38 |
+
Args:
|
| 39 |
+
input_ids: LongTensor of shape (batch_size, seq_len)
|
| 40 |
+
Returns:
|
| 41 |
+
logits: FloatTensor of shape (batch_size, seq_len, 50257)
|
| 42 |
+
"""
|
| 43 |
+
B, T = input_ids.shape
|
| 44 |
+
tok_emb = self.token_emb(input_ids)
|
| 45 |
+
pos_emb = self.pos_emb(torch.arange(T, device=input_ids.device))
|
| 46 |
+
x = tok_emb + pos_emb
|
| 47 |
+
|
| 48 |
+
# Causal mask
|
| 49 |
+
mask = torch.triu(torch.ones(T, T, device=input_ids.device), diagonal=1).bool()
|
| 50 |
+
|
| 51 |
+
for block in self.blocks:
|
| 52 |
+
x = block(x, src_mask=mask, is_causal=True)
|
| 53 |
+
|
| 54 |
+
x = self.ln_f(x)
|
| 55 |
+
logits = self.lm_head(x)
|
| 56 |
+
return logits
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# --- Required: load_model function ---
|
| 60 |
+
|
| 61 |
+
def load_model(checkpoint_path: str, device: str = "cuda") -> torch.nn.Module:
|
| 62 |
+
"""
|
| 63 |
+
Load your trained model from a checkpoint.
|
| 64 |
+
|
| 65 |
+
This function is called by evaluate.py. It must return a model where:
|
| 66 |
+
model(input_ids) -> logits
|
| 67 |
+
- input_ids: LongTensor of shape (batch, seq_len)
|
| 68 |
+
- logits: FloatTensor of shape (batch, seq_len, 50257)
|
| 69 |
+
|
| 70 |
+
Args:
|
| 71 |
+
checkpoint_path: Path to your checkpoint.pt file
|
| 72 |
+
device: Device to load onto ("cuda" or "cpu")
|
| 73 |
+
|
| 74 |
+
Returns:
|
| 75 |
+
model: nn.Module in eval mode
|
| 76 |
+
"""
|
| 77 |
+
# Load checkpoint
|
| 78 |
+
checkpoint = torch.load(checkpoint_path, map_location=device, weights_only=True)
|
| 79 |
+
|
| 80 |
+
# Supports two formats:
|
| 81 |
+
# 1) raw state_dict
|
| 82 |
+
# 2) {"model_state_dict": ..., "config": {...}}
|
| 83 |
+
if isinstance(checkpoint, dict) and "model_state_dict" in checkpoint:
|
| 84 |
+
state_dict = checkpoint["model_state_dict"]
|
| 85 |
+
config = checkpoint.get("config", {})
|
| 86 |
+
model = TinyGPT(**config)
|
| 87 |
+
else:
|
| 88 |
+
state_dict = checkpoint
|
| 89 |
+
model = TinyGPT()
|
| 90 |
+
|
| 91 |
+
unwanted_prefix = '_orig_mod.'
|
| 92 |
+
for k, v in list(state_dict.items()):
|
| 93 |
+
if k.startswith(unwanted_prefix):
|
| 94 |
+
state_dict[k[len(unwanted_prefix):]] = state_dict.pop(k)
|
| 95 |
+
|
| 96 |
+
model.load_state_dict(state_dict)
|
| 97 |
+
|
| 98 |
+
model.to(device)
|
| 99 |
+
model.eval()
|
| 100 |
+
return model
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# --- Optional: quick sanity check ---
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
print("Creating example model...")
|
| 107 |
+
model = TinyGPT()
|
| 108 |
+
n_params = sum(p.numel() for p in model.parameters())
|
| 109 |
+
print(f"Parameters: {n_params:,}")
|
| 110 |
+
|
| 111 |
+
# Test forward pass
|
| 112 |
+
dummy_input = torch.randint(0, 50257, (2, 1024))
|
| 113 |
+
logits = model(dummy_input)
|
| 114 |
+
print(f"Input shape: {dummy_input.shape}")
|
| 115 |
+
print(f"Output shape: {logits.shape}")
|
| 116 |
+
assert logits.shape == (2, 1024, 50257), "Output shape mismatch!"
|
| 117 |
+
print("Interface check passed.")
|
| 118 |
+
|
| 119 |
+
# Save example checkpoint
|
| 120 |
+
torch.save(model.state_dict(), "checkpoint.pt")
|
| 121 |
+
print("Saved example checkpoint.pt")
|