File size: 13,908 Bytes
590a604
 
 
 
 
 
 
 
 
ee1a8a3
1fbc47b
 
 
67c3a83
 
1ec7405
1fbc47b
590a604
67c3a83
1fbc47b
67c3a83
 
 
 
 
 
 
 
1fbc47b
d18b34d
1fbc47b
d18b34d
1fbc47b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d18b34d
1fbc47b
 
1ec7405
1fbc47b
 
590a604
1fbc47b
 
 
 
 
 
 
 
590a604
 
 
 
1fbc47b
590a604
 
 
 
1fbc47b
590a604
1fbc47b
590a604
 
1fbc47b
 
 
590a604
 
 
 
 
 
 
b43ba56
 
590a604
b43ba56
 
67c3a83
1ec7405
 
67c3a83
 
 
 
1ec7405
67c3a83
b43ba56
 
590a604
b43ba56
 
d18b34d
 
590a604
d18b34d
 
 
67c3a83
 
 
 
 
590a604
b43ba56
590a604
b43ba56
 
 
590a604
 
 
 
 
 
 
 
 
 
b43ba56
d18b34d
590a604
d18b34d
590a604
 
 
 
1fbc47b
590a604
 
 
1fbc47b
590a604
1fbc47b
590a604
1ec7405
 
590a604
 
 
1ec7405
590a604
 
 
1fbc47b
590a604
 
 
 
1fbc47b
 
 
590a604
 
 
 
 
 
 
1fbc47b
 
 
590a604
1fbc47b
590a604
 
 
1fbc47b
b43ba56
 
1fbc47b
 
590a604
1fbc47b
590a604
 
1fbc47b
b43ba56
 
1fbc47b
 
 
 
590a604
 
1fbc47b
b43ba56
 
1fbc47b
 
 
 
590a604
1fbc47b
 
590a604
 
 
b43ba56
 
1fbc47b
 
590a604
1fbc47b
 
590a604
 
b43ba56
 
1fbc47b
 
 
 
 
590a604
 
b43ba56
 
1fbc47b
 
 
590a604
 
 
d18b34d
590a604
 
1ec7405
590a604
 
 
 
 
 
 
 
 
 
1fbc47b
 
590a604
1fbc47b
 
 
 
1ec7405
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
590a604
1ec7405
 
 
67c3a83
 
 
1ec7405
67c3a83
 
 
590a604
 
 
 
1ec7405
590a604
 
 
 
 
 
1ec7405
 
 
 
 
 
1fbc47b
 
 
 
1ec7405
1fbc47b
 
d18b34d
b43ba56
1ec7405
 
1fbc47b
 
 
 
 
590a604
 
 
67c3a83
 
590a604
 
 
1fbc47b
590a604
1ec7405
 
 
 
 
 
d18b34d
590a604
1fbc47b
67c3a83
 
 
 
 
 
 
 
590a604
 
 
 
 
 
d18b34d
1fbc47b
590a604
1fbc47b
 
 
590a604
d18b34d
1fbc47b
590a604
 
 
 
 
 
 
 
 
 
d18b34d
ba4cb76
 
1fbc47b
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
"""
Training script for LexiMind.

Orchestrates dataset loading, model construction, torch.compile optimization,
and multi-task training with checkpoint management.

Author: Oliver Perrin
Date: December 2025
"""

from __future__ import annotations

import json
import logging
import os
import re
import sys
import time
import warnings
from pathlib import Path
from typing import Dict, Sequence, cast

# Suppress torch inductor warnings that mess up progress bars
os.environ.setdefault("TORCH_LOGS", "-all")
warnings.filterwarnings("ignore", category=UserWarning, module="torch._inductor")
warnings.filterwarnings("ignore", category=FutureWarning, module="mlflow")
logging.getLogger("torch._inductor").setLevel(logging.ERROR)
logging.getLogger("torch._dynamo").setLevel(logging.ERROR)

import hydra
import torch
from omegaconf import DictConfig, OmegaConf

PROJECT_ROOT = Path(__file__).resolve().parents[1]
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

