File size: 9,618 Bytes
fb11af9 | 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 | # Copyright 2025 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.
from abc import ABC, abstractmethod
from collections import defaultdict
from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
import torch
import torch.nn.functional as F
from torch.nn.utils.rnn import pad_sequence
from torch.utils.data._utils.collate import default_collate
from ..distributed.parallel_state import get_parallel_state
from ..utils.seqlen_pos_transform_utils import len2culen, pos2culen
from .constants import IGNORE_INDEX
@dataclass
class DataCollator(ABC):
"""
Used in dataloader as a collate_fn.
"""
@abstractmethod
def __call__(self, features: Sequence[Dict[str, Any]]) -> Dict[str, "torch.Tensor"]:
"""
Converts a list of features to batched tensor dict.
"""
...
class CollatePipeline:
def __init__(self, data_collators: Optional[Union[Callable, List[Callable]]] = None):
"""
Args:
data_collators: a list of data collators or a single data collator
"""
if not isinstance(data_collators, (list, tuple)):
data_collators = [data_collators]
self.data_collators = data_collators
def __call__(self, batch: Sequence[Dict[str, Any]]):
"""
process data batch through data collators.
Args:
batch: the original input data batch
Returns:
batch: the processed data batch
"""
for data_collator in self.data_collators:
batch = data_collator(batch)
return batch
@dataclass
class DataCollatorWithPadding(DataCollator):
"""
Data collator with padding.
"""
pad_token_id: int = 0
def __call__(self, features: Sequence[Dict[str, "torch.Tensor"]]) -> Dict[str, "torch.Tensor"]:
batch = defaultdict(list)
# batching features
for feature in features:
for key in feature.keys():
batch[key].append(feature[key])
for key in batch.keys():
# process padding features
if key in ["input_ids", "attention_mask", "position_ids", "images_seq_mask"]:
batch[key] = pad_sequence(batch[key], batch_first=True, padding_value=0)
elif key in ["labels", "labels_image"]:
batch[key] = pad_sequence(batch[key], batch_first=True, padding_value=IGNORE_INDEX)
else:
batch[key] = default_collate(batch[key])
return batch
@dataclass
class DataCollatorWithPacking(DataCollator):
"""
Data collator with packing.
"""
def __call__(self, features: Sequence[Dict[str, "torch.Tensor"]]) -> Dict[str, "torch.Tensor"]:
seqlens = torch.tensor([len(feature["input_ids"]) for feature in features], dtype=torch.long)
batch = {"cu_seqlens": len2culen(seqlens)}
for input_name in features[0].keys():
if input_name in ("input_ids", "attention_mask", "labels"):
batch[input_name] = torch.cat([feature[input_name] for feature in features])
else:
batch[input_name] = default_collate([feature[input_name] for feature in features])
return batch
@dataclass
class DataCollatorWithPositionIDs(DataCollator):
"""
Data collator with packing by position ids.
"""
def __call__(self, features: Sequence[Dict[str, "torch.Tensor"]]) -> Dict[str, "torch.Tensor"]:
batch = {}
for input_name in features[0].keys():
if input_name in ("input_ids", "attention_mask", "labels", "position_ids"):
batch[input_name] = torch.cat([feature[input_name] for feature in features], dim=-1).unsqueeze(0)
else:
batch[input_name] = default_collate([feature[input_name] for feature in features])
if "position_ids" not in batch:
batch["position_ids"] = torch.cat(
[torch.arange(len(feature["input_ids"])) for feature in features]
).unsqueeze(0)
if "labels" in batch:
cu_seqlens = pos2culen(batch["position_ids"])
batch["labels"][:, cu_seqlens[1:-1]] = IGNORE_INDEX
return batch
@dataclass
class NoopDataCollator(DataCollator):
"""
Data collator with no operation, used in dynamic batch dataloader at main process.
"""
def __call__(self, features: Sequence[Dict[str, "torch.Tensor"]]) -> List[Dict[str, "torch.Tensor"]]:
return features
@dataclass
class UnpackDataCollator(DataCollator):
"""
Data collator to unpack examples, used in dynamic batch dataloader at worker process.
"""
def __call__(self, features: Sequence[Dict[str, "torch.Tensor"]]) -> Dict[str, "torch.Tensor"]:
return features[0]
@dataclass
class MakeMicroBatchCollator(DataCollator):
"""
Data collator to build micro batches, used in mapping dataloader.
"""
num_micro_batch: int
internal_data_collator: "DataCollator"
def __call__(self, features: Sequence[Tuple[Dict[str, "torch.Tensor"]]]) -> List[Dict[str, "torch.Tensor"]]:
micro_batch_size = len(features) // self.num_micro_batch
if isinstance(features[0], list):
for i in range(len(features)):
features[i] = features[i][0] # 1-to-N inverse transform
micro_batches = []
for i in range(0, len(features), micro_batch_size):
micro_batches.append(self.internal_data_collator(features[i : i + micro_batch_size]))
return micro_batches
@dataclass
class TextSequenceShardCollator(DataCollator):
"""
Data collator to chunk inputs according to sequence parallelism.
Args:
rmpad: whether the samples is packing or not.
rmpad_with_pos_ids: whether the samples is packing by position ids or not.
pad_token_id: the id of the padding token.
"""
rmpad: bool
rmpad_with_pos_ids: bool
pad_token_id: int = 0
def __post_init__(self):
self.sp_size = get_parallel_state().sp_size
self.sp_rank = get_parallel_state().sp_rank
def sp_slice(self, tensor: "torch.Tensor", dim: int = -1) -> "torch.Tensor":
"""
Slices a tensor along the specified dimension for sequence parallelism.
"""
seq_length = tensor.size(dim)
sp_chunk_size = (seq_length + self.sp_size - 1) // self.sp_size
return tensor.narrow(dim, self.sp_rank * sp_chunk_size, sp_chunk_size)
def sp_padding(
self, tensor: "torch.Tensor", dim: int = -1, pad_value: int = 0, pad_length: int = 0
) -> "torch.Tensor":
"""
Pads a tensor with pad_length to aligns tensor with sp size.
"""
if pad_length == 0:
return tensor
pad_shape = list(tensor.shape)
pad_shape[dim] = pad_length
pad = torch.full(pad_shape, fill_value=pad_value, dtype=tensor.dtype, device=tensor.device)
return torch.cat((tensor, pad), dim=dim)
def __call__(self, batch: Sequence[Dict[str, "torch.Tensor"]]) -> Dict[str, "torch.Tensor"]:
input_ids = batch.pop("input_ids")
labels = batch.pop("labels")[..., 1:].contiguous() # shift labels
labels = F.pad(labels, (0, 1), "constant", IGNORE_INDEX)
if self.rmpad_with_pos_ids: # mask the last token of each sequence
cu_seqlens = pos2culen(batch["position_ids"])
labels[:, cu_seqlens[1:-1] - 1] = IGNORE_INDEX
elif self.rmpad:
labels = labels.view(-1)
labels[batch["cu_seqlens"][1:-1] - 1] = IGNORE_INDEX
else:
if "position_ids" not in batch: # we should calculate the position ids before chunking
batch["position_ids"] = torch.arange(0, input_ids.size(-1)).unsqueeze(0)
# sp padding
seq_length = input_ids.size(-1)
sp_chunk_size = (seq_length + self.sp_size - 1) // self.sp_size
pad_length = sp_chunk_size * self.sp_size - seq_length
input_ids = self.sp_padding(input_ids, dim=-1, pad_value=self.pad_token_id, pad_length=pad_length)
labels = self.sp_padding(labels, dim=-1, pad_value=IGNORE_INDEX, pad_length=pad_length)
if self.rmpad_with_pos_ids:
batch["attention_mask"] = self.sp_padding(
batch["attention_mask"], dim=-1, pad_value=1, pad_length=pad_length
)
else:
batch["attention_mask"] = self.sp_padding(
batch["attention_mask"], dim=-1, pad_value=0, pad_length=pad_length
)
if self.rmpad:
if pad_length > 0:
batch["cu_seqlens"] = F.pad(
batch["cu_seqlens"], (0, 1), "constant", batch["cu_seqlens"][-1].item() + pad_length
)
else:
batch["position_ids"] = self.sp_padding(batch["position_ids"], dim=-1, pad_value=0, pad_length=pad_length)
# sp slice
batch["input_ids"] = self.sp_slice(input_ids, dim=-1)
batch["labels"] = self.sp_slice(labels, dim=-1)
return batch
|