tatsurou commited on
Commit
959f168
·
verified ·
1 Parent(s): 6e5b689
Files changed (1) hide show
  1. README.md +28 -46
README.md CHANGED
@@ -15,38 +15,28 @@ language:
15
  LLMでの出力方法
16
 
17
  ```python
18
- from transformers import (
19
- AutoModelForCausalLM,
20
- AutoTokenizer,
21
- BitsAndBytesConfig,
22
- )
23
  import torch
24
- from tqdm import tqdm
25
  import json
26
 
27
- HF_TOKEN = "HF_TOKEN"
28
  model_name = "tatsurou/llm-jp-3-13b-ft-20241123"
29
-
30
- # QLoRA config
31
- bnb_config = BitsAndBytesConfig(
32
- load_in_4bit=True,
33
- bnb_4bit_quant_type="nf4",
34
- bnb_4bit_compute_dtype=torch.bfloat16,
35
- bnb_4bit_use_double_quant=False,
36
- )
37
-
38
- # Load model
39
- model = AutoModelForCausalLM.from_pretrained(
40
- model_name,
41
- quantization_config=bnb_config,
42
- device_map="auto",
43
- token = HF_TOKEN
44
  )
 
45
 
46
- # Load tokenizer
47
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, token = HF_TOKEN)
48
-
49
- # データセットの読み込み。
50
  datasets = []
51
  with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
52
  item = ""
@@ -56,35 +46,27 @@ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
56
  if item.endswith("}"):
57
  datasets.append(json.loads(item))
58
  item = ""
 
 
 
 
59
  results = []
60
- for data in tqdm(datasets):
 
61
 
62
- input = data["input"]
63
 
64
- prompt = f"""### 指示
65
- {input}
66
- ### 回答:
67
- """
68
 
69
- tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
70
- with torch.no_grad():
71
- outputs = model.generate(
72
- tokenized_input,
73
- max_new_tokens=300,
74
- do_sample=False,
75
- repetition_penalty=1.2
76
- )[0]
77
- output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
78
 
79
  results.append({"task_id": data["task_id"], "input": input, "output": output})
80
 
81
- import re
82
- model_name = re.sub(".*/", "", model_name)
83
- with open(f"./{model_name}-my-original-outputs.jsonl", 'w', encoding='utf-8') as f:
84
  for result in results:
85
- json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
86
  f.write('\n')
87
-
88
  ```
89
 
90
 
 
15
  LLMでの出力方法
16
 
17
  ```python
18
+ %%capture
19
+ !pip install unsloth
20
+ !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
21
+
22
+ from unsloth import FastLanguageModel
23
  import torch
 
24
  import json
25
 
 
26
  model_name = "tatsurou/llm-jp-3-13b-ft-20241123"
27
+ max_seq_length = 2048
28
+ dtype = None
29
+ load_in_4bit = True
30
+
31
+ model, tokenizer = FastLanguageModel.from_pretrained(
32
+ model_name = model_name,
33
+ max_seq_length = max_seq_length,
34
+ dtype = dtype,
35
+ load_in_4bit = load_in_4bit,
36
+ token = "HF token",
 
 
 
 
 
37
  )
38
+ FastLanguageModel.for_inference(model)
39
 
 
 
 
 
40
  datasets = []
41
  with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
42
  item = ""
 
46
  if item.endswith("}"):
47
  datasets.append(json.loads(item))
48
  item = ""
49
+
50
+ from tqdm import tqdm
51
+
52
+ # 推論
53
  results = []
54
+ for dt in tqdm(data):
55
+ input = dt["input"]
56
 
57
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
58
 
59
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
 
 
 
60
 
61
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
62
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
 
 
 
 
 
 
 
63
 
64
  results.append({"task_id": data["task_id"], "input": input, "output": output})
65
 
66
+ with open(f"/content/{model_name}_output.jsonl", 'w', encoding='utf-8') as f:
 
 
67
  for result in results:
68
+ json.dump(result, f, ensure_ascii=False)
69
  f.write('\n')
 
70
  ```
71
 
72