File size: 4,068 Bytes
b6102d2 d111f9a 1df4a0a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
---
license: apache-2.0
---
# Traditional Chinese QLoRa Llama2 13b
- Github repo: https://github.com/MIBlue119/traditional_chinese_llama2/
- This is a practice to finetune Llama2 on traditional chinese instruction dataset at Llama2 chat model.
- Use qlora and the alpaca translated dataset to finetune llama2-7b model at rtx3090(24GB VRAM) with 9 hours.
Thanks for these references:
- NTU NLP Lab's alapaca dataset: [alpaca-tw_en-align.json](./alpaca-tw-en-align.json): [ntunpllab](https://github.com/ntunlplab/traditional-chinese-alpaca) translate Stanford Alpaca 52k dataset
- [Chinese Llama 2 7B train.py](https://github.com/LinkSoul-AI/Chinese-Llama-2-7b/blob/main/train.py)
- [Load the pretrained model in 4-bit precision and Set training with LoRA according to hf's trl lib](https://github.com/lvwerra/trl/blob/main/examples/scripts/sft_trainer.py): QLoRA finetuning
## Resources
- traditional chinese qlora finetuned Llama2 7b merge model: [weiren119/traditional_chinese_qlora_llama2_merged](https://huggingface.co/weiren119/traditional_chinese_qlora_llama2_merged)
- traditional chinese qlora 7b adapter model: [weiren119/traditional_chinese_qlora_llama2](https://huggingface.co/weiren119/traditional_chinese_qlora_llama2)
## Online Demo
- [Run the qlora finetuned model at colab](https://colab.research.google.com/drive/1OYXvhY-8KjEDaGhOLrJe4omjtFgOWjy1?usp=sharing): May need colab pro or colab pro+
## Use which pretrained model
- NousResearch: https://huggingface.co/NousResearch/Llama-2-7b-chat-hf
## Training procedure
The following `bitsandbytes` quantization config was used during training:
- load_in_8bit: False
- load_in_4bit: True
- llm_int8_threshold: 6.0
- llm_int8_skip_modules: None
- llm_int8_enable_fp32_cpu_offload: False
- llm_int8_has_fp16_weight: False
- bnb_4bit_quant_type: nf4
- bnb_4bit_use_double_quant: True
- bnb_4bit_compute_dtype: bfloat16
### Framework versions
- PEFT 0.4.0
## Usage
### Installation dependencies
```
$pip install transformers torch peft
```
#### Run the inference
```
import transformers
import torch
from transformers import AutoTokenizer, TextStreamer
# Use the same tokenizer from the source model
model_id="weiren119/traditional_chinese_qlora_llama2_13b_merged"
tokenizer = AutoTokenizer.from_pretrained(original_model_path, use_fast=False)
# Load fine-tuned model, you can replace this with your own model
model = AutoPeftModelForCausalLM.from_pretrained(
model_id,
load_in_4bit=model_id.endswith("4bit"),
torch_dtype=torch.float16,
device_map='auto'
)
system_prompt = """You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."""
def get_prompt(message: str, chat_history: list[tuple[str, str]]) -> str:
texts = [f'[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n']
for user_input, response in chat_history:
texts.append(f'{user_input.strip()} [/INST] {response.strip()} </s><s> [INST] ')
texts.append(f'{message.strip()} [/INST]')
return ''.join(texts)
print ("="*100)
print ("-"*80)
print ("Have a try!")
s = ''
chat_history = []
while True:
s = input("User: ")
if s != '':
prompt = get_prompt(s, chat_history)
print ('Answer:')
tokens = tokenizer(prompt, return_tensors='pt').input_ids
#generate_ids = model.generate(tokens.cuda(), max_new_tokens=4096, streamer=streamer)
generate_ids = model.generate(input_ids=tokens.cuda(), max_new_tokens=4096, streamer=streamer)
output = tokenizer.decode(generate_ids[0, len(tokens[0]):-1]).strip()
chat_history.append([s, output])
print ('-'*80)
``` |