|
| 1 | +# Copyright 2026 The LoongForge Authors. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Strided data access for full_hetero_dp encoder. |
| 5 | +
|
| 6 | +Provides two implementations: |
| 7 | +- EncoderStridedSampler: batch_sampler for indexable (map-style) datasets. |
| 8 | +- EncoderStridedIterator: iterator-level filter for streaming (Energon/WebDataset) dataloaders. |
| 9 | +
|
| 10 | +Both yield only the microbatches assigned to a specific PP rank, |
| 11 | +maintaining data consistency with the decoder by using the same |
| 12 | +step-relative position filtering logic. |
| 13 | +""" |
| 14 | + |
| 15 | +import threading |
| 16 | +import queue |
| 17 | + |
| 18 | +from megatron.legacy.data.data_samplers import MegatronPretrainingRandomSampler |
| 19 | + |
| 20 | +_SENTINEL = object() |
| 21 | + |
| 22 | + |
| 23 | +class PrefetchIterator: |
| 24 | + """Prefetches items from a source iterator in a background thread. |
| 25 | +
|
| 26 | + Allows the next training step's data to be loaded while the current |
| 27 | + step's encoder/decoder computation is running. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__(self, source_iter, prefetch_count): |
| 31 | + self._queue = queue.Queue(maxsize=prefetch_count) |
| 32 | + self._source = source_iter |
| 33 | + self._thread = threading.Thread(target=self._worker, daemon=True) |
| 34 | + self._thread.start() |
| 35 | + |
| 36 | + def _worker(self): |
| 37 | + try: |
| 38 | + while True: |
| 39 | + item = next(self._source) |
| 40 | + self._queue.put(item) |
| 41 | + except StopIteration: |
| 42 | + self._queue.put(_SENTINEL) |
| 43 | + |
| 44 | + def __iter__(self): |
| 45 | + return self |
| 46 | + |
| 47 | + def __next__(self): |
| 48 | + item = self._queue.get() |
| 49 | + if item is _SENTINEL: |
| 50 | + raise StopIteration |
| 51 | + return item |
| 52 | + |
| 53 | + |
| 54 | +class EncoderStridedSampler: |
| 55 | + """Yields only microbatches assigned to this PP rank's encoder. |
| 56 | +
|
| 57 | + Internally iterates the same index sequence as the decoder's sampler, |
| 58 | + but only yields batches at positions belonging to this PP rank. |
| 59 | +
|
| 60 | + Processes items in chunks of num_real_microbatch (one step's worth) |
| 61 | + to ensure the position pattern is always step-relative, staying in |
| 62 | + sync with the decoder's DataLoader across steps. |
| 63 | + """ |
| 64 | + |
| 65 | + def __init__(self, dataset, total_samples, consumed_samples, micro_batch_size, |
| 66 | + data_parallel_rank, data_parallel_size, data_sharding, |
| 67 | + pp_rank, tp_size, model_size, num_real_microbatch): |
| 68 | + self.dataset = dataset |
| 69 | + self.total_samples = total_samples |
| 70 | + self.consumed_samples = consumed_samples |
| 71 | + self.micro_batch_size = micro_batch_size |
| 72 | + self.data_parallel_rank = data_parallel_rank |
| 73 | + self.data_parallel_size = data_parallel_size |
| 74 | + self.data_sharding = data_sharding |
| 75 | + self.pp_rank = pp_rank |
| 76 | + self.tp_size = tp_size |
| 77 | + self.model_size = model_size |
| 78 | + self.num_real_microbatch = num_real_microbatch |
| 79 | + |
| 80 | + def __len__(self): |
| 81 | + return self.total_samples |
| 82 | + |
| 83 | + def __iter__(self): |
| 84 | + base_sampler = MegatronPretrainingRandomSampler( |
| 85 | + self.dataset, |
| 86 | + total_samples=self.total_samples, |
| 87 | + consumed_samples=self.consumed_samples, |
| 88 | + micro_batch_size=self.micro_batch_size, |
| 89 | + data_parallel_rank=self.data_parallel_rank, |
| 90 | + data_parallel_size=self.data_parallel_size, |
| 91 | + data_sharding=self.data_sharding, |
| 92 | + ) |
| 93 | + start = self.pp_rank * self.tp_size |
| 94 | + end = start + self.tp_size |
| 95 | + # Process in step-sized chunks to keep position pattern step-relative |
| 96 | + step_buffer = [] |
| 97 | + for batch in base_sampler: |
| 98 | + step_buffer.append(batch) |
| 99 | + if len(step_buffer) == self.num_real_microbatch: |
| 100 | + for i, b in enumerate(step_buffer): |
| 101 | + if start <= (i % self.model_size) < end: |
| 102 | + yield b |
| 103 | + step_buffer = [] |
| 104 | + # Yield remaining partial step |
| 105 | + for i, b in enumerate(step_buffer): |
| 106 | + if start <= (i % self.model_size) < end: |
| 107 | + yield b |
| 108 | + |
| 109 | + |
| 110 | +class EncoderStridedIterator: |
| 111 | + """Iterator-level strided filter for streaming (Energon/WebDataset) dataloaders. |
| 112 | +
|
| 113 | + Wraps an EnergonDataloader, consumes all microbatches from it but only |
| 114 | + yields those assigned to this PP rank. Logically equivalent to |
| 115 | + EncoderStridedSampler but for iterable (non-indexable) datasets. |
| 116 | +
|
| 117 | + Buffers microbatches in step-sized chunks (num_real_microbatch) to |
| 118 | + maintain correct position-based assignment, identical to the logic in |
| 119 | + EncoderStridedSampler. |
| 120 | + """ |
| 121 | + |
| 122 | + def __init__(self, energon_dataloader, pp_rank, tp_size, model_size, num_real_microbatch): |
| 123 | + self._source = energon_dataloader |
| 124 | + self._pp_rank = pp_rank |
| 125 | + self._tp_size = tp_size |
| 126 | + self._model_size = model_size |
| 127 | + self._num_real_microbatch = num_real_microbatch |
| 128 | + self._gen = self._filter() |
| 129 | + |
| 130 | + def __iter__(self): |
| 131 | + return self |
| 132 | + |
| 133 | + def __next__(self): |
| 134 | + return next(self._gen) |
| 135 | + |
| 136 | + def _filter(self): |
| 137 | + start = self._pp_rank * self._tp_size |
| 138 | + end = start + self._tp_size |
| 139 | + step_buffer = [] |
| 140 | + while True: |
| 141 | + batch = next(self._source) |
| 142 | + step_buffer.append(batch) |
| 143 | + if len(step_buffer) == self._num_real_microbatch: |
| 144 | + for i, b in enumerate(step_buffer): |
| 145 | + if start <= (i % self._model_size) < end: |
| 146 | + yield b |
| 147 | + step_buffer = [] |
0 commit comments