makitanikaze commited on
Commit
65dff1c
1 Parent(s): 518421d

Upload P5Pretraining

Browse files
Files changed (3) hide show
  1. config.json +67 -0
  2. pretrain_model.py +133 -0
  3. pytorch_model.bin +3 -0
config.json ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "t5-small",
3
+ "activation_dropout": 0.1,
4
+ "architectures": [
5
+ "P5Pretraining"
6
+ ],
7
+ "attention_dropout": 0.1,
8
+ "auto_map": {
9
+ "AutoModel": "pretrain_model.P5Pretraining"
10
+ },
11
+ "d_ff": 2048,
12
+ "d_kv": 64,
13
+ "d_model": 512,
14
+ "decoder_start_token_id": 0,
15
+ "dense_act_fn": "relu",
16
+ "dropout": 0.1,
17
+ "dropout_rate": 0.1,
18
+ "eos_token_id": 1,
19
+ "feed_forward_proj": "relu",
20
+ "initializer_factor": 1.0,
21
+ "is_encoder_decoder": true,
22
+ "is_gated_act": false,
23
+ "layer_norm_epsilon": 1e-06,
24
+ "losses": "rating,sequential,explanation,review,traditional",
25
+ "model_type": "t5",
26
+ "n_positions": 512,
27
+ "num_decoder_layers": 6,
28
+ "num_heads": 8,
29
+ "num_layers": 6,
30
+ "output_past": true,
31
+ "pad_token_id": 0,
32
+ "relative_attention_max_distance": 128,
33
+ "relative_attention_num_buckets": 32,
34
+ "task_specific_params": {
35
+ "summarization": {
36
+ "early_stopping": true,
37
+ "length_penalty": 2.0,
38
+ "max_length": 200,
39
+ "min_length": 30,
40
+ "no_repeat_ngram_size": 3,
41
+ "num_beams": 4,
42
+ "prefix": "summarize: "
43
+ },
44
+ "translation_en_to_de": {
45
+ "early_stopping": true,
46
+ "max_length": 300,
47
+ "num_beams": 4,
48
+ "prefix": "translate English to German: "
49
+ },
50
+ "translation_en_to_fr": {
51
+ "early_stopping": true,
52
+ "max_length": 300,
53
+ "num_beams": 4,
54
+ "prefix": "translate English to French: "
55
+ },
56
+ "translation_en_to_ro": {
57
+ "early_stopping": true,
58
+ "max_length": 300,
59
+ "num_beams": 4,
60
+ "prefix": "translate English to Romanian: "
61
+ }
62
+ },
63
+ "torch_dtype": "float32",
64
+ "transformers_version": "4.25.1",
65
+ "use_cache": true,
66
+ "vocab_size": 32100
67
+ }
pretrain_model.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ import numpy as np
5
+
6
+ from modeling_p5 import P5
7
+
8
+ class P5Pretraining(P5):
9
+ def __init__(self, config):
10
+ super().__init__(config)
11
+
12
+ self.losses = self.config.losses.split(',')
13
+
14
+ def train_step(self, batch):
15
+
16
+ device = next(self.parameters()).device
17
+ input_ids = batch['input_ids'].to(device)
18
+ whole_word_ids = batch['whole_word_ids'].to(device)
19
+
20
+ lm_labels = batch["target_ids"].to(device)
21
+
22
+ loss_weights = batch["loss_weights"].to(device)
23
+
24
+ output = self(
25
+ input_ids=input_ids,
26
+ whole_word_ids=whole_word_ids,
27
+ labels=lm_labels,
28
+ return_dict=True
29
+ )
30
+ assert 'loss' in output
31
+
32
+ lm_mask = lm_labels != -100
33
+ lm_mask = lm_mask.float()
34
+ B, L = lm_labels.size()
35
+
36
+ loss = output['loss']
37
+
38
+ loss = loss.view(B, L) * lm_mask
39
+
40
+ loss = loss.sum(dim=1) / lm_mask.sum(dim=1).clamp(min=1)
41
+
42
+ task_counts = {task: 0 for task in self.losses}
43
+ task_loss = {task: 0 for task in self.losses}
44
+
45
+ results = {}
46
+
47
+ results['loss'] = (loss * loss_weights).mean()
48
+ results['total_loss'] = loss.detach().sum()
49
+ results['total_loss_count'] = len(loss)
50
+
51
+ task_counts = {task: 0 for task in self.losses}
52
+ task_loss = {task: 0 for task in self.losses}
53
+
54
+ for _loss, task in zip(loss.detach(), batch['task']):
55
+ task_loss[task] += _loss
56
+ task_counts[task] += 1
57
+
58
+ for task in self.losses:
59
+ if task_counts[task] > 0:
60
+ results[f'{task}_loss'] = task_loss[task]
61
+ results[f'{task}_loss_count'] = task_counts[task]
62
+
63
+ return results
64
+
65
+ @torch.no_grad()
66
+ def valid_step(self, batch):
67
+ self.eval()
68
+ device = next(self.parameters()).device
69
+ input_ids = batch['input_ids'].to(device)
70
+
71
+ lm_labels = batch["target_ids"].to(device)
72
+
73
+ loss_weights = batch["loss_weights"].to(device)
74
+
75
+ output = self(
76
+ input_ids=input_ids,
77
+ labels=lm_labels,
78
+ return_dict=True
79
+ )
80
+ assert 'loss' in output
81
+
82
+ lm_mask = lm_labels != -100
83
+ lm_mask = lm_mask.float()
84
+ B, L = lm_labels.size()
85
+
86
+ loss = output['loss']
87
+
88
+ loss = loss.view(B, L) * lm_mask
89
+
90
+ loss = loss.sum(dim=1) / lm_mask.sum(dim=1).clamp(min=1)
91
+
92
+ results = {}
93
+
94
+ results['loss'] = (loss * loss_weights).mean()
95
+ results['total_loss'] = loss.detach().sum()
96
+ results['total_loss_count'] = len(loss)
97
+
98
+ task_counts = {task: 0 for task in self.losses}
99
+ task_loss = {task: 0 for task in self.losses}
100
+
101
+ for _loss, task in zip(loss.detach(), batch['task']):
102
+ task_loss[task] += _loss
103
+ task_counts[task] += 1
104
+
105
+ for task in self.losses:
106
+ if task_counts[task] > 0:
107
+ results[f'{task}_loss'] = task_loss[task]
108
+ results[f'{task}_loss_count'] = task_counts[task]
109
+
110
+ if 'rating' in self.losses:
111
+ output = self.generate(
112
+ input_ids=input_ids
113
+ )
114
+
115
+ generated_score = self.tokenizer.batch_decode(output, skip_special_tokens=True)
116
+
117
+ results['rating_pred'] = generated_score
118
+
119
+ return results
120
+
121
+ @torch.no_grad()
122
+ def generate_step(self, batch):
123
+ self.eval()
124
+ device = next(self.parameters()).device
125
+ input_ids = batch['input_ids'].to(device)
126
+
127
+ output = self.generate(
128
+ input_ids=input_ids,
129
+ )
130
+
131
+ generated_sents = self.tokenizer.batch_decode(output, skip_special_tokens=True)
132
+
133
+ return generated_sents
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5b7dcf47e8c95b8a985adc0e526a8b416dcf9d969159980005c7500e023ce95a
3
+ size 243063197