File size: 15,110 Bytes
4d88e8a
 
3789adf
4d88e8a
 
 
 
 
 
 
d13fe3a
644bf8b
4d88e8a
 
12106f5
4d88e8a
 
 
 
 
 
 
 
 
 
92b61fd
 
 
e78759d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d88e8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92b61fd
348c52a
644bf8b
4d88e8a
 
 
 
 
 
 
 
92b61fd
4d88e8a
644bf8b
92b61fd
4d88e8a
92b61fd
 
 
 
 
 
 
4d88e8a
92b61fd
 
 
644bf8b
92b61fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e831ea
92b61fd
 
c364843
92b61fd
 
 
 
644bf8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e78759d
 
 
 
 
 
 
 
 
644bf8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e78759d
 
644bf8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f15dd4
 
db45d5c
 
644bf8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69299ee
 
e78759d
db45d5c
 
644bf8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69299ee
 
 
 
 
 
 
644bf8b
69299ee
138c00c
be9590d
0915787
644bf8b
497666d
644bf8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0f3374
644bf8b
4d88e8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348c52a
644bf8b
348c52a
 
4d88e8a
 
644bf8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
""" Fine-tune T5 on topic classification (multi-label multi-class classification)
```
python finetune_t5.py --dataset-name ja --model-alias mt5-small-tweet-topic-ja --model-organization cardiffnlp --low-cpu-mem-usage
```
"""
import json
import logging
import os
import argparse
import gc
from glob import glob
from typing import List, Set
from shutil import copyfile
from statistics import mean
from distutils.dir_util import copy_tree

import torch
import transformers
from datasets import load_dataset
from transformers import Seq2SeqTrainer, Seq2SeqTrainingArguments, pipeline
from huggingface_hub import Repository


os.environ['TOKENIZERS_PARALLELISM'] = 'false'  # turn-off the warning message
os.environ['WANDB_DISABLED'] = 'true'  # disable wandb
_LR = [1e-6, 1e-5, 1e-4]
_BATCH = 32
_EPOCH = 5
_CLASS_MAP = {
    'Arts & Culture': ['Τέχνες & Πολιτισμός', 'Arte y cultura', 'アート&カルチャー'],
    'Business & Entrepreneurs': ['Επιχειρήσεις & Επιχειρηματίες', 'Negocios y emprendedores', 'ビジネス'],
    'Celebrity & Pop Culture': ['Διασημότητες & Ποπ κουλτούρα', 'Celebridades y cultura pop', '芸能'],
    'Diaries & Daily Life': ['Ημερολόγια & Καθημερινή ζωή', 'Diarios y vida diaria', '日常'],
    'Family': ['Οικογένεια', 'Familia', '家族'],
    'Fashion & Style': ['Μόδα & Στυλ', 'Moda y estilo', 'ファッション'],
    'Film, TV & Video': ['Ταινίες, τηλεόραση & βίντεο', 'Cine, televisión y video', '映画&ラジオ'],
    'Fitness & Health': ['Γυμναστική & Υεία', 'Estado físico y salud', 'フィットネス&健康'],
    'Food & Dining': ['Φαγητό & Δείπνο', 'Comida y comedor', '料理'],
    'Learning & Educational': ['Μάθηση & Εκπαίδευση', 'Aprendizaje y educación', '教育関連'],
    'News & Social Concern': ['Ειδήσεις & Κοινωνία', 'Noticias e interés social', '社会'],
    'Relationships': ['Σχέσεις', 'Relaciones', '人間関係'],
    'Science & Technology': ['Επιστήμη & Τεχνολογία', 'Ciencia y Tecnología', 'サイエンス'],
    'Youth & Student Life': ['Νεανική & Φοιτητική ζωή', 'Juventud y Vida Estudiantil', '学校'],
    'Music': ['Μουσική', 'Música', '音楽'],
    'Gaming': ['Παιχνίδια', 'Juegos', 'ゲーム'],
    'Sports': ['Αθλητισμός', 'Deportes', 'スポーツ'],
    'Travel & Adventure': ['Ταξίδια & Περιπέτεια', 'Viajes y aventuras', '旅行'],
    'Other Hobbies': ['Άλλα χόμπι', 'Otros pasatiempos', 'その他']
}


