Skip to content

[MEM] MoE/OOM: shared-input Hessian adoption keeps 3 C^2 per pair (+0.96 GiB/20% peak on Qwen1.5-MoE), quantized MoE reload materializes bf16 experts on host (12.2 vs 4.2 GiB RSS), OOM CPU fallback missing in QQQ/EXL3 and after GPTQ inverse #4400

Closed

Description

@Qubitium

Audit of main @ beb6db8 (first measured at 27c6b7b) on an R9700 (ROCm 10.1, 32 GB), with Llama-3.1-8B-Instruct, OLMoE-1B-7B, Qwen1.5-MoE-A2.7B and Phi-tiny-MoE. Items 1 and 2 are generic (not HIP-specific).

1. Shared-input Hessian adoption keeps three C² per leader/follower pair (−0.96 GiB / −20% peak on Qwen1.5-MoE, output bitwise unchanged) (medium)

adopt_hessian_from (quantization/gptq.py:1394-1441, called from looper/gptq_processor.py:516) makes the leader materialize early, into a new result_accum (materialize_global_hessian, :1335-1391). The leader's fp32 partial is then parked in _deferred_hessian_sources and released only at the leader's own quantize(), and the follower gets a private copy. All pairs adopt at the end of capture, so every pair holds leader H + leader partial + follower copy.

Qwen1.5-MoE-A2.7B (2-layer copy), GPTQ 4-bit, 128 samples; at the MoE subset start: live H 1.906 GiB + deferred partials 0.953 GiB
                                   peak alloc   peak reserved   ckpt sha
baseline                           4.668 GiB    5.514 GiB       c32c5fae7e07aca6
release deferred sources after adoption (leader + follower)
                                   3.711 GiB    4.627 GiB       c32c5fae7e07aca6   (identical)

Fix:

  • Release the leader's reduction sources once adoption is done.
  • For a single same-device fp32 partial, adopt the partial as H (scale in place) instead of allocating zeros and adding.
  • For another ~C² per pair, give the follower H copy-on-write (clone at its quantize()).

This scales with expert count × hidden² (Qwen3-MoE, DeepSeek, any :in=-tagged MoE tree); on dense Llama-8B it's ~128 MiB. Untagged gate/up (OLMoE/Phi-MoE, #4041) currently build duplicate Hessians instead; tagging them today would cost more, not less, until this is fixed.

2. Reloading a quantized MoE whose experts defuser splits at runtime (OLMoE, Phi-tiny-MoE) materializes full bf16 per-expert weights on the host: peak RSS 3.3× the checkpoint (medium)

The quantized-load path builds a real CPU shell (models/loader.py:2615, from_config under suspend_hf_weight_init), then calls _convert_model_with_defuser(..., cleanup_original=True) (:2657). defuser's _unfuse_single_projection (moe_experts_interface.py:448-460, same in 0.0.31) runs .cpu().transpose().contiguous() on the uninitialized fused 3-D params, writing bf16 per-expert copies that QuantLinear replaces right afterwards. defuser skips params that are already on meta.

OLMoE-1B-7B RTN 4-bit ckpt (3.656 GiB): reload peak RssAnon 12.17 GiB (VmHWM 16.5)        GPU 3.658 GiB
  fused expert params moved to meta before defuser, defused linears to_empty(cpu) afterwards:
                                         peak RssAnon  4.19 GiB (VmHWM 8.6)               GPU 3.658 GiB, logits identical
Phi-tiny-MoE (ckpt 2.2 GiB): RssAnon 6.08 GiB (VmHWM 9.0); Qwen1.5-MoE (block replaced at construction): 1.51 GiB for 1.71 (clean)

Fix: on the quantized-load path, put fused expert params on meta before defuser (or build the shell on meta), then to_empty the defused linears.

