Update README.md
Browse files
README.md
CHANGED
@@ -20,3 +20,86 @@ 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 |
+
|
26 |
+
# How to use
|
27 |
+
|
28 |
+
```Python
|
29 |
+
|
30 |
+
def load_model(model_name):
|
31 |
+
# QLoRA config
|
32 |
+
bnb_config = BitsAndBytesConfig(
|
33 |
+
load_in_4bit=True,
|
34 |
+
bnb_4bit_quant_type="nf4",
|
35 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
36 |
+
bnb_4bit_use_double_quant=False,
|
37 |
+
)
|
38 |
+
|
39 |
+
# Load model
|
40 |
+
model = AutoModelForCausalLM.from_pretrained(
|
41 |
+
model_name,
|
42 |
+
quantization_config=bnb_config,
|
43 |
+
device_map="auto",
|
44 |
+
token=HF_TOKEN
|
45 |
+
)
|
46 |
+
|
47 |
+
# Load tokenizer
|
48 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
49 |
+
model_name,
|
50 |
+
trust_remote_code=True,
|
51 |
+
token=HF_TOKEN
|
52 |
+
)
|
53 |
+
return model, tokenizer
|
54 |
+
|
55 |
+
|
56 |
+
def inference(datasets, model, tokenizer):
|
57 |
+
_results = []
|
58 |
+
for data in tqdm(datasets):
|
59 |
+
input = data["input"]
|
60 |
+
|
61 |
+
prompt = f"""### 指示
|
62 |
+
{input}
|
63 |
+
### 回答:
|
64 |
+
"""
|
65 |
+
|
66 |
+
encoded_input = tokenizer.encode_plus(
|
67 |
+
prompt,
|
68 |
+
add_special_tokens=False,
|
69 |
+
return_tensors="pt",
|
70 |
+
padding=True,
|
71 |
+
truncation=True,
|
72 |
+
).to(model.device)
|
73 |
+
|
74 |
+
tokenized_input = encoded_input["input_ids"]
|
75 |
+
attention_mask = encoded_input["attention_mask"]
|
76 |
+
|
77 |
+
with torch.no_grad():
|
78 |
+
outputs = model.generate(
|
79 |
+
tokenized_input,
|
80 |
+
attention_mask=attention_mask,
|
81 |
+
max_new_tokens=100,
|
82 |
+
do_sample=False,
|
83 |
+
repetition_penalty=1.2,
|
84 |
+
pad_token_id=tokenizer.pad_token_id
|
85 |
+
)[0]
|
86 |
+
|
87 |
+
output = tokenizer.decode(
|
88 |
+
outputs[tokenized_input.size(1):],
|
89 |
+
skip_special_tokens=True
|
90 |
+
)
|
91 |
+
|
92 |
+
_results.append({
|
93 |
+
"task_id": data["task_id"],
|
94 |
+
"input": input,
|
95 |
+
"output": output
|
96 |
+
})
|
97 |
+
return _results
|
98 |
+
|
99 |
+
|
100 |
+
model_name = "ak0327/llm-jp-3-13b-finetune-2"
|
101 |
+
|
102 |
+
model, tokenizer = load_model(model_name)
|
103 |
+
datasets = load_test_datasets() # your datasets
|
104 |
+
results = inference(model_name, datasets, model, tokenizer)
|
105 |
+
```
|