def load_model(
        model_name: str,
        use_auth_token: bool = False,
        low_cpu_mem_usage: bool = False) -> transformers.PreTrainedModel:
    """Load language model from huggingface model hub."""
    # config & tokenizer
    config = transformers.AutoConfig.from_pretrained(model_name, use_auth_token=use_auth_token)
    if config.model_type == 't5':  # T5 model requires T5ForConditionalGeneration class
        model_class = transformers.T5ForConditionalGeneration.from_pretrained
    elif config.model_type == 'mt5':
        model_class = transformers.MT5ForConditionalGeneration.from_pretrained
    elif config.model_type == 'bart':
        model_class = transformers.BartForConditionalGeneration.from_pretrained
    elif config.model_type == 'mbart':
        model_class = transformers.MBartForConditionalGeneration.from_pretrained
    else:
        raise ValueError(f'unsupported model type: {config.model_type}')
    param = {'config': config, 'use_auth_token': use_auth_token, 'low_cpu_mem_usage': low_cpu_mem_usage}
    return model_class(model_name, **param)


def train(
        model_name: str,
        model_low_cpu_mem_usage: bool,
        dataset: str,
        dataset_name: str,
        dataset_column_label: str,
        dataset_column_text: str,
        random_seed: int,
        use_auth_token: bool):
    """Fine-tune seq2seq model."""
    logging.info(f'[TRAIN]\n\t *LM: {model_name}, \n\t *Data: {dataset} ({dataset_name})')
    output_dir = f'ckpt/{os.path.basename(model_name)}.{os.path.basename(dataset)}.{dataset_name}'

    tokenizer = transformers.AutoTokenizer.from_pretrained(model_name, use_auth_token=use_auth_token)
    dataset_instance = load_dataset(dataset, dataset_name, split="train", use_auth_token=use_auth_token)
    tokenized_dataset = []
    for d in dataset_instance:
        model_inputs = tokenizer(d[dataset_column_text], truncation=True)
        model_inputs['labels'] = tokenizer(text_target=d[dataset_column_label], truncation=True)['input_ids']
        tokenized_dataset.append(model_inputs)

    for n, lr_tmp in enumerate(_LR):
        logging.info(f"[TRAIN {n}/{len(_LR)}] lr: {lr_tmp}")
        output_dir_tmp = f"{output_dir}/model_lr_{lr_tmp}"
        if os.path.exists(f"{output_dir_tmp}/pytorch_model.bin"):
            continue
        model = load_model(
            model_name=model_name, use_auth_token=use_auth_token, low_cpu_mem_usage=model_low_cpu_mem_usage
        )
        trainer = Seq2SeqTrainer(
            model=model,
            args=Seq2SeqTrainingArguments(
                num_train_epochs=_EPOCH,
                learning_rate=lr_tmp,
                output_dir=output_dir_tmp,
                save_strategy="epoch",
                evaluation_strategy="no",
                seed=random_seed,
                per_device_train_batch_size=_BATCH,
            ),
            data_collator=transformers.DataCollatorForSeq2Seq(tokenizer, model=model),
            train_dataset=tokenized_dataset.copy(),
        )
        # train
        trainer.train()
        del trainer
        del model
        gc.collect()
        torch.cuda.empty_cache()

    for model_path in glob(f"{output_dir}/*/*"):
        tokenizer.save_pretrained(model_path)


def get_f1_score(references: List[Set[str]], predictions: List[Set[str]]) -> float:
    scores = []
    for g, r in zip(references, predictions):
        tp = len(set(g).intersection(set(r)))
        fp = len([_g for _g in g if _g not in r])
        fn = len([_r for _r in r if _r not in g])
        f1 = 0 if tp == 0 else 2 * tp / (2 * tp + fp + fn)
        scores.append(f1)
    return mean(scores)


def unify_label(label: Set[str]):
    new_label = []
    for label_tmp in label:
        label_en = [k for k, v in _CLASS_MAP.items() if label_tmp in v]
        if label_en:
            new_label.append(label_en[0])
    return set(new_label)


