Ryz0208 commited on
Commit
9e056cf
·
verified ·
1 Parent(s): 4e37b42

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +80 -0
README.md CHANGED
@@ -20,3 +20,83 @@ 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
+ # Sample Inference
25
+ ```python
26
+ !pip install -U bitsandbytes
27
+ !pip install -U transformers
28
+ !pip install -U accelerate
29
+ !pip install -U datasets
30
+
31
+ !pip install ipywidgets --upgrade
32
+
33
+ from transformers import (
34
+ AutoModelForCausalLM,
35
+ AutoTokenizer,
36
+ BitsAndBytesConfig,
37
+ )
38
+ import torch
39
+ from tqdm import tqdm
40
+ import json
41
+
42
+ # Hugging Face Token
43
+ HF_TOKEN = "your_token"
44
+ # Model ID
45
+ model_name = "Ryz0208/llm-jp-3-13b-finetune-2"
46
+
47
+ bnb_config = BitsAndBytesConfig(
48
+ load_in_4bit=True,
49
+ bnb_4bit_quant_type="nf4",
50
+ bnb_4bit_compute_dtype=torch.bfloat16,
51
+ bnb_4bit_use_double_quant=False,
52
+ )
53
+
54
+ model = AutoModelForCausalLM.from_pretrained(
55
+ model_name,
56
+ quantization_config=bnb_config,
57
+ device_map="auto",
58
+ token = HF_TOKEN
59
+ )
60
+
61
+ # Load tokenizer
62
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, token = HF_TOKEN)
63
+
64
+ datasets = []
65
+ with open("your_path/elyza-tasks-100-TV_0.jsonl", "r") as f:
66
+ item = ""
67
+ for line in f:
68
+ line = line.strip()
69
+ item += line
70
+ if item.endswith("}"):
71
+ datasets.append(json.loads(item))
72
+ item = ""
73
+
74
+ results = []
75
+ for data in tqdm(datasets):
76
+
77
+ input = data["input"]
78
+
79
+ prompt = f"""### 指示
80
+ {input}
81
+ ### 回答:
82
+ """
83
+
84
+ tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
85
+ with torch.no_grad():
86
+ outputs = model.generate(
87
+ tokenized_input,
88
+ max_new_tokens=100,
89
+ do_sample=False,
90
+ repetition_penalty=1.2
91
+ )[0]
92
+ output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
93
+
94
+ results.append({"task_id": data["task_id"], "input": input, "output": output})
95
+
96
+ import re
97
+ model_name = re.sub(".*/", "", model_name)
98
+ with open(f"./{model_name}-outputs.jsonl", 'w', encoding='utf-8') as f:
99
+ for result in results:
100
+ json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
101
+ f.write('\n')
102
+ ```