File size: 19,648 Bytes
b9412d1
 
 
 
 
c6e4955
 
 
 
b9412d1
 
 
7e3c514
b9412d1
 
 
 
96c4203
d44b5e1
 
a7c7fdd
b9412d1
 
 
 
 
 
 
 
 
 
 
c6e4955
 
 
 
 
b9412d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6e4955
b9412d1
 
84814bd
 
b9412d1
 
 
 
 
 
 
 
 
c6e4955
 
 
 
 
 
 
 
 
b9412d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f8d76d
b9412d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6e4955
b9412d1
c6e4955
 
 
 
b9412d1
 
 
c6e4955
b9412d1
c6e4955
 
 
 
b9412d1
 
 
c6e4955
b9412d1
c6e4955
 
 
 
b9412d1
 
 
 
c6e4955
b9412d1
c6e4955
 
 
 
 
 
 
 
 
b9412d1
 
 
 
 
 
 
 
 
 
 
83ad679
 
 
b9412d1
 
c6e4955
b9412d1
 
 
 
 
 
 
 
 
 
c6e4955
b9412d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6e4955
b9412d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6e4955
b9412d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6e4955
b9412d1
 
 
 
c6e4955
 
 
 
 
83ad679
b9412d1
 
c6e4955
 
 
 
 
b9412d1
 
 
c6e4955
c6912f8
 
 
c6e4955
b9412d1
 
 
 
 
c6e4955
b9412d1
 
c6e4955
b9412d1
 
6730e31
b9412d1
 
 
 
aef2f7d
b9412d1
aef2f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9412d1
 
c6e4955
 
 
 
 
 
 
 
 
 
 
 
 
 
b9412d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f8d76d
 
b9412d1
 
 
 
 
 
 
 
 
3f8d76d
b9412d1
 
 
c6e4955
 
 
 
 
 
b9412d1
 
8d96a34
af84e3a
acc3a63
d44b5e1
b9412d1
 
 
 
 
 
 
 
 
 
 
 
 
 
07c9283
b9412d1
 
a7c7fdd
b9412d1
 
 
 
 
 
a7c7fdd
 
eb97085
a7c7fdd
b9412d1
 
c6e4955
b9412d1
 
 
 
 
 
 
 
14c7a1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9412d1
 
 
 
 
c6e4955
 
 
b9412d1
 
 
 
 
c6e4955
b9412d1
 
 
 
 
 
 
 
 
 
c6e4955
 
 
 
 
 
 
 
 
 
 
 
 
b9412d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c42d8f5
5950d98
 
 
 
c42d8f5
96c4203
c6e4955
96c4203
c6e4955
 
da13cde
96c4203
a49bf5d
973f235
97754b9
6aa8328
96c4203
f50ef72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96c4203
6aa8328
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import torch
import pandas as pd
from transformers import (
    AdamW,
    T5ForConditionalGeneration,
    T5TokenizerFast as T5Tokenizer,
    MT5Tokenizer,
    MT5ForConditionalGeneration,
    ByT5Tokenizer,
)
from torch.utils.data import Dataset, DataLoader
import pytorch_lightning as pl
from dagshub.pytorch_lightning import DAGsHubLogger
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
from pytorch_lightning import LightningDataModule
from pytorch_lightning import LightningModule
from datasets import load_metric
from tqdm.auto import tqdm

import mlflow.pytorch

torch.cuda.empty_cache()
pl.seed_everything(42)


