Add vision flops calculation for vlm - #2575
Conversation
📝 WalkthroughWalkthroughThe PR adds vision FLOPs instrumentation to training pipelines and improves configuration type-safety. Runtime statistics for vision-language models are captured during forward passes, configuration objects receive safe type normalization and finalization guards, and FLOP calculation utilities now estimate vision tower costs using both runtime-derived and config-based assumptions. Changes
Sequence DiagramsequenceDiagram
participant FS as forward_step
participant CFG as Config Object
participant FLOP as FLOP Utils
participant OUT as Output FLOPs
FS->>FS: Process vision/language inputs
FS->>CFG: Store runtime statistics<br/>(_runtime_vision_tokens_pre_per_sample, etc.)
FS->>FS: Continue training forward pass
FLOP->>CFG: Check for vision_config
FLOP->>CFG: Retrieve runtime statistics if available
FLOP->>FLOP: Calculate vision FLOPs<br/>(patch embedding, ViT, mergers)
FLOP->>FLOP: Apply training multiplier (3.0×)
FLOP->>FLOP: Integrate with hybrid/standard<br/>transformer FLOPs
FLOP->>OUT: Return total FLOPs
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/megatron/bridge/training/config.py`:
- Around line 1910-1919: The current ordering causes
cfg.set_data_parallel_size() to access cfg.comm_overlap as if it were an object
while it may still be a dict; before calling cfg.set_data_parallel_size()
normalize cfg.comm_overlap: if cfg.comm_overlap is a dict, replace it with
CommOverlapConfig(**cfg.comm_overlap), and if it has finalize(), call finalize()
so that cfg.comm_overlap is a proper object; then call
cfg.set_data_parallel_size() and only after that call
cfg.comm_overlap.setup(cfg.model, cfg.optimizer, cfg.ddp) (keeping guards for
cfg.comm_overlap is not None).
In `@src/megatron/bridge/training/vlm_step.py`:
- Around line 459-488: The vision FLOPs/statistics computation using vision_cfg,
image_grid_thw, video_grid_thw, and microbatch_size_for_stats should be made
fail-open so telemetry errors never abort training: wrap the entire block that
computes vision_tokens_pre_total, vision_sum_seqlen_sq_pre_total,
vision_tokens_pre_per_sample, vision_tokens_post_per_sample and sets
state.cfg._runtime_vision_* in a try/except, catch Exception, log the exception,
and on any error set the runtime vision stats to safe defaults (e.g., 0) so
forward continues; ensure spatial_merge_size, hw/t, and prod(dim=-1) uses remain
unchanged inside the try and do not propagate exceptions out of the function.
- Around line 424-450: Replace the broad "except Exception" in the block that
inspects forward_args/packed (variables: packed, cu, forward_args, tokens) and
writes to state.cfg._runtime_lm_* with a targeted exception handler that only
catches the expected errors (AttributeError, IndexError, RuntimeError); keep the
try body limited to the attribute/tensor operations (accessing
packed.cu_seqlens_q_padded, indexing cu, computing lengths and tensors) and move
any pure-assignment work into an else branch where possible, and ensure the
fallback sets microbatch_size_for_stats the same way as currently done when
those specific exceptions occur.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/megatron/bridge/training/config.pysrc/megatron/bridge/training/utils/flop_utils.pysrc/megatron/bridge/training/vlm_step.py
|
Closing as superseded by #3529. Current main includes vision-side FLOPs accounting and per-microbatch metadata with focused tests. |


What does this PR do ?
As there is a trend that vision part in vlm is becoming larger and larger, we could not neglect flops costs on vision part any more. Especially MoE language model is becoming more popular, compared with activated weights, we could speculate that compute in vision part would become more understanding.
current flops calculation does not include vision part for vlm, this PR fill the gap
Changelog
GitHub Actions CI
See the CI sectionin the Contributing doc for how to trigger the CI. A Nvidia developer will need to approve and trigger the CI for external contributors.
Before your PR is "Ready for review"
Pre checks:
If you haven't finished some of the above items you can still open "Draft" PR.
Additional Information
There are several options to do this functionality.
Summary by CodeRabbit
New Features
Bug Fixes