kky84176 commited on
Commit
57000da
1 Parent(s): e0701b6

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +82 -0
README.md CHANGED
@@ -20,3 +20,85 @@ 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 Use
25
+
26
+ 以下は、elyza-tasks-100-TV_0.jsonlの回答のためのコードです。
27
+
28
+ ```python
29
+ from transformers import(
30
+ AutoModelForCausalLM,
31
+ AutoTokenizer,
32
+ BitsAndBytesConfig,
33
+ )
34
+ import torch
35
+ from tqdm import tqdm
36
+ import json
37
+
38
+ HF_TOKEN = "your-token"
39
+ model_name = "kky84176/llm-jp-3-13b-finetune"
40
+
41
+ #
42
+ bnb_config = BitsAndBytesConfig(
43
+ load_in_4bit=True,
44
+ bnb_4bit_quant_type="nf4", # nf4は通常のINT4より精度が高く、ニューラルネットワークの分布に最適です
45
+ bnb_4bit_compute_dtype=torch.bfloat16,
46
+ )
47
+
48
+ # モデルの読込み
49
+ model = AutoModelForCausalLM.from_pretrained(
50
+ model_name,
51
+ quantization_config=bnb_config,
52
+ device_map="auto",
53
+ token=HF_TOKEN,
54
+ )
55
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remoe_code=True, token=HF_TOKEN)
56
+
57
+ # データの読込み
58
+ import json
59
+ datasets = []
60
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
61
+ item = ""
62
+ for line in f:
63
+ line = line.strip()
64
+ item += line
65
+ if item.endswith("}"):
66
+ datasets.append(json.loads(item))
67
+ item = ""
68
+
69
+ # モデルによる推論
70
+ results = []
71
+ for data in tqdm(datasets):
72
+
73
+ input = data["input"]
74
+
75
+ prompt = f"""### 指示
76
+ {input}
77
+ ### 回答
78
+ """
79
+
80
+ tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
81
+ attention_mask = torch.ones_like(tokenized_input)
82
+
83
+ with torch.no_grad():
84
+ outputs = model.generate(
85
+ tokenized_input,
86
+ attention_mask=attention_mask,
87
+ max_new_tokens=100,
88
+ do_sample=False,
89
+ repetition_penalty=1.2,
90
+ pad_token_id=tokenizer.eos_token_id
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
+ # jsonl への出力
97
+ import re
98
+ new_model_id = "llm-jp-3-13b-finetune"
99
+ jsonl_id = re.sub(".*/", "", new_model_id)
100
+ with open(f"./{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
101
+ for result in results:
102
+ json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
103
+ f.write('\n')
104
+ ```