Views
No views yet
mistralai/Mistral-7B-Instruct-v0.2 on the
ChatDoctor-HealthCareMagic-100k dataset, adapting the base instruction-tuned model to respond in the
style of a medical Q&A assistant.1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3from peft import PeftModel
4
5base_model_name = "mistralai/Mistral-7B-Instruct-v0.2"
6adapter_repo = "d1wash/mistral-7b-healthcare-qlora"
7
8tokenizer = AutoTokenizer.from_pretrained(base_model_name)
9tokenizer.pad_token = tokenizer.eos_token
10
11bnb_config = BitsAndBytesConfig(
12 load_in_4bit=True,
13 bnb_4bit_quant_type="nf4",
14 bnb_4bit_compute_dtype=torch.float16,
15 bnb_4bit_use_double_quant=True,
16)
17
18base_model = AutoModelForCausalLM.from_pretrained(
19 base_model_name,
20 quantization_config=bnb_config,
21 device_map="auto",
22)
23
24model = PeftModel.from_pretrained(base_model, adapter_repo)
25
26prompt = """<s>[INST] You are a helpful medical assistant.
27I have been experiencing persistent headaches for the past week. What could be the cause?
28
29Patient: I wake up with a throbbing headache every morning. [/INST]
30
31Doctor:"""
32
33inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
34output = model.generate(
35 **inputs,
36 max_new_tokens=200,
37 do_sample=True,
38 temperature=0.7,
39 top_p=0.9,
40 repetition_penalty=1.3,
41 no_repeat_ngram_size=3,
42 pad_token_id=tokenizer.eos_token_id,
43)
44print(tokenizer.decode(output[0], skip_special_tokens=True))<s>[INST] ... [/INST] ...q_proj, v_proj| Step | Training Loss | Validation Loss |
|---|---|---|
| 150 | 2.004 | 2.011 |
| 300 | 1.900 | 1.907 |
| 450 | 1.880 | 1.885 |
| 600 | 1.903 | 1.870 |
transformers, peft, bitsandbytes, accelerate, trl