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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""Training utilities for transformer-based multi-label classification.

This module contains a small training harness around HuggingFace
`AutoModelForSequenceClassification` specialized for the project's
multi-label code-comment classification task. It provides:

- `TransformerConfig` dataclass for configurable training runs.
- `CommentDataset` to wrap tokenization of pandas DataFrames.
- `TransformerTrainer` which runs the training loop, evaluation and
    model export (with MLflow logging hooks).

The helpers are intended for experimental, small-scale training and
instrumentation rather than production-grade distributed training.
"""

from dataclasses import asdict, dataclass
import logging
import os
from typing import Dict, List, Tuple

import mlflow
import numpy as np
import pandas as pd
from sklearn.metrics import (
    accuracy_score,
    classification_report,
    f1_score,
    precision_score,
    recall_score,
)
import torch
from torch.utils.data import DataLoader, Dataset
from tqdm.auto import tqdm
from transformers import (
    AutoModelForSequenceClassification,
    AutoTokenizer,
    get_linear_schedule_with_warmup,
)

from .preprocessing import load_or_prepare_data

logger = logging.getLogger(__name__)

DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {DEVICE}")


# Label names per language, order must match the label vector in the CSV
LABELS: Dict[str, Tuple[str, ...]] = {
    "java": (
        "summary",
        "Ownership",
        "Expand",
        "usage",
        "Pointer",
        "deprecation",
        "rational",
    ),
    "python": (
        "Usage",
        "Parameters",
        "DevelopmentNotes",
        "Expand",
        "Summary",
    ),
    "pharo": (
        "Keyimplementationpoints",
        "Example",
        "Responsibilities",
        "Intent",
        "Keymessages",
        "Collaborators",
    ),
}


@dataclass
class TransformerConfig:
    """Configuration for transformer training runs.

    Attributes are intentionally simple dataclass fields and map directly to
    CLI/YAML configuration keys used by the training harness.
    """

    lang: str
    raw_data_dir: str
    processed_data_dir: str
    model_output_path: str
    pretrained_model_name: str = "microsoft/codebert-base"
    max_length: int = 128
    batch_size: int = 16
    lr: float = 2e-5
    num_epochs: int = 5
    warmup_ratio: float = 0.1
    pos_weight_cap: float = 30.0
    threshold: float = 0.5
    preprocessing: bool = False
    preprocessing_factor: float = 1.0

    def __post_init__(self) -> None:
        """Force correct types even if YAML provides strings."""
        self.max_length = int(self.max_length)
        self.batch_size = int(self.batch_size)
        self.lr = float(self.lr)
        self.num_epochs = int(self.num_epochs)
        self.warmup_ratio = float(self.warmup_ratio)
        self.pos_weight_cap = float(self.pos_weight_cap)
        self.threshold = float(self.threshold)
        self.preprocessing_factor = float(self.preprocessing_factor)

        # allow 'true'/'false' as strings from YAML
        if isinstance(self.preprocessing, str):
            self.preprocessing = self.preprocessing.lower() == "true"


class CommentDataset(Dataset):
    """Simple Dataset wrapper around a pandas DataFrame with 'combo' and 'labels_array'."""

    def __init__(self, df: pd.DataFrame, tokenizer: AutoTokenizer, max_length: int):
        """Create a dataset that tokenizes rows on demand.

        Parameters
        ----------
        df : pandas.DataFrame
            Input frame containing at least `combo` and `labels_array` columns.
        tokenizer : transformers.AutoTokenizer
            Tokenizer used to encode text into model inputs.
        max_length : int
            Maximum tokenization length (used for padding/truncation).

        """
        self.df = df.reset_index(drop=True)
        self.tokenizer = tokenizer
        self.max_length = max_length

    def __len__(self) -> int:
        """Return the number of examples in the dataset."""
        return len(self.df)

    def __getitem__(self, idx: int):
        """Return a single tokenized example and its labels as tensors.

        The returned dict contains tokenized inputs (PyTorch tensors) and a
        `labels` tensor suitable for BCEWithLogitsLoss for multi-label tasks.
        """
        row = self.df.iloc[idx]
        text = str(row["combo"])
        labels = np.asarray(row["labels_array"], dtype=np.float32)

        enc = self.tokenizer(
            text,
            truncation=True,
            max_length=self.max_length,
            padding="max_length",
            return_tensors="pt",
        )

        item = {k: v.squeeze(0) for k, v in enc.items()}
        item["labels"] = torch.from_numpy(labels)
        return item


class TransformerTrainer:
    """End-to-end transformer trainer for the code comment multi-label task."""

    def __init__(self, cfg: TransformerConfig) -> None:
        """Initialize training state, data loaders, model and optimizer.

        Parameters
        ----------
        cfg : TransformerConfig
            Training configuration containing data paths and hyperparameters.

        """
        self.cfg = cfg
        if cfg.lang not in LABELS:
            raise ValueError(f"No LABELS defined for language '{cfg.lang}'.")

        self.label_names = LABELS[cfg.lang]
        self.num_labels = len(self.label_names)

        logger.info("Initializing TransformerTrainer for language '%s'.", cfg.lang)
        logger.info("Raw data directory: %s", cfg.raw_data_dir)
        logger.info("Processed data directory: %s", cfg.processed_data_dir)
        logger.info("Model output path: %s", cfg.model_output_path)

        # --- data loading / preprocessing ---
        self.train_df, self.eval_df, self.preprocessing_used = load_or_prepare_data(
            lang=cfg.lang,
            raw_data_dir=cfg.raw_data_dir,
            processed_data_dir=cfg.processed_data_dir,
            preprocessing_enabled=cfg.preprocessing,
            preprocessing_factor=cfg.preprocessing_factor,
            random_state=42,
        )

        logger.info("Preprocessing used for this run: %s", self.preprocessing_used)
        logger.info("Using device: %s", DEVICE)
        logger.info(
            "Train size: %d rows, Eval size: %d rows",
            len(self.train_df),
            len(self.eval_df),
        )

        # --- log config and dataset info to MLflow ---
        try:
            cfg_dict = asdict(self.cfg)
            mlflow.log_params({f"cfg_{k}": v for k, v in cfg_dict.items()})
            mlflow.log_param("num_labels", self.num_labels)
            mlflow.log_param("label_names", ",".join(self.label_names))
            mlflow.log_param("train_samples", len(self.train_df))
            mlflow.log_param("eval_samples", len(self.eval_df))
            mlflow.log_param("preprocessing_used", self.preprocessing_used)
        except Exception as e:
            logger.warning("Could not log transformer config to MLflow: %s", e)

        # tokenizer
        logger.info("Loading tokenizer '%s'.", cfg.pretrained_model_name)
        self.tokenizer = AutoTokenizer.from_pretrained(cfg.pretrained_model_name)

        # label statistics and pos_weight
        y_train = np.stack(self.train_df["labels_array"].to_numpy())
        self.pos_weight = self._compute_pos_weight(y_train)

        # dataloaders
        train_dataset = CommentDataset(self.train_df, self.tokenizer, cfg.max_length)
        eval_dataset = CommentDataset(self.eval_df, self.tokenizer, cfg.max_length)

        self.train_loader = DataLoader(
            train_dataset,
            batch_size=cfg.batch_size,
            shuffle=True,
        )
        self.eval_loader = DataLoader(
            eval_dataset,
            batch_size=cfg.batch_size,
            shuffle=False,
        )

        logger.info(
            "Hyperparameters – lr=%s (type=%s), batch_size=%s, num_epochs=%s",
            self.cfg.lr,
            type(self.cfg.lr),
            self.cfg.batch_size,
            self.cfg.num_epochs,
        )

        # model
        logger.info("Loading base model '%s'.", cfg.pretrained_model_name)
        self.model = AutoModelForSequenceClassification.from_pretrained(
            cfg.pretrained_model_name,
            num_labels=self.num_labels,
            problem_type="multi_label_classification",
        ).to(DEVICE)

        self.loss_fn = torch.nn.BCEWithLogitsLoss(pos_weight=self.pos_weight.to(DEVICE))
        self.optimizer = torch.optim.AdamW(self.model.parameters(), lr=self.cfg.lr)

        num_training_steps = cfg.num_epochs * len(self.train_loader)
        num_warmup_steps = int(cfg.warmup_ratio * num_training_steps)
        logger.info(
            "Total training steps: %d, warmup steps: %d.",
            num_training_steps,
            num_warmup_steps,
        )

        self.scheduler = get_linear_schedule_with_warmup(
            self.optimizer,
            num_warmup_steps=num_warmup_steps,
            num_training_steps=num_training_steps,
        )

        self.best_state_dict = None
        self.best_val_macro_f1 = 0.0

    def _compute_pos_weight(self, y: np.ndarray) -> torch.Tensor:
        if y.ndim == 1:
            y = y[:, None]
        freq = y.sum(axis=0).astype(np.float64)
        num_samples = y.shape[0]

        pos_weight = (num_samples - freq) / np.clip(freq, 1.0, None)
        pos_weight = np.clip(pos_weight, 1.0, self.cfg.pos_weight_cap)

        logger.info("Positive class weights (clipped): %s", pos_weight.tolist())
        return torch.tensor(pos_weight, dtype=torch.float32)

    def _step_batch(self, batch, train: bool):
        batch = {k: v.to(DEVICE) for k, v in batch.items()}
        labels = batch.pop("labels")

        outputs = self.model(**batch)
        logits = outputs.logits
        loss = self.loss_fn(logits, labels)

        if train:
            loss.backward()
            torch.nn.utils.clip_grad_norm_(self.model.parameters(), 1.0)
            self.optimizer.step()
            self.scheduler.step()
            self.optimizer.zero_grad()

        return loss, logits, labels

    def train_one_epoch(self, epoch: int) -> float:
        """Run a single training epoch over `self.train_loader`.

        Returns
        -------
        float
            The average training loss over the epoch.

        """
        self.model.train()
        total_loss = 0.0
        n_samples = 0

        num_batches = len(self.train_loader)
        logger.info("Starting epoch %d training. Number of batches: %d", epoch, num_batches)

        progress_bar = tqdm(
            self.train_loader,
            desc=f"Epoch {epoch} [train]",
            total=num_batches,
            leave=False,
        )

        for step, batch in enumerate(progress_bar, start=1):
            loss, _, _ = self._step_batch(batch, train=True)
            batch_size = batch["input_ids"].size(0)
            total_loss += loss.item() * batch_size
            n_samples += batch_size

            avg_loss_so_far = total_loss / max(n_samples, 1)
            progress_bar.set_postfix({"loss": f"{avg_loss_so_far:.4f}"})

        avg_loss = total_loss / max(n_samples, 1)
        logger.info("Epoch %d training completed. Average loss: %.4f.", epoch, avg_loss)

        mlflow.log_metric("train_loss", avg_loss, step=epoch)

        return avg_loss

    def evaluate(
        self,
        epoch: int,
        split_name: str = "eval",
    ) -> Tuple[float, float, float, np.ndarray, np.ndarray]:
        """Evaluate the model on `self.eval_loader` and compute metrics.

        Parameters
        ----------
        epoch : int
            Current epoch number (used for logging).
        split_name : str
            Name of the evaluation split used for MLflow metric keys.

        Returns
        -------
        tuple
            (avg_loss, micro_f1, macro_f1, y_true, y_pred)

        """
        self.model.eval()
        total_loss = 0.0
        n_samples = 0
        all_preds: List[np.ndarray] = []
        all_labels: List[np.ndarray] = []

        logger.info("Starting evaluation for epoch %d on split '%s'.", epoch, split_name)

        num_batches = len(self.eval_loader)
        progress_bar = tqdm(
            self.eval_loader,
            desc=f"Epoch {epoch} [{split_name}]",
            total=num_batches,
            leave=False,
        )

        with torch.no_grad():
            for batch in progress_bar:
                loss, logits, labels = self._step_batch(batch, train=False)
                batch_size = logits.size(0)
                total_loss += loss.item() * batch_size
                n_samples += batch_size

                probs = torch.sigmoid(logits)
                preds = (probs > self.cfg.threshold).long()

                all_preds.append(preds.cpu().numpy())
                all_labels.append(labels.cpu().numpy())

                avg_loss_so_far = total_loss / max(n_samples, 1)
                progress_bar.set_postfix({"loss": f"{avg_loss_so_far:.4f}"})

        avg_loss = total_loss / max(n_samples, 1)
        y_pred = np.concatenate(all_preds, axis=0)
        y_true = np.concatenate(all_labels, axis=0)

        # F1
        micro_f1 = f1_score(y_true, y_pred, average="micro", zero_division=0)
        macro_f1 = f1_score(y_true, y_pred, average="macro", zero_division=0)

        # Precision
        micro_precision = precision_score(y_true, y_pred, average="micro", zero_division=0)
        macro_precision = precision_score(y_true, y_pred, average="macro", zero_division=0)

        # Recall
        micro_recall = recall_score(y_true, y_pred, average="micro", zero_division=0)
        macro_recall = recall_score(y_true, y_pred, average="macro", zero_division=0)

        # Accuracy (multi-label)
        # subset_accuracy = exact match of all labels for each sample
        subset_accuracy = accuracy_score(y_true, y_pred)
        # micro_accuracy = accuracy over flattened label indicators
        micro_accuracy = accuracy_score(y_true.flatten(), y_pred.flatten())

        logger.info(
            "Eval results [%s] - loss: %.4f | "
            "micro-F1: %.4f, macro-F1: %.4f | "
            "micro-P: %.4f, macro-P: %.4f | "
            "micro-R: %.4f, macro-R: %.4f | "
            "subset-acc: %.4f, micro-acc: %.4f",
            split_name,
            avg_loss,
            micro_f1,
            macro_f1,
            micro_precision,
            macro_precision,
            micro_recall,
            macro_recall,
            subset_accuracy,
            micro_accuracy,
        )

        # MLflow logging (per epoch)
        mlflow.log_metric(f"{split_name}_loss", avg_loss, step=epoch)
        mlflow.log_metric(f"{split_name}_micro_f1", micro_f1, step=epoch)
        mlflow.log_metric(f"{split_name}_macro_f1", macro_f1, step=epoch)
        mlflow.log_metric(f"{split_name}_micro_precision", micro_precision, step=epoch)
        mlflow.log_metric(f"{split_name}_macro_precision", macro_precision, step=epoch)
        mlflow.log_metric(f"{split_name}_micro_recall", micro_recall, step=epoch)
        mlflow.log_metric(f"{split_name}_macro_recall", macro_recall, step=epoch)
        mlflow.log_metric(f"{split_name}_subset_accuracy", subset_accuracy, step=epoch)
        mlflow.log_metric(f"{split_name}_micro_accuracy", micro_accuracy, step=epoch)

        return avg_loss, micro_f1, macro_f1, y_true, y_pred

    def run(self) -> Dict[str, float]:
        """Execute the full training loop and save the best model.

        Returns
        -------
        dict
            Summary metrics from the final evaluation (micro/macro F1).

        """
        logger.info("Starting training loop for %d epochs.", self.cfg.num_epochs)
        for epoch in range(1, self.cfg.num_epochs + 1):
            train_loss = self.train_one_epoch(epoch)
            val_loss, val_micro_f1, val_macro_f1, _, _ = self.evaluate(epoch, split_name="eval")

            logger.info(
                "[%s] epoch=%d train_loss=%.4f val_loss=%.4f val_micro_f1=%.4f val_macro_f1=%.4f",
                self.cfg.lang,
                epoch,
                train_loss,
                val_loss,
                val_micro_f1,
                val_macro_f1,
            )

            if val_macro_f1 > self.best_val_macro_f1:
                logger.info(
                    "New best macro-F1: %.4f (previous: %.4f). Saving current model state.",
                    val_macro_f1,
                    self.best_val_macro_f1,
                )
                self.best_val_macro_f1 = val_macro_f1
                self.best_state_dict = {k: v.cpu() for k, v in self.model.state_dict().items()}

        if self.best_state_dict is not None:
            logger.info("Loading best model weights (macro-F1 = %.4f).", self.best_val_macro_f1)
            self.model.load_state_dict(self.best_state_dict)

        # final evaluation
        _, micro_f1, macro_f1, y_true, y_pred = self.evaluate(
            epoch=self.cfg.num_epochs,
            split_name="eval",
        )

        logger.info(
            "[%s] FINAL micro-F1 = %.4f, macro-F1 = %.4f.",
            self.cfg.lang,
            micro_f1,
            macro_f1,
        )
        logger.info(
            "Per-label classification report:\n%s",
            classification_report(y_true, y_pred, target_names=self.label_names, zero_division=0),
        )

        # save model and tokenizer
        os.makedirs(self.cfg.model_output_path, exist_ok=True)
        logger.info("Saving model and tokenizer to '%s'.", self.cfg.model_output_path)
        self.model.save_pretrained(self.cfg.model_output_path)
        self.tokenizer.save_pretrained(self.cfg.model_output_path)

        # log model directory as MLflow artifact
        logger.info("Logging final model artifacts to MLflow.")
        mlflow.log_artifacts(
            self.cfg.model_output_path,
            artifact_path=f"{self.cfg.lang}_transformer_model",
        )

        logger.info("Logging HF transformers model to MLflow via mlflow.transformers.log_model.")
        model_info = mlflow.transformers.log_model(
            transformers_model=self.cfg.model_output_path,
            artifact_path=f"{self.cfg.lang}_transformer_model",
            task="text-classification",
        )

        logger.info(
            "Logged transformers model to MLflow with URI: %s",
            model_info.model_uri,
        )

        return {
            "micro_f1": float(micro_f1),
            "macro_f1": float(macro_f1),
        }