class DataModule(Dataset):
    """
    Data Module for pytorch
    """

    def __init__(
        self,
        data: pd.DataFrame,
        tokenizer: T5Tokenizer,
        source_max_token_len: int = 512,
        target_max_token_len: int = 512,
    ):
        """
        :param data:
        :param tokenizer:
        :param source_max_token_len:
        :param target_max_token_len:
        """
        self.data = data
        self.target_max_token_len = target_max_token_len
        self.source_max_token_len = source_max_token_len
        self.tokenizer = tokenizer

    def __len__(self):
        return len(self.data)

    def __getitem__(self, index: int):
        data_row = self.data.iloc[index]

        input_encoding = self.tokenizer(
            data_row["input_text"],
            max_length=self.source_max_token_len,
            padding="max_length",
            truncation=True,
            return_attention_mask=True,
            add_special_tokens=True,
            return_tensors="pt",
        )

        output_encoding = self.tokenizer(
            data_row["output_text"],
            max_length=self.target_max_token_len,
            padding="max_length",
            truncation=True,
            return_attention_mask=True,
            add_special_tokens=True,
            return_tensors="pt",
        )

        labels = output_encoding["input_ids"]
        labels[labels == 0] = -100

        return dict(
            keywords=data_row["input_text"],
            text=data_row["output_text"],
            keywords_input_ids=input_encoding["input_ids"].flatten(),
            keywords_attention_mask=input_encoding["attention_mask"].flatten(),
            labels=labels.flatten(),
            labels_attention_mask=output_encoding["attention_mask"].flatten(),
        )


class PLDataModule(LightningDataModule):
    def __init__(
        self,
        train_df: pd.DataFrame,
        test_df: pd.DataFrame,
        tokenizer: T5Tokenizer,
        source_max_token_len: int = 512,
        target_max_token_len: int = 512,
        batch_size: int = 4,
        split: float = 0.1,
        num_workers: int = 2,
    ):
        """
        :param data_df:
        :param tokenizer:
        :param source_max_token_len:
        :param target_max_token_len:
        :param batch_size:
        :param split:
        """
        super().__init__()
        self.train_df = train_df
        self.test_df = test_df
        self.split = split
        self.batch_size = batch_size
        self.target_max_token_len = target_max_token_len
        self.source_max_token_len = source_max_token_len
        self.tokenizer = tokenizer
        self.num_workers = num_workers

    def setup(self, stage=None):
        self.train_dataset = DataModule(
            self.train_df,
            self.tokenizer,
            self.source_max_token_len,
            self.target_max_token_len,
        )
        self.test_dataset = DataModule(
            self.test_df,
            self.tokenizer,
            self.source_max_token_len,
            self.target_max_token_len,
        )

    def train_dataloader(self):
        """training dataloader"""
        return DataLoader(
            self.train_dataset,
            batch_size=self.batch_size,
            shuffle=True,
            num_workers=self.num_workers,
        )

    def test_dataloader(self):
        """test dataloader"""
        return DataLoader(
            self.test_dataset,
            batch_size=self.batch_size,
            shuffle=False,
            num_workers=self.num_workers,
        )

    def val_dataloader(self):
        """validation dataloader"""
        return DataLoader(
            self.test_dataset,
            batch_size=self.batch_size,
            shuffle=False,
            num_workers=self.num_workers,
        )


