-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtrain_qwen3.5_35b.py
More file actions
executable file
·170 lines (143 loc) · 5.31 KB
/
Copy pathtrain_qwen3.5_35b.py
File metadata and controls
executable file
·170 lines (143 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
import os
os.environ["TORCH_LOGS"] = "recompiles"
os.environ["TRITON_PRINT_AUTOTUNING"] = "1"
from pathlib import Path
from typing import Any, cast
import torch
from datasets import Dataset, load_from_disk
from peft import LoraConfig, TaskType, get_peft_model
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
TrainingArguments,
default_data_collator,
set_seed,
)
from attention_aiter_tuning import configure_qwen35_flash_attention_2
from bf16_adapter_trainer import BF16AdapterTrainer
from fast_lora import register_fast_lora
from fast_moe_lora import register_fast_moe_lora
from fast_moe_ranking import configure_fast_moe_ranking
from fla_tuning import configure_qwen35_fla
from gguf_dequant_compile import configure_compiled_gguf_dequantize
from gguf_liger_loss import apply_gguf_liger_fused_linear_cross_entropy
script_dir = Path(__file__).resolve().parent
# I usually preprocess the dataset into chunks with fixed length. You may change this with your dataset
def fixed_length_lm_collator(examples):
batch = default_data_collator(examples)
input_ids = batch["input_ids"].long()
num_tokens = batch.pop("num_tokens").long()
positions = torch.arange(input_ids.shape[1]).unsqueeze(0)
valid_tokens = positions < num_tokens.unsqueeze(1)
batch["input_ids"] = input_ids
batch["attention_mask"] = valid_tokens.long()
batch["labels"] = input_ids.masked_fill(~valid_tokens, -100)
return batch
def main():
model_dir = Path.home() / "models/qwen3.6"
gguf_file = "Qwen3.6-35B-A3B-APEX-I-Mini.gguf"
tokenizer_id = "Qwen/Qwen3.5-35B-A3B"
dataset_dir = script_dir / "data_tokenized_qwen3.5"
output_dir = script_dir / "out_qwen36_35b"
random_seed = 19260817
set_seed(random_seed)
configure_compiled_gguf_dequantize()
configure_qwen35_flash_attention_2()
configure_qwen35_fla()
tokenizer = cast(Any, AutoTokenizer.from_pretrained(tokenizer_id))
if tokenizer.pad_token_id is None:
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(
model_dir,
gguf_file=gguf_file,
gguf_mmap_policy="release",
dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
device_map={"": "cuda:0"},
)
# Autoregressive decoding cache is not needed in training
model.config.use_cache = False
# Disable load balancing loss to save VRAM
model.config.output_router_logits = False
model.config.router_aux_loss_coef = 0.0
configure_fast_moe_ranking(model)
lora_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
# It's possible to create a LoRA on the routing gate, but this may make the training unstable
target_modules=[
"q_proj",
"k_proj",
"v_proj",
"o_proj",
"down_proj",
"gate_proj",
"up_proj",
"in_proj_qkv",
"in_proj_z",
"out_proj",
"experts",
],
r=4,
lora_alpha=4,
use_rslora=False,
)
register_fast_lora(lora_config)
register_fast_moe_lora(lora_config, model)
model = get_peft_model(model, lora_config, autocast_adapter_dtype=False)
apply_gguf_liger_fused_linear_cross_entropy(model)
# for layer in model.base_model.model.model.layers:
# layer.forward = torch.compile(
# layer.forward,
# fullgraph=False,
# mode="max-autotune-no-cudagraphs",
# )
model.print_trainable_parameters()
# Dataset is shuffled by the trainer by default
dataset = load_from_disk(dataset_dir)
if not isinstance(dataset, Dataset):
raise TypeError(f"expected a Dataset at {dataset_dir}, got DatasetDict")
training_args = TrainingArguments(
output_dir=str(output_dir),
per_device_train_batch_size=1, # Increase batch size if you have more VRAM
gradient_accumulation_steps=1,
learning_rate=1e-4,
weight_decay=1e-3, # For MoE models this can be smaller than dense models
max_grad_norm=1,
num_train_epochs=1,
lr_scheduler_type="linear",
warmup_steps=100,
logging_steps=1,
save_steps=100,
save_total_limit=5,
bf16=True,
optim="adamw_8bit",
use_liger_kernel=True,
liger_kernel_config={
"rope": False, # Liger's Qwen3 RoPE patch is wrong on Qwen3.5
"cross_entropy": False,
"fused_linear_cross_entropy": False, # We use our cross entropy patch
"rms_norm": True,
"swiglu": False, # Liger's MoE SwiGLU patch is incompatible with our MoE LoRA patch
},
gradient_checkpointing=True,
gradient_checkpointing_kwargs={"use_reentrant": False},
remove_unused_columns=False,
# torch_compile=True,
# torch_compile_mode="max-autotune",
report_to="wandb",
seed=random_seed,
)
trainer = BF16AdapterTrainer(
model=model,
processing_class=tokenizer,
train_dataset=dataset,
args=training_args,
data_collator=fixed_length_lm_collator,
)
trainer_stats = trainer.train()
# trainer_stats = trainer.train(resume_from_checkpoint=True)
print("trainer_stats")
print(trainer_stats)
if __name__ == "__main__":
main()