[BUG] Silent state_dict dropping, unchecked zero-padding, and missing VAE-Transformer channel assertions
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.6k
- Forks
- 174
- PR merge metrics
- No merged PRs in 30d
Description
Overview
During a technical audit of the codebase, I identified several critical bugs, architectural edge cases, and performance bottlenecks across the VAE tokenizer, Rectified Flow sampling, CUDA/kernel backends, and pipeline loading logic.
Below is a detailed breakdown of the root causes, reproduction conditions, and recommended fixes for these issues.
1. Latent Tokenizer & Spatial Compression (Mage-VAE)
-
1.1 Extreme Aspect Ratio Boundary Artifacts via Non-Replicating Padding (
mage_vae.py,utils.py)- Issue:
get_noise()usesmath.ceil(height/16)whileencode()requires exact multiples of 16. At extreme ratios (e.g., 4:1),AttnBlockusesmode="replicate"padding along the short axis, propagating boundary features axially and causing sub-pixel smearing. - Fix: Replace
math.ceilwith explicit reflection padding in input space (F.pad(x, ..., mode="reflect")).
- Issue:
-
1.2 FP16 / BF16 Posterior Sampling Quantization Noise (
mage_vae.py)- Issue: Lower-bounded
logvar = -20yields $\exp(-10) \approx 4.54 \times 10^{-5}$. In BF16, this has only ~3 significant mantissa bits, introducing ~12.5% quantization noise in uniform image regions. - Fix: Compute posterior sampling exponentially in
torch.float32regardless of model dtype before casting back.
- Issue: Lower-bounded
-
1.3 Frozen
adaLNModulation Prevents Dynamic Timestep Usage (mage_vae.py)- Issue:
_freeze_adaln_cache()permanently replaces modulation MLPs with constant buffers at $t=0$. Any progressive or iterative VAE refinement extensions silently fail. - Fix: Guard the cache replacement behind an explicit
_enable_adaln_cachetoggle.
- Issue:
2. Flow Matching & Sampling Trajectories (Mage-Flow)
-
2.1 CFG Renormalization Directional Collapse (
pipeline.py)- Issue: Dividing by per-token norm rescales velocity vectors non-uniformly and gates near-zero tokens (e.g., solid backgrounds) to zero velocity, causing spatial distortion.
- Fix: Apply global norm rescaling across tokens to preserve flow-field vector angles (
cond_norm / (comb_norm + 1e-6)).
-
2.2 Trajectory Drift in Few-Step Turbo Schedules (
pipeline.py)- Issue: Linear base sigmas with static shift $6.0$ cause a steep 33% step drop from $\sigma=1.0 \to 0.667$ on step 1, where ODE curvature is highest.
- Fix: Use a cosine distribution for sigmas when
num_steps <= 8.
-
2.3 Multi-Image Sequence Packing VRAM Explosion (
pipeline.py)- Issue:
batch_cfg=Trueduplicates joint token sequences (e.g., $16,384$ tokens for dual $1024 \times 1024$ images), triggering $O(N^2)$ flash-attention workspace allocation OOMs on 24GB GPUs. - Fix: Auto-disable
batch_cfgwhen estimated memory exceeds 70–80% of available VRAM.
- Issue:
3. CUDA Kernels & Hardware Compatibility
-
3.1 SDPA Loop Overhead on Non-FlashAttention Hardware (
_attn_backend.py)- Issue: Fallback SDPA paths dispatch Python loops over packed sequences ($4 \times 28 \times 30 = 3,360$ separate kernel launches per generation), causing massive CPU-bound latency bottlenecks on non-A100 hardware.
- Fix: Write a fused Triton kernel for packing scatter + attention or batch the sequence slice calls.
-
3.2 Silent FA4 Fallback / Crashes on Ampere GPUs (
_attn_backend.py)- Issue: Window size normalization converts
(-1, -1)to(None, None)for FlashAttention-4 without checking CUDA capability, causing silent execution fallbacks or opaque CUTE errors on SM80/86 (A100/RTX 3090). - Fix: Add an explicit architecture capability check (
torch.cuda.get_device_capability() >= (9, 0)).
- Issue: Window size normalization converts
4. Generation & Multimodal Editing Quality Failures
-
4.1 Content Refusal Collision with White Generations (
pipeline.py)- Issue:
make_refusal_image()outputs a plain white RGB image, making policy blocks indistinguishable from valid generations or NaN latent overflows. - Fix: Add a subtle visual indicator pattern or watermark to refusal returns.
- Issue:
-
4.2 Multimodal Aspect Ratio Mismatch in Reference Editing (
pipeline.py)- Issue: Resizing reference images directly to output dimensions squashes non-square ratios (e.g., $1920 \times 1080 \to 1024 \times 1024$), creating a spatial aspect ratio mismatch between VAE latents and text conditioning.
- Fix: Reflection-pad reference images to the target aspect ratio prior to encoding.
5. Codebase & Pipeline Integration Guards
-
5.1 Silent Checkpoint Key Dropping (
mage_flow.py,pipeline.py)- Issue:
load_state_dict(strict=False, assign=True)suppresses missing state dict keys, allowing execution with uninitialized layers when architecture parameters mismatch. - Fix: Add strict verification for required model keys upon initialization.
- Issue:
-
5.2 Unchecked Zero-Padding on Parameter Shape Mismatches (
utils.py)- Issue:
optionally_expand_state_dictsilently zero-pads mismatched parameter shapes into target tensors, leaving critical layers (e.g.,img_in.weight) partially filled with zeros. - Fix: Raise explicit errors on shape mismatches for critical projection parameters.
- Issue:
-
5.3 Unhandled Mode Restoration Exceptions (
mage_text.py)- Issue: Silently caught exceptions in
_full_output_modecan leave the model stuck in the wrong mode, causing content filter evaluations to fail. - Fix: Explicitly log exceptions and re-throw or fallback safely.
- Issue: Silently caught exceptions in
-
5.4 Non-Deterministic VAE Posterior Sampling (
mage_vae.py)- Issue:
encode()uses globaltorch.randn_likeinstead of accepting an explicittorch.Generator, causing seed non-reproducibility across runs. - Fix: Pass an optional local generator parameter to
encode().
- Issue:
-
5.5 Missing VAE Latent vs. Transformer Channel Assertion (
pipeline.py)- Issue: No runtime check confirms
vae.latent_channels == transformer.in_channels, risking silent dimensional slicing on configuration errors. - Fix: Add an explicit channel equality check in
compute_vae_encodings.
- Issue: No runtime check confirms
Conclusion
I have tested local refactors for these issues and would be glad to submit PRs for any specific subsystems if the maintainers agree with the proposed fixes!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read mage_vae.py, pipeline.py, _attn_backend.py, mage_flow.py, utils.py, and mage_text.py, starting with the listed reproduction conditions and loading, padding, and sampling paths. Narrow the report to one subsystem before changing behavior, then verify each proposed guard or failure mode; done means the documented edge cases no longer fail silently or crash opaquely.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, machine-learning, performance
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100