-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_loader.py
More file actions
452 lines (362 loc) · 15.7 KB
/
model_loader.py
File metadata and controls
452 lines (362 loc) · 15.7 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
"""
Dynamic model loader for any architecture
Supports:
- Classification: config.json + head.safetensors + embeddings.safetensors
- LLM Full: config.json + model.safetensors + tokenizer/
"""
import json
from pathlib import Path
from typing import Dict, Any, Optional, List
import numpy as np
import torch
import torch.nn as nn
from safetensors.torch import load_file as load_safetensors
from config import MODELS_CACHE
# ═══════════════════════════════════════════════════════════════════════════════
# Dynamic Head for Classification
# ═══════════════════════════════════════════════════════════════════════════════
class DynamicHead(nn.Module):
"""
Dynamic head module that builds architecture from config
Supports: Linear, Conv2d, BatchNorm, ReLU, Dropout, etc.
"""
SUPPORTED_LAYERS = {
"Linear": nn.Linear,
"Conv1d": nn.Conv1d,
"Conv2d": nn.Conv2d,
"BatchNorm1d": nn.BatchNorm1d,
"BatchNorm2d": nn.BatchNorm2d,
"LayerNorm": nn.LayerNorm,
"ReLU": nn.ReLU,
"GELU": nn.GELU,
"SiLU": nn.SiLU,
"Tanh": nn.Tanh,
"Sigmoid": nn.Sigmoid,
"Softmax": nn.Softmax,
"Dropout": nn.Dropout,
"Dropout2d": nn.Dropout2d,
"MaxPool1d": nn.MaxPool1d,
"MaxPool2d": nn.MaxPool2d,
"AvgPool1d": nn.AvgPool1d,
"AvgPool2d": nn.AvgPool2d,
"AdaptiveAvgPool1d": nn.AdaptiveAvgPool1d,
"AdaptiveAvgPool2d": nn.AdaptiveAvgPool2d,
"Flatten": nn.Flatten,
"Embedding": nn.Embedding,
"LSTM": nn.LSTM,
"GRU": nn.GRU,
"MultiheadAttention": nn.MultiheadAttention,
"TransformerEncoderLayer": nn.TransformerEncoderLayer,
}
def __init__(self, config: Dict[str, Any]):
super().__init__()
self.config = config
self.layers = nn.ModuleList()
self._build_from_config(config)
def _build_from_config(self, config: Dict[str, Any]):
"""Build network from config"""
layers_config = config.get("layers", [])
for layer_cfg in layers_config:
layer_type = layer_cfg.get("type")
params = layer_cfg.get("params", {})
if layer_type not in self.SUPPORTED_LAYERS:
raise ValueError(f"Unsupported layer type: {layer_type}")
layer_cls = self.SUPPORTED_LAYERS[layer_type]
if layer_type in ["ReLU", "GELU", "SiLU", "Tanh", "Sigmoid", "Flatten"]:
layer = layer_cls(**params) if params else layer_cls()
else:
layer = layer_cls(**params)
self.layers.append(layer)
def forward(self, x: torch.Tensor) -> torch.Tensor:
for layer in self.layers:
x = layer(x)
return x
# ═══════════════════════════════════════════════════════════════════════════════
# Model Package (Classification)
# ═══════════════════════════════════════════════════════════════════════════════
class ModelPackage:
"""
Loaded model package with head and embeddings (Classification format)
"""
def __init__(
self,
config: Dict[str, Any],
head: nn.Module,
embeddings: Optional[torch.Tensor] = None,
):
self.config = config
self.head = head
self.embeddings = embeddings
self.device = "cpu"
self.is_llm = False
def to(self, device: str) -> "ModelPackage":
self.device = device
self.head = self.head.to(device)
if self.embeddings is not None:
self.embeddings = self.embeddings.to(device)
return self
def eval(self) -> "ModelPackage":
self.head.eval()
return self
@torch.no_grad()
def predict(self, embeddings: torch.Tensor) -> torch.Tensor:
if embeddings.device != self.device:
embeddings = embeddings.to(self.device)
return self.head(embeddings)
@torch.no_grad()
def predict_batch(
self,
embeddings: np.ndarray,
batch_size: int = 32,
) -> np.ndarray:
self.head.eval()
results = []
for i in range(0, len(embeddings), batch_size):
batch = torch.tensor(embeddings[i:i+batch_size], dtype=torch.float32)
batch = batch.to(self.device)
output = self.head(batch)
results.append(output.cpu().numpy())
return np.concatenate(results, axis=0)
# ═══════════════════════════════════════════════════════════════════════════════
# LLM Model Package (Full LLM)
# ═══════════════════════════════════════════════════════════════════════════════
class LLMModelPackage:
"""
Loaded LLM model package with full model and tokenizer
"""
def __init__(
self,
config: Dict[str, Any],
model: nn.Module,
tokenizer: Any,
):
self.config = config
self.model = model
self.tokenizer = tokenizer
self.device = "cpu"
self.is_llm = True
# Get dataset info
self.dataset_name = config.get("dataset", {}).get("name", "unknown")
self.num_classes = config.get("dataset", {}).get("num_classes", 2)
def to(self, device: str) -> "LLMModelPackage":
self.device = device
self.model = self.model.to(device)
return self
def eval(self) -> "LLMModelPackage":
self.model.eval()
return self
@torch.no_grad()
def generate(self, texts: List[str], max_new_tokens: int = 50) -> List[str]:
"""Generate text completions"""
self.model.eval()
results = []
for text in texts:
inputs = self.tokenizer(
text,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512,
).to(self.device)
outputs = self.model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=False,
pad_token_id=self.tokenizer.pad_token_id,
)
generated = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
results.append(generated)
return results
@torch.no_grad()
def compute_perplexity(self, texts: List[str], batch_size: int = 4) -> float:
"""Compute perplexity on texts"""
self.model.eval()
total_loss = 0.0
total_tokens = 0
for i in range(0, len(texts), batch_size):
batch_texts = texts[i:i+batch_size]
inputs = self.tokenizer(
batch_texts,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512,
).to(self.device)
outputs = self.model(**inputs, labels=inputs["input_ids"])
# Loss is averaged over tokens, multiply by sequence length
loss = outputs.loss.item()
num_tokens = inputs["attention_mask"].sum().item()
total_loss += loss * num_tokens
total_tokens += num_tokens
avg_loss = total_loss / total_tokens
perplexity = torch.exp(torch.tensor(avg_loss)).item()
return perplexity
@torch.no_grad()
def classify_multiple_choice(
self,
questions: List[str],
choices_list: List[List[str]],
batch_size: int = 4,
) -> np.ndarray:
"""
Classify multiple choice questions.
Returns index of predicted answer for each question.
"""
self.model.eval()
predictions = []
for question, choices in zip(questions, choices_list):
choice_losses = []
for choice in choices:
prompt = f"{question} {choice}"
inputs = self.tokenizer(
prompt,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512,
).to(self.device)
outputs = self.model(**inputs, labels=inputs["input_ids"])
choice_losses.append(outputs.loss.item())
# Predict the choice with lowest loss
pred_idx = np.argmin(choice_losses)
predictions.append(pred_idx)
return np.array(predictions)
@torch.no_grad()
def predict_next_token_probs(
self,
texts: List[str],
target_tokens: List[str],
) -> np.ndarray:
"""
Get probability of target tokens being the next token.
Used for classification tasks.
"""
self.model.eval()
probs = []
for text, target in zip(texts, target_tokens):
inputs = self.tokenizer(
text,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512,
).to(self.device)
outputs = self.model(**inputs)
logits = outputs.logits[:, -1, :] # Last token logits
# Get probability of target token
target_ids = self.tokenizer.encode(target, add_special_tokens=False)
if target_ids:
target_id = target_ids[0]
prob = torch.softmax(logits, dim=-1)[0, target_id].item()
else:
prob = 0.0
probs.append(prob)
return np.array(probs)
# ═══════════════════════════════════════════════════════════════════════════════
# Model Loader
# ═══════════════════════════════════════════════════════════════════════════════
class ModelLoader:
"""
Loads model packages from local files or IPFS
Supports both classification (embeddings + head) and LLM (full model) formats
"""
def __init__(self, cache_dir: Path = MODELS_CACHE):
self.cache_dir = cache_dir
self.cache_dir.mkdir(parents=True, exist_ok=True)
def load_from_directory(self, path: Path) -> ModelPackage | LLMModelPackage:
"""Load model from local directory. Auto-detects format."""
config_path = path / "config.json"
if not config_path.exists():
raise FileNotFoundError(f"config.json not found in {path}")
with open(config_path, "r") as f:
config = json.load(f)
# Check package type
pkg_type = config.get("type", "classification")
if pkg_type == "llm_full":
return self._load_llm_package(path, config)
else:
return self._load_classification_package(path, config)
def _load_classification_package(self, path: Path, config: Dict[str, Any]) -> ModelPackage:
"""Load classification model (embeddings + head)"""
head_path = path / "head.safetensors"
embeddings_path = path / "embeddings.safetensors"
head_config = config.get("head", config)
head = DynamicHead(head_config)
if head_path.exists():
state_dict = load_safetensors(str(head_path))
self._load_weights_to_head(head, state_dict)
embeddings = None
if embeddings_path.exists():
embeddings_dict = load_safetensors(str(embeddings_path))
if len(embeddings_dict) == 1:
embeddings = list(embeddings_dict.values())[0]
else:
embeddings = embeddings_dict.get("embeddings")
return ModelPackage(config, head, embeddings)
def _load_llm_package(self, path: Path, config: Dict[str, Any]) -> LLMModelPackage:
"""Load full LLM model"""
model_path = path / "model.safetensors"
tokenizer_path = path / "tokenizer"
if not model_path.exists():
raise FileNotFoundError(f"model.safetensors not found in {path}")
if not tokenizer_path.exists():
raise FileNotFoundError(f"tokenizer/ not found in {path}")
# Get model architecture from config
model_info = config.get("model", {})
model_source = model_info.get("source", "gpt2")
# Load model architecture from HuggingFace
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
# Load config to create empty model
hf_config = AutoConfig.from_pretrained(model_source, trust_remote_code=True)
# Create model with config
model = AutoModelForCausalLM.from_config(hf_config)
# Load weights from safetensors
state_dict = load_safetensors(str(model_path))
# Convert fp16 weights back to model dtype
for key in state_dict:
if state_dict[key].dtype == torch.float16:
state_dict[key] = state_dict[key].float()
model.load_state_dict(state_dict, strict=False)
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(str(tokenizer_path), trust_remote_code=True)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
return LLMModelPackage(config, model, tokenizer)
def _load_weights_to_head(self, head: DynamicHead, state_dict: Dict[str, torch.Tensor]):
"""Load weights into dynamic head"""
try:
head.load_state_dict(state_dict, strict=False)
return
except Exception:
pass
# Manual mapping for common patterns
for name, tensor in state_dict.items():
for i, layer in enumerate(head.layers):
if hasattr(layer, "weight"):
layer_state = layer.state_dict()
if "weight" in layer_state:
if layer_state["weight"].shape == tensor.shape:
layer.weight.data = tensor
if "bias" in state_dict.get(name.replace("weight", "bias"), {}):
layer.bias.data = state_dict[name.replace("weight", "bias")]
break
def load_from_cache(self, cid: str) -> Optional[ModelPackage | LLMModelPackage]:
"""Load model from cache by CID"""
cache_path = self.cache_dir / cid
if cache_path.exists():
return self.load_from_directory(cache_path)
return None
def cache_model(self, cid: str, data: Dict[str, bytes]) -> Path:
"""Cache model data"""
cache_path = self.cache_dir / cid
cache_path.mkdir(parents=True, exist_ok=True)
for filename, content in data.items():
file_path = cache_path / filename
with open(file_path, "wb") as f:
f.write(content)
return cache_path
def get_cache_path(self, cid: str) -> Path:
"""Get cache path for CID"""
return self.cache_dir / cid
# Global loader instance
model_loader = ModelLoader()