Update README.md
Browse files
README.md
CHANGED
@@ -20,3 +20,106 @@ language:
|
|
20 |
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
21 |
|
22 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
21 |
|
22 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
23 |
+
|
24 |
+
|
25 |
+
# how to use
|
26 |
+
Using Google Colab env
|
27 |
+
```python
|
28 |
+
!pip install -U bitsandbytes
|
29 |
+
!pip install -U transformers
|
30 |
+
!pip install -U accelerate
|
31 |
+
!pip install -U datasets
|
32 |
+
!pip install ipywidgets --upgrade
|
33 |
+
```
|
34 |
+
|
35 |
+
|
36 |
+
```python
|
37 |
+
from transformers import (
|
38 |
+
AutoModelForCausalLM,
|
39 |
+
AutoTokenizer,
|
40 |
+
BitsAndBytesConfig,
|
41 |
+
)
|
42 |
+
import torch
|
43 |
+
from tqdm import tqdm
|
44 |
+
import json
|
45 |
+
```
|
46 |
+
|
47 |
+
|
48 |
+
```python
|
49 |
+
# Write here your Hugging Face token and model name
|
50 |
+
HF_TOKEN = "{your Hugging Face token}"
|
51 |
+
model_name = "your model name"
|
52 |
+
```
|
53 |
+
|
54 |
+
|
55 |
+
```python
|
56 |
+
# QLoRA config
|
57 |
+
bnb_config = BitsAndBytesConfig(
|
58 |
+
load_in_4bit=True,
|
59 |
+
bnb_4bit_quant_type="nf4",
|
60 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
61 |
+
bnb_4bit_use_double_quant=False,
|
62 |
+
)
|
63 |
+
```
|
64 |
+
|
65 |
+
|
66 |
+
```python
|
67 |
+
# Load model
|
68 |
+
model = AutoModelForCausalLM.from_pretrained(
|
69 |
+
model_name,
|
70 |
+
quantization_config=bnb_config,
|
71 |
+
device_map="auto",
|
72 |
+
token = HF_TOKEN
|
73 |
+
)
|
74 |
+
|
75 |
+
# Load tokenizer
|
76 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, token = HF_TOKEN)
|
77 |
+
```
|
78 |
+
|
79 |
+
|
80 |
+
```python
|
81 |
+
datasets = []
|
82 |
+
with open("{your task data jsonl file path}", "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 |
+
# llmjp
|
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 |
+
with torch.no_grad():
|
106 |
+
outputs = model.generate(
|
107 |
+
tokenized_input,
|
108 |
+
max_new_tokens=200,
|
109 |
+
do_sample=False,
|
110 |
+
repetition_penalty=1.2
|
111 |
+
)[0]
|
112 |
+
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
113 |
+
|
114 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
115 |
+
```
|
116 |
+
|
117 |
+
```python
|
118 |
+
# get the answers as a jsonl file
|
119 |
+
import re
|
120 |
+
model_name = re.sub(".*/", "", model_name)
|
121 |
+
with open(f"./{model_name}-outputs.jsonl", 'w', encoding='utf-8') as f:
|
122 |
+
for result in results:
|
123 |
+
json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
|
124 |
+
f.write('\n')
|
125 |
+
```
|