migueldeguzmandev
commited on
Commit
•
4878733
1
Parent(s):
8b33ddc
Upload 11 files
Browse files- cached_lm_GPT2Tokenizer_128_q&a_test_v5-2.text +0 -0
- cached_lm_GPT2Tokenizer_128_q&a_test_v5-2.text.lock +0 -0
- config.json +40 -0
- generation_config.json +6 -0
- merges.txt +0 -0
- pytorch_model.bin +3 -0
- q&a_test_v5-2.text +0 -0
- special_tokens_map.json +23 -0
- tokenizer_config.json +33 -0
- train.py +139 -0
- vocab.json +0 -0
cached_lm_GPT2Tokenizer_128_q&a_test_v5-2.text
ADDED
Binary file (935 kB). View file
|
|
cached_lm_GPT2Tokenizer_128_q&a_test_v5-2.text.lock
ADDED
File without changes
|
config.json
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "/Users/migueldeguzman/Desktop/gpt2xl_algos/RLLMv13/layer9/",
|
3 |
+
"activation_function": "gelu_new",
|
4 |
+
"architectures": [
|
5 |
+
"GPT2LMHeadModel"
|
6 |
+
],
|
7 |
+
"attn_pdrop": 0.1,
|
8 |
+
"bos_token_id": 50256,
|
9 |
+
"embd_pdrop": 0.1,
|
10 |
+
"eos_token_id": 50256,
|
11 |
+
"initializer_range": 0.02,
|
12 |
+
"layer_norm_epsilon": 1e-05,
|
13 |
+
"model_type": "gpt2",
|
14 |
+
"n_ctx": 1024,
|
15 |
+
"n_embd": 1600,
|
16 |
+
"n_head": 25,
|
17 |
+
"n_inner": null,
|
18 |
+
"n_layer": 48,
|
19 |
+
"n_positions": 1024,
|
20 |
+
"output_past": true,
|
21 |
+
"reorder_and_upcast_attn": false,
|
22 |
+
"resid_pdrop": 0.1,
|
23 |
+
"scale_attn_by_inverse_layer_idx": false,
|
24 |
+
"scale_attn_weights": true,
|
25 |
+
"summary_activation": null,
|
26 |
+
"summary_first_dropout": 0.1,
|
27 |
+
"summary_proj_to_labels": true,
|
28 |
+
"summary_type": "cls_index",
|
29 |
+
"summary_use_proj": true,
|
30 |
+
"task_specific_params": {
|
31 |
+
"text-generation": {
|
32 |
+
"do_sample": true,
|
33 |
+
"max_length": 1024
|
34 |
+
}
|
35 |
+
},
|
36 |
+
"torch_dtype": "float32",
|
37 |
+
"transformers_version": "4.33.3",
|
38 |
+
"use_cache": true,
|
39 |
+
"vocab_size": 50257
|
40 |
+
}
|
generation_config.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_from_model_config": true,
|
3 |
+
"bos_token_id": 50256,
|
4 |
+
"eos_token_id": 50256,
|
5 |
+
"transformers_version": "4.33.3"
|
6 |
+
}
|
merges.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:96338e2b9a8053a1caa56551e25e9fdfeb848552edcae20b11b5a9d9d9ad880f
|
3 |
+
size 6230624769
|
q&a_test_v5-2.text
ADDED
The diff for this file is too large to render.
See raw diff
|
|
special_tokens_map.json
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": {
|
3 |
+
"content": "<|endoftext|>",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": true,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"eos_token": {
|
10 |
+
"content": "<|endoftext|>",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": true,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"unk_token": {
|
17 |
+
"content": "<|endoftext|>",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": true,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false
|
22 |
+
}
|
23 |
+
}
|
tokenizer_config.json
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_bos_token": false,
|
3 |
+
"add_prefix_space": false,
|
4 |
+
"bos_token": {
|
5 |
+
"__type": "AddedToken",
|
6 |
+
"content": "<|endoftext|>",
|
7 |
+
"lstrip": false,
|
8 |
+
"normalized": true,
|
9 |
+
"rstrip": false,
|
10 |
+
"single_word": false
|
11 |
+
},
|
12 |
+
"clean_up_tokenization_spaces": true,
|
13 |
+
"eos_token": {
|
14 |
+
"__type": "AddedToken",
|
15 |
+
"content": "<|endoftext|>",
|
16 |
+
"lstrip": false,
|
17 |
+
"normalized": true,
|
18 |
+
"rstrip": false,
|
19 |
+
"single_word": false
|
20 |
+
},
|
21 |
+
"errors": "replace",
|
22 |
+
"model_max_length": 1000000000000000019884624838656,
|
23 |
+
"pad_token": null,
|
24 |
+
"tokenizer_class": "GPT2Tokenizer",
|
25 |
+
"unk_token": {
|
26 |
+
"__type": "AddedToken",
|
27 |
+
"content": "<|endoftext|>",
|
28 |
+
"lstrip": false,
|
29 |
+
"normalized": true,
|
30 |
+
"rstrip": false,
|
31 |
+
"single_word": false
|
32 |
+
}
|
33 |
+
}
|
train.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
# Set the KMP_DUPLICATE_LIB_OK environment variable to handle a known issue with PyTorch
|
3 |
+
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
|
4 |
+
import sys
|
5 |
+
import torch
|
6 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel, TextDataset, DataCollatorForLanguageModeling, Trainer, TrainingArguments, get_linear_schedule_with_warmup
|
7 |
+
|
8 |
+
class GPT2Assistant:
|
9 |
+
def __init__(self):
|
10 |
+
# Load the GPT-2 tokenizer from the specified path
|
11 |
+
self.tokenizer = GPT2Tokenizer.from_pretrained("/Users/migueldeguzman/Desktop/gpt2xl_algos/RLLMv13/layer9/")
|
12 |
+
|
13 |
+
def fine_tune(self, answer_file_path, model_output_dir, epochs=1.0):
|
14 |
+
# Load the pre-trained GPT-2 model from the specified path
|
15 |
+
self.model = GPT2LMHeadModel.from_pretrained("/Users/migueldeguzman/Desktop/gpt2xl_algos/RLLMv13/layer9/")
|
16 |
+
# Create a text dataset from the specified file path and tokenizer, with a block size of 128
|
17 |
+
train_dataset = TextDataset(
|
18 |
+
tokenizer=self.tokenizer,
|
19 |
+
file_path=answer_file_path,
|
20 |
+
block_size=128
|
21 |
+
)
|
22 |
+
|
23 |
+
# Create a data collator for language modeling tasks
|
24 |
+
data_collator = DataCollatorForLanguageModeling(
|
25 |
+
tokenizer=self.tokenizer,
|
26 |
+
mlm=False
|
27 |
+
)
|
28 |
+
|
29 |
+
# Calculate the total number of training steps based on the dataset length and number of epochs
|
30 |
+
total_steps = len(train_dataset) * epochs
|
31 |
+
# Set the number of warmup steps for the learning rate scheduler
|
32 |
+
warmup_steps = 0.1 * total_steps
|
33 |
+
|
34 |
+
# Create an Adam optimizer with specified learning rate and weight decay
|
35 |
+
optimizer = torch.optim.Adam(self.model.parameters(), lr=42e-6, weight_decay=0.005)
|
36 |
+
# Create a linear learning rate scheduler with warmup steps
|
37 |
+
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=warmup_steps, num_training_steps=total_steps)
|
38 |
+
|
39 |
+
# Define the training arguments
|
40 |
+
training_args = TrainingArguments(
|
41 |
+
output_dir=model_output_dir,
|
42 |
+
overwrite_output_dir=True,
|
43 |
+
num_train_epochs=epochs,
|
44 |
+
per_device_train_batch_size=4,
|
45 |
+
save_steps=10_000,
|
46 |
+
save_total_limit=2,
|
47 |
+
gradient_accumulation_steps=8,
|
48 |
+
lr_scheduler_type='cosine',
|
49 |
+
warmup_steps=500
|
50 |
+
)
|
51 |
+
|
52 |
+
# Create a Trainer instance with the specified model, arguments, data collator, dataset, and optimizers
|
53 |
+
trainer = Trainer(
|
54 |
+
model=self.model,
|
55 |
+
args=training_args,
|
56 |
+
data_collator=data_collator,
|
57 |
+
train_dataset=train_dataset,
|
58 |
+
optimizers=(optimizer, scheduler)
|
59 |
+
)
|
60 |
+
|
61 |
+
# Fine-tune the model using the Trainer
|
62 |
+
trainer.train()
|
63 |
+
# Save the fine-tuned model and tokenizer to the specified output directory
|
64 |
+
self.model.save_pretrained(model_output_dir)
|
65 |
+
self.tokenizer.save_pretrained(model_output_dir)
|
66 |
+
|
67 |
+
def generate_answer(self, prompt, max_length=1000):
|
68 |
+
# Encode the input prompt using the tokenizer
|
69 |
+
input_ids = self.tokenizer.encode(prompt, return_tensors="pt")
|
70 |
+
|
71 |
+
# Check if the tokenizer has a pad token and set it if not
|
72 |
+
if self.tokenizer.pad_token_id is None:
|
73 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
74 |
+
|
75 |
+
# Create an attention mask for the input ids
|
76 |
+
attention_mask = (input_ids != self.tokenizer.pad_token_id).long()
|
77 |
+
|
78 |
+
# Generate text using the fine-tuned model with the specified parameters
|
79 |
+
output = self.model.generate(
|
80 |
+
input_ids,
|
81 |
+
attention_mask=attention_mask,
|
82 |
+
max_length=max_length,
|
83 |
+
num_return_sequences=1,
|
84 |
+
no_repeat_ngram_size=2,
|
85 |
+
do_sample=True,
|
86 |
+
top_k=50,
|
87 |
+
top_p=0.95,
|
88 |
+
temperature=0.0000000000000000000000000001
|
89 |
+
)
|
90 |
+
|
91 |
+
# Decode the generated output using the tokenizer, skipping special tokens
|
92 |
+
answer = self.tokenizer.decode(output[0], skip_special_tokens=True)
|
93 |
+
# Return the generated answer, excluding the original prompt
|
94 |
+
return answer[len(prompt):]
|
95 |
+
|
96 |
+
def query(self, prompt):
|
97 |
+
# Generate an answer for the given prompt
|
98 |
+
generated_answer = self.generate_answer(prompt)
|
99 |
+
print(generated_answer)
|
100 |
+
return generated_answer
|
101 |
+
|
102 |
+
def main():
|
103 |
+
# Set the file path for the text file to fine-tune on
|
104 |
+
text_file_path = "/Users/migueldeguzman/Desktop/gpt2xl_algos/RLLMv13/layer10/q&a_test_v5-2.text"
|
105 |
+
# Set the output directory path for the fine-tuned model
|
106 |
+
model_output_dir = "/Users/migueldeguzman/Desktop/gpt2xl_algos/RLLMv13/layer10/"
|
107 |
+
|
108 |
+
assistant = GPT2Assistant()
|
109 |
+
# Prompt the user to choose whether to fine-tune a new model or load an existing one
|
110 |
+
choice = input("Do you want to fine-tune a new model (n) or load an existing one (e)? (n/e): ")
|
111 |
+
|
112 |
+
if choice.lower() == "n":
|
113 |
+
# Fine-tune the model if the user chooses 'n'
|
114 |
+
print("Fine-tuning the model...")
|
115 |
+
assistant.fine_tune(text_file_path, model_output_dir)
|
116 |
+
print("Model fine-tuning complete.")
|
117 |
+
elif choice.lower() == "e":
|
118 |
+
print("Loading the existing model...")
|
119 |
+
# Load the existing fine-tuned model if the user chooses 'e'
|
120 |
+
assistant.model = GPT2LMHeadModel.from_pretrained(model_output_dir)
|
121 |
+
print("Existing model loaded.")
|
122 |
+
else:
|
123 |
+
print("Invalid choice. Exiting the program.")
|
124 |
+
sys.exit()
|
125 |
+
|
126 |
+
while True:
|
127 |
+
# Prompt the user for a question# Prompt the user for a question
|
128 |
+
prompt = input("Enter your question (or type 'exit' to stop): ")
|
129 |
+
if prompt.lower() == "exit":
|
130 |
+
break
|
131 |
+
|
132 |
+
print("Answering in progress...")
|
133 |
+
# Generate an answer for the user's prompt
|
134 |
+
generated_answer = assistant.query(prompt)
|
135 |
+
|
136 |
+
print("\n")
|
137 |
+
|
138 |
+
if __name__ == "__main__":
|
139 |
+
main()
|
vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|