bridge.training.utils.flop_utils#
Module Contents#
Classes#
Data-parallel-global FLOPS statistics collected during one training step. |
Functions#
Recognize GDN variants across current main and older MCore dev branches. |
|
Return True if a packed dataset file exists, for both parquet and npy formats. |
|
Return the virtual-pipeline stage assigned to a model chunk, if any. |
|
Coerce a FLOPs accumulator ( |
|
Resolve all data-parallel-global FLOPS statistics used by training. |
|
Resolve sequence statistics and the legacy total vision-patch count. |
|
Add an int or scalar tensor to a state accumulator. |
|
Return a scalar sum without forcing a CUDA host sync inside forward_step. |
|
Extract sub-sequence lengths from cu_seqlens metadata. |
|
Accumulate per-microbatch FLOPS metadata onto |
|
Return a direct or thinker-nested vision config when available. |
|
Calculate ViT FLOPS from additive per-attention-sequence statistics. |
|
Build additive Qwen ViT patch statistics without a device-to-host sync. |
|
Calculate exact ViT FLOPS for Qwen-style temporal-height-width grids. |
|
Calculate FLOPs for a Vision Transformer (ViT) encoder + patch merger. |
|
Return the number of floating point operations. |
Data#
API#
- bridge.training.utils.flop_utils._lora_seq_stats_cache: dict#
None
- bridge.training.utils.flop_utils._mcore_is_gated_delta_net_variant#
‘cast(…)’
- bridge.training.utils.flop_utils._is_gated_delta_net_variant(
- experimental_attention_variant: str | None,
Recognize GDN variants across current main and older MCore dev branches.
- class bridge.training.utils.flop_utils.GlobalFlopsRuntimeStats#
Data-parallel-global FLOPS statistics collected during one training step.
.. attribute:: seqlen_sum
Total padded language tokens, or
Nonewhen unavailable... attribute:: seqlen_squared_sum
Sum of squared language subsequence lengths.
.. attribute:: num_vision_patches
Legacy aggregate vision-patch count.
.. attribute:: vision_patch_sum
Exact sum of independent vision patch counts.
.. attribute:: vision_patch_squared_sum
Exact sum of squared independent patch counts.
.. attribute:: vision_merged_token_sum
Exact sum of post-merger vision tokens.
.. attribute:: cross_seqlen_sum
Total cross-attention key/value length.
.. attribute:: cross_seqlen_product_sum
Sum of query/key-value length products.
- seqlen_sum: int | None#
None
- seqlen_squared_sum: int | None#
None
- num_vision_patches: int#
0
- vision_patch_sum: int#
0
- vision_patch_squared_sum: int#
0
- vision_merged_token_sum: int#
0
- cross_seqlen_sum: int | None#
None
- cross_seqlen_product_sum: int | None#
None
- property has_exact_vision_stats: bool#
Return whether exact additive vision-patch statistics were collected.
- bridge.training.utils.flop_utils._packed_data_exists(path: str | None) bool#
Return True if a packed dataset file exists, for both parquet and npy formats.
Parquet specs may be a single file, a glob, or a directory, so they are detected with
is_packed_parquet_specand validated via the packed-parquet resolver rather than a barePath.exists()check (which would fail for globs/directories and silently disable LoRA-aware FLOP accounting). This mirrors the format detection incalculate_avg_seqlenso a spec that passes this gate also reads correctly.
- bridge.training.utils.flop_utils.get_model_chunk_vp_stage(model: torch.nn.Module) int | None#
Return the virtual-pipeline stage assigned to a model chunk, if any.
- Parameters:
model – Model chunk, possibly wrapped by mixed precision or DDP.
- Returns:
The integer virtual-pipeline stage, or
Nonefor an unchunked model or a model that does not expose the stage.
- bridge.training.utils.flop_utils._accumulator_to_int(value) int#
Coerce a FLOPs accumulator (
intor scalarTensor) toint.
- bridge.training.utils.flop_utils.resolve_global_flops_runtime_stats(
- state,
- *,
- data_parallel_size: int,
- vp_size: int | None = None,
- dp_group=None,
- include_vision_patch_stats: bool = False,
- include_cross_attention_stats: bool = False,
Resolve all data-parallel-global FLOPS statistics used by training.
Reads the accumulators populated by the forward step (
_flops_seqlen_sum= Σ padded tokens,_flops_seqlen_sq_sum= Σᵢ sᵢ² over real sub-sequences,_flops_vision_patches, optional exact additive ViT patch statistics, and optional cross-attention key/value and query-key products) and reduces them to global totals across the data-parallel group.Under variable-length (THD packed) training the per-rank
Σᵢ sᵢ²can differ across DP ranks, so a single SUM all-reduce overdp_groupis used to get the exact global sum. Dense BSHD training never requests this reduce: every DP rank contributes the same fixed sequence statistics, so extrapolatinglocal * data_parallel_sizeis exact and avoids an unnecessary collective.- Parameters:
state – Object carrying the
_flops_*accumulators (GlobalState).data_parallel_size – Size of the data-parallel group (used for the extrapolation fallback).
vp_size – Virtual pipeline size. Kept for call-site compatibility; VPP does not rescale these accumulators because they already represent the executed training step consumed by the full-model FLOPS formula.
dp_group – Data-parallel process group to SUM-reduce over. Must be the pure DP group (excluding CP) matching
data_parallel_size— CP ranks share the samecu_seqlensand would double-count.include_vision_patch_stats – Whether the collective includes the three exact additive ViT values. This must be uniform across every rank in
dp_group.include_cross_attention_stats – Whether the collective includes the two optional cross-attention values. This must be uniform across every rank in
dp_group; callers should derive it from model capability, not local batch contents.
- Returns:
Global statistics with sequence values set to
Nonewhen no corresponding accumulation happened and vision values set to0when no matching metadata was accumulated.
- bridge.training.utils.flop_utils.resolve_global_flops_seqlen_stats(
- state,
- *,
- data_parallel_size: int,
- vp_size: int | None = None,
- dp_group=None,
Resolve sequence statistics and the legacy total vision-patch count.
This compatibility wrapper preserves the established three-value return contract and its three-integer collective.
- bridge.training.utils.flop_utils._add_flops_accumulator(state, name: str, delta) None#
Add an int or scalar tensor to a state accumulator.
- bridge.training.utils.flop_utils._scalar_sum_for_accumulator(value: torch.Tensor) int | torch.Tensor#
Return a scalar sum without forcing a CUDA host sync inside forward_step.
- bridge.training.utils.flop_utils._real_subseq_lengths(
- cu_seqlens: torch.Tensor | None,
- cu_seqlens_argmin: torch.Tensor | None = None,
- cu_seqlens_unpadded: torch.Tensor | None = None,
- cu_seqlens_unpadded_argmin: torch.Tensor | None = None,
Extract sub-sequence lengths from cu_seqlens metadata.
Prefers
cu_seqlens_unpadded(true sub-sequence boundaries whenpad_seq_to_mult > 1) over the paddedcu_seqlens. Truncates by the corresponding*_argminwhen provided. ReturnsNonewhen no cu_seqlens info is available.Runs once per micro-batch, so it must stay free of GPU→CPU syncs:
cu_seqlensis a (monotonic non-decreasing) cumulative sum, so the diffs are always>= 0and we do not filter them — a boolean mask likesub_seq_lens[sub_seq_lens > 0]would force a data-dependent-size device sync every micro-batch (the cause of a ~7% throughput regression). Zero-length entries (padding) contribute0toΣᵢ sᵢ²so dropping them is unnecessary; the result is identical.
- bridge.training.utils.flop_utils.accumulate_flops_metadata(
- state,
- tokens: torch.Tensor | None,
- *,
- vp_stage: int | None = None,
- config_seq_len: int | None = None,
- cu_seqlens: torch.Tensor | None = None,
- cu_seqlens_argmin: torch.Tensor | None = None,
- cu_seqlens_unpadded: torch.Tensor | None = None,
- cu_seqlens_unpadded_argmin: torch.Tensor | None = None,
- cross_cu_seqlens: torch.Tensor | None = None,
- cross_cu_seqlens_unpadded: torch.Tensor | None = None,
- num_vision_patches: int | torch.Tensor | None = None,
- vision_patch_stats: tuple[int | torch.Tensor, int | torch.Tensor, int | torch.Tensor] | None = None,
Accumulate per-microbatch FLOPS metadata onto
state.Under interleaved pipeline parallelism, the forward step runs once per virtual model chunk for the same logical data microbatch. Only virtual stage 0 contributes metadata so model chunking does not multiply the full-model FLOPS estimate.
Noneand0both represent the primary/only chunk.Writes accumulators consumed by
train.pyat end of step:_flops_seqlen_sum:mbs * tokens.shape[1](padded total tokens this microbatch contributes), ormbs * config_seq_lenfor dense non-packed batches whose tensors were already context-parallel sliced. Drives the linear MLP/proj/logit terms._flops_seqlen_sq_sum: the THD attention term Σᵢ sᵢ², computed inline fromcu_seqlens(preferringcu_seqlens_unpadded). The per-pack sub-sequence lengths are reduced via :func:_scalar_sum_for_accumulator, which keeps the result on-device (no.item()) — so the per-microbatch path stays sync-free and the single host sync happens once per step in- func:
resolve_global_flops_seqlen_stats. Whencu_seqlensis absent (dense / non-packed) or degenerate, the host-int BSHD fallbackmbs * dense_seq_len²is accumulated instead (bit-exact with the pre-fix value).dense_seq_lenisconfig_seq_lenwhen provided, otherwisetokens.shape[1].
_flops_vision_patches: legacy total patch-count approximation._flops_vision_patch_sum,_flops_vision_patch_sq_sum, and_flops_vision_merged_token_sum: exact additive ViT statistics that preserve independent media/frame attention boundaries._flops_cross_seqlen_sumand_flops_cross_seqlen_product_sum: optional cross-attention Σᵢ kᵢ and Σᵢ qᵢkᵢ terms for model-specific estimators such as WAN.
num_vision_patchesremains supported for model callers that only expose a total patch count.vision_patch_statsis the exact(Σp, Σp², Σmerged_tokens)tuple computed by a caller that knows the encoder’s attention boundaries. Each element may be anintor scalarTensor; device tensors avoid a host sync here. This argument also opts into a conditional DP collective: if one rank supplies it, every rank in that pure DP group must supply a tuple, using(0, 0, 0)on ranks with no media.For THD packed training (offline packed LLM SFT or VLM in-batch packing), treating the whole pack as one length-
seq_lensequence over-counts attention FLOPS by a large factor: actual attention work is Σᵢ sᵢ², not (Σᵢ sᵢ)². Usingcu_seqlenshere closes that gap.
- bridge.training.utils.flop_utils._get_vision_config(
- cfg: megatron.bridge.training.config.ConfigContainer,
Return a direct or thinker-nested vision config when available.
- bridge.training.utils.flop_utils.vit_flops_from_patch_stats(
- cfg: megatron.bridge.training.config.ConfigContainer,
- *,
- patch_sum: int | torch.Tensor,
- patch_squared_sum: int | torch.Tensor,
- merged_token_sum: int | torch.Tensor,
Calculate ViT FLOPS from additive per-attention-sequence statistics.
- bridge.training.utils.flop_utils.vision_patch_stats_from_grid_thw(
- grid_thw: torch.Tensor,
- *,
- spatial_merge_size: int,
Build additive Qwen ViT patch statistics without a device-to-host sync.
- Parameters:
grid_thw – Integer tensor shaped
[..., 3]with temporal, height, and width patch-grid dimensions.spatial_merge_size – Vision encoder’s spatial merge size.
- Returns:
(patch_sum, patch_squared_sum, merged_token_sum). CUDA inputs produce scalar CUDA tensors; CPU inputs produce Python integers.- Raises:
ValueError – If the grid shape or spatial merge size is invalid.
- bridge.training.utils.flop_utils.vit_flops_from_grid_thw(
- cfg: megatron.bridge.training.config.ConfigContainer,
- grid_thw: torch.Tensor,
Calculate exact ViT FLOPS for Qwen-style temporal-height-width grids.
Qwen vision attention treats every temporal frame as an independent THD sequence. For a grid row
[t, h, w], linear ViT work therefore seest*h*wpatches while full-attention work seest*(h*w)^2rather than(t*h*w)^2. Computing the additive statistics directly avoids expanding frame lengths withrepeat_interleaveand keeps the microbatch path free of device-to-host synchronization.- Parameters:
cfg – Configuration container with a direct or thinker-nested vision config.
grid_thw – Integer tensor shaped
[..., 3]. Each row contains temporal frames, patch-grid height, and patch-grid width.
- Returns:
Scalar ViT training FLOPS on the same device as
grid_thw. Returns0if no vision config or no media rows are present.- Raises:
ValueError – If
grid_thwis not shaped[..., 3].
- bridge.training.utils.flop_utils.vit_flops(
- cfg: megatron.bridge.training.config.ConfigContainer,
- batch_size: int,
- num_patches: int,
Calculate FLOPs for a Vision Transformer (ViT) encoder + patch merger.
Includes:
ViT transformer layers (bidirectional full attention, not causal)
Patch merger (spatial merge + MLP projection to LLM hidden size)
- Parameters:
cfg – Configuration container. ViT hyper-parameters are read from the direct or thinker-nested vision config (
depth,hidden_size,num_heads,intermediate_size,spatial_merge_size,out_hidden_size). Passing the whole config keeps the public signature stable as the list of required ViT attributes grows.batch_size – Batch size.
num_patches – Per-image number of vision patches (before spatial merge). Callers that track the total patch count across the batch should divide by
batch_sizebefore invoking, because ViT attention is per-image (not cross-image) and scales quadratically with the per-image patch count.
- Returns:
Total training FLOPs (forward * 3 for fwd+bwd). Returns 0 when no
vision_configis attached ornum_patchesis non-positive.
- bridge.training.utils.flop_utils.num_floating_point_operations(
- cfg: megatron.bridge.training.config.ConfigContainer,
- batch_size: int = 1,
- seqlen_sum: int | None = None,
- seqlen_squared_sum: int | None = None,
- num_vision_patches: int = 0,
- cross_seqlen_sum: int | None = None,
- cross_seqlen_product_sum: int | None = None,
Return the number of floating point operations.
- Parameters:
cfg – Configuration container.
batch_size – Batch size.
seqlen_sum – Sum of actual sequence lengths across the batch (batch_size * actual_seq_length). When provided, overrides cfg.model.seq_length for more accurate FLOPS estimation with dynamic-length sequences (e.g., VLM with dynamic padding).
seqlen_squared_sum – Sum of squared sequence lengths across the batch (sum_i actual_seq_length_i^2). Used for attention core FLOPS which scale quadratically with sequence length; when omitted, falls back to
batch_size * effective_seq_length^2so the result matches the legacy constant-length estimate.num_vision_patches – Total number of vision patches in the batch (before spatial merge). Used to compute ViT encoder FLOPS.
cross_seqlen_sum – Sum of cross-attention key/value sequence lengths.
cross_seqlen_product_sum – Sum of per-sample query and key/value length products.