Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
mlabonne committed Mar 24, 2024
1 parent 87306ca commit 4dc551d
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions Fine_tune_a_Mistral_7b_model_with_DPO.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"provenance": [],
"machine_shape": "hm",
"gpuType": "A100",
"authorship_tag": "ABX9TyOJJCuqxZQnS1q+Fvz5+URG",
"authorship_tag": "ABX9TyNuIN7/ICiXCX5xELzN1Y3R",
"include_colab_link": true
},
"kernelspec": {
Expand Down Expand Up @@ -380,6 +380,8 @@
"source": [
"# Fine-tune a Mistral-7b model with DPO\n",
"\n",
"> 🗣️ [Large Language Model Course](https://github.com/mlabonne/llm-course)\n",
"\n",
"❤️ Created by [@maximelabonne](https://twitter.com/maximelabonne)."
],
"metadata": {
Expand Down Expand Up @@ -469,10 +471,10 @@
" prompt = tokenizer.apply_chat_template([message], tokenize=False, add_generation_prompt=True)\n",
"\n",
" # Format chosen answer\n",
" chosen = example['chatgpt'] + \"<|im_end|>\\n\"\n",
" chosen = example['chosen'] + \"<|im_end|>\\n\"\n",
"\n",
" # Format rejected answer\n",
" rejected = example['llama2-13b-chat'] + \"<|im_end|>\\n\"\n",
" rejected = example['rejected'] + \"<|im_end|>\\n\"\n",
"\n",
" return {\n",
" \"prompt\": system + prompt,\n",
Expand Down Expand Up @@ -561,13 +563,6 @@
")\n",
"model.config.use_cache = False\n",
"\n",
"# Reference model\n",
"ref_model = AutoModelForCausalLM.from_pretrained(\n",
" model_name,\n",
" torch_dtype=torch.float16,\n",
" load_in_4bit=True\n",
")\n",
"\n",
"# Training arguments\n",
"training_args = TrainingArguments(\n",
" per_device_train_batch_size=4,\n",
Expand All @@ -588,7 +583,6 @@
"# Create DPO trainer\n",
"dpo_trainer = DPOTrainer(\n",
" model,\n",
" ref_model,\n",
" args=training_args,\n",
" train_dataset=dataset,\n",
" tokenizer=tokenizer,\n",
Expand Down Expand Up @@ -624,7 +618,7 @@
"tokenizer.save_pretrained(\"final_checkpoint\")\n",
"\n",
"# Flush memory\n",
"del dpo_trainer, model, ref_model\n",
"del dpo_trainer, model\n",
"gc.collect()\n",
"torch.cuda.empty_cache()\n",
"\n",
Expand Down

0 comments on commit 4dc551d

Please sign in to comment.