Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
---
|
| 4 |
+
|
| 5 |
+
this is a demo how fine tune phi-2 model.
|
| 6 |
+
|
| 7 |
+
```
|
| 8 |
+
import torch
|
| 9 |
+
import datasets
|
| 10 |
+
from transformers import TrainingArguments, AutoConfig, AutoTokenizer, AutoModelForCausalLM
|
| 11 |
+
import trl
|
| 12 |
+
from transformers import BitsAndBytesConfig
|
| 13 |
+
|
| 14 |
+
train_dataset = datasets.load_dataset('HuggingFaceTB/cosmopedia-20k', split='train')
|
| 15 |
+
|
| 16 |
+
args = TrainingArguments(
|
| 17 |
+
output_dir="./test-sft",
|
| 18 |
+
max_steps=20000,
|
| 19 |
+
per_device_train_batch_size=1,
|
| 20 |
+
optim="adafactor", report_to="none",
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
model_id = "microsoft/phi-2"
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 25 |
+
nf4_config = BitsAndBytesConfig(
|
| 26 |
+
load_in_4bit=True,
|
| 27 |
+
bnb_4bit_quant_type="nf4",
|
| 28 |
+
bnb_4bit_use_double_quant=True,
|
| 29 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=nf4_config,device_map="auto")
|
| 33 |
+
print(model)
|
| 34 |
+
|
| 35 |
+
from peft import LoraConfig
|
| 36 |
+
|
| 37 |
+
peft_config = LoraConfig(
|
| 38 |
+
lora_alpha=16,
|
| 39 |
+
lora_dropout=0.1,
|
| 40 |
+
r=64, target_modules=["q_proj", "v_proj", "k_proj", "dense", "lm_head", "fc1", "fc2"],
|
| 41 |
+
bias="none",
|
| 42 |
+
task_type="CAUSAL_LM",
|
| 43 |
+
)
|
| 44 |
+
model.add_adapter(peft_config)
|
| 45 |
+
|
| 46 |
+
trainer = trl.SFTTrainer(
|
| 47 |
+
model=model,
|
| 48 |
+
args=args,
|
| 49 |
+
train_dataset=train_dataset,
|
| 50 |
+
dataset_text_field='text',
|
| 51 |
+
max_seq_length=1024
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
trainer.train()
|
| 55 |
+
|
| 56 |
+
trainer.model.save_pretrained("sft", dtype=torch.bfloat16)
|
| 57 |
+
trainer.tokenizer.save_pretrained("sft")
|
| 58 |
+
|
| 59 |
+
```
|