1from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
2
3tokenizer = AutoTokenizer.from_pretrained("hiyouga/baichuan-13b-sft", trust_remote_code=True)
4model = AutoModelForCausalLM.from_pretrained("hiyouga/baichuan-13b-sft", trust_remote_code=True).cuda()
5streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
6
7query = "晚上睡不着怎么办"
8template = (
9 "A chat between a curious user and an artificial intelligence assistant. "
10 "The assistant gives helpful, detailed, and polite answers to the user's questions.\n"
11 "Human: {}\nAssistant: "
12)
13
14inputs = tokenizer([template.format(query)], return_tensors="pt")
15inputs = inputs.to("cuda")
16generate_ids = model.generate(**inputs, max_new_tokens=256, streamer=streamer)
You could also alternatively launch a CLI demo by using the script in
https://github.com/hiyouga/LLaMA-Factory
You can reproduce our results by visiting the following step-by-step (Chinese) guide:
or using the following scripts in
LLaMA-Factory:
1CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \
2 --stage sft \
3 --model_name_or_path baichuan-inc/Baichuan-13B-Base \
4 --do_train \
5 --dataset alpaca_gpt4_en,alpaca_gpt4_zh,self_cognition,oaast_sft,sharegpt_zh,refgpt_zh_p1,lima,self_cognition \
6 --template default \
7 --finetuning_type lora \
8 --lora_rank 32 \
9 --lora_target all \
10 --output_dir baichuan_13b_lora \
11 --per_device_train_batch_size 4 \
12 --gradient_accumulation_steps 8 \
13 --preprocessing_num_workers 16 \
14 --cutoff_len 1024 \
15 --optim paged_adamw_32bit \
16 --lr_scheduler_type cosine \
17 --logging_steps 10 \
18 --save_steps 100 \
19 --eval_steps 100 \
20 --warmup_steps 100 \
21 --learning_rate 3e-5 \
22 --max_grad_norm 0.5 \
23 --num_train_epochs 2.0 \
24 --quantization_bit 4 \
25 --plot_loss \
26 --fp16