def get_metric(
        prediction_file: str,
        metric_file: str,
        model_path: str,
        data: List[str],
        label: List[str]) -> float:
    if os.path.exists(metric_file):
        with open(metric_file) as f:
            eval_metric = json.load(f)
        return eval_metric['f1']
    if not os.path.exists(prediction_file):
        pipe = pipeline(
            'text2text-generation',
            model=model_path,
            device='cuda:0' if torch.cuda.is_available() else 'cpu',
        )
        output = pipe(data, batch_size=_BATCH)
        output = [i['generated_text'] for i in output]
        with open(prediction_file, 'w') as f:
            f.write('\n'.join(output))
    with open(prediction_file) as f:
        output = [unify_label(set(i.split(','))) for i in f.read().split('\n')]
    label = [unify_label(set(i.split(','))) for i in label]
    eval_metric = {'f1': get_f1_score(label, output)}
    logging.info(json.dumps(eval_metric, indent=4))
    with open(metric_file, 'w') as f:
        json.dump(eval_metric, f)
    return eval_metric['f1']


def validate(
        model_name: str,
        dataset: str,
        dataset_name: str,
        dataset_column_text: str,
        use_auth_token: bool,
        dataset_column_label: str):
    logging.info(f'[VALIDATE]\n\t *LM: {model_name}, \n\t *Data: {dataset} ({dataset_name})')
    output_dir = f'ckpt/{os.path.basename(model_name)}.{os.path.basename(dataset)}.{dataset_name}'
    dataset_instance = load_dataset(dataset, dataset_name, split='validation', use_auth_token=use_auth_token)
    label = [i[dataset_column_label] for i in dataset_instance]
    data = [i[dataset_column_text] for i in dataset_instance]
    model_score = []
    for model_path in glob(f"{output_dir}/*/*/pytorch_model.bin"):
        model_path = os.path.dirname(model_path)
        prediction_file = f"{model_path}/prediction.validate.{os.path.basename(dataset)}.{dataset_name}.txt"
        metric_file = f"{model_path}/metric.validate.{os.path.basename(dataset)}.{dataset_name}.json"
        metric = get_metric(
            prediction_file=prediction_file,
            metric_file=metric_file,
            model_path=model_path,
            label=label,
            data=data
        )
        model_score.append([model_path, metric])
    model_score = sorted(model_score, key=lambda x: x[1])
    logging.info('Validation Result')
    for k, v in model_score:
        logging.info(f'{k}: {v}')
    best_model = model_score[-1][0]
    best_model_path = f'{output_dir}/best_model'
    copy_tree(best_model, best_model_path)


def test(
        model_name: str,
        dataset: str,
        dataset_name: str,
        dataset_column_text: str,
        use_auth_token: bool,
        dataset_column_label: str):
    logging.info(f'[TEST]\n\t *LM: {model_name}, \n\t *Data: {dataset} ({dataset_name})')
    output_dir = f'ckpt/{os.path.basename(model_name)}.{os.path.basename(dataset)}.{dataset_name}'
    dataset_instance = load_dataset(dataset, dataset_name, split='test', use_auth_token=use_auth_token)
    label = [i[dataset_column_label] for i in dataset_instance]
    data = [i[dataset_column_text] for i in dataset_instance]
    model_path = f'{output_dir}/best_model'
    if not os.path.exists(model_path):
        model_path = os.path.basename(model_name)

    prediction_file = f"{model_path}/prediction.{os.path.basename(dataset)}.{dataset_name}.txt"
    metric_file = f"{model_path}/metric.{os.path.basename(dataset)}.{dataset_name}.json"
    metric = get_metric(
        prediction_file=prediction_file,
        metric_file=metric_file,
        model_path=model_path,
        label=label,
        data=data
    )
    logging.info(f'Test Result: {metric}')


