hachimada commited on
Commit
36b52ef
1 Parent(s): b794f46

update readme

Browse files
Files changed (1) hide show
  1. README.md +94 -1
README.md CHANGED
@@ -30,4 +30,97 @@ The models have been fine-tuned on the following datasets.
30
 
31
 
32
  ### Usage
33
- TODO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
 
32
  ### Usage
33
+
34
+ ```terminal
35
+ pip install -U bitsandbytes
36
+ pip install -U transformers
37
+ pip install -U accelerate
38
+ pip install -U datasets
39
+ pip install -U peft
40
+ ```
41
+
42
+ ```python
43
+ from transformers import (
44
+ AutoModelForCausalLM,
45
+ AutoTokenizer,
46
+ BitsAndBytesConfig,
47
+ )
48
+ from peft import PeftModel
49
+ import torch
50
+ from tqdm import tqdm
51
+ import json
52
+
53
+ HF_TOKEN = "your_hf_token"
54
+ model_id = "llm-jp/llm-jp-3-13b"
55
+ adapter_id = "hachimada/llm-jp-3-13b-finetune-v0"
56
+ ```
57
+
58
+ ```python
59
+ # QLoRA config
60
+ bnb_config = BitsAndBytesConfig(
61
+ load_in_4bit=True,
62
+ bnb_4bit_quant_type="nf4",
63
+ bnb_4bit_compute_dtype=torch.bfloat16,
64
+ )
65
+ # Load model
66
+ model = AutoModelForCausalLM.from_pretrained(
67
+ model_id,
68
+ quantization_config=bnb_config,
69
+ device_map="auto",
70
+ token = HF_TOKEN
71
+ )
72
+ # Load tokenizer
73
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True, token = HF_TOKEN)
74
+ # Apply adapter
75
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
76
+ ```
77
+
78
+
79
+ ```python
80
+ # Load your dataset
81
+ datasets = []
82
+ with open("./your_dataset.jsonl", "r") as f:
83
+ item = ""
84
+ for line in f:
85
+ line = line.strip()
86
+ item += line
87
+ if item.endswith("}"):
88
+ datasets.append(json.loads(item))
89
+ item = ""
90
+ ```
91
+
92
+ ```python
93
+ # Inference
94
+ results = []
95
+ for data in tqdm(datasets):
96
+
97
+ input = data["input"]
98
+
99
+ prompt = f"""### 指示
100
+ {input}
101
+ ### 回答
102
+ """
103
+
104
+ tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
105
+ attention_mask = torch.ones_like(tokenized_input)
106
+ with torch.no_grad():
107
+ outputs = model.generate(
108
+ tokenized_input,
109
+ attention_mask=attention_mask,
110
+ max_new_tokens=512,
111
+ do_sample=False,
112
+ repetition_penalty=1.2,
113
+ pad_token_id=tokenizer.eos_token_id
114
+ )[0]
115
+ output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
116
+
117
+ results.append({"task_id": data["task_id"], "input": input, "output": output})
118
+
119
+ # dump to jsonl
120
+ import re
121
+ jsonl_id = re.sub(".*/", "", adapter_id)
122
+ with open(f"./{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
123
+ for result in results:
124
+ json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
125
+ f.write('\n')
126
+ ```