-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
397 lines (330 loc) · 13.4 KB
/
Copy pathmodels.py
File metadata and controls
397 lines (330 loc) · 13.4 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
import os
import glob
import numpy as np
from abc import ABC, abstractmethod
from typing import Dict, Type
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset
from .logging_config import get_logger
logger = get_logger(__name__)
def _make_padding_mask(x: torch.Tensor) -> torch.Tensor:
"""Detect all-zero (padding) positions BEFORE positional embedding.
Args:
x: (B, L, D) raw chunk embeddings from the base model.
Returns:
(B, L) boolean mask — ``True`` for padded (all-zero) positions.
"""
return x.abs().sum(dim=-1) == 0 # (B, L)
def _masked_mean_pool(x: torch.Tensor, padding_mask: torch.Tensor) -> torch.Tensor:
"""Mean-pool over non-padding positions only.
This fixes a consistency issue where ``collate_fn`` zero-pads shorter
chunk sequences, but ``x.mean(dim=1)`` was averaging over all
positions (including padding zeros), diluting the representation.
Single-sample inference (query pipeline) had no padding and thus
produced different pooled values for the same sample.
Args:
x: (B, L, D) encoded sequence (post-encoder).
padding_mask: (B, L) boolean — ``True`` for padded positions.
Returns:
(B, D) mean-pooled tensor.
"""
real = (~padding_mask).float().unsqueeze(-1) # (B, L, 1)
lengths = real.sum(dim=1).clamp(min=1) # (B, 1)
return (x * real).sum(dim=1) / lengths # (B, D)
# Model Registry
class ModelRegistry:
"""Registry for model classes with factory pattern."""
_models: Dict[str, Type["BaseModel"]] = {}
@classmethod
def register(cls, name: str):
"""Decorator to register a model class."""
def decorator(model_class: Type["BaseModel"]):
cls._models[name] = model_class
return model_class
return decorator
@classmethod
def create_model(cls, name: str, **kwargs) -> "BaseModel":
"""Factory method to create a model instance."""
if name not in cls._models:
available = ", ".join(cls._models.keys())
raise ValueError(
f"Model '{name}' not registered. Available models: {available}"
)
return cls._models[name](**kwargs)
@classmethod
def get_model_class(cls, name: str) -> Type["BaseModel"]:
"""Get the model class by name."""
if name not in cls._models:
available = ", ".join(cls._models.keys())
raise ValueError(
f"Model '{name}' not registered. Available models: {available}"
)
return cls._models[name]
class DatasetRegistry:
"""Registry for dataset classes with factory pattern."""
_datasets: Dict[str, Type[Dataset]] = {}
@classmethod
def register(cls, name: str):
"""Decorator to register a dataset class."""
def decorator(dataset_class: Type[Dataset]):
cls._datasets[name] = dataset_class
return dataset_class
return decorator
@classmethod
def create_dataset(cls, name: str, **kwargs) -> Dataset:
"""Factory method to create a dataset instance."""
if name not in cls._datasets:
available = ", ".join(cls._datasets.keys())
raise ValueError(
f"Dataset '{name}' not registered. Available datasets: {available}"
)
return cls._datasets[name](**kwargs)
@classmethod
def get_dataset_class(cls, name: str) -> Type[Dataset]:
"""Get the dataset class by name."""
if name not in cls._datasets:
available = ", ".join(cls._datasets.keys())
raise ValueError(
f"Dataset '{name}' not registered. Available datasets: {available}"
)
return cls._datasets[name]
# Convenience decorator
def register_model(name: str):
"""Convenience decorator for registering models."""
return ModelRegistry.register(name)
# TODO merge with model registry
def register_dataset(name: str):
"""Convenience decorator for registering datasets."""
return DatasetRegistry.register(name)
# Abstract Base Model
class BaseModel(nn.Module, ABC):
"""Abstract base class for all models."""
def __init__(self, **kwargs):
super().__init__()
@abstractmethod
def forward(self, x):
"""Forward pass of the model."""
pass
def get_model_info(self) -> dict:
"""Get model information including parameter count."""
total_params = sum(p.numel() for p in self.parameters())
trainable_params = sum(p.numel() for p in self.parameters() if p.requires_grad)
return {
"model_class": self.__class__.__name__,
"total_parameters": total_params,
"trainable_parameters": trainable_params,
}
@classmethod
def from_config(cls, config: dict):
"""Create model instance from configuration dictionary."""
return cls(**config)
@register_model("contrastive_head")
class ContrastiveHead(BaseModel):
def __init__(self, input_dim, output_dim=128, **kwargs):
super().__init__(**kwargs)
self.net = nn.Sequential(
nn.Linear(input_dim, 256), nn.ReLU(), nn.Linear(256, output_dim)
)
def forward(self, x):
return self.net(x)
@register_dataset("contrastive_chunked")
class ContrastiveChunkedDataset(Dataset):
def __init__(
self,
embeddings_dir,
sample_ids,
serotype_labels,
capsule_labels,
serotype_known=None,
):
"""
embeddings_path: str, path to directory with npy entries of variable length chunked embeddings
serotype_labels: pd.DataFrame, DataFrame with serotype labels indexed by sample ID.
serotype_known: optional array-like of bool, True if the serotype is resolved (not serogroup/compound).
"""
self.embedding_dir = embeddings_dir
self.serotypes = serotype_labels
self.is_capsule = capsule_labels
self.sample_ids = sample_ids
self.serotype_known = (
serotype_known if serotype_known is not None else [True] * len(sample_ids)
)
# TODO Validate sub-folders too
all_embeddings = glob.glob(os.path.join(embeddings_dir, "**/*.npy"))
file_names = [os.path.basename(f).split(".")[0] for f in all_embeddings]
missing_samples = set(self.sample_ids) - set(file_names)
if missing_samples:
logger.warning(
"%d sample_ids do not have a corresponding embedding file: %s",
len(missing_samples),
missing_samples,
)
def __len__(self) -> int:
return len(self.sample_ids)
def __getitem__(self, idx):
subdir = "cbl" if self.is_capsule[idx] else "non-cbl"
embedding_path = os.path.join(
self.embedding_dir, subdir, f"{self.sample_ids[idx]}.npy"
)
return {
"sample_id": self.sample_ids[idx],
"embedding": torch.tensor(np.load(embedding_path), dtype=torch.float32),
"serotype": self.serotypes[idx],
"is_capsule": self.is_capsule[idx],
"serotype_known": self.serotype_known[idx],
}
@register_dataset("multidomain_chunked")
class MultidomainChunkedDataset(Dataset):
def __init__(
self,
embeddings_dir,
sample_ids,
serotype_labels,
capsule_labels,
serotype_known=None,
):
"""
embeddings_path: str, path to directory with npy entries of variable length chunked embeddings
serotype_labels: pd.DataFrame, DataFrame with serotype labels indexed by sample ID.
serotype_known: optional array-like of bool, True if the serotype is resolved (not serogroup/compound).
"""
self.embedding_dir = embeddings_dir
self.serotypes = serotype_labels
self.is_capsule = capsule_labels
self.sample_ids = sample_ids
self.serotype_known = (
serotype_known if serotype_known is not None else [True] * len(sample_ids)
)
# TODO Validate sub-folders too
all_embeddings = glob.glob(os.path.join(embeddings_dir, "*.npy"))
file_names = [os.path.basename(f).split(".")[0] for f in all_embeddings]
missing_samples = set(self.sample_ids) - set(file_names)
if missing_samples:
logger.warning(
"%d sample_ids do not have a corresponding embedding file: %s",
len(missing_samples),
missing_samples,
)
def __len__(self) -> int:
return len(self.sample_ids)
def __getitem__(self, idx):
# subdir = "cbl" if self.is_capsule[idx] else "non-cbl"
embedding_path = os.path.join(self.embedding_dir, f"{self.sample_ids[idx]}.npy")
return {
"sample_id": self.sample_ids[idx],
"embedding": torch.tensor(np.load(embedding_path), dtype=torch.float32),
"serotype": self.serotypes[idx],
"is_capsule": self.is_capsule[idx],
"serotype_known": self.serotype_known[idx],
}
@register_model("transformer_contrastive_head")
class TransformerContrastiveHead(BaseModel):
def __init__(
self, input_dim, output_dim=128, max_len=64, nhead=4, num_layers=2, **kwargs
):
super().__init__(**kwargs)
self.pos_embed = nn.Embedding(
max_len, input_dim
) # TODO Dynamically expand or clamp
encoder_layer = nn.TransformerEncoderLayer(
d_model=input_dim,
nhead=nhead,
dim_feedforward=4 * input_dim,
batch_first=True,
)
self.encoder = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
self.cbl_classifier = nn.Linear(output_dim, 2)
self.project = nn.Sequential(
nn.Linear(input_dim, input_dim), nn.ReLU(), nn.Linear(input_dim, output_dim)
)
def forward(self, x):
B, L, D = x.size()
padding_mask = _make_padding_mask(x) # (B, L)
pos = torch.arange(L, device=x.device).unsqueeze(0) # (1, L)
x = x + self.pos_embed(pos)
x = self.encoder(x, src_key_padding_mask=padding_mask)
x = _masked_mean_pool(x, padding_mask)
z = F.normalize(self.project(x), dim=1)
logits = self.cbl_classifier(z) # Classifier output (B, output_dim)
return logits, z
@register_model("transformer_lr_classifier")
class TransformerLRClassifier(BaseModel):
def __init__(
self,
input_dim,
num_classes,
output_dim=128,
max_len=64,
nhead=4,
num_layers=2,
**kwargs,
):
super().__init__(**kwargs)
self.pos_embed = nn.Embedding(max_len, input_dim)
encoder_layer = nn.TransformerEncoderLayer(
d_model=input_dim,
nhead=nhead,
dim_feedforward=4 * input_dim,
batch_first=True,
)
self.encoder = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
self.cbl_classifier = nn.Linear(output_dim, 2)
self.serotype_classifier = nn.Linear(output_dim, num_classes)
self.project = nn.Sequential(
nn.Linear(input_dim, input_dim), nn.ReLU(), nn.Linear(input_dim, output_dim)
)
def forward(self, x):
B, L, D = x.size()
padding_mask = _make_padding_mask(x) # (B, L)
pos = torch.arange(L, device=x.device).unsqueeze(0) # (1, L)
x = x + self.pos_embed(pos)
x = self.encoder(x, src_key_padding_mask=padding_mask)
x = _masked_mean_pool(x, padding_mask)
z = F.normalize(self.project(x), dim=1)
logits = self.cbl_classifier(z) # Classifier output (B, output_dim)
serotype_logits = self.serotype_classifier(z)
return logits, serotype_logits, z
@register_model("transformer_trihead_lr")
class TransformerTriHeadLR(BaseModel):
def __init__(
self,
input_dim,
num_classes,
output_dim=128,
max_len=64,
nhead=4,
num_layers=2,
**kwargs,
):
super().__init__(**kwargs)
self.pos_embed = nn.Embedding(max_len, input_dim)
encoder_layer = nn.TransformerEncoderLayer(
d_model=input_dim,
nhead=nhead,
dim_feedforward=4 * input_dim,
batch_first=True,
)
self.encoder = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
self.cbl_classifier = nn.Linear(output_dim, 2)
assert len(num_classes) == 2, (
"num_classes should be a list or tuple of length 2 for the two classifiers."
)
self.serotype_classifier = nn.Linear(output_dim, num_classes[0])
self.genogroup_classifier = nn.Linear(output_dim, num_classes[1])
self.project = nn.Sequential(
nn.Linear(input_dim, input_dim), nn.ReLU(), nn.Linear(input_dim, output_dim)
)
def forward(self, x):
B, L, D = x.size()
padding_mask = _make_padding_mask(x) # (B, L)
pos = torch.arange(L, device=x.device).unsqueeze(0) # (1, L)
x = x + self.pos_embed(pos)
x = self.encoder(x, src_key_padding_mask=padding_mask)
x = _masked_mean_pool(x, padding_mask)
z = F.normalize(self.project(x), dim=1)
logits = self.cbl_classifier(z) # Classifier output (B, output_dim)
serotype_logits = self.serotype_classifier(z)
genogroup_logits = self.genogroup_classifier(z)
return logits, (serotype_logits, genogroup_logits), z