def upload(
        model_name: str,
        dataset: str,
        dataset_name: str,
        dataset_column_text: str,
        use_auth_token: bool,
        model_alias: str,
        model_organization: str):
    assert model_alias is not None and model_organization is not None,\
        'model_organization must be specified when model_alias is specified'
    logging.info('uploading to huggingface')
    output_dir = f'ckpt/{os.path.basename(model_name)}.{os.path.basename(dataset)}.{dataset_name}'
    args = {'use_auth_token': use_auth_token, 'organization': model_organization}
    model_path = f'{output_dir}/best_model'
    if not os.path.exists(model_path):
        model_path = os.path.basename(model_name)
    model = load_model(model_name=model_path)
    tokenizer = transformers.AutoTokenizer.from_pretrained(model_name, use_auth_token=use_auth_token)
    model.push_to_hub(model_alias, **args)
    tokenizer.push_to_hub(model_alias, **args)
    repo = Repository(model_alias, f'{model_organization}/{model_alias}')
    for i in glob(f'{model_path}/*'):
        if not os.path.exists(f'{model_alias}/{os.path.basename(i)}'):
            copyfile(i, f'{model_alias}/{os.path.basename(i)}')
    dataset_instance = load_dataset(dataset, dataset_name, split='validation', use_auth_token=use_auth_token)
    sample = [i[dataset_column_text] for i in dataset_instance]
    sample = [i for i in sample if "'" not in i and '"' not in i][:3]
    widget = '\n'.join([f"- text: '{t}'\n  example_title: example {_n + 1}" for _n, t in enumerate(sample)])
    with open(f'{model_alias}/README.md', 'w') as f:
        f.write(f"""
---
widget:
{widget}
---

# {model_organization}/{model_alias}

This is [{model_name}](https://huggingface.co/{model_name}) fine-tuned on [{dataset} ({dataset_name})](https://huggingface.co/datasets/{dataset}).

### Usage

```python
from transformers import pipeline

pipe = pipeline('text2text-generation', model='{model_organization}/{model_alias}')
output = pipe('{sample[0]}')
```
        """)
    repo.push_to_hub()


if __name__ == '__main__':
    # arguments
    logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s', level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S')
    parser = argparse.ArgumentParser(description='Seq2Seq LM Fine-tuning on topic classification.')
    parser.add_argument('-m', '--model-name', default='google/mt5-small', type=str)
    parser.add_argument('--low-cpu-mem-usage', action='store_true')
    parser.add_argument('-d', '--dataset', default='cardiffnlp/tweet_topic_multilingual', type=str)
    parser.add_argument('--dataset-name', default='ja', type=str)
    parser.add_argument('--dataset-column-label', default='label_name_flatten', type=str)
    parser.add_argument('--dataset-column-text', default='text', type=str)
    parser.add_argument('--random-seed', default=42, type=int)
    parser.add_argument('--use-auth-token', action='store_true')
    parser.add_argument('--model-alias', default=None, type=str)
    parser.add_argument('--model-organization', default=None, type=str)
    parser.add_argument('--skip-train', action='store_true')
    parser.add_argument('--skip-validate', action='store_true')
    parser.add_argument('--skip-test', action='store_true')
    parser.add_argument('--skip-upload', action='store_true')
    opt = parser.parse_args()

    if not opt.skip_train:
        train(
            model_name=opt.model_name,
            model_low_cpu_mem_usage=opt.low_cpu_mem_usage,
            dataset=opt.dataset,
            dataset_name=opt.dataset_name,
            dataset_column_label=opt.dataset_column_label,
            dataset_column_text=opt.dataset_column_text,
            random_seed=opt.random_seed,
            use_auth_token=opt.use_auth_token,
        )
    if not opt.skip_validate:
        validate(
            model_name=opt.model_name,
            dataset=opt.dataset,
            dataset_name=opt.dataset_name,
            dataset_column_label=opt.dataset_column_label,
            dataset_column_text=opt.dataset_column_text,
            use_auth_token=opt.use_auth_token
        )
    if not opt.skip_test:
        test(
            model_name=opt.model_name,
            dataset=opt.dataset,
            dataset_name=opt.dataset_name,
            dataset_column_label=opt.dataset_column_label,
            dataset_column_text=opt.dataset_column_text,
            use_auth_token=opt.use_auth_token
        )
    if not opt.skip_upload:
        upload(
            model_name=opt.model_name,
            dataset=opt.dataset,
            dataset_name=opt.dataset_name,
            dataset_column_text=opt.dataset_column_text,
            use_auth_token=opt.use_auth_token,
            model_alias=opt.model_alias,
            model_organization=opt.model_organization
        )