from src.data.dataloader import (
    build_emotion_dataloader,
    build_summarization_dataloader,
    build_topic_dataloader,
)
from src.data.dataset import (
    EmotionDataset,
    SummarizationDataset,
    TopicDataset,
    load_emotion_jsonl,
    load_summarization_jsonl,
    load_topic_jsonl,
)
from src.data.tokenization import Tokenizer, TokenizerConfig
from src.models.factory import ModelConfig, build_multitask_model
from src.training.trainer import Trainer, TrainerConfig
from src.training.utils import set_seed
from src.utils.io import load_state, save_state
from src.utils.labels import LabelMetadata, save_label_metadata

# --------------- Data Loading ---------------

SPLIT_ALIASES: Dict[str, Sequence[str]] = {
    "train": ("train",),
    "val": ("val", "validation"),
    "test": ("test",),
}


def load_splits(data_dir: Path, loader) -> Dict[str, list]:
    """Load train/val/test splits from data directory."""
    splits = {}
    for name, aliases in SPLIT_ALIASES.items():
        for alias in aliases:
            for ext in ("jsonl", "json"):
                path = data_dir / f"{alias}.{ext}"
                if path.exists():
                    splits[name] = loader(str(path))
                    break
            if name in splits:
                break
        if name not in splits:
            raise FileNotFoundError(f"Missing {name} split in {data_dir}")
    return splits


def limit_samples(splits: Dict[str, list], cfg: DictConfig) -> None:
    """Apply sample limits for dev/debug runs."""
    for split, key in [("train", "max_train_samples"), ("val", "max_val_samples")]:
        limit = cfg.get(key)
        if limit and split in splits and len(splits[split]) > limit:
            splits[split] = splits[split][: int(limit)]
            print(f"  {split}: limited to {limit} samples")


# --------------- Model Compilation ---------------


def compile_model(model: torch.nn.Module) -> torch.nn.Module:
    """Compile model with inductor backend (optimized for speed)."""
    print(f"  -> Enabling torch.compile for {model.__class__.__name__}...")
    from src.training.safe_compile import apply_safe_config, compile_model_safe

    # Apply safe configuration first
    apply_safe_config()
    # Compile with default mode (inductor) - most stable
    return compile_model_safe(model, mode="default")


# --------------- Main ---------------


