yufuruno commited on
Commit
ba4b0f0
·
verified ·
1 Parent(s): b996cb2
Files changed (1) hide show
  1. README.md +146 -0
README.md CHANGED
@@ -20,3 +20,149 @@ 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
+ # Sample Use
26
+
27
+ 以下はelyza-tasks-100-TV_0.jsonlを生成するためのコードです。
28
+
29
+ ```python
30
+
31
+ !pip uninstall unsloth -y
32
+ !pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
33
+
34
+ !pip install --upgrade torch
35
+ !pip install --upgrade xformers
36
+
37
+ !pip install ipywidgets --upgrade
38
+
39
+ import torch
40
+ if torch.cuda.get_device_capability()[0] >= 8:
41
+ !pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
42
+
43
+ HF_TOKEN = "your_token"
44
+
45
+ from unsloth import FastLanguageModel
46
+ import torch
47
+ max_seq_length = 512
48
+ dtype = None
49
+ load_in_4bit = True
50
+
51
+ model_id = "llm-jp/llm-jp-3-13b"
52
+ new_model_id = "llm-jp-3-13b_SFT"
53
+
54
+ model, tokenizer = FastLanguageModel.from_pretrained(
55
+ model_name=model_id,
56
+ dtype=dtype,
57
+ load_in_4bit=load_in_4bit,
58
+ trust_remote_code=True,
59
+ )
60
+
61
+ model = FastLanguageModel.get_peft_model(
62
+ model,
63
+ r = 32,
64
+ target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
65
+ "gate_proj", "up_proj", "down_proj",],
66
+ lora_alpha = 32,
67
+ lora_dropout = 0.05,
68
+ bias = "none",
69
+ use_gradient_checkpointing = "unsloth",
70
+ random_state = 3407,
71
+ use_rslora = False,
72
+ loftq_config = None,
73
+ max_seq_length = max_seq_length,
74
+ )
75
+
76
+ from datasets import load_dataset
77
+
78
+ dataset = load_dataset("json", data_files="/content/ichikara-instruction-003-001-1.json")
79
+
80
+ prompt = """### 指示
81
+ {}
82
+ ### 回答
83
+ {}"""
84
+
85
+
86
+ """
87
+ formatting_prompts_func: 各データをプロンプトに合わせた形式に合わせる
88
+ """
89
+ EOS_TOKEN = tokenizer.eos_token
90
+ def formatting_prompts_func(examples):
91
+ input = examples["text"]
92
+ output = examples["output"]
93
+ text = prompt.format(input, output) + EOS_TOKEN
94
+ return { "formatted_text" : text, }
95
+ pass
96
+
97
+
98
+ dataset = dataset.map(
99
+ formatting_prompts_func,
100
+ num_proc= 4,
101
+ )
102
+
103
+ from trl import SFTTrainer
104
+ from transformers import TrainingArguments
105
+ from unsloth import is_bfloat16_supported
106
+
107
+ trainer = SFTTrainer(
108
+ model = model,
109
+ tokenizer = tokenizer,
110
+ train_dataset=dataset["train"],
111
+ max_seq_length = max_seq_length,
112
+ dataset_text_field="formatted_text",
113
+ packing = False,
114
+ args = TrainingArguments(
115
+ per_device_train_batch_size = 2,
116
+ gradient_accumulation_steps = 4,
117
+ num_train_epochs = 1,
118
+ logging_steps = 10,
119
+ warmup_steps = 10,
120
+ save_steps=100,
121
+ save_total_limit=2,
122
+ max_steps=-1,
123
+ learning_rate = 2e-4,
124
+ fp16 = not is_bfloat16_supported(),
125
+ bf16 = is_bfloat16_supported(),
126
+ group_by_length=True,
127
+ seed = 3407,
128
+ output_dir = "outputs",
129
+ report_to = "none",
130
+ ),
131
+ )
132
+
133
+ trainer_stats = trainer.train()
134
+
135
+ import json
136
+ datasets = []
137
+ with open("/content//elyza-tasks-100-TV_0.jsonl", "r") as f:
138
+ item = ""
139
+ for line in f:
140
+ line = line.strip()
141
+ item += line
142
+ if item.endswith("}"):
143
+ datasets.append(json.loads(item))
144
+ item = ""
145
+
146
+ from tqdm import tqdm
147
+
148
+ FastLanguageModel.for_inference(model)
149
+
150
+ results = []
151
+ for dt in tqdm(datasets):
152
+ input = dt["input"]
153
+
154
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
155
+
156
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
157
+
158
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
159
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
160
+
161
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
162
+
163
+ with open(f"{new_model_id}_output.jsonl", 'w', encoding='utf-8') as f:
164
+ for result in results:
165
+ json.dump(result, f, ensure_ascii=False)
166
+ f.write('\n')
167
+
168
+ ```