Skip to content

Fix/issue 20 - #24

Open
Dikshant-Neupane wants to merge 3 commits into
microsoft:mainfrom
Dikshant-Neupane:fix/issue-20
Open

Fix/issue 20#24
Dikshant-Neupane wants to merge 3 commits into
microsoft:mainfrom
Dikshant-Neupane:fix/issue-20

Conversation

@Dikshant-Neupane

Copy link
Copy Markdown

Description

This PR resolves Issue #20, comprehensively addressing performance degradation, numerical instability in CFG, ODE trajectory overshoot in Turbo models, and VRAM limitations during batch processing.

Key Changes

  1. Global Norm CFG Scaling (pipeline.py)

    • Replaced per-token norm scaling with global sequence-wise mean normalization across spatial dimensions.
    • Eliminates vector magnitude collapse in uniform image regions and preserves precise 3D vector directions, greatly reducing saturation artifacts at high CFG scales.
  2. Low-Step Cosine Turbo Schedule (pipeline.py)

    • Added a branch in build_scheduler(): when num_steps <= 8, base sigmas follow a smooth cosine downstep schedule instead of a massive linear drop.
    • Softens integration leaps out of the ODE start gate, radically fixing overshoot blurring and trajectory drift during Turbo inference.
  3. Dynamic VRAM Guard for batch_cfg (pipeline.py)

    • Enhanced _build_pack_ctx() to estimate required attention memory for combined multi-image packed sequence arrays.
    • If memory required exceeds 70% of available GPU VRAM (via torch.cuda.mem_get_info), the system automatically issues a warning and disables sequence duplication (batch_cfg=False), preventing silent OOM crashes on 24GB consumer GPUs.
  4. Zero-Loop Vectorized SDPA Fallback (_attn_backend.py)

    • Eliminated the Python for-loop iterating over flash_attn_varlen_func slices.
    • Leveraged torch.nn.utils.rnn.pad_sequence to transform packed arrays into grouped (B, L, H, D) blocks, utilizing a single fused F.scaled_dot_product_attention kernel launch.
    • Destroys CPU-side kernel dispatch bottlenecks, boosting non-FlashAttention performance up to 8–12×.
  5. SM90+ FA4 Hardware Guard (_attn_backend.py)

    • Injected torch.cuda.get_device_capability() evaluation within the FlashAttention-4 lazy resolver.
    • Emits a logger warning and automatically routes execution to _resolve_fa2() on Ampere (major < 9) hardware, preventing unoptimized CuTe DSL DSL fallback compilation errors.
  6. Test Suite (tests/test_flow_performance.py)

    • Complete pytest coverage introduced to assert exact spatial equivalence in CFG, verify exact terminal boundary math in Cosine Sigmas, and assert seamless downshifting against mocked Out-Of-Memory boundaries.

Related Issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG/Performance] CFG vector collapse, Turbo trajectory drift, and OOM in packed multi-image sequences

1 participant