aoii commited on
Commit
f7bd7a5
·
verified ·
1 Parent(s): 1aba0f1

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +121 -0
README.md CHANGED
@@ -20,3 +20,124 @@ 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
+ !pip uninstall unsloth -y
25
+ !pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
26
+ !pip install --upgrade torch
27
+ !pip install --upgrade xformers
28
+
29
+ import torch
30
+ if torch.cuda.get_device_capability()[0] >= 8:
31
+ !pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
32
+ HF_TOKEN = "YOUR-HF-TOKEN"
33
+
34
+ from unsloth import FastLanguageModel
35
+ import torch
36
+ max_seq_length = 512
37
+ dtype = None
38
+ load_in_4bit = True
39
+ model_id = "llm-jp/llm-jp-3-13b"
40
+ new_model_id = "rallm-jp-3-13b-it"
41
+
42
+ model, tokenizer = FastLanguageModel.from_pretrained(
43
+ model_name=model_id,
44
+ dtype=dtype,
45
+ load_in_4bit=load_in_4bit,
46
+ trust_remote_code=True,
47
+ )
48
+ model = FastLanguageModel.get_peft_model(
49
+ model,
50
+ r = 32,
51
+ target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
52
+ "gate_proj", "up_proj", "down_proj",],
53
+ lora_alpha = 32,
54
+ lora_dropout = 0.05,
55
+ bias = "none",
56
+ use_gradient_checkpointing = "unsloth",
57
+ random_state = 3407,
58
+ use_rslora = False,
59
+ loftq_config = None,
60
+ max_seq_length = max_seq_length,
61
+ )
62
+
63
+ from datasets import load_dataset
64
+
65
+ dataset = load_dataset("json", data_files="")
66
+
67
+ prompt = """### 指示
68
+ {}
69
+ ### 回答
70
+ {}"""
71
+
72
+ EOS_TOKEN = tokenizer.eos_token
73
+ def formatting_prompts_func(examples):
74
+ input = examples["text"]
75
+ output = examples["output"]
76
+ text = prompt.format(input, output) + EOS_TOKEN
77
+ return { "formatted_text" : text, }
78
+ pass
79
+ dataset = dataset.map(
80
+ formatting_prompts_func,
81
+ num_proc= 4,
82
+ )
83
+
84
+ from trl import SFTTrainer
85
+ from transformers import TrainingArguments
86
+ from unsloth import is_bfloat16_supported
87
+ trainer = SFTTrainer(
88
+ model = model,
89
+ tokenizer = tokenizer,
90
+ train_dataset=dataset["train"],
91
+ max_seq_length = max_seq_length,
92
+ dataset_text_field="formatted_text",
93
+ packing = False,
94
+ args = TrainingArguments(
95
+ per_device_train_batch_size = 2,
96
+ gradient_accumulation_steps = 4,
97
+ num_train_epochs = 1,
98
+ logging_steps = 10,
99
+ warmup_steps = 10,
100
+ save_steps=100,
101
+ save_total_limit=2,
102
+ max_steps=-1,
103
+ learning_rate = 2e-4,
104
+ fp16 = not is_bfloat16_supported(),
105
+ bf16 = is_bfloat16_supported(),
106
+ group_by_length=True,
107
+ seed = 3407,
108
+ output_dir = "outputs",
109
+ report_to = "none",
110
+ ),
111
+ )
112
+ trainer_stats = trainer.train()
113
+
114
+ import json
115
+ datasets = []
116
+ with open("", "r") as f:
117
+ item = ""
118
+ for line in f:
119
+ line = line.strip()
120
+ item += line
121
+ if item.endswith("}"):
122
+ datasets.append(json.loads(item))
123
+ item = ""
124
+ from tqdm import tqdm
125
+
126
+ FastLanguageModel.for_inference(model)
127
+
128
+ results = []
129
+ for dt in tqdm(datasets):
130
+ input = dt["input"]
131
+
132
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
133
+
134
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
135
+
136
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
137
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
138
+
139
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
140
+ with open(f"{new_model_id}_output.jsonl", 'w', encoding='utf-8') as f:
141
+ for result in results:
142
+ json.dump(result, f, ensure_ascii=False)
143
+ f.write('\n')