Repository navigation
Expand file tree
/
Copy pathulysses.py
More file actions
404 lines (334 loc) · 14.2 KB
/
Copy pathulysses.py
File metadata and controls
404 lines (334 loc) · 14.2 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
395
396
397
398
399
400
401
402
403
404
# Copyright 2024 Bytedance Ltd. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Utilities for DeepSpeed Ulysses Sequence Parallelism.
DeepSpeed Ulysses Paper: https://arxiv.org/abs/2309.14509
Inspired from: https://github.com/deepspeedai/DeepSpeed/blob/master/deepspeed/sequence/layer.py
"""
from typing import TYPE_CHECKING, Any, Optional
import torch
import torch.distributed as dist
from torch import Tensor
from torch.distributed import ProcessGroup
from torch.distributed.device_mesh import DeviceMesh
if TYPE_CHECKING:
from verl import DataProto
_ULYSSES_SEQUENCE_PARALLEL_GROUP = None
def set_ulysses_sequence_parallel_group(group: dist.ProcessGroup):
"""
Set ulysses sequence parallel process group.
"""
global _ULYSSES_SEQUENCE_PARALLEL_GROUP
_ULYSSES_SEQUENCE_PARALLEL_GROUP = group
def get_ulysses_sequence_parallel_group() -> Optional[dist.ProcessGroup]:
"""
Get ulysses sequence parallel process group.
"""
global _ULYSSES_SEQUENCE_PARALLEL_GROUP
return _ULYSSES_SEQUENCE_PARALLEL_GROUP
def get_ulysses_sequence_parallel_world_size(group: ProcessGroup = None) -> int:
"""
Get ulysses sequence parallel world size.
"""
group = get_ulysses_sequence_parallel_group() if group is None else group
return dist.get_world_size(group) if group else 1
def get_ulysses_sequence_parallel_rank(group: ProcessGroup = None) -> int:
"""
Get ulysses sequence parallel rank.
"""
group = get_ulysses_sequence_parallel_group() if group is None else group
return dist.get_rank(group) if group else 0
def gather_seq_scatter_heads(
x: Tensor,
seq_dim: int,
head_dim: int,
unpadded_dim_size: int = 0,
group: ProcessGroup = None,
) -> Tensor:
"""
A func to sync embedding input with alltoall in sequence parallel
gather sequence dimension and scatter head dim:
e.g. seq_dim: 1, head_dim: 2
[bsz, seq/n, h, ...] -> [bsz, seq, h/n, ...]
"""
group = get_ulysses_sequence_parallel_group() if group is None else group
if not group:
return x
sp_world = get_ulysses_sequence_parallel_world_size(group)
x = SeqAllToAll.apply(group, x, head_dim, seq_dim)
if unpadded_dim_size and unpadded_dim_size % sp_world != 0:
padding_size = x.size(seq_dim) - unpadded_dim_size
x = _unpad_tensor(x, seq_dim, padding_size)
return x
def gather_heads_scatter_seq(x: Tensor, head_dim: int, seq_dim: int, group: ProcessGroup = None) -> Tensor:
"""
A func to sync attention result with alltoall in sequence parallel
gather head dimension and scatter seq dim:
e.g. seq_dim: 1, head_dim: 2
[bsz, seq, h/n, ...] -> [bsz, seq/n, h, ...]
"""
group = get_ulysses_sequence_parallel_group() if group is None else group
if not group:
return x
dim_size = x.size(seq_dim)
sp_world = get_ulysses_sequence_parallel_world_size(group)
if dim_size % sp_world != 0:
padding_size = sp_world - (dim_size % sp_world)
x = _pad_tensor(x, seq_dim, padding_size)
return SeqAllToAll.apply(group, x, seq_dim, head_dim, False)
def _pad_tensor(x: Tensor, dim: int, padding_size: int) -> Tensor:
shape = list(x.shape)
shape[dim] = padding_size
pad = torch.zeros(shape, dtype=x.dtype, device=x.device)
return torch.cat([x, pad], dim=dim)
def _unpad_tensor(x: Tensor, dim: int, padding_size: int) -> Tensor:
slc = [slice(None)] * len(x.shape)
slc[dim] = slice(0, -padding_size)
return x[tuple(slc)]
def slice_input_tensor(x: Tensor, dim: int, padding: bool = True, group: ProcessGroup = None) -> Tensor:
group = get_ulysses_sequence_parallel_group() if group is None else group
sp_world_size = dist.get_world_size(group)
sp_rank = get_ulysses_sequence_parallel_rank()
dim_size = x.size(dim)
# pad before slice
if padding and dim_size % sp_world_size:
padding_size = sp_world_size - (dim_size % sp_world_size)
x = _pad_tensor(x, dim, padding_size)
# slice the input tensor
parts = x.size(dim) // sp_world_size
slc = [slice(None)] * len(x.shape)
slc[dim] = slice(sp_rank * parts, (sp_rank + 1) * parts)
return x[tuple(slc)].contiguous()
def all_to_all_tensor(
local_input: Tensor,
scatter_dim: int,
gather_dim: int,
group: Optional[dist.ProcessGroup] = None,
async_op: bool = False,
):
group = get_ulysses_sequence_parallel_group() if group is None else group
seq_world_size = dist.get_world_size(group)
input_list = [t.contiguous() for t in torch.tensor_split(local_input, seq_world_size, scatter_dim)]
output_list = [torch.empty_like(input_list[0]) for _ in range(seq_world_size)]
comm = dist.all_to_all(output_list, input_list, group=group, async_op=async_op)
if async_op:
def wait():
comm.wait()
return torch.cat(output_list, dim=gather_dim).contiguous()
return wait
return torch.cat(output_list, dim=gather_dim).contiguous()
def all_gather_tensor(local_tensor: Tensor, group: Optional[dist.ProcessGroup] = None, async_op: bool = False):
group = get_ulysses_sequence_parallel_group() if group is None else group
sp_world_size = dist.get_world_size(group=group)
output_shape = list(l
8000
ocal_tensor.shape)
output_shape[0] = output_shape[0] * sp_world_size
output = torch.empty(output_shape, dtype=local_tensor.dtype, device=local_tensor.device)
dist.all_gather_into_tensor(output, local_tensor, group=group, async_op=async_op)
return output
class SeqAllToAll(torch.autograd.Function):
@staticmethod
def forward(
ctx: Any,
group: dist.ProcessGroup,
local_input: Tensor,
scatter_dim: int,
gather_dim: int,
async_op: bool = False,
) -> Tensor:
ctx.group = group
ctx.scatter_dim = scatter_dim
ctx.gather_dim = gather_dim
ctx.async_op = async_op
return all_to_all_tensor(local_input, scatter_dim, gather_dim, group, async_op)
@staticmethod
def backward(ctx: Any, *grad_output: Tensor) -> tuple[None, Tensor, None, None]:
input_t = torch.cat(grad_output[1:], dim=ctx.gather_dim).contiguous() if ctx.async_op else grad_output[0]
return (
None,
all_to_all_tensor(input_t, ctx.gather_dim, ctx.scatter_dim, ctx.group, False),
None,
None,
None,
None,
)
class Gather(torch.autograd.Function):
@staticmethod
def forward(
ctx: Any,
group: dist.ProcessGroup,
local_tensor: Tensor,
gather_dim: int,
grad_scaler: bool = True,
async_op=False,
) -> Tensor:
ctx.group = group
ctx.gather_dim = gather_dim
ctx.grad_scaler = grad_scaler
ctx.async_op = async_op
sp_world_size = dist.get_world_size(group=group)
ctx.sp_world_size = sp_world_size
sp_rank = dist.get_rank(group=group)
ctx.sp_rank = sp_rank
local_shape = list(local_tensor.size())
split_size = local_shape[0]
part_size = local_shape[gather_dim] # store original size
ctx.part_size = part_size
output = all_gather_tensor(local_tensor, group, async_op)
return torch.cat(output.split(split_size, dim=0), dim=gather_dim)
@staticmethod
def backward(ctx: Any, grad_output: Tensor) -> Any:
if ctx.grad_scaler:
grad_output = grad_output * ctx.sp_world_size
return (
None,
grad_output.split(ctx.part_size, dim=ctx.gather_dim)[ctx.sp_rank].contiguous(),
None,
None,
None,
None,
)
def gather_outpus_and_unpad(*args, **kwargs):
raise RuntimeError(
"please use verl.utils.ulysses.gather_outputs_and_unpad instead of verl.utils.ulysses.gather_outpus_and_unpad"
)
def gather_outputs_and_unpad(
x: Tensor,
gather_dim: int,
unpad_dim: int = None,
padding_size: int = 0,
grad_scaler: bool = True,
group: Optional[dist.ProcessGroup] = None,
):
"""
Gather a tensor across a process group and optionally unpad its padded elements.
Args:
x (Tensor): Input tensor to gather.
gather_dim (int): Dimension along which to gather across ranks.
unpad_dim (int, optional): Dimension from which to remove padding. If None, no unpadding.
padding_size (int): Number of padding elements to remove on `unpad_dim`. Defaults to 0.
grad_scaler (bool): Whether to apply gradient scaling during gather. Defaults to True.
group (ProcessGroup, optional): Process group for gathering. If None, uses
`get_ulysses_sequence_parallel_group()`. If still None, returns `x` unchanged.
Returns:
Tensor: The gathered tensor, with padding removed if requested.
"""
group = get_ulysses_sequence_parallel_group() if group is None else group
if group is None:
return x
x = Gather.apply(group, x, gather_dim, grad_scaler)
if unpad_dim is not None:
assert isinstance(padding_size, int), "padding size is not given or is not an integer"
if padding_size == 0:
return x
x = _unpad_tensor(x, unpad_dim, padding_size)
return x
def ulysses_pad(
input_ids_rmpad: torch.Tensor, position_ids_rmpad: Optional[torch.Tensor] = None, sp_size: int = 1, pad_value=0
):
if position_ids_rmpad is not None:
assert position_ids_rmpad.size(-2) == 1
assert input_ids_rmpad.size(-1) == position_ids_rmpad.size(-1)
if sp_size <= 1:
return input_ids_rmpad, position_ids_rmpad, 0
_, total_seq_len = input_ids_rmpad.shape
pad_size = (sp_size - total_seq_len % sp_size) % sp_size
if pad_size > 0:
input_ids_rmpad = torch.nn.functional.pad(input_ids_rmpad, (0, pad_size), value=pad_value)
if position_ids_rmpad is not None:
pad_pos_ids = torch.arange(pad_size, device=position_ids_rmpad.device).unsqueeze(0)
if position_ids_rmpad.dim() == 3:
pad_pos_ids = pad_pos_ids.unsqueeze(0).repeat(position_ids_rmpad.size(0), 1, 1)
position_ids_rmpad = torch.cat((position_ids_rmpad, pad_pos_ids), dim=-1)
return input_ids_rmpad, position_ids_rmpad, pad_size
def ulysses_pad_and_slice_inputs(
input_ids_rmpad: torch.Tensor,
position_ids_rmpad: Optional[torch.Tensor] = None,
sp_size: int = 1,
skip_position_ids_rmpad: bool = False,
pad_value=0,
):
"""
Pad and slice input_ids to be divisible by sp_size
Pad position_ids to be divisible by sp_size.
Note both input_ids_rmpad and position_ids_rmpad will be padded and sliced.
The is the utility of pre-forward for ulysses sequence parallelism
Args:
input_ids_rmpad: shape of [bsz, seqlen]
position_ids_rmpad: shape of [bsz, seqlen], where bsz must be 1
sp_size (int): ulysses sequence parallelism size
skip_position_ids_rmpad: whether to skip position_ids_rmpad for VeOmniEngine
Returns:
torch.Tensor: padded and sliced input_ids
torch.Tensor: padded and sliced position_ids
int: pad size
"""
input_ids_rmpad, position_ids_rmpad, pad_size = ulysses_pad(
input_ids_rmpad, position_ids_rmpad, sp_size, pad_value=pad_value
)
input_ids_rmpad = slice_input_tensor(input_ids_rmpad, dim=1, padding=False)
if position_ids_rmpad is not None and not skip_position_ids_rmpad:
position_ids_rmpad = slice_input_tensor(position_ids_rmpad, dim=1, padding=False)
return input_ids_rmpad, position_ids_rmpad, pad_size
def validate_ulysses_config(num_heads, ulysses_sequence_size):
if ulysses_sequence_size > 1:
assert num_heads % ulysses_sequence_size == 0, (
f"num_heads ({num_heads}) must be divisible by ulysses sequence size({ulysses_sequence_size})"
)
class BaseShardingManager:
"""Base sharding manager used for resharding weights/data across parallel groups."""
def __init__(self):
self.timing = {}
def __enter__(self):
pass
def __exit__(self, exc_type, exc_value, traceback):
pass
def preprocess_data(self, data: "DataProto") -> "DataProto":
return data
def postprocess_data(self, data: "DataProto") -> "DataProto":
return data
class FSDPUlyssesShardingManager(BaseShardingManager):
"""
Sharding manager to support data resharding when using FSDP + Ulysses sequence parallelism.
"""
def __init__(self, device_mesh: DeviceMesh):
super().__init__()
self.device_mesh = device_mesh
self.seed_offset = 12345
def __enter__(self):
if self.device_mesh is not None:
self.prev_sp_group = get_ulysses_sequence_parallel_group()
set_ulysses_sequence_parallel_group(self.device_mesh["sp"].get_group())
def __exit__(self, exc_type, exc_value, traceback):
if self.device_mesh is not None:
set_ulysses_sequence_parallel_group(self.prev_sp_group)
def preprocess_data(self, data: "DataProto") -> "DataProto":
"""
AllGather data from sp region.
This is because the data is first sharded along the FSDP dimension as we utilize the DP_COMPUTE.
In Ulysses, we need to make sure the same data is used across a SP group.
"""
if self.device_mesh is not None:
from verl.protocol import all_gather_data_proto
group = self.device_mesh["sp"].get_group()
all_gather_data_proto(data=data, process_group=group)
return data
def postprocess_data(self, data: "DataProto") -> "DataProto":
"""
Split the data to follow FSDP partition.
"""
if self.device_mesh is not None:
sp_size = self.device_mesh["sp"].size()
sp_rank = self.device_mesh["sp"].get_local_rank()
data = data.chunk(chunks=sp_size)[sp_rank]
return data