Upload 2 files
Browse files- code/model.py +75 -35
- code/train.py +325 -345
code/model.py
CHANGED
|
@@ -3,29 +3,47 @@ import torch
|
|
| 3 |
import torch.nn as nn
|
| 4 |
import torch.nn.functional as F
|
| 5 |
from torch.utils.checkpoint import checkpoint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
class RMSNorm(nn.Module):
|
| 8 |
-
def __init__(self, dim, eps=1e-
|
| 9 |
super().__init__()
|
| 10 |
-
self.
|
| 11 |
self.eps = eps
|
| 12 |
|
| 13 |
-
def forward(self, x):
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
class ResonanceLayerKaggle(nn.Module):
|
| 17 |
-
def __init__(self,
|
| 18 |
super().__init__()
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
self.
|
| 23 |
-
self.
|
| 24 |
-
self.
|
| 25 |
-
|
| 26 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
def forward(self, x):
|
| 29 |
B, T, C = x.shape
|
| 30 |
h = self.norm(x)
|
| 31 |
A = self.to_amp(h).view(B, T, self.n_heads, self.head_dim)
|
|
@@ -33,33 +51,43 @@ class ResonanceLayerKaggle(nn.Module):
|
|
| 33 |
freq = torch.tanh(self.to_freq(h)) * 2.0 # B,T,H
|
| 34 |
phase = torch.tanh(self.to_phase(h)) * math.pi
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
pos = torch.arange(T, device=x.device, dtype=
|
| 38 |
-
chunk = 512 # safe for Kaggle T4
|
| 39 |
out = torch.zeros(B, T, C, device=x.device, dtype=x.dtype)
|
| 40 |
|
| 41 |
-
for i in range(0, T,
|
| 42 |
-
end = min(i+
|
| 43 |
phase_i = phase[:, i:end, :].permute(0,2,1).unsqueeze(-1) # B,H,chunk,1
|
| 44 |
phase_j = phase.permute(0,2,1).unsqueeze(-2) # B,H,1,T
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
freq_i = freq[:, i:end, :].permute(0,2,1).unsqueeze(-1)
|
| 50 |
angle = (phase_i - phase_j) + freq_i * dist * 0.05
|
| 51 |
-
score = torch.cos(angle) / math.sqrt(self.head_dim) # Normalize to prevent explosion
|
| 52 |
|
| 53 |
-
|
| 54 |
-
score =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
amp = A.norm(dim=-1).permute(0,2,1) # B,H,T
|
| 57 |
amp_i = amp[:,:,i:end].unsqueeze(-1)
|
| 58 |
amp_j = amp.unsqueeze(-2)
|
| 59 |
score = score * torch.sigmoid(amp_i * amp_j)
|
| 60 |
|
| 61 |
-
V_t = V.permute(0,2,1,3) # B,H,T,D
|
| 62 |
Bh = B * self.n_heads
|
|
|
|
|
|
|
| 63 |
s = score.reshape(Bh, end-i, T)
|
| 64 |
v = V_t.reshape(Bh, T, self.head_dim)
|
| 65 |
o = torch.bmm(s, v).view(B, self.n_heads, end-i, self.head_dim).permute(0,2,1,3).reshape(B, end-i, C)
|
|
@@ -68,28 +96,40 @@ class ResonanceLayerKaggle(nn.Module):
|
|
| 68 |
return self.to_out(out)
|
| 69 |
|
| 70 |
class ViuResonance100M(nn.Module):
|
| 71 |
-
def __init__(self,
|
| 72 |
super().__init__()
|
| 73 |
-
self.
|
| 74 |
-
self.
|
| 75 |
-
self.
|
| 76 |
-
self.
|
| 77 |
-
self.head
|
| 78 |
-
self.resid_dropout = nn.Dropout(dropout)
|
|
|
|
|
|
|
|
|
|
| 79 |
self.apply(self._init_weights)
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
def _init_weights(self, module):
|
| 82 |
if isinstance(module, nn.Linear):
|
| 83 |
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 84 |
elif isinstance(module, nn.Embedding):
|
| 85 |
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
def forward(self, idx, targets=None):
|
| 88 |
x = self.emb(idx)
|
| 89 |
for layer in self.layers:
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
logits = self.head(self.norm(x))
|
| 92 |
loss = None
|
| 93 |
if targets is not None:
|
|
|
|
| 94 |
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-100)
|
| 95 |
return logits, loss
|
|
|
|
| 3 |
import torch.nn as nn
|
| 4 |
import torch.nn.functional as F
|
| 5 |
from torch.utils.checkpoint import checkpoint
|
| 6 |
+
from dataclasses import dataclass
|
| 7 |
+
|
| 8 |
+
@dataclass
|
| 9 |
+
class ModelConfig:
|
| 10 |
+
"""Configuration class for ViuResonance100M."""
|
| 11 |
+
vocab_size: int = 64000
|
| 12 |
+
d_model: int = 1024
|
| 13 |
+
n_layers: int = 12
|
| 14 |
+
n_heads: int = 16
|
| 15 |
+
dropout: float = 0.1
|
| 16 |
+
chunk_size: int = 512
|
| 17 |
|
| 18 |
class RMSNorm(nn.Module):
|
| 19 |
+
def __init__(self, dim: int, eps: float = 1e-6):
|
| 20 |
super().__init__()
|
| 21 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 22 |
self.eps = eps
|
| 23 |
|
| 24 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 25 |
+
# Enforce float32 accumulation for stability, then cast back to original dtype
|
| 26 |
+
norm_x = x.float() * torch.rsqrt(x.float().pow(2).mean(-1, keepdim=True) + self.eps) * self.weight.float()
|
| 27 |
+
return norm_x.to(x.dtype)
|
| 28 |
|
| 29 |
class ResonanceLayerKaggle(nn.Module):
|
| 30 |
+
def __init__(self, config: ModelConfig):
|
| 31 |
super().__init__()
|
| 32 |
+
if config.d_model % config.n_heads != 0:
|
| 33 |
+
raise ValueError(f"d_model ({config.d_model}) must be divisible by n_heads ({config.n_heads})")
|
| 34 |
+
|
| 35 |
+
self.n_heads = config.n_heads
|
| 36 |
+
self.head_dim = config.d_model // config.n_heads
|
| 37 |
+
self.chunk_size = config.chunk_size
|
| 38 |
+
|
| 39 |
+
self.to_amp = nn.Linear(config.d_model, config.d_model, bias=False)
|
| 40 |
+
self.to_freq = nn.Linear(config.d_model, config.n_heads, bias=False)
|
| 41 |
+
self.to_phase = nn.Linear(config.d_model, config.n_heads, bias=False)
|
| 42 |
+
self.to_v = nn.Linear(config.d_model, config.d_model, bias=False)
|
| 43 |
+
self.to_out = nn.Linear(config.d_model, config.d_model, bias=False)
|
| 44 |
+
self.norm = RMSNorm(config.d_model)
|
| 45 |
|
| 46 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 47 |
B, T, C = x.shape
|
| 48 |
h = self.norm(x)
|
| 49 |
A = self.to_amp(h).view(B, T, self.n_heads, self.head_dim)
|
|
|
|
| 51 |
freq = torch.tanh(self.to_freq(h)) * 2.0 # B,T,H
|
| 52 |
phase = torch.tanh(self.to_phase(h)) * math.pi
|
| 53 |
|
| 54 |
+
# Use torch.long for absolute positions to avoid float precision issues during subtraction
|
| 55 |
+
pos = torch.arange(T, device=x.device, dtype=torch.long)
|
|
|
|
| 56 |
out = torch.zeros(B, T, C, device=x.device, dtype=x.dtype)
|
| 57 |
|
| 58 |
+
for i in range(0, T, self.chunk_size):
|
| 59 |
+
end = min(i+self.chunk_size, T)
|
| 60 |
phase_i = phase[:, i:end, :].permute(0,2,1).unsqueeze(-1) # B,H,chunk,1
|
| 61 |
phase_j = phase.permute(0,2,1).unsqueeze(-2) # B,H,1,T
|
| 62 |
|
| 63 |
+
# Robust distance calculation in torch.long to prevent overflow/dtype issues
|
| 64 |
+
row_idx = torch.arange(i, end, device=x.device, dtype=torch.long)[:, None]
|
| 65 |
+
col_idx = pos[None, :]
|
| 66 |
+
dist_long = (row_idx - col_idx).clamp(min=0)
|
| 67 |
+
dist = dist_long.to(freq.dtype).view(1, 1, end-i, T)
|
| 68 |
|
| 69 |
freq_i = freq[:, i:end, :].permute(0,2,1).unsqueeze(-1)
|
| 70 |
angle = (phase_i - phase_j) + freq_i * dist * 0.05
|
|
|
|
| 71 |
|
| 72 |
+
# Pure Cosine similarity. We apply the sqrt(d) scaling during the gating phase below
|
| 73 |
+
score = torch.cos(angle)
|
| 74 |
+
|
| 75 |
+
# Boolean causal mask for precision and stability
|
| 76 |
+
causal_bool = (row_idx >= col_idx).view(1,1,end-i,T)
|
| 77 |
+
|
| 78 |
+
# Mask out future tokens
|
| 79 |
+
score = score.masked_fill(~causal_bool, 0.0)
|
| 80 |
+
score = score / math.sqrt(self.head_dim)
|
| 81 |
|
| 82 |
amp = A.norm(dim=-1).permute(0,2,1) # B,H,T
|
| 83 |
amp_i = amp[:,:,i:end].unsqueeze(-1)
|
| 84 |
amp_j = amp.unsqueeze(-2)
|
| 85 |
score = score * torch.sigmoid(amp_i * amp_j)
|
| 86 |
|
| 87 |
+
V_t = V.permute(0,2,1,3).contiguous() # B,H,T,D
|
| 88 |
Bh = B * self.n_heads
|
| 89 |
+
assert C == self.n_heads * self.head_dim, f"C ({C}) != n_heads*head_dim ({self.n_heads*self.head_dim})"
|
| 90 |
+
|
| 91 |
s = score.reshape(Bh, end-i, T)
|
| 92 |
v = V_t.reshape(Bh, T, self.head_dim)
|
| 93 |
o = torch.bmm(s, v).view(B, self.n_heads, end-i, self.head_dim).permute(0,2,1,3).reshape(B, end-i, C)
|
|
|
|
| 96 |
return self.to_out(out)
|
| 97 |
|
| 98 |
class ViuResonance100M(nn.Module):
|
| 99 |
+
def __init__(self, config: ModelConfig):
|
| 100 |
super().__init__()
|
| 101 |
+
self.config = config
|
| 102 |
+
self.emb = nn.Embedding(config.vocab_size, config.d_model)
|
| 103 |
+
self.layers = nn.ModuleList([ResonanceLayerKaggle(config) for _ in range(config.n_layers)])
|
| 104 |
+
self.norm = RMSNorm(config.d_model)
|
| 105 |
+
self.head = nn.Linear(config.d_model, config.vocab_size, bias=False)
|
| 106 |
+
self.resid_dropout = nn.Dropout(config.dropout)
|
| 107 |
+
|
| 108 |
+
# Proper initialization sequence to preserve weight tying!
|
| 109 |
+
# First randomly initialize everything...
|
| 110 |
self.apply(self._init_weights)
|
| 111 |
+
# THEN explicitly tie the weights
|
| 112 |
+
self.head.weight = self.emb.weight
|
| 113 |
|
| 114 |
+
def _init_weights(self, module: nn.Module):
|
| 115 |
if isinstance(module, nn.Linear):
|
| 116 |
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 117 |
elif isinstance(module, nn.Embedding):
|
| 118 |
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 119 |
+
elif isinstance(module, RMSNorm):
|
| 120 |
+
torch.nn.init.ones_(module.weight)
|
| 121 |
|
| 122 |
+
def forward(self, idx: torch.Tensor, targets: torch.Tensor = None, use_checkpoint: bool = True) -> tuple[torch.Tensor, torch.Tensor | None]:
|
| 123 |
x = self.emb(idx)
|
| 124 |
for layer in self.layers:
|
| 125 |
+
if self.training and use_checkpoint:
|
| 126 |
+
x = x + self.resid_dropout(checkpoint(layer, x, use_reentrant=False))
|
| 127 |
+
else:
|
| 128 |
+
x = x + self.resid_dropout(layer(x))
|
| 129 |
+
|
| 130 |
logits = self.head(self.norm(x))
|
| 131 |
loss = None
|
| 132 |
if targets is not None:
|
| 133 |
+
# targets with value -100 are automatically ignored in cross_entropy
|
| 134 |
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-100)
|
| 135 |
return logits, loss
|
code/train.py
CHANGED
|
@@ -1,387 +1,367 @@
|
|
| 1 |
import os
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
import json, math, time, random, sys, contextlib
|
| 5 |
-
import numpy as np
|
| 6 |
import torch
|
| 7 |
-
|
| 8 |
-
torch.set_float32_matmul_precision('high')
|
| 9 |
-
torch.backends.cuda.matmul.allow_tf32 = True
|
| 10 |
-
torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = True
|
| 11 |
-
|
| 12 |
import torch.distributed as dist
|
| 13 |
from torch.nn.parallel import DistributedDataParallel as DDP
|
| 14 |
-
from
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
rank = dist.get_rank()
|
| 20 |
-
local_rank = int(os.environ["LOCAL_RANK"])
|
| 21 |
-
world_size = dist.get_world_size()
|
| 22 |
-
torch.cuda.set_device(local_rank)
|
| 23 |
-
return rank, local_rank, world_size
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
print(msg, flush=True)
|
| 32 |
|
| 33 |
def get_hf_token():
|
| 34 |
token = os.environ.get("HF_TOKEN")
|
| 35 |
if token:
|
|
|
|
| 36 |
return token
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
HF_TOKEN
|
| 47 |
-
login(token=HF_TOKEN)
|
| 48 |
-
api = HfApi(token=HF_TOKEN)
|
| 49 |
-
|
| 50 |
-
from transformers import AutoTokenizer
|
| 51 |
-
|
| 52 |
-
DATA_REPO = "ViuAI/viuai-500m-data"
|
| 53 |
-
MODEL_REPO = "ViuAI/ViuRec"
|
| 54 |
-
TOKENIZER_SUBFOLDER = "tokenizer"
|
| 55 |
-
CODE_SUBFOLDER = "code"
|
| 56 |
-
CKPT_SUBFOLDER = "checkpoints"
|
| 57 |
-
CKPT_REPO = MODEL_REPO
|
| 58 |
|
| 59 |
-
def
|
| 60 |
-
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
try:
|
| 75 |
-
|
| 76 |
-
if IS_MAIN:
|
| 77 |
-
print(f"✅ Tokenizer loaded from HF ({MODEL_REPO}/{TOKENIZER_SUBFOLDER})")
|
| 78 |
-
return tok
|
| 79 |
except Exception as e:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
if IS_MAIN:
|
| 88 |
-
create_repo(CKPT_REPO, repo_type="model", token=HF_TOKEN, exist_ok=True, private=True)
|
| 89 |
-
dist.barrier()
|
| 90 |
-
|
| 91 |
-
if os.path.exists("/kaggle/working"):
|
| 92 |
-
WORK = "/kaggle/working"
|
| 93 |
-
elif os.path.exists("/root/working"):
|
| 94 |
-
WORK = "/root/working"
|
| 95 |
-
else:
|
| 96 |
-
WORK = "./working"
|
| 97 |
-
os.makedirs(WORK, exist_ok=True)
|
| 98 |
-
|
| 99 |
-
sys.path.insert(0, WORK)
|
| 100 |
-
sys.path.insert(0, os.path.join(WORK, CODE_SUBFOLDER))
|
| 101 |
-
hf_hub_download(MODEL_REPO, f"{CODE_SUBFOLDER}/model.py", local_dir=WORK, token=HF_TOKEN)
|
| 102 |
-
from model import ViuResonance100M
|
| 103 |
|
| 104 |
-
def
|
| 105 |
-
|
|
|
|
| 106 |
try:
|
| 107 |
-
|
| 108 |
except Exception as e:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
class ShardPool:
|
| 118 |
-
def __init__(self,
|
| 119 |
-
self.
|
| 120 |
-
self.
|
| 121 |
-
self.
|
| 122 |
-
self.
|
| 123 |
-
self.
|
| 124 |
-
|
| 125 |
self.pool = []
|
| 126 |
-
self.
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
self.
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
continue
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
try: os.remove(old_path)
|
| 152 |
-
except OSError: pass
|
| 153 |
-
self.fill()
|
| 154 |
-
|
| 155 |
-
def sample_batch(self, batch_size, ctx_len):
|
| 156 |
xs, ys = [], []
|
| 157 |
while len(xs) < batch_size:
|
| 158 |
-
|
|
|
|
| 159 |
max_start = len(tokens) - ctx_len - 1
|
| 160 |
if max_start <= 0:
|
| 161 |
continue
|
| 162 |
-
i =
|
| 163 |
xs.append(np.array(tokens[i:i+ctx_len], dtype=np.int64))
|
| 164 |
ys.append(np.array(tokens[i+1:i+1+ctx_len], dtype=np.int64))
|
| 165 |
return np.stack(xs), np.stack(ys)
|
| 166 |
-
|
| 167 |
-
def load_val_tokens(val_names, repo_id, token, local_dir):
|
| 168 |
-
os.makedirs(local_dir, exist_ok=True)
|
| 169 |
-
arrays = []
|
| 170 |
-
for name in val_names:
|
| 171 |
-
path = with_retry(hf_hub_download, repo_id, name, repo_type="dataset", local_dir=local_dir, token=token)
|
| 172 |
-
arrays.append(np.memmap(path, dtype=np.uint16, mode="r"))
|
| 173 |
-
return np.concatenate(arrays)
|
| 174 |
-
|
| 175 |
-
def get_batch(source, batch_size, ctx_len):
|
| 176 |
-
if isinstance(source, ShardPool):
|
| 177 |
-
x_np, y_np = source.sample_batch(batch_size, ctx_len)
|
| 178 |
-
else:
|
| 179 |
-
i = np.random.randint(0, len(source) - ctx_len - 1, size=batch_size)
|
| 180 |
-
x_np = np.stack([np.array(source[j:j+ctx_len], dtype=np.int64) for j in i])
|
| 181 |
-
y_np = np.stack([np.array(source[j+1:j+1+ctx_len], dtype=np.int64) for j in i])
|
| 182 |
-
x = torch.from_numpy(x_np).to(DEVICE, non_blocking=True)
|
| 183 |
-
y = torch.from_numpy(y_np).to(DEVICE, non_blocking=True)
|
| 184 |
-
return x, y
|
| 185 |
-
|
| 186 |
-
def extract_clean_state_dict(model):
|
| 187 |
-
raw_model = model.module if hasattr(model, "module") else model
|
| 188 |
-
state_dict = raw_model.state_dict()
|
| 189 |
-
return {k.replace("_orig_mod.", ""): v for k, v in state_dict.items()}
|
| 190 |
-
|
| 191 |
-
def save_checkpoint(model, optimizer, step, val_loss, filename):
|
| 192 |
-
if not IS_MAIN:
|
| 193 |
-
return
|
| 194 |
-
path = f"{WORK}/{filename}"
|
| 195 |
-
state_dict = extract_clean_state_dict(model)
|
| 196 |
-
data = {"model": state_dict, "step": step, "val_loss": val_loss}
|
| 197 |
-
|
| 198 |
-
if filename == "ckpt_latest.pt" and optimizer is not None:
|
| 199 |
-
data["optimizer"] = optimizer.state_dict()
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
def load_checkpoint(model, optimizer):
|
| 207 |
-
try:
|
| 208 |
try:
|
| 209 |
-
|
| 210 |
-
|
| 211 |
except Exception as e:
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
CONTEXT_LEN = 2048
|
| 253 |
-
MAX_LR = 3e-4
|
| 254 |
-
MIN_LR = 3e-5
|
| 255 |
-
WARMUP_STEPS = 1000
|
| 256 |
-
TOTAL_STEPS = 40_000
|
| 257 |
-
WEIGHT_DECAY = 0.1
|
| 258 |
-
GRAD_CLIP = 1.0
|
| 259 |
-
MAX_STEPS = int(os.environ.get("MAX_STEPS", TOTAL_STEPS))
|
| 260 |
-
EVAL_EVERY = 500
|
| 261 |
-
SAVE_EVERY = 2000
|
| 262 |
-
ROTATE_EVERY = 500
|
| 263 |
-
|
| 264 |
-
torch.backends.cudnn.benchmark = False
|
| 265 |
-
|
| 266 |
-
# Model 100M init
|
| 267 |
-
model = ViuResonance100M(vocab_size=VOCAB_SIZE, d_model=1024, n_layers=12, n_heads=16).to(DEVICE)
|
| 268 |
-
|
| 269 |
-
if IS_MAIN:
|
| 270 |
-
log(f"Model params: {sum(p.numel() for p in model.parameters())/1e6:.1f}M | world_size={world_size}")
|
| 271 |
-
log("Bypassing torch.compile (Disabled for Kaggle T4 compatibility)...")
|
| 272 |
|
| 273 |
-
# torch.compile
|
| 274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
dist.broadcast(start_step_t, src=0)
|
| 281 |
-
start_step = start_step_t.item()
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
val_shards = sorted(f for f in files if f.startswith("shards/") and f.endswith("_val.bin"))
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
if
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
model.train()
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
for step in range(start_step, end_step):
|
| 315 |
-
lr = get_lr(step, WARMUP_STEPS, TOTAL_STEPS, MAX_LR, MIN_LR)
|
| 316 |
-
for g in optimizer.param_groups:
|
| 317 |
-
g["lr"] = lr
|
| 318 |
-
|
| 319 |
-
optimizer.zero_grad(set_to_none=True)
|
| 320 |
-
accum_loss = 0.0
|
| 321 |
-
for micro_i in range(GRAD_ACCUM):
|
| 322 |
-
x, y = get_batch(shard_pool, MICRO_BATCH, CONTEXT_LEN)
|
| 323 |
-
with torch.autocast(device_type="cuda", dtype=torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16):
|
| 324 |
-
_, loss = model(x, y)
|
| 325 |
-
loss = loss / GRAD_ACCUM
|
| 326 |
-
loss.backward()
|
| 327 |
-
accum_loss += loss.item()
|
| 328 |
-
del x, y, loss
|
| 329 |
-
|
| 330 |
-
torch.nn.utils.clip_grad_norm_(model.parameters(), GRAD_CLIP)
|
| 331 |
|
| 332 |
-
if not math.isfinite(accum_loss):
|
| 333 |
-
if IS_MAIN:
|
| 334 |
-
log(f"⚠️ NaN/Inf loss at step {step}! Skipping optimizer step.")
|
| 335 |
optimizer.zero_grad(set_to_none=True)
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
# Setup env vars for robust Kaggle training
|
| 3 |
+
os.environ["NCCL_P2P_DISABLE"] = "1"
|
| 4 |
+
os.environ["NCCL_IB_DISABLE"] = "1"
|
| 5 |
+
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
| 6 |
+
os.environ["HF_HUB_DISABLE_PROGRESS_BARS"] = "1"
|
| 7 |
+
|
| 8 |
+
import math
|
| 9 |
+
import time
|
| 10 |
+
import argparse
|
| 11 |
+
import logging
|
| 12 |
+
from pathlib import Path
|
| 13 |
|
|
|
|
|
|
|
| 14 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
import torch.distributed as dist
|
| 16 |
from torch.nn.parallel import DistributedDataParallel as DDP
|
| 17 |
+
from torch.cuda.amp import GradScaler
|
| 18 |
+
import numpy as np
|
| 19 |
+
from transformers import AutoTokenizer
|
| 20 |
+
from huggingface_hub import hf_hub_download, HfApi
|
| 21 |
+
from huggingface_hub.utils import EntryNotFoundError, HfHubHTTPError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
try:
|
| 24 |
+
from kaggle_secrets import UserSecretsClient
|
| 25 |
+
HAS_KAGGLE_SECRETS = True
|
| 26 |
+
except ImportError:
|
| 27 |
+
HAS_KAGGLE_SECRETS = False
|
| 28 |
|
| 29 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 30 |
+
logger = logging.getLogger(__name__)
|
|
|
|
| 31 |
|
| 32 |
def get_hf_token():
|
| 33 |
token = os.environ.get("HF_TOKEN")
|
| 34 |
if token:
|
| 35 |
+
logger.info("Loaded HF_TOKEN from environment.")
|
| 36 |
return token
|
| 37 |
+
if HAS_KAGGLE_SECRETS:
|
| 38 |
+
try:
|
| 39 |
+
token = UserSecretsClient().get_secret("HF_TOKEN")
|
| 40 |
+
if token:
|
| 41 |
+
os.environ["HF_TOKEN"] = token
|
| 42 |
+
logger.info("Loaded HF_TOKEN from kaggle_secrets.")
|
| 43 |
+
return token
|
| 44 |
+
except Exception as e:
|
| 45 |
+
logger.warning(f"Failed to get HF_TOKEN from kaggle_secrets: {e}")
|
| 46 |
+
raise ValueError("HF_TOKEN not found in environment or kaggle_secrets.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
def setup_ddp():
|
| 49 |
+
if "RANK" in os.environ and "WORLD_SIZE" in os.environ:
|
| 50 |
+
dist.init_process_group("nccl")
|
| 51 |
+
rank = int(os.environ["RANK"])
|
| 52 |
+
local_rank = int(os.environ["LOCAL_RANK"])
|
| 53 |
+
world_size = int(os.environ["WORLD_SIZE"])
|
| 54 |
+
is_ddp = True
|
| 55 |
+
else:
|
| 56 |
+
rank = 0
|
| 57 |
+
local_rank = 0
|
| 58 |
+
world_size = 1
|
| 59 |
+
is_ddp = False
|
| 60 |
+
device = torch.device(f"cuda:{local_rank}")
|
| 61 |
+
torch.cuda.set_device(device)
|
| 62 |
+
return rank, local_rank, world_size, device, is_ddp
|
| 63 |
+
|
| 64 |
+
def load_tokenizer_robust(token: str):
|
| 65 |
+
logger.info("Loading tokenizer...")
|
| 66 |
try:
|
| 67 |
+
return AutoTokenizer.from_pretrained("ViuAI/ViuRec", token=token, use_fast=True)
|
|
|
|
|
|
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
+
logger.warning(f"Failed to load fast tokenizer: {e}. Trying use_fast=False.")
|
| 70 |
+
try:
|
| 71 |
+
return AutoTokenizer.from_pretrained("ViuAI/ViuRec", token=token, use_fast=False)
|
| 72 |
+
except Exception as e2:
|
| 73 |
+
logger.error(f"Failed to load tokenizer: {e2}")
|
| 74 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
def load_model_code(token: str):
|
| 77 |
+
if not Path("model.py").exists():
|
| 78 |
+
logger.info("model.py not found locally. Downloading from HuggingFace...")
|
| 79 |
try:
|
| 80 |
+
hf_hub_download(repo_id="ViuAI/ViuRec", filename="code/model.py", token=token, local_dir=".")
|
| 81 |
except Exception as e:
|
| 82 |
+
logger.error(f"Failed to download model.py: {e}")
|
| 83 |
+
raise
|
| 84 |
+
|
| 85 |
+
def load_checkpoint(model, optimizer, scaler, api, token, repo_id, filename="checkpoints/latest_chk.pt", strict=False):
|
| 86 |
+
local_path = "latest_chk.pt"
|
| 87 |
+
try:
|
| 88 |
+
logger.info(f"Checking for existing checkpoint on HF: {filename}")
|
| 89 |
+
hf_hub_download(repo_id=repo_id, filename=filename, local_dir=".", token=token)
|
| 90 |
+
# Security Note: weights_only=False is used here to allow loading the optimizer state safely inside our trusted environment.
|
| 91 |
+
ckpt = torch.load(local_path, map_location="cpu", weights_only=False)
|
| 92 |
+
missing, unexpected = model.load_state_dict(ckpt["model"], strict=strict)
|
| 93 |
+
if missing or unexpected:
|
| 94 |
+
logger.warning(f"Checkpoint loaded with Missing keys: {missing} | Unexpected keys: {unexpected}")
|
| 95 |
+
else:
|
| 96 |
+
logger.info("Model weights loaded successfully.")
|
| 97 |
+
|
| 98 |
+
optimizer.load_state_dict(ckpt["optimizer"])
|
| 99 |
+
if "scaler" in ckpt and scaler is not None:
|
| 100 |
+
scaler.load_state_dict(ckpt["scaler"])
|
| 101 |
+
|
| 102 |
+
step = ckpt.get("step", 0)
|
| 103 |
+
logger.info(f"Resuming from step {step}")
|
| 104 |
+
Path(local_path).unlink(missing_ok=True)
|
| 105 |
+
return step
|
| 106 |
+
except (EntryNotFoundError, HfHubHTTPError):
|
| 107 |
+
logger.info("No checkpoint found on HF. Starting from scratch.")
|
| 108 |
+
return 0
|
| 109 |
+
except Exception as e:
|
| 110 |
+
logger.error(f"Error loading checkpoint: {e}")
|
| 111 |
+
return 0
|
| 112 |
|
| 113 |
class ShardPool:
|
| 114 |
+
def __init__(self, api, repo_id, work_dir, token, vocab_size, max_pool=3):
|
| 115 |
+
self.api = api
|
| 116 |
+
self.repo_id = repo_id
|
| 117 |
+
self.work_dir = Path(work_dir)
|
| 118 |
+
self.work_dir.mkdir(parents=True, exist_ok=True)
|
| 119 |
+
self.token = token
|
| 120 |
+
self.max_pool = max_pool
|
| 121 |
self.pool = []
|
| 122 |
+
self.available_files = []
|
| 123 |
+
self.vocab_size = vocab_size
|
| 124 |
+
self._refresh_file_list()
|
| 125 |
+
|
| 126 |
+
def _refresh_file_list(self):
|
| 127 |
+
files = self.api.list_repo_files(repo_id=self.repo_id, repo_type="dataset", token=self.token)
|
| 128 |
+
self.available_files = [f for f in files if f.startswith("shards/hi_indiccorp_") and f.endswith(".bin")]
|
| 129 |
+
np.random.shuffle(self.available_files)
|
| 130 |
+
|
| 131 |
+
def fill(self, ctx_len):
|
| 132 |
+
max_attempts = self.max_pool * 5
|
| 133 |
+
attempts = 0
|
| 134 |
+
while len(self.pool) < self.max_pool:
|
| 135 |
+
if attempts >= max_attempts:
|
| 136 |
+
raise RuntimeError("Failed to fill ShardPool: too many invalid shards.")
|
| 137 |
+
if not self.available_files:
|
| 138 |
+
self._refresh_file_list()
|
| 139 |
+
f = self.available_files.pop()
|
| 140 |
+
local_p = hf_hub_download(repo_id=self.repo_id, repo_type="dataset", filename=f, local_dir=self.work_dir, token=self.token)
|
| 141 |
+
|
| 142 |
+
# Safe dtype selection
|
| 143 |
+
dtype = np.uint16 if self.vocab_size <= 65535 else np.uint32
|
| 144 |
+
mmap_arr = np.memmap(local_p, dtype=dtype, mode="r")
|
| 145 |
+
|
| 146 |
+
if len(mmap_arr) < ctx_len + 1:
|
| 147 |
+
logger.warning(f"Shard {f} is too small ({len(mmap_arr)} tokens). Discarding.")
|
| 148 |
+
del mmap_arr
|
| 149 |
+
Path(local_p).unlink(missing_ok=True)
|
| 150 |
+
attempts += 1
|
| 151 |
continue
|
| 152 |
+
|
| 153 |
+
self.pool.append((local_p, mmap_arr))
|
| 154 |
+
logger.info(f"Loaded shard {f}")
|
| 155 |
+
|
| 156 |
+
def sample_batch(self, batch_size, ctx_len, rng):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
xs, ys = [], []
|
| 158 |
while len(xs) < batch_size:
|
| 159 |
+
idx = rng.integers(0, len(self.pool))
|
| 160 |
+
_, tokens = self.pool[idx]
|
| 161 |
max_start = len(tokens) - ctx_len - 1
|
| 162 |
if max_start <= 0:
|
| 163 |
continue
|
| 164 |
+
i = rng.integers(0, max_start)
|
| 165 |
xs.append(np.array(tokens[i:i+ctx_len], dtype=np.int64))
|
| 166 |
ys.append(np.array(tokens[i+1:i+1+ctx_len], dtype=np.int64))
|
| 167 |
return np.stack(xs), np.stack(ys)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
def rotate_one(self):
|
| 170 |
+
if not self.pool: return
|
| 171 |
+
old_path, old_mmap = self.pool.pop(0)
|
| 172 |
+
del old_mmap
|
|
|
|
|
|
|
|
|
|
| 173 |
try:
|
| 174 |
+
Path(old_path).unlink(missing_ok=True)
|
| 175 |
+
logger.info(f"Rotated out shard {Path(old_path).name}")
|
| 176 |
except Exception as e:
|
| 177 |
+
logger.warning(f"Failed to delete {old_path}: {e}")
|
| 178 |
+
|
| 179 |
+
def main():
|
| 180 |
+
parser = argparse.ArgumentParser()
|
| 181 |
+
parser.add_argument("--max-steps", type=int, default=100)
|
| 182 |
+
parser.add_argument("--micro-batch", type=int, default=2)
|
| 183 |
+
parser.add_argument("--grad-accum", type=int, default=16)
|
| 184 |
+
parser.add_argument("--eval-every", type=int, default=500)
|
| 185 |
+
parser.add_argument("--save-every", type=int, default=500)
|
| 186 |
+
parser.add_argument("--rotate-every", type=int, default=500)
|
| 187 |
+
parser.add_argument("--lr", type=float, default=3e-4)
|
| 188 |
+
parser.add_argument("--weight-decay", type=float, default=0.1)
|
| 189 |
+
parser.add_argument("--ctx-len", type=int, default=2048)
|
| 190 |
+
args = parser.parse_args()
|
| 191 |
+
|
| 192 |
+
token = get_hf_token()
|
| 193 |
+
rank, local_rank, world_size, device, is_ddp = setup_ddp()
|
| 194 |
+
is_main = (rank == 0)
|
| 195 |
+
|
| 196 |
+
# Random seed
|
| 197 |
+
seed = 42 + rank
|
| 198 |
+
torch.manual_seed(seed)
|
| 199 |
+
np.random.seed(seed)
|
| 200 |
+
rng = np.random.default_rng(seed)
|
| 201 |
+
|
| 202 |
+
# Load resources
|
| 203 |
+
tokenizer = load_tokenizer_robust(token)
|
| 204 |
+
vocab_size = len(tokenizer)
|
| 205 |
+
load_model_code(token)
|
| 206 |
+
|
| 207 |
+
from model import ViuResonance100M, ModelConfig
|
| 208 |
+
config = ModelConfig(vocab_size=vocab_size)
|
| 209 |
+
model = ViuResonance100M(config).to(device)
|
| 210 |
+
|
| 211 |
+
if is_ddp:
|
| 212 |
+
model = DDP(model, device_ids=[local_rank], output_device=local_rank, broadcast_buffers=False)
|
| 213 |
+
unwrapped_model = model.module
|
| 214 |
+
else:
|
| 215 |
+
unwrapped_model = model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
+
# Optional torch.compile fallback
|
| 218 |
+
try:
|
| 219 |
+
model = torch.compile(model)
|
| 220 |
+
if is_main: logger.info("Compiled model successfully.")
|
| 221 |
+
except Exception as e:
|
| 222 |
+
if is_main: logger.warning(f"Failed to compile model: {e}. Falling back to eager mode.")
|
| 223 |
|
| 224 |
+
# Optimizer
|
| 225 |
+
try:
|
| 226 |
+
optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr, weight_decay=args.weight_decay, betas=(0.9, 0.95), fused=True)
|
| 227 |
+
except:
|
| 228 |
+
if is_main: logger.warning("fused=True failed for AdamW. Falling back to non-fused.")
|
| 229 |
+
optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr, weight_decay=args.weight_decay, betas=(0.9, 0.95))
|
| 230 |
|
| 231 |
+
# GradScaler for safe fp16/bf16
|
| 232 |
+
scaler = GradScaler(enabled=not torch.cuda.is_bf16_supported())
|
|
|
|
|
|
|
| 233 |
|
| 234 |
+
api = HfApi()
|
| 235 |
+
start_step = load_checkpoint(unwrapped_model, optimizer, scaler, api, token, "ViuAI/ViuRec", strict=False)
|
|
|
|
| 236 |
|
| 237 |
+
work_dir = Path("/kaggle/working" if Path("/kaggle/working").exists() else ".")
|
| 238 |
+
shard_pool = ShardPool(api, "ViuAI/ViuCorp", work_dir / "shards", token, vocab_size)
|
| 239 |
+
shard_pool.fill(args.ctx_len)
|
| 240 |
|
| 241 |
+
# Val Data Safely (cap at 20M tokens)
|
| 242 |
+
val_tokens = None
|
| 243 |
+
if is_main:
|
| 244 |
+
logger.info("Loading validation data...")
|
| 245 |
+
try:
|
| 246 |
+
val_files = [f for f in api.list_repo_files(repo_id="ViuAI/ViuCorp", repo_type="dataset", token=token) if f.startswith("val_shards/")]
|
| 247 |
+
if val_files:
|
| 248 |
+
val_p = hf_hub_download(repo_id="ViuAI/ViuCorp", repo_type="dataset", filename=val_files[0], local_dir=work_dir, token=token)
|
| 249 |
+
dtype = np.uint16 if vocab_size <= 65535 else np.uint32
|
| 250 |
+
val_arr = np.memmap(val_p, dtype=dtype, mode="r")
|
| 251 |
+
max_toks = 20_000_000
|
| 252 |
+
val_tokens = np.array(val_arr[:max_toks]) # Copy to RAM, safely capped
|
| 253 |
+
del val_arr
|
| 254 |
+
Path(val_p).unlink(missing_ok=True)
|
| 255 |
+
except Exception as e:
|
| 256 |
+
logger.warning(f"Failed to load val data: {e}")
|
| 257 |
+
|
| 258 |
+
@torch.no_grad()
|
| 259 |
+
def evaluate():
|
| 260 |
+
if val_tokens is None or len(val_tokens) < args.ctx_len + 1: return 0.0
|
| 261 |
+
unwrapped_model.eval()
|
| 262 |
+
val_loss = 0.0
|
| 263 |
+
val_iters = 20
|
| 264 |
+
for _ in range(val_iters):
|
| 265 |
+
i = rng.integers(0, len(val_tokens) - args.ctx_len - 1)
|
| 266 |
+
x = torch.from_numpy(val_tokens[i:i+args.ctx_len].astype(np.int64)).unsqueeze(0).to(device, non_blocking=True)
|
| 267 |
+
y = torch.from_numpy(val_tokens[i+1:i+1+args.ctx_len].astype(np.int64)).unsqueeze(0).to(device, non_blocking=True)
|
| 268 |
+
with torch.autocast(device_type="cuda", dtype=torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16):
|
| 269 |
+
# Pass use_checkpoint=False for evaluation
|
| 270 |
+
_, loss = unwrapped_model(x, y, use_checkpoint=False)
|
| 271 |
+
val_loss += loss.item()
|
| 272 |
+
unwrapped_model.train()
|
| 273 |
+
return val_loss / val_iters
|
| 274 |
+
|
| 275 |
+
# Training Loop
|
| 276 |
model.train()
|
| 277 |
+
if is_main:
|
| 278 |
+
logger.info(f"Starting training for {args.max_steps} steps...")
|
| 279 |
+
|
| 280 |
+
for step in range(start_step, args.max_steps):
|
| 281 |
+
# LR Schedule
|
| 282 |
+
warmup = 1000
|
| 283 |
+
progress = min(1.0, step / max(1, warmup))
|
| 284 |
+
lr = args.lr * progress if step < warmup else args.lr * (0.5 * (1.0 + math.cos(math.pi * (step - warmup) / max(1, args.max_steps - warmup))))
|
| 285 |
+
for param_group in optimizer.param_groups:
|
| 286 |
+
param_group['lr'] = lr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
|
|
|
|
|
|
|
|
|
| 288 |
optimizer.zero_grad(set_to_none=True)
|
| 289 |
+
accum_loss = 0.0
|
| 290 |
+
|
| 291 |
+
t0 = time.time()
|
| 292 |
+
for micro_i in range(args.grad_accum):
|
| 293 |
+
x_np, y_np = shard_pool.sample_batch(args.micro_batch, args.ctx_len, rng)
|
| 294 |
+
x = torch.from_numpy(x_np).to(device, non_blocking=True)
|
| 295 |
+
y = torch.from_numpy(y_np).to(device, non_blocking=True)
|
| 296 |
+
|
| 297 |
+
with torch.autocast(device_type="cuda", dtype=torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16):
|
| 298 |
+
_, loss = model(x, y)
|
| 299 |
+
loss = loss / args.grad_accum
|
| 300 |
+
|
| 301 |
+
scaler.scale(loss).backward()
|
| 302 |
+
accum_loss += loss.item()
|
| 303 |
+
del x, y, loss
|
| 304 |
+
|
| 305 |
+
# Safe Gradient Clipping with Scaler
|
| 306 |
+
scaler.unscale_(optimizer)
|
| 307 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
|
| 308 |
+
|
| 309 |
+
# Check for NaNs
|
| 310 |
+
has_nan = not math.isfinite(accum_loss)
|
| 311 |
+
for p in model.parameters():
|
| 312 |
+
if p.grad is not None and not torch.isfinite(p.grad).all():
|
| 313 |
+
has_nan = True
|
| 314 |
+
break
|
| 315 |
+
|
| 316 |
+
if has_nan:
|
| 317 |
+
if is_main: logger.warning(f"NaN/Inf detected at step {step}! Skipping optimizer step.")
|
| 318 |
+
optimizer.zero_grad(set_to_none=True)
|
| 319 |
+
continue
|
| 320 |
+
|
| 321 |
+
scaler.step(optimizer)
|
| 322 |
+
scaler.update()
|
| 323 |
+
|
| 324 |
+
dt = time.time() - t0
|
| 325 |
+
|
| 326 |
+
if is_main and step % 10 == 0:
|
| 327 |
+
tok_s = (args.micro_batch * args.grad_accum * args.ctx_len * world_size) / dt if dt > 0 else 0
|
| 328 |
+
mem_alloc = torch.cuda.memory_allocated(device) / 1e9
|
| 329 |
+
mem_res = torch.cuda.memory_reserved(device) / 1e9
|
| 330 |
+
logger.info(f"step {step:5d}/{args.max_steps} | loss {accum_loss:.4f} | lr {lr:.2e} | {tok_s:.0f} tok/s | mem alloc={mem_alloc:.2f}GB res={mem_res:.2f}GB")
|
| 331 |
+
|
| 332 |
+
if step > start_step and step % args.rotate_every == 0:
|
| 333 |
+
shard_pool.rotate_one()
|
| 334 |
+
shard_pool.fill(args.ctx_len)
|
| 335 |
+
|
| 336 |
+
if step > start_step and step % args.eval_every == 0 and is_main:
|
| 337 |
+
val_loss = evaluate()
|
| 338 |
+
logger.info(f"--- Eval step {step}: val_loss {val_loss:.4f} ---")
|
| 339 |
+
|
| 340 |
+
if step > start_step and step % args.save_every == 0:
|
| 341 |
+
if is_ddp: dist.barrier()
|
| 342 |
+
if is_main:
|
| 343 |
+
logger.info(f"Saving checkpoint at step {step}...")
|
| 344 |
+
chk = {
|
| 345 |
+
"model": unwrapped_model.state_dict(),
|
| 346 |
+
"optimizer": optimizer.state_dict(),
|
| 347 |
+
"scaler": scaler.state_dict(),
|
| 348 |
+
"step": step,
|
| 349 |
+
}
|
| 350 |
+
torch.save(chk, "latest_chk.pt")
|
| 351 |
+
try:
|
| 352 |
+
api.upload_file(
|
| 353 |
+
path_or_fileobj="latest_chk.pt",
|
| 354 |
+
path_in_repo="checkpoints/latest_chk.pt",
|
| 355 |
+
repo_id="ViuAI/ViuRec",
|
| 356 |
+
token=token
|
| 357 |
+
)
|
| 358 |
+
logger.info("Upload successful.")
|
| 359 |
+
except Exception as e:
|
| 360 |
+
logger.error(f"Upload failed: {e}")
|
| 361 |
+
if is_ddp: dist.barrier()
|
| 362 |
+
|
| 363 |
+
if is_ddp:
|
| 364 |
+
dist.destroy_process_group()
|
| 365 |
+
|
| 366 |
+
if __name__ == "__main__":
|
| 367 |
+
main()
|