bridge.data.megatron_mimo.reorder_buffer#
Per-sample reorder buffer for MegatronMIMO intra-microbatch reordering.
Each data rank reads only its disjoint scalable-data-parallel shard of the global micro-batch.
The per-microbatch cost imbalance between shards is corrected by a cost all-gather plus one
ragged per-sample all_to_all_single over the module DP group, optionally overlapped with
compute. Paired modules derive the same assignment from the same costs and canonical grid, so
the BridgeCommunicator positional routing stays valid without cross-module communication.
Module Contents#
Classes#
Carry an overlap-worker exception across the queue for re-raising on the consumer thread. |
|
Wrap a MegatronMIMO data iterator so each yielded micro-batch is cost-balanced. |
Functions#
Return the zero-padding bytes that round |
|
Return |
|
Permute indices so contiguous equal-size groups have balanced total cost. |
|
Map each sample of one micro-batch to its owner rank and canonical position. |
|
Lift a single-slot assignment to the |
|
Build the slot-major window route by balancing each slot independently. |
|
Raise if any sample’s |
|
Compute per-slot per-rank cost max/min before and after balancing and the remote-sample count. |
|
Return the sorted tensor keys of a sample’s metadata, identical on sender and receiver. |
|
Build per-field |
|
Serialize one sample’s tensors into a 1D aligned |
|
Return the serialized byte size of a sample from its metadata alone. |
|
Reconstruct one sample from a |
|
Build the kept-local partition, packed send buffer, and splits for one |
|
Flatten a nested batch dict to dotted keys, preserving tensors and |
|
Rebuild the nested dict from dotted keys (inverse of :func: |
|
Return the cumulative image-offset table |
|
Return empty |
|
Return the per-image patch count |
|
Permute |
|
Gather a vision |
|
Gather |
|
Split a micro-batch into |
|
Rebuild a micro-batch from per-sample flat dicts (inverse of :func: |
|
Move a nested batch dict’s tensors to CUDA (non-blocking). |
|
Return the sample count of a micro-batch from one of its LM tensors. |
|
Return |
|
Rebuild this rank’s |
|
Run one ragged |
|
Cost-balance a window of |
|
Permute a window into canonical order on a |
|
Tie every CUDA tensor in |
|
Create the Gloo and side NCCL process groups for this rank’s module DP group. |
Data#
API#
- bridge.data.megatron_mimo.reorder_buffer.logger#
‘getLogger(…)’
- bridge.data.megatron_mimo.reorder_buffer._SERIALIZED_TENSOR_BYTE_ALIGNMENT#
8
- bridge.data.megatron_mimo.reorder_buffer._alignment_padding_bytes(nbytes: int) int#
Return the zero-padding bytes that round
nbytesup to the serialization alignment.
- bridge.data.megatron_mimo.reorder_buffer._DTYPE_INFO#
None
- bridge.data.megatron_mimo.reorder_buffer._dtype_info(dtype_name: str) Tuple[torch.dtype, int]#
Return
(dtype, byte width)for a dtype name from sample metadata.
- bridge.data.megatron_mimo.reorder_buffer.balanced_index_order(
- costs: List[float],
- n_groups: int,
Permute indices so contiguous equal-size groups have balanced total cost.
Equal-cardinality LPT with a stable tie-break by group index, so paired modules that feed the same costs produce the same permutation.
- Parameters:
costs – Per-item cost.
n_groups – Number of contiguous shards; must divide
len(costs).
- Returns:
A permutation of
range(len(costs))laid out group-by-group.- Raises:
ValueError – If
n_groups <= 0or does not dividelen(costs).
- bridge.data.megatron_mimo.reorder_buffer.balanced_assignment(
- costs: List[float],
- n_groups: int,
- dp_size: int,
Map each sample of one micro-batch to its owner rank and canonical position.
n_groupsis the canonical grid (LCM of the module DP sizes); a module of sizedp_sizeownsn_groups // dp_sizeconsecutive groups, so modules with different DP sizes agree.- Parameters:
costs – Per-sample cost indexed by global position in the micro-batch.
n_groups – Canonical group count; must divide
len(costs).dp_size – This module’s DP size; must divide
n_groups.
- Returns:
(owner_rank, canonical_pos)per global sample index.- Raises:
ValueError – If
n_groupsdoes not dividelen(costs)ordp_sizedoes not divide it.
- bridge.data.megatron_mimo.reorder_buffer.intra_route(
- assignment: List[Tuple[int, int]],
- *,
- src_slot: int = 0,
Lift a single-slot assignment to the
(dst_slot, owner_rank, canonical_pos)window route.Intra routing never moves a sample across slots, so
dst_slot == src_slotfor every sample.- Parameters:
assignment – Output of :func:
balanced_assignmentfor one slot.src_slot – Slot the samples were read from.
- Returns:
(dst_slot, owner_rank, canonical_pos)per global sample index.
- bridge.data.megatron_mimo.reorder_buffer.build_window_route(
- costs_per_slot: List[List[float]],
- n_groups: int,
- dp_size: int,
Build the slot-major window route by balancing each slot independently.
The window global index of slot
s, slot-local positionjisg = s * B_global + j.- Parameters:
costs_per_slot – Per-slot per-sample costs indexed by slot-local position.
n_groups – Canonical group count; must divide each slot’s sample count.
dp_size – This module’s DP size.
- Returns:
(dst_slot, owner_rank, canonical_pos)per window global index.
- bridge.data.megatron_mimo.reorder_buffer.assert_intra_no_cross_slot(
- route: List[Tuple[int, int, int]],
- b_global: int,
Raise if any sample’s
dst_slotdiffers from its source slotg // b_global.- Parameters:
route –
(dst_slot, owner_rank, canonical_pos)per window global index.b_global – Samples per micro-batch slot;
0skips the check.
- Raises:
RuntimeError – If a sample’s
dst_slotdiffers from its source slot.
- bridge.data.megatron_mimo.reorder_buffer.window_cost_spread(
- costs_per_slot: List[List[float]],
- route: List[Tuple[int, int, int]],
- dp_size: int,
Compute per-slot per-rank cost max/min before and after balancing and the remote-sample count.
- Parameters:
costs_per_slot – Per-slot per-sample costs indexed by slot-local position.
route – Window route from :func:
build_window_route.dp_size – This module’s DP size.
- Returns:
One
{before_max, before_min, after_max, after_min, remote}dict per slot.
- bridge.data.megatron_mimo.reorder_buffer.sample_keys(meta: Dict[str, Any]) List[str]#
Return the sorted tensor keys of a sample’s metadata, identical on sender and receiver.
- Parameters:
meta – Sample metadata from :func:
tensor_metadata.- Returns:
Sorted keys whose metadata entry carries a
dtype.
- bridge.data.megatron_mimo.reorder_buffer.tensor_metadata(
- flat: Dict[str, Any],
Build per-field
{shape, dtype}/{non_tensor: value}/Nonemetadata for one sample.- Parameters:
flat – One flattened sample dict.
- Returns:
Metadata keyed like
flat, sufficient to size and decode the sample without its data.- Raises:
ValueError – If a tensor field has a dtype the serializer does not support.
- bridge.data.megatron_mimo.reorder_buffer.serialize_sample(
- flat: Dict[str, Any],
- keys: List[str],
Serialize one sample’s tensors into a 1D aligned
uint8buffer inkeysorder.- Parameters:
flat – Flattened sample dict;
Nonevalues are skipped.keys – Sorted tensor keys from :func:
sample_keys.
- Returns:
1D
uint8tensor with each field padded to the serialization alignment.
- bridge.data.megatron_mimo.reorder_buffer.sample_byte_size(
- meta: Dict[str, Any],
- keys: List[str],
Return the serialized byte size of a sample from its metadata alone.
- Parameters:
meta – Sample metadata from :func:
tensor_metadata.keys – Sorted tensor keys from :func:
sample_keys.
- Returns:
Byte count matching :func:
serialize_samplefor the same sample.
- bridge.data.megatron_mimo.reorder_buffer.deserialize_sample(
- buf: torch.Tensor,
- offset: int,
- meta: Dict[str, Any],
- keys: List[str],
Reconstruct one sample from a
uint8buffer (inverse of :func:serialize_sample).- Parameters:
buf – Received 1D
uint8buffer.offset – Byte offset of this sample in
buf.meta – This sample’s metadata from :func:
tensor_metadata.keys – Sorted tensor keys from :func:
sample_keys.
- Returns:
the rebuilt sample and the byte offset just past it in
buf.- Return type:
(flat, new_offset)
- bridge.data.megatron_mimo.reorder_buffer.prepare_sample_exchange(
- local_flats: List[Dict[str, Any]],
- local_global_indices: List[int],
- route: List[Tuple[int, int, int]],
- all_global_indices: List[List[int]],
- all_tensor_meta: List[List[Dict[str, Any]]],
- dp_rank: int,
- dp_size: int,
- *,
- window_size: int = 1,
Build the kept-local partition, packed send buffer, and splits for one
all_to_all_single.Sender and receiver sort moves by
(dst_slot, canonical_pos, src_local_idx), so the packed bytes andrecv_scheduleline up without further communication.- Parameters:
local_flats – This rank’s flattened samples in local order.
local_global_indices – Global index of each entry in
local_flats.route –
(dst_slot, owner_rank, canonical_pos)per global sample index.all_global_indices – Global indices held by each rank, all-gathered.
all_tensor_meta –
- func:
tensor_metadataof each rank’s samples, all-gathered.
dp_rank – This rank’s module-local DP rank.
dp_size – This module’s DP size.
window_size – Number of micro-batch slots; every
dst_slotmust be in[0, window_size).
- Returns:
Dict with
local_samples((dst_slot, canonical_pos, local_idx)kept here),send_buf, per-rank bytesend_splits/recv_splits, andrecv_schedule({src_rank: [(dst_slot, canonical_pos, src_local_idx)]}in receive order).- Raises:
ValueError – If a
dst_slotis outside[0, window_size).
- bridge.data.megatron_mimo.reorder_buffer._flatten_nested(
- d: Dict[str, Any],
- prefix: str = '',
Flatten a nested batch dict to dotted keys, preserving tensors and
None.
- bridge.data.megatron_mimo.reorder_buffer._unflatten_nested(
- flat: Dict[str, Any],
Rebuild the nested dict from dotted keys (inverse of :func:
_flatten_nested).
- bridge.data.megatron_mimo.reorder_buffer._cu_img_from_counts(image_counts: Any) List[int]#
Return the cumulative image-offset table
[0, c0, c0+c1, ...]from per-sample image counts.
- bridge.data.megatron_mimo.reorder_buffer.empty_like_vision(
- ref_hidden_states: torch.Tensor,
- ref_grid_thw: torch.Tensor,
Return empty
[0, d]hidden states and[0, 3]grid matching a reference sample’s dtype and device.Text-only samples contribute these placeholders so every sample carries the same vision keys and the merge concat and byte-split metadata stay symmetric across ranks.
- Parameters:
ref_hidden_states – A
[patches, d]hidden-states tensor to match.ref_grid_thw – A
[n_images, 3]grid tensor to match.
- Returns:
(hidden_states, grid_thw)with zero rows.
- bridge.data.megatron_mimo.reorder_buffer.patches_per_image(grid_thw: torch.Tensor) torch.Tensor#
Return the per-image patch count
prod(t, h, w)for a[n_images, 3]grid.- Parameters:
grid_thw – Per-image
(t, h, w)grid.- Returns:
[n_images]patch counts.
- bridge.data.megatron_mimo.reorder_buffer._reorder_vision_by_images(
- hidden_states: torch.Tensor,
- grid_thw: torch.Tensor,
- image_perm: list[int],
Permute
hidden_states(patch dim) andgrid_thw(image dim) by whole image.
- bridge.data.megatron_mimo.reorder_buffer._gather_vision_subdict(
- value: Dict[str, Any],
- sample_indices: list[int],
- n_samples: int,
- *,
- cu_img: list[int] | None = None,
Gather a vision
{hidden_states, grid_thw}sub-dict by sample, supporting 0..N images per sample.
- bridge.data.megatron_mimo.reorder_buffer._apply_sample_dispatch(
- batch: Dict[str, Any],
- sample_indices: list[int],
- *,
- n_samples: int,
- cu_img: list[int] | None = None,
Gather
sample_indicesfrom every tensor, vision sub-dict, and per-sample list in a batch.
- bridge.data.megatron_mimo.reorder_buffer.split_microbatch(
- batch: Dict[str, Any],
- *,
- cu_img: List[int] | None = None,
Split a micro-batch into
Bper-sample flat (dotted-key) dicts with size-1 batch dims.- Parameters:
batch – Nested micro-batch.
cu_img – Cumulative per-sample image offsets (length
B + 1);Noneassumes one image per sample.
- Returns:
Bflat sample dicts in batch order.
- bridge.data.megatron_mimo.reorder_buffer.merge_samples(
- flats: List[Dict[str, Any]],
Rebuild a micro-batch from per-sample flat dicts (inverse of :func:
split_microbatch).- Parameters:
flats – Per-sample flat dicts in the desired batch order.
- Returns:
The nested micro-batch, tensors concatenated on their batch, patch, or image dim.
- Raises:
ValueError – If
flatsis empty.
- bridge.data.megatron_mimo.reorder_buffer._merge_nested(
- samples: List[Dict[str, Any]],
- bridge.data.megatron_mimo.reorder_buffer._batch_to_cuda(
- batch: Dict[str, Any],
Move a nested batch dict’s tensors to CUDA (non-blocking).
- bridge.data.megatron_mimo.reorder_buffer._batch_size(batch: Dict[str, Any]) int#
Return the sample count of a micro-batch from one of its LM tensors.
- bridge.data.megatron_mimo.reorder_buffer.sample_cost(
- flat: Dict[str, Any],
- *,
- encoder_cost_weight: float,
- language_cost_weight: float = 0.0,
- image_token_id: int | None = None,
- square_merge_size: int = 1,
Return
encoder_cost_weight * patches + language_cost_weight * real_tokensfor one sample.Both terms must be collation-independent so vision and language derive the same cost without communicating: with
image_token_idthe patch count comes frominput_ids, which every module holds (grid_thwis nulled on language shards), and tokens come fromattention_mask.- Parameters:
flat – One per-sample flat (dotted-key) dict.
encoder_cost_weight – Weight per vision patch.
language_cost_weight – Weight per real token;
0.0disables the term.image_token_id – Placeholder token id counted in
input_ids;Nonesumsgrid_thwinstead.square_merge_size – Patches represented by one placeholder token (
spatial_merge_size**2).
- Returns:
The weighted per-sample cost.
- bridge.data.megatron_mimo.reorder_buffer.reassemble_window(
- local_flats: List[Dict[str, Any]],
- plan: Dict[str, Any],
- recv_buf: torch.Tensor,
- all_tensor_meta: List[List[Dict[str, Any]]],
- *,
- dp_size: int,
- window_size: int,
- local: int,
Rebuild this rank’s
Wmicro-batches from kept-local and received samples.- Parameters:
local_flats – This rank’s input samples; kept-local ones are indexed by
local_idx.plan – Output of :func:
prepare_sample_exchange.recv_buf – The
all_to_all_singleoutput buffer.all_tensor_meta – All-gathered per-rank tensor metadata used to decode
recv_buf.dp_size – This module’s DP size.
window_size – Number of micro-batch slots
W.local – Expected samples per slot on this rank.
- Returns:
Wmicro-batches, slotsat indexs, each in canonical order.- Raises:
RuntimeError – If the received byte count, a slot’s sample count, or the canonical positions do not match
plan.
- bridge.data.megatron_mimo.reorder_buffer.exchange_by_route(
- local_flats: List[Dict[str, Any]],
- route: List[Tuple[int, int, int]],
- all_global_indices: List[List[int]],
- all_tensor_meta: List[List[Dict[str, Any]]],
- *,
- dp_rank: int,
- dp_size: int,
- window_size: int,
- local: int,
- dp_group_nccl: Any,
- nccl_stream: Any = None,
Run one ragged
all_to_all_singleforrouteand reassembleWmicro-batches.- Parameters:
local_flats – This rank’s input samples.
route –
(dst_slot, owner_rank, canonical_pos)per global sample index.all_global_indices – Global indices held by each rank, all-gathered.
all_tensor_meta – All-gathered per-rank tensor metadata.
dp_rank – This rank’s module-local DP rank.
dp_size – This module’s DP size.
window_size – Number of micro-batch slots
W.local – Samples per slot on this rank.
dp_group_nccl – NCCL process group for the sample all-to-all.
nccl_stream – Optional CUDA stream to run the all-to-all on.
- Returns:
Wmicro-batches for this rank, slotsat indexs.
- bridge.data.megatron_mimo.reorder_buffer.exchange_window(
- batches: List[Dict[str, Any]],
- *,
- dp_rank: int,
- dp_size: int,
- n_groups: int,
- cost_of: Any,
- dp_group_gloo: Any,
- dp_group_nccl: Any,
- nccl_stream: Any = None,
- image_count_of: Any = None,
- probe: bool = False,
Cost-balance a window of
Wmicro-batches across a module DP group in one exchange.Costs and metadata are all-gathered over Gloo once per window and the samples move in one
all_to_all_single; paired modules must pass the samen_groupsandcost_of.- Parameters:
batches – This rank’s
Wmicro-batch shards, each with the same local sample count.dp_rank – This rank’s module-local DP rank.
dp_size – This module’s DP size.
n_groups – Canonical group count (LCM of the module DP sizes).
cost_of –
callable(flat) -> floatper-sample cost (see :func:sample_cost).dp_group_gloo – Gloo process group for the metadata all-gather.
dp_group_nccl – NCCL process group for the sample all-to-all.
nccl_stream – Optional CUDA stream to run the all-to-all on.
image_count_of – Optional
callable(batch) -> LongTensorper-sample image counts.probe – When
True, log the per-slot cost spread ondp_rank == 0.
- Returns:
Wrebalanced micro-batches for this rank, slotsat indexs.- Raises:
ValueError – If
batchesis empty or the shards differ in local size.
- bridge.data.megatron_mimo.reorder_buffer.reorder_window_local(
- batches: List[Dict[str, Any]],
- *,
- n_groups: int,
- cost_of: Any,
- image_count_of: Any = None,
Permute a window into canonical order on a
dp_size == 1module without any exchange.- Parameters:
batches – This rank’s
Wwhole micro-batches.n_groups – Canonical group count (LCM of the module DP sizes), same as the paired module.
cost_of –
callable(flat) -> floatper-sample cost, same as the paired module.image_count_of – Optional
callable(batch) -> LongTensorper-sample image counts.
- Returns:
Wmicro-batches permuted into canonical order.
- bridge.data.megatron_mimo.reorder_buffer._SENTINEL#
‘object(…)’
- class bridge.data.megatron_mimo.reorder_buffer._WorkerException(exc: BaseException)#
Carry an overlap-worker exception across the queue for re-raising on the consumer thread.
Initialization
- bridge.data.megatron_mimo.reorder_buffer._record_stream_for_batch(
- value: Any,
- stream: torch.cuda.Stream,
Tie every CUDA tensor in
valuetostreambefore a cross-thread handoff.
- class bridge.data.megatron_mimo.reorder_buffer.ReorderingBuffer(
- data_iterator: Any,
- *,
- dp_rank: int,
- dp_size: int,
- n_groups: int,
- cost_of: Any,
- dp_group_gloo: Any,
- dp_group_nccl: Any,
- overlap: bool = True,
- image_count_of: Any = None,
- window_size: int = 1,
Wrap a MegatronMIMO data iterator so each yielded micro-batch is cost-balanced.
window_sizemicro-batches are balanced in one :func:exchange_windowcall and served one at a time; atdp_size == 1they are permuted locally. Withoverlap=Truea background thread exchanges the next window on a dedicated CUDA stream and the sidedp_group_ncclwhile the main thread computes the current one, so up to three windows are resident at once.CUDA_DEVICE_MAX_CONNECTIONS=1is rejected withoverlap=Truebecause the side all-to-all and the DDP all-reduce then share one hardware queue and can deadlock.- Parameters:
data_iterator – Source iterator yielding this rank’s micro-batch shards.
dp_rank – This rank’s module-local DP rank.
dp_size – This module’s DP size.
n_groups – Canonical group count (LCM of the module DP sizes).
cost_of –
callable(flat) -> floatper-sample cost.dp_group_gloo – Gloo process group for the metadata all-gather.
dp_group_nccl – NCCL process group for the sample all-to-all.
overlap – Exchange the next window on a background thread.
image_count_of – Optional
callable(batch) -> LongTensorper-sample image counts.window_size – Micro-batches balanced per exchange.
- Raises:
ValueError – If
window_size < 1oroverlap=TrueunderCUDA_DEVICE_MAX_CONNECTIONS=1.
Initialization
- __iter__()#
- shutdown() None#
Stop the overlap worker and wait briefly for it to exit.
- __del__() None#
Shut down the worker for runs that drop the iterator without closing it.
- _exchange_window(
- batches: List[Dict[str, Any]],
- stream: Any,
- _collect_window() List[Dict[str, Any]]#
Pull up to
window_sizemicro-batches from the source iterator.
- _refill_window() None#
Collect and exchange the next window synchronously; leave
_activeempty when exhausted.
- _put_worker_item(item: Any) None#
Put an item on the bounded queue unless shutdown is requested while it is full.
- _worker() None#
- __next__() Dict[str, Any]#
- bridge.data.megatron_mimo.reorder_buffer.build_module_dp_process_groups(
- dp_group_nccl_main: Any,
- *,
- overlap: bool,
Create the Gloo and side NCCL process groups for this rank’s module DP group.
dist.new_groupis collective over the world, so every rank creates every module’s groups in the same sorted order and keeps the ones it belongs to.- Parameters:
dp_group_nccl_main – This rank’s main DP NCCL group (
grid.get_pg(["dp"])).overlap – Create a separate NCCL group for the side-stream exchange; otherwise reuse the main one.
- Returns:
(dp_rank, dp_size, dp_group_gloo, dp_group_nccl).