@hydra.main(version_base=None, config_path="../configs", config_name="config")
def main(cfg: DictConfig) -> None:
    start_time = time.perf_counter()
    print(OmegaConf.to_yaml(cfg))
    set_seed(cfg.seed)

    # Benchmark mode: skip saving checkpoints (for speed testing)
    benchmark_mode = cfg.get("benchmark", False)
    if benchmark_mode:
        print("⚡ BENCHMARK MODE: Checkpoints will NOT be saved")

    # Enable TF32 for Ampere+ GPUs (RTX 30xx/40xx) - ~2x matmul speedup
    if torch.cuda.is_available() and torch.cuda.get_device_capability()[0] >= 8:
        print("✓ TF32 enabled for Ampere GPU")
        torch.set_float32_matmul_precision("high")
        torch.backends.cuda.matmul.allow_tf32 = True
        torch.backends.cudnn.allow_tf32 = True
        torch.backends.cudnn.benchmark = True  # Auto-tune convolutions
        torch.backends.cuda.enable_flash_sdp(True)  # Flash attention if available
        torch.backends.cuda.enable_mem_efficient_sdp(True)  # Memory-efficient attention

    # Disable debug APIs for max speed
    torch.autograd.set_detect_anomaly(False)
    torch.autograd.profiler.profile(False)
    torch.autograd.profiler.emit_nvtx(False)

    # --------------- Load Data ---------------

    data_cfg = cfg.data
    trainer_cfg = cfg.training.get("trainer", {})

    print("\nLoading datasets...")
    summ_splits = load_splits(Path(data_cfg.processed.summarization), load_summarization_jsonl)
    emot_splits = load_splits(Path(data_cfg.processed.emotion), load_emotion_jsonl)
    topic_splits = load_splits(Path(data_cfg.processed.topic), load_topic_jsonl)

    # Apply dev/debug sample limits
    for splits in [summ_splits, emot_splits, topic_splits]:
        limit_samples(splits, trainer_cfg)

    # --------------- Tokenizer & Datasets ---------------

    tok_cfg = data_cfg.get("tokenizer", {})
    # Allow training overrides for max_length to run shorter dev sweeps
    override_max_len = cfg.training.get("tokenizer_max_length")
    tokenizer = Tokenizer(
        TokenizerConfig(
            pretrained_model_name=tok_cfg.get("pretrained_model_name", "google/flan-t5-base"),
            max_length=int(override_max_len or tok_cfg.get("max_length", 512)),
            lower=bool(tok_cfg.get("lower", False)),
        )
    )

    summ_train = SummarizationDataset(summ_splits["train"])
    summ_val = SummarizationDataset(summ_splits["val"])
    emot_train = EmotionDataset(emot_splits["train"])
    emot_val = EmotionDataset(emot_splits["val"], binarizer=emot_train.binarizer)
    topic_train = TopicDataset(topic_splits["train"])
    topic_val = TopicDataset(topic_splits["val"], encoder=topic_train.encoder)

    # --------------- DataLoaders ---------------

    dl_cfg = cfg.training.get("dataloader", {})
    batch_size = int(dl_cfg.get("batch_size", 8))
    num_workers = int(dl_cfg.get("num_workers", 4))
    pin_memory = bool(dl_cfg.get("pin_memory", True))
    max_len = tokenizer.config.max_length

    train_loaders = {
        "summarization": build_summarization_dataloader(
            summ_train,
            tokenizer,
            shuffle=True,
            max_source_length=max_len,
            max_target_length=max_len,
            batch_size=batch_size,
            num_workers=num_workers,
            pin_memory=pin_memory,
        ),
        "emotion": build_emotion_dataloader(
            emot_train,
            tokenizer,
            shuffle=True,
            max_length=max_len,
            batch_size=batch_size,
            num_workers=num_workers,
            pin_memory=pin_memory,
        ),
        "topic": build_topic_dataloader(
            topic_train,
            tokenizer,
            shuffle=True,
            max_length=max_len,
            batch_size=batch_size,
            num_workers=num_workers,
            pin_memory=pin_memory,
        ),
    }
    val_loaders = {
        "summarization": build_summarization_dataloader(
            summ_val,
            tokenizer,
            shuffle=False,
            max_source_length=max_len,
            max_target_length=max_len,
            batch_size=batch_size,
            num_workers=num_workers,
            pin_memory=pin_memory,
        ),
        "emotion": build_emotion_dataloader(
            emot_val,
            tokenizer,
            shuffle=False,
            max_length=max_len,
            batch_size=batch_size,
            num_workers=num_workers,
            pin_memory=pin_memory,
        ),
        "topic": build_topic_dataloader(
            topic_val,
            tokenizer,
            shuffle=False,
            max_length=max_len,
            batch_size=batch_size,
            num_workers=num_workers,
            pin_memory=pin_memory,
        ),
    }

    # --------------- Model ---------------

    print("\nBuilding model...")
    device = torch.device(cfg.device)
    model_cfg = ModelConfig(
        d_model=cfg.model.d_model,
        vocab_size=getattr(cfg.model, "vocab_size", None),  # Override tokenizer vocab if specified
        num_encoder_layers=cfg.model.num_encoder_layers,
        num_decoder_layers=cfg.model.num_decoder_layers,
        num_attention_heads=cfg.model.num_attention_heads,
        ffn_dim=cfg.model.ffn_dim,
        dropout=cfg.model.dropout,
        use_pretrained=cfg.model.use_pretrained,
        pretrained_model_name=cfg.model.pretrained_model_name,
        activation=getattr(cfg.model, "activation", "gelu"),
        use_relative_position_bias=getattr(cfg.model, "use_relative_position_bias", False),
    )
    model = build_multitask_model(
        tokenizer,
        num_emotions=len(emot_train.emotion_classes),
        num_topics=len(topic_train.topic_classes),
        config=model_cfg,
    ).to(device)

    # If Training Crashes: Resume from checkpoint if provided (load before compile to avoid key mismatches)
    start_epoch = 1
    resume_path = cfg.get("resume_from")
    if resume_path:
        ckpt_path = Path(resume_path)
        if ckpt_path.exists():
            print(f"\n↩Resuming from checkpoint: {ckpt_path}")
            load_state(model, str(ckpt_path))
            # Parse epoch number robustly from filename (e.g., epoch_5.pt)
            epoch_num = None
            try:
                # Prefer stem (no suffix); fallback to any digit sequence in name
                digits = re.findall(r"\d+", ckpt_path.stem)
                if digits:
                    epoch_num = int(digits[-1])
            except Exception:
                epoch_num = None

            if epoch_num is not None:
                start_epoch = epoch_num + 1
                print(f"  -> Starting from epoch {start_epoch}")
            else:
                print("  -> Could not parse epoch number; starting from epoch 1")
                start_epoch = 1
        else:
            print(f"⚠ Resume checkpoint not found: {ckpt_path}. Starting from scratch.")

    # Compile encoder/decoder for faster training (skip heads - small overhead)
    compile_encoder = bool(cfg.training.get("compile_encoder", True))
    compile_decoder = bool(cfg.training.get("compile_decoder", True))
    if compile_encoder and model.encoder is not None:
        from src.models.encoder import TransformerEncoder

        model.encoder = cast(TransformerEncoder, compile_model(model.encoder))
    if compile_decoder and model.decoder is not None:
        from src.models.decoder import TransformerDecoder

        model.decoder = cast(TransformerDecoder, compile_model(model.decoder))

    # --------------- Optimizer & Trainer ---------------

    opt_cfg = cfg.training.get("optimizer", {})
    sched_cfg = cfg.training.get("scheduler", {})
    optimizer = torch.optim.AdamW(
        model.parameters(),
        lr=float(opt_cfg.get("lr", 3e-5)),
        weight_decay=float(opt_cfg.get("weight_decay", 0.01)),
    )

    # Clamp start_epoch to max_epochs to avoid empty loop
    max_epochs = int(trainer_cfg.get("max_epochs", 1))
    if start_epoch > max_epochs:
        print(f"⚠ resume_from points past max_epochs ({max_epochs}); nothing to train. Setting start_epoch to {max_epochs}")
        start_epoch = max_epochs

    trainer = Trainer(
        model=model,
        optimizer=optimizer,
        config=TrainerConfig(
            max_epochs=max_epochs,
            gradient_clip_norm=float(trainer_cfg.get("gradient_clip_norm", 1.0)),
            task_weights=trainer_cfg.get("task_weights"),
            label_smoothing=float(trainer_cfg.get("label_smoothing", 0.0)),
            gradient_accumulation_steps=int(trainer_cfg.get("gradient_accumulation_steps", 1)),
            scheduler_type=str(sched_cfg.get("name", "constant")),
            warmup_steps=int(sched_cfg.get("warmup_steps", 0)),
        ),
        device=device,
        tokenizer=tokenizer,
    )

    # --------------- Train ---------------

    def save_checkpoint(epoch: int, model: torch.nn.Module, history: Dict) -> None:
        if benchmark_mode:
            return  # Skip saving in benchmark mode
        path = Path(cfg.checkpoint_out).parent / f"epoch_{epoch}.pt"
        path.parent.mkdir(parents=True, exist_ok=True)
        save_state(model, str(path))

    print("\nStarting training...")
    history = trainer.fit(
        train_loaders,
        val_loaders,
        checkpoint_callback=save_checkpoint,
        start_epoch=start_epoch,
    )

    # --------------- Save Outputs ---------------

    if benchmark_mode:
        total_time = time.perf_counter() - start_time
        print(f"\n{'=' * 50}")
        print(f"⚡ Benchmark complete in {total_time:.1f}s")
        print("  (No files saved in benchmark mode)")
        print(f"{'=' * 50}")
        return

    # Best checkpoint
    ckpt_path = Path(cfg.checkpoint_out)
    ckpt_path.parent.mkdir(parents=True, exist_ok=True)
    save_state(model, str(ckpt_path))

    # Labels
    labels_path = Path(cfg.labels_out)
    save_label_metadata(
        LabelMetadata(emotion=emot_train.emotion_classes, topic=topic_train.topic_classes),
        labels_path,
    )

    # History
    history_path = Path(cfg.history_out)
    history_path.parent.mkdir(parents=True, exist_ok=True)
    with history_path.open("w") as f:
        json.dump(history, f, indent=2)

    total_time = time.perf_counter() - start_time
    print(f"\n{'=' * 50}")
    print(f"Training complete in {total_time:.1f}s")
    print(f"  Checkpoint: {ckpt_path}")
    print(f"  Labels: {labels_path}")
    print(f"  History: {history_path}")
    print(f"{'=' * 50}")


if __name__ == "__main__":
    main()