text
stringlengths
1
93.6k
def main():
parser = argparse.ArgumentParser(description="Finetune a base instruct/chat model using (Q)LoRA and PEFT using DPO (RLHF)")
parser.add_argument('tuned_model', type=str,
help='The name of the resulting tuned model.')
parser.add_argument('dataset_name', type=str,
help='The name of the dataset to use for fine-tuning. This should be the output of the combine_checkpoints script.')
parser.add_argument('instruction_prompt', type=str,
help='An instruction message added to every prompt given to the chatbot to force it to answer in the target language. Example: "You are a generic chatbot that always answers in English."')
parser.add_argument('--base_model', type=str, default="NousResearch/Meta-Llama-3-8B-Instruct",
help='The base foundation model. Default is "NousResearch/Meta-Llama-3-8B-Instruct".')
parser.add_argument('--base_dataset_text_field', type=str, default="text",
help="The dataset's column name containing the actual text to translate. Defaults to text")
parser.add_argument('--base_dataset_rank_field', type=str, default="rank",
help="The dataset's column name containing the rank of an answer given to a prompt. Defaults to rank")
parser.add_argument('--base_dataset_id_field', type=str, default="message_id",
help="The dataset's column name containing the id of a text. Defaults to message_id")
parser.add_argument('--base_dataset_parent_field', type=str, default="parent_id",
help="The dataset's column name containing the parent id of a text. Defaults to parent_id")
parser.add_argument('--quant8', action='store_true',
help='Finetunes the model in 8 bits. Requires more memory than the default 4 bit.')
parser.add_argument('--noquant', action='store_true',
help='Do not quantize the finetuning. Requires more memory than the default 4 bit and optional 8 bit.')
parser.add_argument('--max_seq_length', type=int, default=512,
help='The maximum sequence length to use in finetuning. Should most likely line up with your base model\'s default max_seq_length. Default is 512.')
parser.add_argument('--max_prompt_length', type=int, default=512,
help='The maximum length of the prompts to use. Default is 512.')
parser.add_argument('--num_train_epochs', type=int, default=2,
help='Number of epochs to use. 2 is default and has been shown to work well.')
parser.add_argument('--batch_size', type=int, default=4,
help='The batch size to use in finetuning. Adjust to fit in your GPU vRAM. Default is 4')
parser.add_argument('--threads_output_name', type=str, default=None,
help='If specified, the threads created in this script for finetuning will also be saved to disk or HuggingFace Hub.')
parser.add_argument('--thread_template', type=str, default="threads/template_default.txt",
help='A file containing the thread template to use. Default is threads/template_fefault.txt')
parser.add_argument('--max_steps', type=int, default=-1,
help='The maximum number of steps to run DPO for. Default is -1 which will run the data through fully for the number of epochs but this will be very time-consuming.')
parser.add_argument('--padding', type=str, default="left",
help='What padding to use, can be either left or right.')
args = parser.parse_args()
base_model = args.base_model
tuned_model = args.tuned_model
dataset_name = args.dataset_name
instruction_prompt = args.instruction_prompt
base_dataset_text_field = args.base_dataset_text_field
base_dataset_rank_field = args.base_dataset_rank_field
base_dataset_id_field = args.base_dataset_id_field
base_dataset_parent_field = args.base_dataset_parent_field
quant8 = args.quant8
noquant = args.noquant
max_seq_length = args.max_seq_length
num_train_epochs = args.num_train_epochs
per_device_train_batch_size = args.batch_size
threads_output_name = args.threads_output_name
thread_template_file = args.thread_template
max_prompt_length = args.max_prompt_length
max_steps = args.max_steps
padding = args.padding
# Check for HF_TOKEN
if 'HF_TOKEN' not in os.environ:
print("[WARNING] Environment variable 'HF_TOKEN' is not set!")
user_input = input("Do you want to continue? (yes/no): ").strip().lower()
if user_input != "yes":
print("Terminating the program.")
exit()
# Load the base translated dataset
if os.path.isdir(dataset_name):
dataset = load_from_disk(dataset_name)
else:
dataset = load_dataset(dataset_name)
# Load base tokenizer
tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
# Get the template
with open(thread_template_file, 'r', encoding="utf8") as f:
chat_template = f.read()
# Compute the threads
prompts = {k: [] for k in dataset.keys()}
for fold in prompts:
print(f"[---- LLaMa2Lang ----] Generating prompts using chat template {thread_template_file} for fold {fold}")
templated_prompts = finetune_prompts_dpo.create_prompts(dataset[fold], tokenizer, base_dataset_rank_field, base_dataset_parent_field, base_dataset_id_field, base_dataset_text_field, instruction_prompt, chat_template)
prompts[fold] = Dataset.from_pandas(pd.DataFrame(data=templated_prompts))
prompts = DatasetDict(prompts)
# Check if we need to write out
if threads_output_name is not None:
# Also do the other folds
print(f"[---- LLaMa2Lang ----] Writing out DPO thread prompts dataset to {threads_output_name}")
if os.path.isdir(threads_output_name):
prompts.save_to_disk(threads_output_name)
else:
prompts.push_to_hub(threads_output_name)
if noquant:
# Load base model
model = AutoModelForCausalLM.from_pretrained(base_model, device_map={"": 0}, trust_remote_code=True)