class LightningModel(LightningModule):
    """PyTorch Lightning Model class"""

    def __init__(
        self,
        tokenizer,
        model,
        learning_rate,
        adam_epsilon,
        weight_decay,
        output: str = "outputs",
    ):
        """
        initiates a PyTorch Lightning Model
        Args:
            tokenizer : T5 tokenizer
            model : T5 model
            output (str, optional): output directory to save model checkpoints. Defaults to "outputs".
        """
        super().__init__()
        self.model = model
        self.tokenizer = tokenizer
        self.output = output
        self.learning_rate = learning_rate
        self.adam_epsilon = adam_epsilon
        self.weight_decay = weight_decay

    def forward(self, input_ids, attention_mask, decoder_attention_mask, labels=None):
        """forward step"""
        output = self.model(
            input_ids,
            attention_mask=attention_mask,
            labels=labels,
            decoder_attention_mask=decoder_attention_mask,
        )

        return output.loss, output.logits

    def training_step(self, batch, batch_size):
        """training step"""
        input_ids = batch["keywords_input_ids"]
        attention_mask = batch["keywords_attention_mask"]
        labels = batch["labels"]
        labels_attention_mask = batch["labels_attention_mask"]

        loss, outputs = self(
            input_ids=input_ids,
            attention_mask=attention_mask,
            decoder_attention_mask=labels_attention_mask,
            labels=labels,
        )
        self.log("train_loss", loss, prog_bar=True, logger=True)
        return loss

    def validation_step(self, batch, batch_size):
        """validation step"""
        input_ids = batch["keywords_input_ids"]
        attention_mask = batch["keywords_attention_mask"]
        labels = batch["labels"]
        labels_attention_mask = batch["labels_attention_mask"]

        loss, outputs = self(
            input_ids=input_ids,
            attention_mask=attention_mask,
            decoder_attention_mask=labels_attention_mask,
            labels=labels,
        )
        self.log("val_loss", loss, prog_bar=True, logger=True)
        return loss

    def test_step(self, batch, batch_size):
        """test step"""
        input_ids = batch["keywords_input_ids"]
        attention_mask = batch["keywords_attention_mask"]
        labels = batch["labels"]
        labels_attention_mask = batch["labels_attention_mask"]

        loss, outputs = self(
            input_ids=input_ids,
            attention_mask=attention_mask,
            decoder_attention_mask=labels_attention_mask,
            labels=labels,
        )

        self.log("test_loss", loss, prog_bar=True, logger=True)
        return loss

    def configure_optimizers(self):
        """configure optimizers"""
        model = self.model
        no_decay = ["bias", "LayerNorm.weight"]
        optimizer_grouped_parameters = [
            {
                "params": [
                    p
                    for n, p in model.named_parameters()
                    if not any(nd in n for nd in no_decay)
                ],
                "weight_decay": self.weight_decay,
            },
            {
                "params": [
                    p
                    for n, p in model.named_parameters()
                    if any(nd in n for nd in no_decay)
                ],
                "weight_decay": 0.0,
            },
        ]
        optimizer = AdamW(
            optimizer_grouped_parameters,
            lr=self.learning_rate,
            eps=self.adam_epsilon,
        )
        self.opt = optimizer
        return [optimizer]


