JunichiroMorita commited on
Commit
adb7d34
·
verified ·
1 Parent(s): 534fcb9

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +59 -18
README.md CHANGED
@@ -21,27 +21,68 @@ widget:
21
  - **Finetuned from model :** llm-jp/llm-jp-3-13b
22
 
23
  # Usage
 
 
 
 
 
 
 
 
24
  ```python
 
 
25
  import torch
26
- from transformers import AutoTokenizer, AutoModelForCausalLM
27
- tokenizer = AutoTokenizer.from_pretrained("JunichiroMorita/llm-jp-3-13b_lora_20241130")
28
- model = AutoModelForCausalLM.from_pretrained("JunichiroMorita/llm-jp-3-13b_lora_20241130", device_map="auto", torch_dtype=torch.bfloat16)
29
- chat = [
30
- {"role": "system", "content": "以下は、タスクを説明する指示です。要求を適切に満たす応答を書きなさい。"},
31
- {"role": "user", "content": "自然言語処理とは何か"},
32
- ]
33
- tokenized_input = tokenizer.apply_chat_template(chat, add_generation_prompt=True, tokenize=True, return_tensors="pt").to(model.device)
34
- with torch.no_grad():
35
- output = model.generate(
36
- tokenized_input,
37
- max_new_tokens=100,
38
- do_sample=True,
39
- top_p=0.95,
40
- temperature=0.7,
41
- repetition_penalty=1.05,
42
- )[0]
43
- print(tokenizer.decode(output))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
 
 
 
 
45
  ```
46
 
47
  # Data
 
21
  - **Finetuned from model :** llm-jp/llm-jp-3-13b
22
 
23
  # Usage
24
+
25
+ ```python
26
+ !pip install unsloth
27
+ !pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
28
+ !pip install -U torch
29
+ !pip install -U peft
30
+ ```
31
+
32
  ```python
33
+ from unsloth import FastLanguageModel
34
+ from peft import PeftModel
35
  import torch
36
+ import json
37
+ from tqdm import tqdm
38
+ import re
39
+
40
+ model_id = "llm-jp/llm-jp-3-13b"
41
+ adapter_id = f"JunichiroMorita/llm-jp-3-13b-it_lora_20241216"
42
+
43
+ HF_TOKEN = 'your_hugging_face_token'
44
+
45
+ dtype = None
46
+ load_in_4bit = True
47
+
48
+ model, tokenizer = FastLanguageModel.from_pretrained(
49
+ model_name=model_id,
50
+ dtype=dtype,
51
+ load_in_4bit=load_in_4bit,
52
+ trust_remote_code=True,
53
+ )
54
+
55
+ model = PeftModel.from_pretrained(model, adapter_id, token=HF_TOKEN)
56
+
57
+ datasets = []
58
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
59
+ item = ""
60
+ for line in f:
61
+ line = line.strip()
62
+ item += line
63
+ if item.endswith("}"):
64
+ datasets.append(json.loads(item))
65
+ item = ""
66
+
67
+ FastLanguageModel.for_inference(model)
68
+
69
+ results = []
70
+ for dt in tqdm(datasets):
71
+ input = dt["input"]
72
+
73
+ prompt = f"""### 指示\n{input}\n\n### 回答\n"""
74
+
75
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
76
+
77
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
78
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答\n')[-1]
79
+
80
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
81
 
82
+ with open(f'./llm-jp-3-13b-it_lora_20241216_output.jsonl', 'w', encoding='utf-8') as f:
83
+ for result in results:
84
+ json.dump(result, f, ensure_ascii=False)
85
+ f.write('\n')
86
  ```
87
 
88
  # Data