-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_tensor_parallel.py
More file actions
394 lines (299 loc) · 10.4 KB
/
Copy pathtrain_tensor_parallel.py
File metadata and controls
394 lines (299 loc) · 10.4 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import os
import time
from dataclasses import dataclass
import torch
import torch.nn as nn
import torch.distributed as dist
from torch.utils.data import DataLoader
from torch.distributed.device_mesh import init_device_mesh
from torch.distributed.tensor.parallel import (
parallelize_module,
ColwiseParallel,
RowwiseParallel,
)
from src.data import RandomTextDataset
@dataclass
class Config:
vocab_size: int = 32000
seq_len: int = 256
d_model: int = 768
hidden_dim: int = 3072
num_layers: int = 6
num_classes: int = 10
num_samples: int = 10000
# IMPORTANT FOR PURE TENSOR PARALLELISM:
# This is the global batch processed by the tensor-parallel group.
# All TP ranks receive the same batch.
#
# It is NOT per-GPU batch size like DDP.
batch_size: int = 32
num_workers: int = 4
num_epochs: int = 1
max_steps: int = 100
lr: float = 1e-4
class TensorParallelMLPBlock(nn.Module):
"""
Transformer-style feed-forward block:
x -> Linear(d_model, hidden_dim)
-> GELU
-> Linear(hidden_dim, d_model)
-> residual + LayerNorm
fc1 is column-parallel:
output hidden dimension is split across GPUs.
fc2 is row-parallel:
input hidden dimension is split across GPUs.
"""
def __init__(self, d_model: int, hidden_dim: int):
super().__init__()
self.ln = nn.LayerNorm(d_model)
self.fc1 = nn.Linear(d_model, hidden_dim, bias=False)
self.act = nn.GELU()
self.fc2 = nn.Linear(hidden_dim, d_model, bias=False)
def forward(self, x):
residual = x
x = self.ln(x)
x = self.fc1(x)
x = self.act(x)
x = self.fc2(x)
return x + residual
class TensorParallelToyTransformer(nn.Module):
"""
Toy Transformer-like classifier.
For learning tensor parallelism, we keep attention out and focus on MLP blocks.
Real LLM tensor parallelism commonly shards:
- attention Q/K/V/O projections
- MLP up/gate/down projections
"""
def __init__(self, cfg: Config):
super().__init__()
self.cfg = cfg
self.token_emb = nn.Embedding(cfg.vocab_size, cfg.d_model)
self.pos_emb = nn.Embedding(cfg.seq_len, cfg.d_model)
self.layers = nn.ModuleList(
[
TensorParallelMLPBlock(
d_model=cfg.d_model,
hidden_dim=cfg.hidden_dim,
)
for _ in range(cfg.num_layers)
]
)
self.final_ln = nn.LayerNorm(cfg.d_model)
self.head = nn.Linear(cfg.d_model, cfg.num_classes)
def forward(self, input_ids):
batch_size, seq_len = input_ids.shape
positions = torch.arange(seq_len, device=input_ids.device)
positions = positions.unsqueeze(0).expand(batch_size, seq_len)
x = self.token_emb(input_ids) + self.pos_emb(positions)
for layer in self.layers:
x = layer(x)
# CLS-style pooling
x = self.final_ln(x[:, 0])
logits = self.head(x)
return logits
def setup_distributed():
dist.init_process_group(backend="nccl")
local_rank = int(os.environ["LOCAL_RANK"])
global_rank = int(os.environ["RANK"])
world_size = int(os.environ["WORLD_SIZE"])
torch.cuda.set_device(local_rank)
device = torch.device(f"cuda:{local_rank}")
return local_rank, global_rank, world_size, device
def cleanup_distributed():
dist.destroy_process_group()
def is_main_process():
# Gives global rank
return dist.get_rank() == 0
def parallelize_model(model: nn.Module, world_size: int):
"""
Apply tensor parallelism to each MLP block.
IMPORTANT:
hidden_dim must be divisible by world_size.
With 3 GPUs:
hidden_dim = 3072
each rank gets 1024 hidden features
"""
cfg = model.cfg
if cfg.hidden_dim % world_size != 0:
raise ValueError(
f"hidden_dim={cfg.hidden_dim} must be divisible by world_size={world_size}"
)
# Creates a 1D tensor-parallel mesh over CUDA devices.
#
# With:
# torchrun --standalone --nproc_per_node=3 train_tensor_parallel.py
#
# mesh is conceptually:
# [cuda:0, cuda:1, cuda:2]
tp_mesh = init_device_mesh(
device_type="cuda",
mesh_shape=(world_size,),
)
for layer_id, layer in enumerate(model.layers):
parallelize_module(
module=layer,
device_mesh=tp_mesh,
parallelize_plan={
# fc1 weight shape:
# [hidden_dim, d_model]
#
# ColwiseParallel splits output features.
#
# Conceptually:
# GPU 0 computes first 1/3 of hidden features
# GPU 1 computes second 1/3
# GPU 2 computes third 1/3
"fc1": ColwiseParallel(),
# fc2 weight shape:
# [d_model, hidden_dim]
#
# RowwiseParallel splits input features.
#
# Each GPU consumes its hidden-feature shard,
# then outputs are reduced/combined.
"fc2": RowwiseParallel(),
},
)
if is_main_process():
print(f"Tensor parallelized MLP layer {layer_id}")
return model
def print_parameter_summary(model: nn.Module):
if not is_main_process():
return
print("\nParameter summary:")
for name, param in model.named_parameters():
print(f"{name:40s} shape={tuple(param.shape)} type={type(param)}")
print()
def broadcast_batch_from_rank0(
input_ids,
labels,
cfg: Config,
device: torch.device,
):
"""
Pure tensor parallelism rule:
All TP ranks must process the SAME batch.
Rank 0 loads the batch from the DataLoader.
Then rank 0 broadcasts input_ids and labels to every other TP rank.
This is correct for pure TP learning/demo code.
In real large-scale training, TP is usually combined with DP:
- ranks inside same TP group get the same batch
- different DP groups get different batches
"""
if is_main_process():
input_ids = input_ids.to(device, non_blocking=True)
labels = labels.to(device, non_blocking=True)
else:
input_ids = torch.empty(
(cfg.batch_size, cfg.seq_len),
dtype=torch.long,
device=device,
)
labels = torch.empty(
(cfg.batch_size,),
dtype=torch.long,
device=device,
)
dist.broadcast(input_ids, src=0)
dist.broadcast(labels, src=0)
return input_ids, labels
def main():
cfg = Config()
local_rank, global_rank, world_size, device = setup_distributed()
# IMPORTANT:
# For pure tensor parallelism, all ranks are cooperating on one logical model.
# So model initialization should be consistent across ranks.
#
# Do NOT use:
# torch.manual_seed(1234 + global_rank)
#
# for pure TP model initialization.
torch.manual_seed(1234)
if is_main_process():
print("Pure Tensor Parallel training started")
print(f"world_size / TP size: {world_size}")
print(f"global TP batch size: {cfg.batch_size}")
print("effective global batch size is NOT batch_size * world_size for pure TP")
print(f"d_model: {cfg.d_model}")
print(f"hidden_dim: {cfg.hidden_dim}")
print(f"hidden_dim per GPU: {cfg.hidden_dim // world_size}")
# Only rank 0 needs to load/generate the batch.
#
# Other ranks receive the same batch through dist.broadcast().
if is_main_process():
dataset = RandomTextDataset(
num_samples=cfg.num_samples,
seq_len=cfg.seq_len,
vocab_size=cfg.vocab_size,
num_classes=cfg.num_classes,
)
loader = DataLoader(
dataset,
batch_size=cfg.batch_size,
shuffle=True,
num_workers=cfg.num_workers,
pin_memory=True,
drop_last=True,
)
else:
loader = None
# Build model on each rank's GPU.
model = TensorParallelToyTransformer(cfg).to(device)
# Apply tensor parallelism to selected submodules.
model = parallelize_model(model, world_size)
print_parameter_summary(model)
optimizer = torch.optim.AdamW(
model.parameters(),
lr=cfg.lr,
foreach=False,
)
criterion = nn.CrossEntropyLoss()
model.train()
start = time.time()
global_step = 0
for epoch in range(cfg.num_epochs):
if is_main_process():
data_iter = iter(loader)
# Since only rank 0 has a DataLoader, all ranks follow the same
# step count controlled by cfg.max_steps.
while global_step < cfg.max_steps:
if is_main_process():
try:
input_ids, labels = next(data_iter)
except StopIteration:
# Restart loader if max_steps is larger than one epoch.
data_iter = iter(loader)
input_ids, labels = next(data_iter)
else:
input_ids, labels = None, None
input_ids, labels = broadcast_batch_from_rank0(
input_ids=input_ids,
labels=labels,
cfg=cfg,
device=device,
)
optimizer.zero_grad(set_to_none=True)
logits = model(input_ids)
loss = criterion(logits, labels)
loss.backward()
optimizer.step()
if is_main_process() and global_step % 10 == 0:
elapsed = time.time() - start
allocated = torch.cuda.memory_allocated(device) / 1024**3
reserved = torch.cuda.memory_reserved(device) / 1024**3
print(
f"step={global_step}, "
f"loss={loss.item():.4f}, "
f"gpu_mem_allocated={allocated:.2f} GB, "
f"gpu_mem_reserved={reserved:.2f} GB, "
f"elapsed={elapsed:.2f}s"
)
global_step += 1
if global_step >= cfg.max_steps:
break
dist.barrier()
if is_main_process():
print("Pure Tensor Parallel training finished.")
cleanup_distributed()
if __name__ == "__main__":
main()