Views
No views yet
1import torch
2import os
3from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM
4from peft import PeftModel
5
6base_model_name = "NousResearch/Llama-2-13b-hf"
7peft_model_name = "FinGPT/fingpt-sentiment_llama2-13b_lora"
8output_path = "merged_model"
9
10model = AutoModelForCausalLM.from_pretrained(
11 base_model_name,
12 device_map="auto",
13 load_in_8bit=True
14)
15
16# Dequantize base model weights to match precision of LoRA weights
17model.dequantize()
18
19model = PeftModel.from_pretrained(model, peft_model_name)
20
21merged_model = model.merge_and_unload()
22
23merged_model.save_pretrained(output_path)
24
25tokenizer = AutoTokenizer.from_pretrained(base_model_name)
26tokenizer.save_pretrained(output_path)1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4device = "cuda" if torch.cuda.is_available() else "cpu"
5
6model_path = "sgzsh269/fingpt-sentiment_llama2-13b_merged"
7
8model = AutoModelForCausalLM.from_pretrained(
9 model_path,
10 device_map=device,
11 torch_dtype=torch.float16
12)
13
14tokenizer = AutoTokenizer.from_pretrained(model_path)
15tokenizer.pad_token = tokenizer.eos_token
16
17model.eval()
18
19prompts = [
20'''Instruction: What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}
21Input: FINANCING OF ASPOCOMP 'S GROWTH Aspocomp is aggressively pursuing its growth strategy by increasingly focusing on technologically more demanding HDI printed circuit boards PCBs .
22Answer: ''',
23'''Instruction: What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}
24Input: According to Gran , the company has no plans to move all production to Russia , although that is where the company is growing .
25Answer: ''',
26'''Instruction: What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}
27Input: A tinyurl link takes users to a scamming site promising that users can earn thousands of dollars by becoming a Google ( NASDAQ : GOOG ) Cash advertiser .
28Answer: ''',
29]
30
31model_inputs = tokenizer(prompts, return_tensors='pt', padding=True).to(device)
32
33with torch.no_grad():
34 generated_ids = model.generate(**model_inputs, max_length=512)
35
36 output_ids = generated_ids[:, model_inputs.input_ids.shape[1]:].tolist()
37
38 output_texts = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
39
40 print(output_texts) # Expected: ['positive', 'neutral', 'negative']