class Summarization:
    """Custom Summarization class"""

    def __init__(self) -> None:
        """initiates Summarization class"""
        pass

    def from_pretrained(self, model_type="t5", model_name="t5-base") -> None:
        """
        loads T5/MT5 Model model for training/finetuning
        Args:
            model_name (str, optional): exact model architecture name, "t5-base" or "t5-large". Defaults to "t5-base".
            :param model_type:
        """
        if model_type == "t5":
            self.tokenizer = T5Tokenizer.from_pretrained(f"{model_name}")
            self.model = T5ForConditionalGeneration.from_pretrained(
                f"{model_name}", return_dict=True
            )
        elif model_type == "mt5":
            self.tokenizer = MT5Tokenizer.from_pretrained(f"{model_name}")
            self.model = MT5ForConditionalGeneration.from_pretrained(
                f"{model_name}", return_dict=True
            )
        elif model_type == "byt5":
            self.tokenizer = ByT5Tokenizer.from_pretrained(f"{model_name}")
            self.model = T5ForConditionalGeneration.from_pretrained(
                f"{model_name}", return_dict=True
            )

    def train(
        self,
        train_df: pd.DataFrame,
        eval_df: pd.DataFrame,
        source_max_token_len: int = 512,
        target_max_token_len: int = 512,
        batch_size: int = 8,
        max_epochs: int = 5,
        use_gpu: bool = True,
        outputdir: str = "models",
        early_stopping_patience_epochs: int = 0,  # 0 to disable early stopping feature
        learning_rate: float = 0.0001,
        adam_epsilon: float = 0.01,
        num_workers: int = 2,
        weight_decay: float = 0.0001,
    ):
        """
        trains T5/MT5 model on custom dataset
        Args:
            train_df (pd.DataFrame): training datarame. Dataframe must have 2 column --> "input_text" and "output_text"
            eval_df ([type], optional): validation datarame. Dataframe must have 2 column --> "input_text" and
            "output_text"
            source_max_token_len (int, optional): max token length of source text. Defaults to 512.
            target_max_token_len (int, optional): max token length of target text. Defaults to 512.
            batch_size (int, optional): batch size. Defaults to 8.
            max_epochs (int, optional): max number of epochs. Defaults to 5.
            use_gpu (bool, optional): if True, model uses gpu for training. Defaults to True.
            outputdir (str, optional): output directory to save model checkpoints. Defaults to "outputs".
            early_stopping_patience_epochs (int, optional): monitors val_loss on epoch end and stops training,
            if val_loss does not improve after the specied number of epochs. set 0 to disable early stopping.
            Defaults to 0 (disabled)
            :param learning_rate:
            :param adam_epsilon:
        """
        self.target_max_token_len = target_max_token_len
        self.data_module = PLDataModule(
            train_df,
            eval_df,
            self.tokenizer,
            batch_size=batch_size,
            source_max_token_len=source_max_token_len,
            target_max_token_len=target_max_token_len,
            num_workers=num_workers,
        )

        self.T5Model = LightningModel(
            tokenizer=self.tokenizer,
            model=self.model,
            output=outputdir,
            learning_rate=learning_rate,
            adam_epsilon=adam_epsilon,
            weight_decay=weight_decay,
        )

        logger = DAGsHubLogger(
            metrics_path="reports/training_metrics.csv", should_log_hparams=False
        )

        early_stop_callback = (
            [
                EarlyStopping(
                    monitor="val_loss",
                    min_delta=0.00,
                    patience=early_stopping_patience_epochs,
                    verbose=True,
                    mode="min",
                )
            ]
            if early_stopping_patience_epochs > 0
            else None
        )

        gpus = -1 if use_gpu and torch.cuda.is_available() else 0

        trainer = Trainer(
            logger=logger,
            callbacks=early_stop_callback,
            max_epochs=max_epochs,
            gpus=gpus,
            progress_bar_refresh_rate=5,
        )

        mlflow.pytorch.autolog(log_models=False)

        with mlflow.start_run() as run:  # noqa: F841
            trainer.fit(self.T5Model, self.data_module)

    def load_model(
        self, model_type: str = "t5", model_dir: str = "models", use_gpu: bool = False
    ):
        """
        loads a checkpoint for inferencing/prediction
        Args:
            model_type (str, optional): "t5" or "mt5". Defaults to "t5".
            model_dir (str, optional): path to model directory. Defaults to "outputs".
            use_gpu (bool, optional): if True, model uses gpu for inferencing/prediction. Defaults to True.
        """
        if model_type == "t5":
            self.tokenizer = T5Tokenizer.from_pretrained(f"{model_dir}")
            self.model = T5ForConditionalGeneration.from_pretrained(
                f"{model_dir}", return_dict=True
            )
        elif model_type == "mt5":
            self.tokenizer = MT5Tokenizer.from_pretrained(f"{model_dir}")
            self.model = MT5ForConditionalGeneration.from_pretrained(
                f"{model_dir}", return_dict=True
            )
        elif model_type == "byt5":
            self.tokenizer = ByT5Tokenizer.from_pretrained(f"{model_dir}")
            self.model = T5ForConditionalGeneration.from_pretrained(
                f"{model_dir}", return_dict=True
            )

        if use_gpu:
            if torch.cuda.is_available():
                self.device = torch.device("cuda")
            else:
                raise Exception(
                    "exception ---> no gpu found. set use_gpu=False, to use CPU"
                )
        else:
            self.device = torch.device("cpu")

        self.model = self.model.to(self.device)

    def save_model(self, model_dir="models"):
        """
        Save model to dir
        :param model_dir:
        :return: model is saved
        """
        path = f"{model_dir}"
        self.tokenizer.save_pretrained(path)
        self.model.save_pretrained(path)

    def predict(
        self,
        source_text: str,
        max_length: int = 512,
        num_return_sequences: int = 1,
        num_beams: int = 2,
        top_k: int = 50,
        top_p: float = 0.95,
        do_sample: bool = True,
        repetition_penalty: float = 2.5,
        length_penalty: float = 1.0,
        early_stopping: bool = True,
        skip_special_tokens: bool = True,
        clean_up_tokenization_spaces: bool = True,
    ):
        """
        generates prediction for T5/MT5 model
        Args:
            source_text (str): any text for generating predictions
            max_length (int, optional): max token length of prediction. Defaults to 512.
            num_return_sequences (int, optional): number of predictions to be returned. Defaults to 1.
            num_beams (int, optional): number of beams. Defaults to 2.
            top_k (int, optional): Defaults to 50.
            top_p (float, optional): Defaults to 0.95.
            do_sample (bool, optional): Defaults to True.
            repetition_penalty (float, optional): Defaults to 2.5.
            length_penalty (float, optional): Defaults to 1.0.
            early_stopping (bool, optional): Defaults to True.
            skip_special_tokens (bool, optional): Defaults to True.
            clean_up_tokenization_spaces (bool, optional): Defaults to True.
        Returns:
            list[str]: returns predictions
        """
        input_ids = self.tokenizer.encode(
            source_text, return_tensors="pt", add_special_tokens=True
        )

        input_ids = input_ids.to(self.device)
        generated_ids = self.model.generate(
            input_ids=input_ids,
            num_beams=num_beams,
            max_length=max_length,
            repetition_penalty=repetition_penalty,
            length_penalty=length_penalty,
            early_stopping=early_stopping,
            top_p=top_p,
            top_k=top_k,
            num_return_sequences=num_return_sequences,
        )
        preds = self.tokenizer.decode(
            generated_ids[0],
            skip_special_tokens=skip_special_tokens,
            clean_up_tokenization_spaces=clean_up_tokenization_spaces,
        )
        return preds

    def evaluate(self, test_df: pd.DataFrame, metrics: str = "rouge"):
        metric = load_metric(metrics)
        input_text = test_df["input_text"]
        references = test_df["output_text"]
        references = references.to_list()

        predictions = [self.predict(x) for x in tqdm(input_text)]

        results = metric.compute(predictions=predictions, references=references)

        output = {
            "Rouge_1 Low Precision": results["rouge1"].low.precision,
            "Rouge_1 Low recall": results["rouge1"].low.recall,
            "Rouge_1 Low F1": results["rouge1"].low.fmeasure,
            "Rouge_1 Mid Precision": results["rouge1"].mid.precision,
            "Rouge_1 Mid recall": results["rouge1"].mid.recall,
            "Rouge_1 Mid F1": results["rouge1"].mid.fmeasure,
            "Rouge_1 High Precision": results["rouge1"].high.precision,
            "Rouge_1 High recall": results["rouge1"].high.recall,
            "Rouge_1 High F1": results["rouge1"].high.fmeasure,
            "Rouge_2 Low Precision": results["rouge2"].low.precision,
            "Rouge_2 Low recall": results["rouge2"].low.recall,
            "Rouge_2 Low F1": results["rouge2"].low.fmeasure,
            "Rouge_2 Mid Precision": results["rouge2"].mid.precision,
            "Rouge_2 Mid recall": results["rouge2"].mid.recall,
            "Rouge_2 Mid F1": results["rouge2"].mid.fmeasure,
            "Rouge_2 High Precision": results["rouge2"].high.precision,
            "Rouge_2 High recall": results["rouge2"].high.recall,
            "Rouge_2 High F1": results["rouge2"].high.fmeasure,
            "Rouge_L Low Precision": results["rougeL"].low.precision,
            "Rouge_L Low recall": results["rougeL"].low.recall,
            "Rouge_L Low F1": results["rougeL"].low.fmeasure,
            "Rouge_L Mid Precision": results["rougeL"].mid.precision,
            "Rouge_L Mid recall": results["rougeL"].mid.recall,
            "Rouge_L Mid F1": results["rougeL"].mid.fmeasure,
            "Rouge_L High Precision": results["rougeL"].high.precision,
            "Rouge_L High recall": results["rougeL"].high.recall,
            "Rouge_L High F1": results["rougeL"].high.fmeasure,
            "rougeLsum Low Precision": results["rougeLsum"].low.precision,
            "rougeLsum Low recall": results["rougeLsum"].low.recall,
            "rougeLsum Low F1": results["rougeLsum"].low.fmeasure,
            "rougeLsum Mid Precision": results["rougeLsum"].mid.precision,
            "rougeLsum Mid recall": results["rougeLsum"].mid.recall,
            "rougeLsum Mid F1": results["rougeLsum"].mid.fmeasure,
            "rougeLsum High Precision": results["rougeLsum"].high.precision,
            "rougeLsum High recall": results["rougeLsum"].high.recall,
            "rougeLsum High F1": results["rougeLsum"].high.fmeasure,
        }
        return output