Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / data / parallel / batch_parallel.py: 74%
243 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
1# Copyright 2025-2026 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15# pylint: disable=missing-apache-license-header
17"""data.parallel.batch_parallel: CP-aware batch sharding.
19``shard_batch_for_cp``: data-pipeline CP sharding (aligned with the THD
20contract of the 02 collater); ``_shard_seq_lens_for_cp`` recomputes
21seq_lens/seq_lens_padded per CP rank. ``CPBatchSharder`` slices
22``input_ids``/``labels`` contiguously along the sequence dimension and
23``TPBatchBroadcaster`` moves the CP-local tensors and global sequence
24boundaries to the target device, broadcasting metadata and tensor fields
25to peer TP ranks (the canonical batch exists only on TP rank zero for the
26active CP coordinate).
28Split out of components/distributed/cp_utils.py in stage 4e; merged with
29components/datasets/parallel/batch_parallel.py in stage 6 (05 §11.2).
30"""
32from __future__ import annotations
34from collections.abc import Mapping
35from typing import Any
37import torch
38import torch.distributed as dist
40from hyper_parallel.core.dtensor.device_mesh import DeviceMesh
41from hyper_parallel.data.parallel.dataloader_parallel import DataLoaderParallelContext
44def _pad_batch_for_cp(batch: dict[str, Any], pad_len: int) -> dict[str, Any]:
45 """Pad sequence-dimension tensor fields for context parallelism."""
46 padded = dict(batch)
47 if pad_len <= 0:
48 return padded
50 pad_values = {"labels": -100, "input_ids": 0, "attention_mask": 0}
51 for key, value in batch.items():
52 if key == "qkv_format" or not isinstance(value, torch.Tensor) or value.ndim < 1:
53 continue
54 if key in ("seq_lens", "seq_lens_padded"):
55 continue # recomputed separately, not padded
56 if key == "position_ids":
57 # position_ids increment-pad: continue incrementing from the last value
58 last = value[..., -1:].to(torch.long)
59 increment = torch.arange(1, pad_len + 1, device=value.device,
60 dtype=value.dtype)
61 increment = increment.reshape(*([1] * (value.ndim - 1)), pad_len)
62 pad_block = increment.expand(*value.shape[:-1], pad_len) + last
63 else:
64 shape = list(value.shape)
65 shape[-1] = pad_len
66 pad_block = torch.full(shape, pad_values.get(key, 0),
67 dtype=value.dtype, device=value.device)
68 padded[key] = torch.cat([value, pad_block], dim=-1)
69 return padded
72def shard_batch_for_cp(batch: dict[str, Any], cp_mesh: DeviceMesh) -> dict[str, Any]:
73 """Shard the sequence-dim tensors of a batch along the CP mesh
74 (05 §6.3.4 canonical).
76 Contract (aligned with the 02 collater output):
77 - input_ids/labels/position_ids: [B, S] int64
78 - seq_lens / seq_lens_padded: [B, max_num_packs] int64, padded with the
79 -1000 sentinel
80 - qkv_format: "thd" (passthrough)
82 Sharding strategy: pad to a multiple of 2*cp, then slice the token
83 interval [cp_rank*chunk, (cp_rank+1)*chunk); the seq_lens family is
84 recomputed separately (_shard_seq_lens_for_cp).
86 Args:
87 batch: Canonical token batch to shard.
88 cp_mesh: Context-parallel process mesh.
89 """
90 cp_size = cp_mesh.size()
91 if cp_size <= 1:
92 return batch
94 cp_rank = cp_mesh.get_local_rank()
95 seq_len = batch["input_ids"].shape[1]
96 pad_len = (-seq_len) % (cp_size * 2)
97 chunk = (seq_len + pad_len) // cp_size
98 lo = cp_rank * chunk
99 hi = lo + chunk
100 slc = slice(lo, hi)
102 padded = _pad_batch_for_cp(batch, pad_len)
104 out = {}
105 for k, v in padded.items():
106 if k in ("seq_lens", "seq_lens_padded"):
107 continue
108 if k == "qkv_format":
109 out[k] = v
110 elif isinstance(v, torch.Tensor) and v.ndim >= 1:
111 out[k] = v[..., slc]
112 else:
113 out[k] = v
115 if "seq_lens" in batch and "seq_lens_padded" in batch:
116 out["seq_lens"], out["seq_lens_padded"] = _shard_seq_lens_for_cp(
117 batch["seq_lens"], batch["seq_lens_padded"],
118 cp_rank=cp_rank, chunk=chunk,
119 )
120 return out
123def _shard_seq_lens_row(row_lens, row_padded, lo, hi):
124 """Recompute one sample's pack lengths inside a CP shard."""
125 local_lens, local_padded = [], []
126 offset = 0
127 for raw_len, raw_pad in zip(row_lens, row_padded):
128 if raw_len == -1000:
129 break
130 pack_start = offset
131 pack_end = offset + raw_pad
132 offset = pack_end
133 inter_start = max(pack_start, lo)
134 inter_end = min(pack_end, hi)
135 if inter_start >= inter_end:
136 continue
137 local_actual = max(min(pack_start + raw_len, hi) - max(pack_start, lo), 0)
138 local_pad = inter_end - inter_start
139 if local_actual > 0 or local_pad > 0:
140 local_lens.append(local_actual)
141 local_padded.append(local_pad)
142 return local_lens, local_padded
145def _shard_seq_lens_for_cp(seq_lens, seq_lens_padded, *, cp_rank: int, chunk: int):
146 """Recompute seq_lens/seq_lens_padded per CP shard (preserving the -1000
147 sentinel semantics).
149 Walk the cumulative pack offsets of each sample (accumulated by
150 seq_lens_padded); for each pack:
151 - fully inside [lo, hi): kept as-is;
152 - crossing the boundary: truncated to [lo, hi), with the actual /
153 padding-inclusive lengths recomputed after truncation;
154 - fully outside: skipped.
155 The output is shifted to the local coordinate system; when
156 max_local_packs=0 it is set to 1 to avoid an empty tensor.
157 """
158 lo = cp_rank * chunk
159 hi = lo + chunk
161 local_lens_b, local_lens_padded_b = [], []
162 max_local_packs = 0
163 for b in range(seq_lens.shape[0]):
164 local_lens, local_padded = _shard_seq_lens_row(
165 seq_lens[b].tolist(), seq_lens_padded[b].tolist(), lo, hi,
166 )
167 local_lens_b.append(local_lens)
168 local_lens_padded_b.append(local_padded)
169 max_local_packs = max(max_local_packs, len(local_lens))
171 if max_local_packs == 0:
172 max_local_packs = 1
174 out_lens = torch.full((seq_lens.shape[0], max_local_packs), -1000,
175 dtype=seq_lens.dtype, device=seq_lens.device)
176 out_padded = torch.full((seq_lens.shape[0], max_local_packs), -1000,
177 dtype=seq_lens_padded.dtype, device=seq_lens.device)
178 for b in range(seq_lens.shape[0]):
179 n = len(local_lens_b[b])
180 if n > 0:
181 out_lens[b, :n] = torch.tensor(
182 local_lens_b[b], dtype=seq_lens.dtype, device=seq_lens.device)
183 out_padded[b, :n] = torch.tensor(
184 local_lens_padded_b[b], dtype=seq_lens_padded.dtype, device=seq_lens.device)
185 return out_lens, out_padded
188# ── DataLoader batch distribution (merged from components/datasets/parallel/batch_parallel.py, 05 §11.2) ──
191class CPBatchSharder:
192 """Select contiguous CP token intervals with optional multimodal field rules."""
194 def __init__(
195 self,
196 parallel_context: DataLoaderParallelContext,
197 token_fields: frozenset[str] | None = None,
198 ) -> None:
199 """Initialize context-parallel batch sharding.
201 Args:
202 parallel_context: DataLoader and TP/CP topology information.
203 token_fields: Optional sequence fields to slice.
204 When supplied, fields not listed here remain complete on each CP rank.
205 """
206 self.parallel_context = parallel_context
207 self.token_fields = token_fields
209 def shard(
210 self,
211 canonical_batch: Mapping[str, Any] | None,
212 ) -> dict[str, Any] | None:
213 """Shard input IDs and labels along the sequence dim.
215 Args:
216 canonical_batch: Normalized complete batch on TP rank zero, or ``None`` elsewhere.
218 Returns:
219 CP-local input IDs and labels on TP rank zero.
220 """
221 if canonical_batch is None:
222 cp_batch = None
223 return cp_batch
225 if self.token_fields is not None:
226 cp_batch = self._shard_configured_fields(canonical_batch)
227 return cp_batch
230 input_ids = canonical_batch["input_ids"]
231 cp_batch = {
232 "input_ids": input_ids,
233 "labels": canonical_batch["labels"],
234 }
236 cp_size = self.parallel_context.cp_world_size
237 if cp_size <= 1:
238 return cp_batch
240 seq_len = input_ids.shape[1]
241 if seq_len % cp_size != 0:
242 raise ValueError(f"seq_len ({seq_len}) must be divisible by cp_size ({cp_size})")
244 cp_rank = self.parallel_context.cp_rank
245 for field, value in cp_batch.items():
246 local_value = torch.chunk(value, cp_size, dim=1)[cp_rank]
247 cp_batch[field] = local_value.contiguous()
249 return cp_batch
251 @staticmethod
252 def _extend_cu_seq_lens(cp_batch: dict[str, Any], sequence_length: int, pad_length: int) -> None:
253 """Keep packed sequence boundaries aligned with token padding."""
255 if "cu_seq_lens" not in cp_batch:
256 return
258 boundaries = cp_batch["cu_seq_lens"].reshape(-1)
260 if int(boundaries[0]) != 0 or int(boundaries[-1]) != sequence_length:
261 raise ValueError("cu_seq_lens must cover the complete CP input sequence")
263 if pad_length:
264 boundaries = torch.cat((boundaries, boundaries[-1:] + pad_length))
266 cp_batch["cu_seq_lens"] = boundaries
268 def _shard_configured_fields(self, canonical_batch: Mapping[str, Any]) -> dict[str, Any]:
269 """Slice aligned token fields and preserve global boundaries and media.
271 Example:
272 CP=2, TP=4: collate supplies [1, 24], then each CP rank
273 receives [1, 12]. A length of 18 is rejected here.
274 """
275 cp_batch = dict(canonical_batch)
276 cp_size = self.parallel_context.cp_world_size
277 alignment = cp_size * self.parallel_context.tp_world_size
279 if cp_size == 1 and alignment == 1:
280 return cp_batch
282 input_ids = cp_batch["input_ids"]
284 if not torch.is_tensor(input_ids) or input_ids.ndim != 2:
285 raise ValueError("Configured CP sharding requires input_ids with shape [batch, sequence]")
287 sequence_length = input_ids.shape[1]
289 if sequence_length % alignment:
290 raise ValueError(
291 f"sequence_length ({sequence_length}) must be divisible by tp_size * cp_size ({alignment}); "
292 "align token fields in collate_fn before get_batch"
293 )
295 if "cu_seq_lens" in cp_batch:
296 boundaries = cp_batch["cu_seq_lens"].reshape(-1)
298 if boundaries.numel() < 2 or int(boundaries[0]) != 0 or int(boundaries[-1]) != input_ids.numel():
299 raise ValueError("cu_seq_lens must cover the complete CP input batch")
301 cp_batch["cu_seq_lens"] = boundaries
303 chunk_length = sequence_length // cp_size
304 local_start = self.parallel_context.cp_rank * chunk_length
305 local_end = local_start + chunk_length
307 # Slice each row's sequence axis; boundary and media metadata stay global.
308 for field in self.token_fields:
309 if field not in cp_batch:
310 continue
312 value = cp_batch[field]
314 if not torch.is_tensor(value) or value.ndim != 2 or value.shape != input_ids.shape:
315 raise ValueError(f"CP token field {field!r} must match input_ids shape")
317 cp_batch[field] = value[:, local_start:local_end].contiguous()
319 return cp_batch
322class TPBatchBroadcaster:
323 """Broadcast CP-local batch fields within one TP group."""
325 def __init__(
326 self,
327 parallel_context: DataLoaderParallelContext,
328 device: Any,
329 ) -> None:
330 """Initialize tensor-parallel batch transport.
332 Args:
333 parallel_context: DataLoader and TP/CP topology information.
334 device: Device receiving the broadcast batch.
335 """
336 self.parallel_context = parallel_context
337 self.device = device
339 def broadcast(
340 self,
341 cp_local_batch: Mapping[str, Any] | None,
342 cu_seq_lens: Any = None,
343 *,
344 broadcast_all_fields: bool = False,
345 ) -> dict[str, Any]:
346 """Broadcast CP-local fields and global boundaries across TP ranks.
348 Args:
349 cp_local_batch: CP-local fields on source owners, or ``None`` elsewhere.
350 When ``tp_source_rank=None``, every rank already has its fields
351 and this method only normalizes device and dtype.
352 cu_seq_lens: Global sequence boundaries, unsharded across CP ranks.
353 broadcast_all_fields: Preserve heterogeneous Omni fields and dtypes.
355 Returns:
356 Local batch populated on every TP rank.
357 """
358 if broadcast_all_fields:
359 parallel_batch = self._broadcast_mapping(cp_local_batch)
360 return parallel_batch
362 tp_rank = self.parallel_context.tp_rank
363 source_rank = self.parallel_context.tp_source_rank
364 if source_rank is None or tp_rank == source_rank:
365 parallel_batch = {
366 field: value.to(self.device, dtype=torch.int64, non_blocking=True)
367 for field, value in cp_local_batch.items()
368 }
369 local_cu_seq_lens = None
370 if cu_seq_lens is not None:
371 local_cu_seq_lens = cu_seq_lens.to(self.device, dtype=torch.int32, non_blocking=True)
372 else:
373 parallel_batch = None
374 local_cu_seq_lens = None
376 tp_size = self.parallel_context.tp_world_size
377 if source_rank is None or tp_size <= 1:
378 parallel_batch["cu_seq_lens"] = local_cu_seq_lens
379 return parallel_batch
381 # Dynamic Online batches do not have a fixed local sequence length.
382 # Broadcast compact shape metadata before receivers allocate tensors.
383 if tp_rank == source_rank:
384 batch_size, seq_len = parallel_batch["input_ids"].shape
385 num_boundaries = 0 if local_cu_seq_lens is None else local_cu_seq_lens.numel()
386 batch_meta = torch.tensor(
387 [batch_size, seq_len, num_boundaries],
388 dtype=torch.int64,
389 device=self.device,
390 )
391 else:
392 batch_meta = torch.empty((3,), dtype=torch.int64, device=self.device)
394 tp_group = self.parallel_context.tp_group
395 dist.broadcast(batch_meta, group=tp_group, group_src=source_rank)
397 # Non-source TP ranks allocate the same device shapes and dtypes.
398 if tp_rank != source_rank:
399 batch_size, seq_len, num_boundaries = [int(value) for value in batch_meta.tolist()]
400 shape = (batch_size, seq_len)
401 parallel_batch = {
402 "input_ids": torch.empty(shape, dtype=torch.int64, device=self.device),
403 "labels": torch.empty(shape, dtype=torch.int64, device=self.device),
404 }
405 if num_boundaries > 0:
406 local_cu_seq_lens = torch.empty((num_boundaries,), dtype=torch.int32, device=self.device)
408 # Broadcast only model fields that cannot be regenerated locally.
409 for field in ("input_ids", "labels"):
410 tensor = parallel_batch.get(field)
411 if tensor is None:
412 raise ValueError(f"parallel batch requires {field!r}")
414 dist.broadcast(tensor, group=tp_group, group_src=source_rank)
416 # Packed boundaries remain global across CP ranks and variable in size.
417 if local_cu_seq_lens is not None:
418 dist.broadcast(local_cu_seq_lens, group=tp_group, group_src=source_rank)
420 parallel_batch["cu_seq_lens"] = local_cu_seq_lens
422 return parallel_batch
424 def _broadcast_mapping(self, cp_local_batch: Mapping[str, Any] | None) -> dict[str, Any]:
425 """Broadcast variable-shaped Omni tensors and scalar metadata."""
426 source_rank = self.parallel_context.tp_source_rank
427 if source_rank is None or self.parallel_context.tp_world_size == 1:
428 parallel_batch = self._move_mapping_to_device(cp_local_batch)
429 return parallel_batch
431 is_source = self.parallel_context.tp_rank == source_rank
432 if is_source:
433 parallel_batch = self._move_mapping_to_device(cp_local_batch)
434 metadata = []
435 for name, value in parallel_batch.items():
436 if torch.is_tensor(value):
437 metadata.append((name, tuple(value.shape), value.dtype, None))
438 else:
439 metadata.append((name, None, None, value))
440 else:
441 parallel_batch = {}
442 metadata = None
443 object_list = [metadata]
444 dist.broadcast_object_list(
445 object_list, group=self.parallel_context.tp_group, group_src=source_rank, device=self.device
446 )
447 for name, shape, dtype, value in object_list[0]:
448 if shape is None:
449 parallel_batch[name] = value
450 continue
451 if not is_source:
452 parallel_batch[name] = torch.empty(shape, dtype=dtype, device=self.device)
453 dist.broadcast(parallel_batch[name], group=self.parallel_context.tp_group, group_src=source_rank)
454 return parallel_batch
456 def _move_mapping_to_device(self, batch: Mapping[str, Any]) -> dict[str, Any]:
457 """Transfer Omni tensors without changing their native dtypes."""
458 return {
459 field: value.to(self.device, non_blocking=True) if torch.is_tensor(value) else value
460 for field, value in batch.items()
461 }
464def broadcast_batch_mapping(batch: Mapping[str, Any] | None, group: dist.ProcessGroup,
465 source_rank: int, device: Any) -> dict[str, Any]:
466 """Broadcast heterogeneous decoder fields from their owner to a process group.
468 Args:
469 batch: Mapping at the source rank.
470 group: Consumer process group.
471 source_rank: Source index within the group.
472 device: Target tensor device.
473 """
474 is_source = dist.get_rank(group) == source_rank
475 if is_source:
476 parallel_batch = {name: value.to(device, non_blocking=True) if torch.is_tensor(value) else value
477 for name, value in batch.items()}
478 metadata = []
479 for name, value in parallel_batch.items():
480 if torch.is_tensor(value):
481 metadata.append((name, tuple(value.shape), value.dtype, None))
482 else:
483 metadata.append((name, None, None, value))
484 else:
485 parallel_batch = {}
486 metadata = None
487 object_list = [metadata]
488 dist.broadcast_object_list(
489 object_list, group=group, group_src=source_rank, device=device
490 )
491 for name, shape, dtype, value in object_list[0]:
492 if shape is None:
493 parallel_batch[name] = value
494 continue
495 if not is_source:
496 parallel_batch[name] = torch.empty(shape, dtype=dtype, device=device)
497 dist.broadcast(parallel_batch[name], group=group, group_src=source_rank)
498 return parallel_batch