Repository navigation
perf(fix): accumulate per-microbatch FLOPS metadata for accurate… #3529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7f1ff31
perf(training): accumulate per-microbatch FLOPS metadata for accurate…
TianweiDing d9c66f6
Merge branch 'NVIDIA-NeMo:main' into merge-to-upstream
SophusDavid d7c722f
perf(fix): VLM FLOPS accuracy & TFLOP/s consistency
TianweiDing f5d422b
perf(fix): add UT "test_attention_term_scales_linearly_with_seqlen_sq…
TianweiDing 5f387e4
Merge branch 'main' into merge-to-upstream
SophusDavid 4a7f00b
Merge branch 'NVIDIA-NeMo:main' into merge-to-upstream
SophusDavid 2b5012c
Merge branch 'NVIDIA-NeMo:main' into merge-to-upstream
SophusDavid dfc2fd4
[perf] fix: reformat commit.
TianweiDing bbf8079
Merge branch 'main' into merge-to-upstream
meatybobby 25a1598
Merge branch 'NVIDIA-NeMo:main' into merge-to-upstream
SophusDavid 91e73a0
[area] style: fix ruff-format
TianweiDing 565b630
Merge branch 'main' into merge-to-upstream
SophusDavid d7f8279
Merge branch 'NVIDIA-NeMo:main' into merge-to-upstream
SophusDavid 3312d40
training] fix: correct FLOPS seqlen accumulator over-counting under VPP
TianweiDing b7cf908
[training] fix: guard vp_size type check to avoid MagicMock issues in…
TianweiDing 0369c27
Merge branch 'main' into merge-to-upstream
aroshanghias-nvd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,8 +26,98 @@ | |
| _lora_seq_stats_cache: dict = {} | ||
|
|
||
|
|
||
| def num_floating_point_operations(cfg: ConfigContainer, batch_size: int = 1): | ||
| """Return the number of floating point operations""" | ||
| def vit_flops( | ||
| batch_size, | ||
| num_patches, | ||
| depth, | ||
| hidden_size, | ||
| num_heads, | ||
| intermediate_size, | ||
| spatial_merge_size=2, | ||
| out_hidden_size=4096, | ||
| ): | ||
| """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) | ||
|
|
||
| Args: | ||
| batch_size: Batch size. | ||
| num_patches: Total number of vision patches (before spatial merge). | ||
| depth: Number of ViT transformer layers. | ||
| hidden_size: ViT hidden dimension. | ||
| num_heads: Number of attention heads in ViT. | ||
| intermediate_size: ViT MLP intermediate dimension. | ||
| spatial_merge_size: Spatial merge factor (2 means 2x2 merge). | ||
| out_hidden_size: Output hidden size after patch merger (LLM hidden size). | ||
|
|
||
| Returns: | ||
| Total training FLOPs (forward * 3 for fwd+bwd). | ||
| """ | ||
| if num_patches <= 0: | ||
| return 0 | ||
|
|
||
| # ViT Transformer layers (bidirectional attention) | ||
| per_token_per_layer = ( | ||
| # QKV + O projections: 4 matmuls of h x h => 4 * 2 * h^2 FMA = 8h^2 | ||
| # but standard counting: Q,K,V each h->h (3 * 2h^2) + O h->h (2h^2) = 8h^2 | ||
| 8 * hidden_size**2 | ||
| # Attention core (full bidirectional, not causal): QK^T + attn*V | ||
| # = 2 * 2 * h * num_patches = 4 * h * num_patches | ||
| + 4 * hidden_size * num_patches | ||
| # MLP (GELU, 2 matmuls): fc1 h->intermediate + fc2 intermediate->h | ||
| # = 2 * 2 * h * intermediate = 4 * h * intermediate | ||
| + 4 * hidden_size * intermediate_size | ||
| ) | ||
| transformer_flops_val = per_token_per_layer * num_patches * depth | ||
|
|
||
| # Patch Merger: spatial merge (2x2) + MLP projection | ||
| merge_unit = spatial_merge_size**2 | ||
| merged_hidden = hidden_size * merge_unit # concatenated hidden dim | ||
| num_merged_tokens = num_patches // merge_unit if merge_unit > 0 else num_patches | ||
| merger_flops_val = num_merged_tokens * ( | ||
| 2 * merged_hidden * merged_hidden # fc1: merged_hidden -> merged_hidden | ||
| + 2 * merged_hidden * out_hidden_size # fc2: merged_hidden -> out_hidden_size | ||
| ) | ||
|
|
||
| return (transformer_flops_val + merger_flops_val) * batch_size * 3 # 3x for training (fwd + bwd) | ||
|
|
||
|
|
||
| def num_floating_point_operations( | ||
| cfg: ConfigContainer, | ||
| batch_size: int = 1, | ||
| seqlen_sum: int = None, | ||
| seqlen_squared_sum: int = None, | ||
|
SophusDavid marked this conversation as resolved.
Outdated
|
||
| num_vision_patches: int = 0, | ||
| ): | ||
| """Return the number of floating point operations. | ||
|
|
||
| Args: | ||
| 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 | ||
| (batch_size * actual_seq_length^2). Used for attention core FLOPS | ||
| which scale quadratically with sequence length. | ||
| num_vision_patches: Total number of vision patches in the batch | ||
| (before spatial merge). Used to compute ViT encoder FLOPS. | ||
| """ | ||
| # Compute effective sequence length from actual values or fall back to config. | ||
| if seqlen_sum is not None and batch_size > 0: | ||
| effective_seq_length = seqlen_sum / batch_size | ||
| else: | ||
| effective_seq_length = cfg.model.seq_length | ||
| seqlen_sum = batch_size * cfg.model.seq_length | ||
|
|
||
| if seqlen_squared_sum is not None and batch_size > 0: | ||
| effective_seq_length_sq = seqlen_squared_sum / batch_size | ||
| else: | ||
| effective_seq_length_sq = effective_seq_length**2 | ||
|
SophusDavid marked this conversation as resolved.
Outdated
|
||
|
|
||
| peft = getattr(cfg, "peft", None) | ||
| is_lora = isinstance(peft, LoRA) | ||
| # If the model provider has a custom TFLOPS calculation method, use it (non-LoRA only). | ||
|
|
@@ -387,13 +477,13 @@ def transformer_flops(): | |
| ## o proj | ||
| + (cfg.model.num_attention_heads * getattr(cfg.model, "v_head_dim", 64)) * cfg.model.hidden_size | ||
| ## core attn | ||
| + cfg.model.seq_length | ||
| + effective_seq_length | ||
| * ( | ||
| cfg.model.num_attention_heads | ||
| * (getattr(cfg.model, "qk_head_dim", 64) + getattr(cfg.model, "qk_pos_emb_head_dim", 0)) | ||
| ) | ||
| / 2 | ||
| + cfg.model.seq_length * cfg.model.num_attention_heads * getattr(cfg.model, "v_head_dim", 64) / 2 | ||
| + effective_seq_length * cfg.model.num_attention_heads * getattr(cfg.model, "v_head_dim", 64) / 2 | ||
| ) | ||
| ) | ||
|
|
||
|
|
@@ -416,7 +506,7 @@ def transformer_flops(): | |
| effective_window = window_size[0] + window_size[1] + 1 | ||
| else: | ||
| effective_window = window_size | ||
| swa_context = min(effective_window, cfg.model.seq_length) | ||
| swa_context = min(effective_window, effective_seq_length) | ||
|
|
||
| if window_attn_skip_freq is None: | ||
| num_swa_layers = num_layers | ||
|
|
@@ -433,7 +523,7 @@ def transformer_flops(): | |
| num_swa_layers = 0 | ||
| num_full_attn_layers = num_layers | ||
|
|
||
| full_core = query_projection_size * cfg.model.seq_length / 2 * 2 | ||
| full_core = query_projection_size * effective_seq_length / 2 * 2 | ||
| swa_core = query_projection_size * swa_context / 2 * 2 | ||
|
|
||
| self_attn_term = ( | ||
|
|
@@ -445,7 +535,7 @@ def transformer_flops(): | |
| ) | ||
| ) | ||
| else: | ||
| full_core = query_projection_size * cfg.model.seq_length / 2 * 2 | ||
| full_core = query_projection_size * effective_seq_length / 2 * 2 | ||
| self_attn_term = 3 * 2 * num_layers * (proj_per_layer + full_core) | ||
|
|
||
| # Handle GDN (Gated DeltaNet) hybrid attention variant. | ||
|
|
@@ -525,8 +615,7 @@ def transformer_flops(): | |
| ) + 2 * moe_latent_size | ||
|
|
||
| total_floating_point_operations = ( | ||
| batch_size | ||
| * cfg.model.seq_length | ||
| seqlen_sum | ||
| * ( | ||
| # MLP | ||
| 3 | ||
|
|
@@ -556,7 +645,29 @@ def transformer_flops(): | |
| + 3 * 2 * cfg.model.hidden_size * padded_vocab_size * (mtp_num_layers + 1) | ||
| ) | ||
| ) | ||
| return total_floating_point_operations | ||
| return total_floating_point_operations + _compute_vit_flops() | ||
|
|
||
| def _compute_vit_flops(): | ||
| """Compute ViT encoder FLOPs if vision config is available. | ||
|
|
||
| Note: num_vision_patches is the *total* patches across the batch. | ||
| ViT attention is per-image (not cross-image), so we compute FLOPs | ||
| using per-image patch count to get correct quadratic attention scaling. | ||
| """ | ||
| vision_config = getattr(cfg.model, "vision_config", None) | ||
| if vision_config is None or num_vision_patches <= 0: | ||
| return 0 | ||
| patches_per_image = num_vision_patches / batch_size if batch_size > 0 else num_vision_patches | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this has an implicit assumption that batch size == num images? |
||
| return vit_flops( | ||
| batch_size=batch_size, | ||
| num_patches=patches_per_image, | ||
| depth=getattr(vision_config, "depth", 0), | ||
| hidden_size=getattr(vision_config, "hidden_size", 0), | ||
| num_heads=getattr(vision_config, "num_heads", 16), | ||
| intermediate_size=getattr(vision_config, "intermediate_size", 0), | ||
| spatial_merge_size=getattr(vision_config, "spatial_merge_size", 2), | ||
| out_hidden_size=getattr(vision_config, "out_hidden_size", cfg.model.hidden_size), | ||
| ) | ||
|
|
||
| # Main entrypoint for FLOPs calculation. | ||
| if getattr(cfg.model, "is_hybrid_model", False): | ||
|
|
@@ -588,9 +699,9 @@ def transformer_flops(): | |
| ) | ||
|
|
||
| # Compute hybrid model FLOPs. | ||
| return hybrid_flops( | ||
| llm_flops = hybrid_flops( | ||
| batch_size=batch_size, | ||
| seq_len=cfg.model.seq_length, | ||
| seq_len=effective_seq_length, | ||
| hidden_size=cfg.model.hidden_size, | ||
| num_attn_layers=num_attn_layers, | ||
| num_mamba_layers=num_mamba_layers, | ||
|
|
@@ -626,6 +737,7 @@ def transformer_flops(): | |
| vocab_size=padded_vocab_size, | ||
| mtp_num_layers=mtp_num_layers, | ||
| ) | ||
| return llm_flops + _compute_vit_flops() | ||
| else: | ||
| # Compute standard Transformer model FLOPs. | ||
| return transformer_flops() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.