forked from jzhang38/EasyContext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
212 lines (185 loc) · 7.82 KB
/
train.py
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import argparse
import torch
import os
from datasets import load_dataset, load_from_disk, DatasetDict
from datetime import timedelta
from torch.utils.data import DataLoader
from accelerate import Accelerator
from accelerate.utils import InitProcessGroupKwargs, set_seed
from tqdm import tqdm
from transformers import set_seed, default_data_collator
from transformers import AutoModelForCausalLM
import transformers
from flash_attn.losses.cross_entropy import CrossEntropyLoss
import math
from accelerate.utils import (
InitProcessGroupKwargs,
set_seed,
DummyOptim,
DummyScheduler,
)
from easy_context import (
prepare_seq_parallel_inputs,
apply_seq_parallel_monkey_patch,
prepare_dataloader,
apply_unsloth_offloaded_gradient_checkpoint_monkey_patch
)
# apply_unsloth_offloaded_gradient_checkpoint_monkey_patch()
def main(args):
if args.output_dir:
os.makedirs(args.output_dir, exist_ok=True)
if args.wandb:
import wandb
wandb.login()
set_seed(args.seed)
timeout = InitProcessGroupKwargs(timeout=timedelta(seconds=1_000_000))
accelerator = Accelerator(
gradient_accumulation_steps=args.gradient_accumulate_every,
mixed_precision="bf16",
log_with="wandb" if args.wandb else None,
kwargs_handlers=[timeout],
# fsdp_plugin=fsdp_plugin,
)
accelerator.init_trackers(project_name=args.wandb, init_kwargs={"wandb":{"name":args.output_dir.split("/")[-1]}})
accelerator.print(f"Total GPUS: {accelerator.num_processes}")
try:
train_dataset = load_dataset(args.dataset)
except:
train_dataset = load_from_disk(args.dataset)
if isinstance(train_dataset, DatasetDict):
train_dataset = train_dataset["train"]
model = AutoModelForCausalLM.from_pretrained(
args.model,
device_map=accelerator.device,
torch_dtype=torch.bfloat16,
rope_theta=args.rope_theta,
_attn_implementation="flash_attention_2",
)
assert isinstance(
model, (transformers.LlamaForCausalLM, transformers.MistralForCausalLM)
), "Only support llama and mistral model"
model_type = (
"llama" if isinstance(model, transformers.LlamaForCausalLM) else "mistral"
)
apply_seq_parallel_monkey_patch(args.parallel_mode, model_type)
if "input_ids" not in train_dataset.column_names:
raise RuntimeError("Dataset must include an `input_ids` feature")
# remove everything that is not input_ids
to_remove = [col for col in train_dataset.column_names if col != "input_ids"]
train_dataset = train_dataset.remove_columns(to_remove)
train_dataset = train_dataset.shuffle(seed=args.seed)
print("Dataset Size:", len(train_dataset))
train_loader = DataLoader(
train_dataset,
collate_fn=default_data_collator,
shuffle=True,
batch_size=args.batch_size,
)
if args.learning_rate != 2e-5:
accelerator.print(f"Warning: You also need to modify accelerate_configs/zero3_offload.json to change the learning rate")
optim = DummyOptim(model.parameters(), lr=args.learning_rate)
scheduler = DummyScheduler(
optim,
num_training_steps=args.max_train_steps,
total_num_steps=args.max_train_steps,
)
model, optim, scheduler = accelerator.prepare(model, optim, scheduler)
train_loader = prepare_dataloader(args.parallel_mode, train_loader, accelerator)
model.gradient_checkpointing_enable()
accelerator.register_for_checkpointing(scheduler)
accelerator.print(f"Max train steps: {args.max_train_steps}")
progress_bar = tqdm(
range(args.max_train_steps), disable=not accelerator.is_local_main_process
)
completed_steps = 0
model.train()
loss_func = CrossEntropyLoss(inplace_backward=True)
for step, batch in enumerate(train_loader):
input_ids = batch["input_ids"][..., : args.seq_length + 1][..., :-1]
target_ids = batch["input_ids"][..., : args.seq_length + 1][..., 1:]
position_ids = (
torch.arange(args.seq_length).unsqueeze(0).expand(input_ids.shape[0], -1)
)
# shard the input_ids according to the world size and rank according to zig zag attention
prepared = prepare_seq_parallel_inputs(
args.parallel_mode,
input_ids,
position_ids,
target_ids,
accelerator.process_index,
accelerator.num_processes,
accelerator.device,
)
local_input_ids = prepared["local_input_ids"]
local_position_ids = prepared["local_position_ids"]
local_target_ids = prepared["local_target_ids"]
loss_log = None
with accelerator.accumulate(model):
logits = model(
local_input_ids,
position_ids=local_position_ids,
).logits
loss = loss_func(
logits.reshape(-1, logits.shape[-1]), local_target_ids.reshape(-1)
)
accelerator.backward(loss)
if accelerator.sync_gradients:
# pay attention here. When any seq parallel algo is turned on. This technically only log the very first chunk's loss
# and what is the first chunk really depends on how do you shard the sequence
# for zig zag attention, the first chunk contains the left most and rightmost tokens
# so you cannot compare the (logged) loss of dist attention and zigzag ring attention.
# loss_log = {"loss": loss.item(), "ppl": math.exp(loss.item())}
# we now try gathered loss to verify if ring attention and dist flash attention produce the same loss
# this may slow down the training
gathered_loss = accelerator.reduce(loss.clone().detach(), "mean")
loss_log = {
"loss": gathered_loss.item(),
"ppl": math.exp(gathered_loss.item()),
}
accelerator.log(loss_log, step=completed_steps)
optim.step()
scheduler.step()
optim.zero_grad()
if accelerator.sync_gradients:
progress_bar.update(1)
if loss_log is not None:
progress_bar.set_postfix(loss_log)
completed_steps += 1
if completed_steps >= args.max_train_steps:
break
accelerator.print(f"Training Finished")
accelerator.end_training()
if args.output_dir is not None:
accelerator.print(f"Saving model to {args.output_dir}")
accelerator.wait_for_everyone()
state_dict = accelerator.get_state_dict(model)
accelerator.unwrap_model(model).save_pretrained(
f"{args.output_dir}",
is_main_process=accelerator.is_main_process,
save_function=accelerator.save,
state_dict=state_dict,
)
accelerator.print(f"Saving Finished")
if __name__ == "__main__":
args = argparse.ArgumentParser()
args.add_argument("--batch-size", type=int, default=1)
args.add_argument("--gradient-accumulate-every", type=int, default=8)
args.add_argument("--output-dir", type=str, required=True)
args.add_argument("--wandb", type=str)
args.add_argument("--seed", type=int, default=42)
args.add_argument("--max-train-steps", type=int, default=400)
args.add_argument("--learning-rate", type=float, default=2e-5)
args.add_argument("--rope-theta", type=float, default=100000)
args.add_argument("--model", type=str, default="meta-llama/Llama-2-7b-hf")
args.add_argument(
"--dataset",
type=str,
default="emozilla/pg_books-tokenized-bos-eos-chunked-65536",
)
args.add_argument("--seq-length", type=int, default=16384)
args.add_argument(
"--parallel_mode",
type=str,
choices=["zigzag_ring_attn", "dist_flash_attn", "data_parallel"],
)
main(args.parse_args())