Shriti09 commited on
Commit
3337791
·
verified ·
1 Parent(s): b8d9fc5

Create merge_and_save_model.py

Browse files
Files changed (1) hide show
  1. merge_and_save_model.py +25 -0
merge_and_save_model.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from peft import PeftModel
4
+
5
+ # Use GPU if available
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
+
8
+ # Base model and adapter paths
9
+ base_model_name = "microsoft/phi-2" # Pull from HF Hub directly
10
+ adapter_path = "Shriti09/Microsoft-Phi-QLora" # Update with your Hugging Face repo path
11
+
12
+ # Load the base model
13
+ base_model = AutoModelForCausalLM.from_pretrained(
14
+ base_model_name,
15
+ torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32
16
+ )
17
+
18
+ # Load the LoRA adapter
19
+ adapter_model = PeftModel.from_pretrained(base_model, adapter_path)
20
+
21
+ # Merge the LoRA adapter into the base model
22
+ merged_model = adapter_model.merge_and_unload()
23
+
24
+ # Save the merged model to Hugging Face (space storage)
25
+ merged_model.save_pretrained("./merged_model")