Create hybridModel.py
Browse files- hybridModel.py +29 -0
hybridModel.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
|
3 |
+
|
4 |
+
# Load base model
|
5 |
+
base_model_name = "NousResearch/Llama-2-13b-hf"
|
6 |
+
base_model = AutoModelForCausalLM.from_pretrained(base_model_name)
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
8 |
+
|
9 |
+
# Load LoRA weights
|
10 |
+
lora_model_name = "FinGPT/fingpt-sentiment_llama2-13b_lora"
|
11 |
+
lora_model = AutoModelForCausalLM.from_pretrained(lora_model_name)
|
12 |
+
|
13 |
+
# Apply LoRA weights to the base model
|
14 |
+
def apply_lora_weights(base_model, lora_model):
|
15 |
+
base_model_state_dict = base_model.state_dict()
|
16 |
+
lora_model_state_dict = lora_model.state_dict()
|
17 |
+
|
18 |
+
for name, param in lora_model_state_dict.items():
|
19 |
+
if name in base_model_state_dict:
|
20 |
+
base_model_state_dict[name].copy_(param)
|
21 |
+
|
22 |
+
base_model.load_state_dict(base_model_state_dict)
|
23 |
+
|
24 |
+
apply_lora_weights(base_model, lora_model)
|
25 |
+
|
26 |
+
# Save the merged model
|
27 |
+
output_dir = "./hybrid_model"
|
28 |
+
base_model.save_pretrained(output_dir)
|
29 |
+
tokenizer.save_pretrained(output_dir)
|