3. #4359 (for #4350) only covers gptq.py: QQQ has no OOM→CPU fallback, and EXL3 block_ldl misses solver allocation failures (low-medium)

  • QQQ (quantization/qqq.py:817-850): any allocation failure in the Cholesky → inverse → Cholesky sequence re-raises unless the sample-count fallback applies. Under a 1.3 GiB per-process cap on Llama-3.2-1B: torch.OutOfMemoryError ... Tried to allocate 256.00 MiB at qqq.py:826 (cholesky_inverse), and the run aborts. GPTQ under the same cap falls back to CPU and continues.
  • EXL3 (exllamav3/.../exl3_lib/quantize.py:444-447): its handler only matches OutOfMemoryError / "CUDA|HIP out of memory". A real RuntimeError: hipsolver error: HIPSOLVER_STATUS_ALLOC_FAILED, when calling hipsolverDnCreate(handle) (injected with an LD_PRELOAD hipMalloc-failure shim) propagates, even though gptq.py's _is_memory_allocation_failure classifies it correctly. On a warm handle, potrf doesn't allocate, so the exposure is the first solver call per worker thread.

Fix: reuse _is_memory_allocation_failure and the CPU retry in qqq.py and block_ldl.

4. GPTQ's CPU fallback stops at the Hessian inverse: the W-sized allocations and block update that follow still abort the run (low-medium)

After the fallback computes the inverse on CPU, gptq.py:2526-2527 (Losses = torch.zeros_like(W), Q = torch.zeros_like(W)) and the block update (W[:, i2:] -= Err1.matmul(Hinv[i1:i2, i2:])) run unprotected:

Llama-3.2-1B gptq4, 1.15 GiB per-process cap: 8x "CUDA OOM during act-group Hessian permutation ... falling back to CPU",
1x "... during Hessian inverse ... falling back to CPU", then
  gptq.py:2527 _quantize_impl / W[:, i2:] -= Err1.matmul(Hinv[i1:i2, i2:])
  torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 32.00 MiB  -> quantize() aborts

Fix: wrap the post-inverse allocations and block update in the same handler (retry the module on CPU), or allocate Q/Losses before the inverse. Otherwise the "finish on CPU instead of aborting" intent doesn't hold on shared GPUs.

Measurements (Llama-3.1-8B-Instruct, 128 C4 samples, bf16, defaults, ROCm)

method peak alloc / reserved per-layer reserved PPL (40 win) reload GPU vs ckpt
GPTQ 4-bit 4.17 / 8.10 GiB flat 9.69 5.339 = 5.339 GiB
AWQ 4-bit 1.95 / 3.63 flat (#4342 holds) 9.79 5.335 = 5.335
QQQ 4.58 / 6.21 flat 9.96 10.565 vs 5.314 (reduce_buffer, #4387)

The GPTQ peak is dominated by four down_proj C² fp32 buffers (784 MiB each): H after the GAR permutation (gptq.py:2456), L (:1916) and two temporaries in inverse_factor (:1936-1937). QQQ has the same pattern at qqq.py:825-826. Everything fits on 32 GB. Leftover EXL3 allocation after del is #4391.

Checked, clean

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed by an AI agent: items 2-4 (MoE reload host materialization, QQQ/EXL3 OOM fallback, GPTQ post-inverse fallback). Item 1 is already merged via #4430.

  2. added a commit that references this issue on Nov 16, 2026
  3. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Re-checked at 489aed6 on an RTX 4090 with Qwen1.5-MoE-A2.7B (2-layer truncation, GPTQ 4-bit).

    • The item-1 saving from 260af2a is real. Peak allocated is 4.350 GiB at def6e61 and 3.452 GiB at HEAD (−0.90 GiB). Adoption activates on 63 modules per layer, all verified, and the quantized weights are bit-identical with dedup on or off.
    • Dedup still uses slightly more peak memory than no dedup. HessianConfig(dedup_shared_inputs=True) peaks at 3.452 GiB vs 3.388 GiB with it off; with a dynamic override, 3.462 vs 3.351 GiB.
    • Not implemented yet: the copy-on-write / in-place adoption ideas suggested here. So turning dedup on does not yet lower peak memory.
    • Correctness is not affected. This comment only notes that the expected memory win from dedup hasn't happened yet.

    (AI-assisted review, round 13 at 489aed6)

  4. added a commit that references this issue on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions