File size: 14,327 Bytes
48d0053
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# train_eval_utils.py
# Helpers for training and benchmarking
# Feel free to adjust these setup acc. to your use case
# - gbyuvd

import os
import math
import torch
from torch.utils.data import DataLoader, IterableDataset
from tqdm import tqdm
import json
import random
import pandas as pd
from sklearn.model_selection import train_test_split
from ranger21 import Ranger21
from datasets import load_dataset

# ----------------------------
# CSV Logger
# ----------------------------
import csv

class CSVLogger:
    def __init__(self, filename, fieldnames):
        self.filename = filename
        self.fieldnames = fieldnames
        self._initialized = False

    def log(self, row_dict):
        if not self._initialized:
            self._init_file()
        with open(self.filename, 'a', newline='', encoding='utf-8') as f:
            writer = csv.DictWriter(f, fieldnames=self.fieldnames)
            writer.writerow(row_dict)

    def _init_file(self):
        with open(self.filename, 'w', newline='', encoding='utf-8') as f:
            writer = csv.DictWriter(f, fieldnames=self.fieldnames)
            writer.writeheader()
        self._initialized = True

# ----------------------------
# Streaming dataset
# ----------------------------
class SelfiesStreamingDataset(IterableDataset):
    def __init__(self, csv_file, tokenizer, max_seq_len=512, mask_prob=0.15, global_token_ids=None):
        self.tokenizer = tokenizer
        self.max_seq_len = max_seq_len
        self.mask_prob = mask_prob
        self.global_token_ids = global_token_ids or []

        dataset = load_dataset("csv", data_files=csv_file, split="train", streaming=True)
        dataset = dataset.shuffle(seed=42, buffer_size=10000)
        self.dataset_iter = iter(dataset)

        self.mask_id = tokenizer.mask_token_id
        self.pad_id = tokenizer.pad_token_id

    def __iter__(self):
        for example in self.dataset_iter:
            smiles = example["SMILES"]
            enc = self.tokenizer(smiles, truncation=True, max_length=self.max_seq_len, return_tensors=None)

            input_ids = enc["input_ids"]
            attention_mask = enc["attention_mask"]
            labels = input_ids.copy()

            vocab_size = len(self.tokenizer)
            for i in range(len(input_ids)):
                if input_ids[i] in self.global_token_ids:
                    continue
                if random.random() < self.mask_prob:
                    rand = random.random()
                    if rand < 0.8:
                        input_ids[i] = self.mask_id
                    elif rand < 0.9:
                        input_ids[i] = random.randint(0, vocab_size - 1)
                        while input_ids[i] in self.global_token_ids:
                            input_ids[i] = random.randint(0, vocab_size - 1)
                    else:
                        pass

            global_positions = [idx for idx, tid in enumerate(input_ids) if tid in self.global_token_ids]

            yield (
                torch.tensor(input_ids, dtype=torch.long),
                torch.tensor(attention_mask, dtype=torch.long),
                torch.tensor(labels, dtype=torch.long),
                global_positions,
            )


def collate_fn(batch):
    input_ids_list, attention_mask_list, labels_list, global_positions_list = zip(*batch)
    input_ids = torch.nn.utils.rnn.pad_sequence(input_ids_list, batch_first=True, padding_value=0)
    attention_mask = torch.nn.utils.rnn.pad_sequence(attention_mask_list, batch_first=True, padding_value=0)
    labels = torch.nn.utils.rnn.pad_sequence(labels_list, batch_first=True, padding_value=-100)
    return input_ids, attention_mask, labels, global_positions_list


def get_dataloader(csv_file, tokenizer, batch_size=16, max_seq_len=512, mask_prob=0.15, global_token_ids=None):
    dataset = SelfiesStreamingDataset(csv_file, tokenizer, max_seq_len, mask_prob, global_token_ids)
    return DataLoader(dataset, batch_size=batch_size, collate_fn=collate_fn)


# ----------------------------
# Dataset splitting
# ----------------------------
def prepare_train_val_test_split(full_csv, train_csv, val_csv, test_csv,

                                 val_test_size=0.3, test_size_ratio=0.5, random_state=42):
    if all(os.path.exists(f) for f in [train_csv, val_csv, test_csv]):
        print(f"  Train/val/test splits already exist. Skipping split.")
        train_count = sum(1 for _ in open(train_csv, encoding='utf-8')) - 1
        val_count = sum(1 for _ in open(val_csv, encoding='utf-8')) - 1
        test_count = sum(1 for _ in open(test_csv, encoding='utf-8')) - 1
        return train_count, val_count, test_count

    df = pd.read_csv(full_csv)
    train_df, val_test_df = train_test_split(df, test_size=val_test_size, random_state=random_state)
    val_df, test_df = train_test_split(val_test_df, test_size=test_size_ratio, random_state=random_state)

    train_df.to_csv(train_csv, index=False)
    val_df.to_csv(val_csv, index=False)
    test_df.to_csv(test_csv, index=False)

    return len(train_df), len(val_df), len(test_df)


# ----------------------------
# Model call helper
# ----------------------------
import inspect

def call_model(model, input_ids, attention_mask, labels, global_positions):
    # Prepare base args
    model_kwargs = {
        "input_ids": input_ids,
        "attention_mask": attention_mask,
        "labels": labels,
        "output_attentions": False,
    }

    # Check if model's forward method accepts 'global_positions'
    sig = inspect.signature(model.forward)
    if "global_positions" in sig.parameters:
        # Convert global_positions to tensor if needed (for RougeBERT)
        if isinstance(global_positions, (tuple, list)):
            if len(global_positions) == 0:
                global_positions = None
            else:
                max_len = max(len(g) for g in global_positions)
                padded = [
                    list(g) + [-1] * (max_len - len(g)) if isinstance(g, (list, tuple)) else [g] + [-1] * (max_len - 1)
                    for g in global_positions
                ]
                global_positions = torch.tensor(padded, dtype=torch.long, device=input_ids.device)
        elif isinstance(global_positions, torch.Tensor):
            pass  # already good
        elif global_positions is None:
            pass
        else:
            raise TypeError(f"Unsupported type for global_positions: {type(global_positions)}")

        model_kwargs["global_positions"] = global_positions

    # Call model
    return model(**model_kwargs)


# ----------------------------
# Metrics
# ----------------------------
def compute_metrics(logits, labels):
    """Compute MLM loss sum, correct preds, total tokens."""
    loss_fn = torch.nn.CrossEntropyLoss(ignore_index=-100, reduction="sum")

    vocab_size = logits.size(-1)
    logits_flat = logits.view(-1, vocab_size)
    labels_flat = labels.view(-1)

    loss = loss_fn(logits_flat, labels_flat)
    count = (labels_flat != -100).sum().item()

    preds = torch.argmax(logits_flat, dim=-1)
    correct = ((preds == labels_flat) & (labels_flat != -100)).sum().item()

    return loss.item(), correct, count


def evaluate_model(model, dataloader, device):
    model.eval()
    total_loss, total_correct, total_count = 0.0, 0, 0

    with torch.no_grad():
        for input_ids, attention_mask, labels, global_positions in dataloader:
            input_ids, attention_mask, labels = (
                input_ids.to(device),
                attention_mask.to(device),
                labels.to(device),
            )
            outputs = call_model(model, input_ids, attention_mask, labels, global_positions)
            logits = outputs.logits
            loss, correct, count = compute_metrics(logits, labels)
            total_loss += loss
            total_correct += correct
            total_count += count

    avg_loss = total_loss / total_count if total_count > 0 else float("inf")
    perplexity = math.exp(avg_loss) if avg_loss < 20 else float("inf")
    accuracy = total_correct / total_count if total_count > 0 else 0.0

    return avg_loss, perplexity, accuracy


