Views
No views yet
Efficient Finetuning of Quantized LLMs for Finance
pip3 install -r requirements.txtsetup.sh to install the python and cuda package.bash scripts/setup.shbash script/finetune.shBitsandbytesConfigload_in_4bitbnb_4bit_compute_dtypebnb_4bit_use_double_quantbnb_4bit_quant_type. Note that there are two supported
quantization datatypes fp4 (four bit float) and nf4 (normal four bit float). The latter is theoretically optimal
for normally distributed weights and we recommend using nf4.1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3
4pretrained_model_name_or_path = "bavest/fin-llama-33b-merge"
5model = AutoModelForCausalLM.from_pretrained(
6 pretrained_model_name_or_path=pretrained_model_name_or_path,
7 load_in_4bit=True,
8 device_map='auto',
9 torch_dtype=torch.bfloat16,
10 quantization_config=BitsAndBytesConfig(
11 load_in_4bit=True,
12 bnb_4bit_compute_dtype=torch.bfloat16,
13 bnb_4bit_use_double_quant=True,
14 bnb_4bit_quant_type='nf4'
15 ),
16)
17
18tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path)
19
20question = "What is the market cap of apple?"
21input = "" # context if needed
22
23prompt = f"""
24A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's question.
25'### Instruction:\n{question}\n\n### Input:{input}\n""\n\n### Response:
26"""
27
28input_ids = tokenizer.encode(prompt, return_tensors="pt").to('cuda:0')
29
30with torch.no_grad():
31 generated_ids = model.generate(
32 input_ids,
33 do_sample=True,
34 top_p=0.9,
35 temperature=0.8,
36 max_length=128
37 )
38
39generated_text = tokenizer.decode(
40 [el.item() for el in generated_ids[0]], skip_special_tokens=True
41)bnb_4bit_compute_type='fp16' can lead to instabilities.tokenizer.bos_token_id = 1 to avoid generation issues.I want you to act as an accountant and come up with creative ways to manage finances. You'll need to consider budgeting, investment strategies and risk management when creating a financial plan for your client. In some cases, you may also need to provide advice on taxation laws and regulations in order to help them maximize their profits. My first suggestion request is “Create a financial plan for a small business that focuses on cost savings and long-term investments".
1@misc{Fin-LLAMA,
2 author = {William Todt, Ramtin Babaei, Pedram Babaei},
3 title = {Fin-LLAMA: Efficient Finetuning of Quantized LLMs for Finance},
4 year = {2023},
5 publisher = {GitHub},
6 journal = {GitHub repository},
7 howpublished = {\url{https://github.com/Bavest/fin-llama}},
8}| Metric | Value |
|---|---|
| Avg. | 51.76 |
| ARC (25-shot) | 65.02 |
| HellaSwag (10-shot) | 86.2 |
| MMLU (5-shot) | 58.73 |
| TruthfulQA (0-shot) | 49.75 |
| Winogrande (5-shot) | 80.03 |
| GSM8K (5-shot) | 16.22 |
| DROP (3-shot) | 6.36 |