File size: 8,280 Bytes
05c5c96 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | """
LLM Distillation with GGUF Teacher (Correct Tokenizer + Stable)
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import AdamW
from torch.utils.data import DataLoader, Dataset
from transformers import AutoTokenizer, get_cosine_schedule_with_warmup
import logging
from pathlib import Path
from llama_cpp import Llama
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# ============================================================================
# GGUF TEACHER
# ============================================================================
class GGUFTeacher:
def __init__(self, model_path, n_ctx=512, n_gpu_layers=20, n_threads=6):
self.model = Llama(
model_path=model_path,
n_ctx=n_ctx,
logits_all=True,
n_gpu_layers=n_gpu_layers,
n_threads=n_threads,
verbose=False,
)
self.cache = {}
def get_logits(self, input_ids):
logits_batch = []
for seq in input_ids:
tokens = tuple(seq.tolist())
if tokens in self.cache:
logits = self.cache[tokens]
else:
try:
self.model.reset()
self.model.eval(tokens)
logits = torch.tensor(self.model._scores, dtype=torch.float32)
# Safety: ensure shape matches sequence
if logits.shape[0] != len(tokens):
logits = logits[:len(tokens)]
self.cache[tokens] = logits
except Exception as e:
print("⚠️ GGUF error, skipping sequence:", e)
logits = torch.zeros(len(tokens), self.model.n_vocab())
logits_batch.append(logits)
return torch.stack(logits_batch)
# ============================================================================
# CONFIG
# ============================================================================
class DistillationConfig:
def __init__(self):
self.teacher_gguf_path = "/home/pragadeesh/model/mistral-7b-instruct-v0.2.Q2_K.gguf"
self.student_hidden_dim = 512
self.student_num_layers = 8
self.student_num_heads = 8
self.batch_size = 2
self.gradient_accumulation_steps = 4
self.learning_rate = 5e-4
self.max_steps = 1000
self.warmup_steps = 100
self.temperature = 4.0
self.max_seq_length = 128
self.log_interval = 10
# ============================================================================
# DATASET
# ============================================================================
class TextDataset(Dataset):
def __init__(self, texts, tokenizer, max_length=128):
self.texts = texts
self.tokenizer = tokenizer
self.max_length = max_length
def __len__(self):
return len(self.texts)
def __getitem__(self, idx):
enc = self.tokenizer(
self.texts[idx],
padding="max_length",
truncation=True,
max_length=self.max_length,
return_tensors="pt",
add_special_tokens=True
)
return {
"input_ids": enc["input_ids"].squeeze()
}
# ============================================================================
# STUDENT MODEL
# ============================================================================
class StudentModel(nn.Module):
def __init__(self, config, vocab_size):
super().__init__()
self.embedding = nn.Embedding(vocab_size, config.student_hidden_dim)
self.pos_embedding = nn.Embedding(config.max_seq_length, config.student_hidden_dim)
self.blocks = nn.ModuleList([
nn.TransformerEncoderLayer(
d_model=config.student_hidden_dim,
nhead=config.student_num_heads,
dim_feedforward=config.student_hidden_dim * 4,
batch_first=True
)
for _ in range(config.student_num_layers)
])
self.lm_head = nn.Linear(config.student_hidden_dim, vocab_size)
def forward(self, input_ids):
x = self.embedding(input_ids)
pos = torch.arange(input_ids.shape[1], device=input_ids.device).unsqueeze(0)
x = x + self.pos_embedding(pos)
for block in self.blocks:
x = block(x)
return self.lm_head(x)
# ============================================================================
# LOSS
# ============================================================================
class DistillationLoss(nn.Module):
def __init__(self, temperature=4.0):
super().__init__()
self.temperature = temperature
self.kl = nn.KLDivLoss(reduction="batchmean")
def forward(self, student_logits, teacher_logits):
s = F.log_softmax(student_logits / self.temperature, dim=-1)
t = F.softmax(teacher_logits / self.temperature, dim=-1)
return self.kl(s, t)
# ============================================================================
# TRAINER
# ============================================================================
class Trainer:
def __init__(self, config, device):
self.config = config
self.device = device
logger.info("Loading Mistral tokenizer...")
self.tokenizer = AutoTokenizer.from_pretrained(
"mistralai/Mistral-7B-Instruct-v0.2"
)
# Fix padding
self.tokenizer.pad_token = self.tokenizer.eos_token
logger.info("Loading GGUF teacher...")
self.teacher = GGUFTeacher(config.teacher_gguf_path)
logger.info("Creating student...")
self.student = StudentModel(
config,
self.tokenizer.vocab_size
).to(device)
self.optimizer = AdamW(self.student.parameters(), lr=config.learning_rate)
self.scheduler = get_cosine_schedule_with_warmup(
self.optimizer,
config.warmup_steps,
config.max_steps
)
self.criterion = DistillationLoss(config.temperature)
self.step = 0
def train_step(self, batch):
input_ids = batch["input_ids"].to(self.device)
student_logits = self.student(input_ids)
with torch.no_grad():
teacher_logits = self.teacher.get_logits(input_ids).to(self.device)
# Match sequence length (safety)
min_len = min(student_logits.shape[1], teacher_logits.shape[1])
student_logits = student_logits[:, :min_len, :]
teacher_logits = teacher_logits[:, :min_len, :]
loss = self.criterion(student_logits, teacher_logits)
loss.backward()
if self.step % self.config.gradient_accumulation_steps == 0:
torch.nn.utils.clip_grad_norm_(self.student.parameters(), 1.0)
self.optimizer.step()
self.scheduler.step()
self.optimizer.zero_grad()
self.step += 1
return loss.item()
def train(self, dataloader):
self.student.train()
while self.step < self.config.max_steps:
for batch in dataloader:
loss = self.train_step(batch)
if self.step % self.config.log_interval == 0:
logger.info(f"Step {self.step} | Loss: {loss:.4f}")
if self.step >= self.config.max_steps:
break
Path("checkpoints").mkdir(exist_ok=True)
torch.save(self.student.state_dict(), "checkpoints/student.pt")
logger.info("Training complete!")
# ============================================================================
# MAIN
# ============================================================================
def main():
config = DistillationConfig()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
trainer = Trainer(config, device)
texts = ["AI is transforming the world." * 10 for _ in range(200)]
dataset = TextDataset(texts, trainer.tokenizer, config.max_seq_length)
dataloader = DataLoader(dataset, batch_size=config.batch_size, shuffle=True)
trainer.train(dataloader)
if __name__ == "__main__":
main()
|