# ----------------------------
# Training + Evaluation loop
# ----------------------------
def train_and_eval(

    model,

    tokenizer,

    train_csv,

    val_csv,

    test_csv,

    config,

    run_name="experiment",

    batch_size=16,

    grad_accum=4,

    num_epochs=1,

    learning_rate=3e-6,

    mask_prob=0.15,

    max_seq_len=None,

    patience=10,

    save_dir="./checkpoints",

):
    # pick max seq length
    if max_seq_len is None:
        if hasattr(config, "max_seq"):
            max_seq_len = config.max_seq
        elif hasattr(config, "max_position_embeddings"):
            max_seq_len = config.max_position_embeddings
        else:
            max_seq_len = 512

    device = "cuda" if torch.cuda.is_available() else "cpu"
    model.to(device)

    global_token_ids = [tokenizer.bos_token_id, tokenizer.eos_token_id, tokenizer.mask_token_id]

    # Dataset counts
    train_count = sum(1 for _ in open(train_csv, encoding="utf-8")) - 1
    val_count = sum(1 for _ in open(val_csv, encoding="utf-8")) - 1
    test_count = sum(1 for _ in open(test_csv, encoding="utf-8")) - 1

    train_steps_per_epoch = max(1, train_count // batch_size)
    optimizer_steps_per_epoch = max(1, train_steps_per_epoch // grad_accum)
    val_steps_total = max(1, val_count // batch_size)
    test_steps_total = max(1, test_count // batch_size)

    print(f"  Train steps/epoch: {train_steps_per_epoch}")
    print(f"  Optimizer steps/epoch: {optimizer_steps_per_epoch}")
    print(f"  Val steps total: {val_steps_total}")
    print(f"  Test steps total: {test_steps_total}")

    # Optimizer
    optimizer = Ranger21(
        model.parameters(),
        lr=learning_rate,
        weight_decay=0.01,
        use_adabelief=True,
        use_warmup=True,
        use_madgrad=True,
        num_epochs=num_epochs,
        warmdown_active=False,
        num_batches_per_epoch=optimizer_steps_per_epoch,
    )

    os.makedirs(save_dir, exist_ok=True)
    with open(os.path.join(save_dir, f"{run_name}_config.json"), "w") as f:
        if hasattr(config, "to_dict"):
            json.dump(config.to_dict(), f, indent=2)
        else:
            json.dump(config.__dict__, f, indent=2)

    best_val_loss = float("inf")
    patience_counter = 0
    final_val_loss, final_val_perplexity, final_val_acc = None, None, None

    #   Initialize unified CSV logger
    metrics_log_path = os.path.join(save_dir, f"{run_name}_metrics.csv")
    metrics_logger = CSVLogger(metrics_log_path, ["epoch", "step", "train_loss", "val_loss", "ppl", "mlm_acc"])

    for epoch in range(num_epochs):
        model.train()
        train_loader = get_dataloader(train_csv, tokenizer, batch_size, max_seq_len, mask_prob, global_token_ids)

        running_loss = 0.0
        optimizer.zero_grad()

        pbar = tqdm(enumerate(train_loader), desc=f"{run_name} | Epoch {epoch+1}/{num_epochs}", total=train_steps_per_epoch)
        for step, (input_ids, attention_mask, labels, global_positions) in pbar:
            input_ids, attention_mask, labels = input_ids.to(device), attention_mask.to(device), labels.to(device)

            outputs = call_model(model, input_ids, attention_mask, labels, global_positions)
            loss = outputs.loss / grad_accum
            loss.backward()
            running_loss += loss.item()

            if (step + 1) % grad_accum == 0:
                optimizer.step()
                optimizer.zero_grad()

            #   Log every 10 steps
            if (step + 1) % 10 == 0:
                avg_loss = running_loss / 10
                running_loss = 0.0

                #   Validation every 10 steps
                val_loader = get_dataloader(val_csv, tokenizer, batch_size, max_seq_len, mask_prob, global_token_ids)
                val_loss, val_perplexity, val_acc = evaluate_model(model, val_loader, device)

                #   Log unified metrics
                metrics_logger.log({
                    "epoch": epoch + 1,
                    "step": step + 1,
                    "train_loss": avg_loss,
                    "val_loss": val_loss,
                    "ppl": val_perplexity,
                    "mlm_acc": val_acc
                })

                #   Print to console
                print(f"\n[Step {step+1}] Train Loss: {avg_loss:.4f} | "
                      f"Val Loss: {val_loss:.4f} | Perplexity: {val_perplexity:.4f} | MLM Acc: {val_acc:.2%}")

                #   Save best model
                if val_loss < best_val_loss:
                    best_val_loss = val_loss
                    patience_counter = 0
                    model.save_pretrained(os.path.join(save_dir, f"{run_name}_best"))
                    tokenizer.save_pretrained(save_dir)
                else:
                    patience_counter += 1
                    if patience_counter >= patience:
                        print(" Early stopping triggered")
                        break

                model.train()  # back to train mode after val

        if patience_counter >= patience:
            break

    # Final test evaluation
    test_loader = get_dataloader(test_csv, tokenizer, batch_size, max_seq_len, mask_prob, global_token_ids)
    test_loss, test_perplexity, test_acc = evaluate_model(model, test_loader, device)

    print(f"\n  Final Test | Loss: {test_loss:.4f} | "
          f"Perplexity: {test_perplexity:.4f} | MLM Acc: {test_acc:.2%}")

    model.save_pretrained(os.path.join(save_dir, f"{run_name}_final"))
    tokenizer.save_pretrained(save_dir)

    return {
        "best_val_loss": best_val_loss,
        "final_val_loss": final_val_loss,
        "final_val_perplexity": final_val_perplexity,
        "final_val_acc": final_val_acc,
        "test_loss": test_loss,
        "test_perplexity": test_perplexity,
        "test_acc": test_acc,

    }