File size: 6,532 Bytes
f90f02b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
ANLP Assignment 2: Utilities Module
Contains helper functions for:
- Random seed reproduction
- Parameter counting (total vs active parameters for MoE)
- Metric computations (Perplexity, BLEU, ROUGE)
- WandB / Local experiment logging
- Benchmarking and timing
"""

import os
import math
import time
import random
import json
import logging
from typing import Dict, Any, List, Optional, Tuple, Union

import numpy as np
import torch
import torch.nn as nn

logger = logging.getLogger("ANLP_Utils")
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")


def set_seed(seed: int = 42) -> None:
    """Sets random seeds across random, numpy, and torch for reproducibility."""
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False
    logger.info(f"Random seed set to {seed}")


def count_parameters(model: nn.Module) -> Dict[str, int]:
    """
    Computes both total parameters and active parameters per token.
    For standard dense models, active_parameters == total_parameters.
    For MoE models, active_parameters accounts for only the router + active experts per token.
    """
    total_params = sum(p.numel() for p in model.parameters())
    trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)

    # Check if model has custom get_active_parameters method
    if hasattr(model, "get_active_parameters"):
        active_params = model.get_active_parameters()
    else:
        active_params = total_params

    return {
        "total_parameters": total_params,
        "trainable_parameters": trainable_params,
        "active_parameters": active_params,
    }


def compute_perplexity(loss: float) -> float:
    """Computes perplexity from cross-entropy loss with numerical clamping."""
    try:
        if math.isnan(loss) or math.isinf(loss) or loss > 100.0:
            return float("inf")
        return math.exp(loss)
    except OverflowError:
        return float("inf")


def compute_bleu(
    predictions: List[str],
    references: List[List[str]],
    smooth_method: str = "floor"
) -> float:
    """
    Computes corpus-level BLEU-4 score.
    Attempts to use sacrebleu or nltk; provides built-in fallback if unavailable.
    """
    # Clean inputs
    preds = [p.strip() for p in predictions]
    refs = [[r.strip() for r in ref_list] for ref_list in references]

    try:
        import sacrebleu
        # sacrebleu expects references as list of lists across each ref position
        # e.g., [[ref1_doc1, ref1_doc2], [ref2_doc1, ref2_doc2]]
        num_refs = max(len(r) for r in refs)
        transposed_refs = []
        for i in range(num_refs):
            transposed_refs.append([r[i] if i < len(r) else r[0] for r in refs])
        bleu = sacrebleu.corpus_bleu(preds, transposed_refs)
        return float(bleu.score)
    except ImportError:
        pass

    try:
        from nltk.translate.bleu_score import corpus_bleu, SmoothingFunction
        smooth = SmoothingFunction().method1 if smooth_method == "floor" else SmoothingFunction().method2
        tokenized_preds = [p.split() for p in preds]
        tokenized_refs = [[r.split() for r in ref_list] for ref_list in refs]
        score = corpus_bleu(tokenized_refs, tokenized_preds, smoothing_function=smooth)
        return float(score * 100.0)
    except ImportError:
        pass

    # Simple built-in n-gram BLEU fallback (unigram/bigram overlap)
    total_matches = 0
    total_possible = 0
    for p, r_list in zip(preds, refs):
        p_tokens = p.lower().split()
        if not p_tokens:
            continue
        ref_tokens = r_list[0].lower().split() if r_list else []
        matches = sum(1 for tok in p_tokens if tok in ref_tokens)
        total_matches += matches
        total_possible += len(p_tokens)
    if total_possible == 0:
        return 0.0
    return float((total_matches / total_possible) * 100.0)


class WandbLogger:
    """
    Robust logger wrapping Weights & Biases with safe local fallback.
    Prevents pipeline failures if WandB API key is missing or offline.
    """
    def __init__(
        self,
        project: str = "ANLP-Assignment-2",
        name: Optional[str] = None,
        config: Optional[Dict[str, Any]] = None,
        tags: Optional[List[str]] = None,
        enabled: bool = True
    ):
        self.enabled = enabled
        self.run = None
        self.local_history: List[Dict[str, Any]] = []

        if self.enabled:
            try:
                import wandb
                # If WANDB_API_KEY is not set in environment, wandb can run in offline mode
                mode = "online" if os.environ.get("WANDB_API_KEY") else "offline"
                self.run = wandb.init(
                    project=project,
                    name=name,
                    config=config,
                    tags=tags,
                    mode=mode,
                    reinit=True
                )
                logger.info(f"WandB initialized successfully (mode={mode}, run={name})")
            except Exception as e:
                logger.warning(f"Could not initialize WandB ({e}). Defaulting to local logging.")
                self.enabled = False

    def log(self, metrics: Dict[str, Any], step: Optional[int] = None) -> None:
        """Logs metrics to WandB and stores in local history."""
        entry = dict(metrics)
        if step is not None:
            entry["step"] = step
        entry["timestamp"] = time.time()
        self.local_history.append(entry)

        if self.enabled and self.run is not None:
            try:
                import wandb
                wandb.log(metrics, step=step)
            except Exception as e:
                logger.warning(f"Failed to push metrics to WandB: {e}")

    def finish(self) -> None:
        """Closes WandB run."""
        if self.enabled and self.run is not None:
            try:
                import wandb
                wandb.finish()
            except Exception:
                pass

    def save_local_history(self, filepath: str) -> None:
        """Saves local metric logs to a JSON file."""
        os.makedirs(os.path.dirname(os.path.abspath(filepath)), exist_ok=True)
        with open(filepath, "w", encoding="utf-8") as f:
            json.dump(self.local_history, f, indent=2)
        logger.info(f"Saved local run log to {filepath}")