|
| 1 | +import sys |
| 2 | +import numpy as np |
| 3 | +import jsonlines as jsonl |
| 4 | +from transformers import GPT2TokenizerFast, TFGPT2LMHeadModel |
| 5 | +import tensorflow as tf |
| 6 | +from tensorflow.keras import metrics |
| 7 | + |
| 8 | + |
| 9 | +def get_dataset(fil): |
| 10 | + data = [] |
| 11 | + with jsonl.open(fil) as reader: |
| 12 | + for line in reader: |
| 13 | + data.append(line['text']) |
| 14 | + return data |
| 15 | + |
| 16 | +if len(sys.argv) == 1: |
| 17 | + model_size = "Small" |
| 18 | + data_dir = '/dockerx/data/tf-gpt-2/data/' |
| 19 | + num_epochs = 1 |
| 20 | + num_gpus = len(tf.config.list_physical_devices(device_type='GPU')) |
| 21 | + truncate = True |
| 22 | +else: |
| 23 | + model_size = sys.argv[1] |
| 24 | + data_dir = sys.argv[2] |
| 25 | + num_epochs = int(sys.argv[3]) |
| 26 | + num_gpus = int(sys.argv[4]) |
| 27 | + if int(sys.argv[5]) == 1: |
| 28 | + truncate = True |
| 29 | + else: |
| 30 | + truncate = False |
| 31 | + |
| 32 | +if model_size == "Small": |
| 33 | + model_name = "gpt2" |
| 34 | + train_file = data_dir+'small-117M-k40.train.jsonl' |
| 35 | + valid_file = data_dir+'small-117M-k40.valid.jsonl' |
| 36 | +elif model_size == "Medium": |
| 37 | + model_name = "gpt2-medium" |
| 38 | + train_file = data_dir+'medium-345M-k40.train.jsonl' |
| 39 | + valid_file = data_dir+'medium-345M-k40.valid.jsonl' |
| 40 | +elif model_size == "Large": |
| 41 | + model_name = "gpt2-large" |
| 42 | + train_file = data_dir+'large-762M-k40.train.jsonl' |
| 43 | + valid_file = data_dir+'large-762M-k40.valid.jsonl' |
| 44 | +elif model_size == "XL": |
| 45 | + model_name = 'gpt2-xl' |
| 46 | + train_file = data_dir+'xl-1542M-k40.train.jsonl' |
| 47 | + valid_file = data_dir+'xl-1542M-k40.valid.jsonl' |
| 48 | +print("Finetuning model " + model_name) |
| 49 | +print("With dataset "+train_file) |
| 50 | + |
| 51 | +def tokenize(data, tokenizer, truncate=False): |
| 52 | + if truncate: |
| 53 | + data = tokenizer(data[:1000], return_tensors='tf', padding=True, truncation=True) |
| 54 | + else: |
| 55 | + data = tokenizer(data, return_tensors='tf', padding=True, truncation=True) |
| 56 | + return tf.data.Dataset.from_tensor_slices((dict(data), data['input_ids'])) |
| 57 | + |
| 58 | +print("============================ Creating Distributed Strategy ===========================") |
| 59 | +devices = [] |
| 60 | +for i in range(num_gpus): |
| 61 | + devices.append("GPU:"+str(i)) |
| 62 | +strategy = tf.distribute.MirroredStrategy(devices=devices) |
| 63 | +print('Number of devices: {}'.format(strategy.num_replicas_in_sync)) |
| 64 | +print("============================ Loading model from pretrained and compiling ===========================") |
| 65 | +with strategy.scope(): |
| 66 | + tokenizer = GPT2TokenizerFast.from_pretrained(model_name) |
| 67 | + tokenizer.pad_token = tokenizer.eos_token |
| 68 | + print("========================= Loading dataset ========================") |
| 69 | + train_dataset = tokenize(get_dataset(train_file),tokenizer, truncate).batch(num_gpus) |
| 70 | + valid_dataset = tokenize(get_dataset(valid_file),tokenizer, truncate).batch(num_gpus) |
| 71 | + model = TFGPT2LMHeadModel.from_pretrained(model_name) |
| 72 | + #Disable past key values |
| 73 | + model.config.use_cache=False |
| 74 | + optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5) |
| 75 | + loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) |
| 76 | + metric = metrics.SparseCategoricalAccuracy(name='Accuracy') |
| 77 | + model.compile(optimizer=optimizer, loss=[loss, *[None] * model.config.n_layer], metrics=[metric]) |
| 78 | +print("========================= Finetuning Model ==================================") |
| 79 | +model.fit(train_dataset, batch_size=64, epochs=num_epochs) |
| 80 | +print("========================= Evaluating Model ==================================") |
| 81 | +model.evaluate(valid_dataset) |
| 82 | + |
0 commit comments