ai-toolkit icon indicating copy to clipboard operation
ai-toolkit copied to clipboard

FLUX.2 Klein 9B qfloat8 block quantization repeatedly triggers CUDA unknown/GPU loss during CPU→GPU block transfers on RTX 5090 + WSL2

Open kutaelee opened this issue 4 months ago • 1 comments

Summary

I observed two separate system-level GPU-loss incidents while AI Toolkit was loading and qfloat8-quantizing the FLUX.2 Klein 9B transformer on Windows 11 + WSL2 with an RTX 5090.

Both incidents failed at the same AI Toolkit statement in toolkit/util/quantize.py:

block.to(
    base_model.device_torch,
    dtype=base_model.torch_dtype,
    non_blocking=True,
)

The exceptions were identical:

torch.AcceleratorError: CUDA error: unknown error

The first run stopped after 25/32 transformer blocks and the second after 23/32. Both Windows System logs produced the same set of 28 unique (Event ID, payload) fingerprints, including repeated GpuRcReset TDR, UCodeReset TDR, and PCIE REORDER/P2PREQ Uncorrectable SRAM/ECC messages. The display was lost and the GPU required a full Windows reboot.

This report does not claim that AI Toolkit is the hardware/driver root cause of the PCIe SRAM/ECC reports. It reports a reproducible user-space trigger locus: repeated block-wise BF16 H2D transfer, Quanto qfloat8 conversion/freeze on GPU, and D2H return without a per-block synchronization/cache-release boundary.

Post-incident recurrence after reboot

After the second qfloat8 incident, the machine subsequently suffered two consecutive boot-session crashes even though no AI Toolkit job and no model inference/training job was running.

Both boots recorded the same EFI-supplied kernel bugcheck in Windows Event 41:

BugcheckCode: 127 (0x7F UNEXPECTED_KERNEL_MODE_TRAP)
BugcheckParameter1: 0x8 (DOUBLE_FAULT)
BugcheckInfoFromEFI: true
WHEABootErrorCount: 0

There were no new nvlddmkm Event 14/153 payloads and no new WHEA records during these two boot sessions. There was also no crash dump, which is consistent with the system failing too early or too deeply to write one.

The only GPU-facing activity found after boot was device discovery/telemetry:

  • GPUQ started its telemetry service; no managed GPU job was admitted.
  • Ollama started and enumerated the RTX 5090 through CUDA; its log contains no /api/embed, model runner, or weight-load request.
  • The Local Knowledge Portal performed HTTP health checks only; no embedding model was loaded.

The machine then became unusable and required forced restarts at approximately 04:07 and 04:12 local time. This makes a specific knowledge-base model or active training workload unlikely. The current interpretation is that the original qfloat8 incident left a low-level GPU/GSP/WDDM/WSL GPU-PV state that was not cleared by warm/forced reboot, and a later CUDA/NVML device probe exposed or amplified it. This strengthens the distinction between:

  1. an AI Toolkit user-space trigger path worth removing, and
  2. an NVIDIA/Windows/WSL/hardware fault-recovery failure that user-space code should not be able to escalate into repeated kernel double faults.

GPUQ, Local Knowledge startup, and Ollama autostart are now disabled. I will not run the original GPU path again on this machine without a cold power cycle and a vendor-directed tracing procedure.

Environment

  • OS: Windows 11 + WSL2 Ubuntu
  • GPU: NVIDIA GeForce RTX 5090
  • NVIDIA driver: 610.62
  • AI Toolkit base commit: e00f3791e221eb65d87351224a7788906b361b89
  • PyTorch: 2.9.1+cu128
  • Optimum Quanto: 0.2.4
  • Model: FLUX.2 Klein 9B base
  • Transformer dtype before quantization: BF16
  • Transformer qtype: qfloat8
  • Transformer structure: 8 double_blocks + 24 single_blocks = 32 blocks

Repeated failure signature

Incident 1:

 - quantizing 32 transformer blocks
78%|...| 25/32
File .../toolkit/util/quantize.py, line 414, in quantize_model
    block.to(base_model.device_torch, dtype=base_model.torch_dtype, non_blocking=True)
torch.AcceleratorError: CUDA error: unknown error

Incident 2:

 - quantizing 32 transformer blocks
72%|...| 23/32
File .../toolkit/util/quantize.py, line 414, in quantize_model
    block.to(base_model.device_torch, dtype=base_model.torch_dtype, non_blocking=True)
torch.AcceleratorError: CUDA error: unknown error

The block enumeration order is double_blocks[0..7] followed by single_blocks[0..23]. If tqdm's completed count is used, the next attempted transfers were probably single_blocks[17] and single_blocks[15]. Because CUDA errors may be reported asynchronously, I am not claiming that those exact H2D copies were necessarily the original failing operations.

The model header shows approximately:

  • each double block: 0.812501 GiB BF16
  • each single block: 0.406250 GiB BF16
  • all 32 blocks: 16.250019 GiB BF16

The incidents were not near a simple thermal, board-power, or VRAM-capacity limit. The second incident peaked at about 6.2 GiB reported VRAM, 93.5 W, and 44°C, with no CUDA OOM.

Code-path analysis

The current block loop performs:

CPU BF16 block
  -> non-blocking H2D to CUDA
  -> optimum.quanto.quantize(qfloat8)
  -> optimum.quanto.freeze()
  -> blocking D2H to CPU
  -> next block

There is no explicit per-block torch.cuda.synchronize() and no per-block flush()/torch.cuda.empty_cache(). The later flush() in the FLUX.2 loader only runs if quantize_model() returns successfully, so it is skipped when this exception unwinds the call.

Optimum Quanto replaces supported modules during quantize() and materializes/replaces their floating weights during freeze(). Therefore the qfloat8 work and allocations happen on the device where each block currently resides.

The transformer state dict is loaded on CPU and assigned using load_state_dict(..., assign=True). Keeping the state-dict mapping alive retains additional references to the pre-quantization BF16 storages after Quanto replaces module parameters, delaying their reclamation.

Related upstream reports

  • #927 reproduces Krea2 block-wise quantization OOM around block 25/28 and reports success after per-block synchronization and flush on an RTX 3090 24GB.
  • #930 fixes Quanto qfloat8 tensor handling in the offload/memory-manager path, which shows that qfloat8 memory lifecycle support is still evolving.
  • #617 reports hard GPU loss around model/training↔inference transitions; an RTX 5090 commenter specifically suspected repeated RAM↔VRAM model juggling.
  • #799 and #836 are Klein 9B/Blackwell/WSL-adjacent but fail in different phases and with different signatures.

Proposed fix

Please consider adding a supported quantize_device: cpu|cuda|auto option, or an equivalent quantize_on_cpu option, to quantize_model().

For the CPU path:

for block_name, block in blocks:
    block.to(cpu, dtype=base_model.torch_dtype)
    quantize(block, weights=quantization_type, raise_on_error=True)
    freeze(block)

This removes all repeated per-block H2D/D2H transitions and performs Quanto qfloat8 conversion on CPU. The already-quantized transformer can then be moved to the training device once.

For the GPU fallback, please use explicit phase boundaries:

block.to(device, dtype=dtype, non_blocking=False)
torch.cuda.synchronize(device)
quantize(block, weights=qtype, raise_on_error=True)
freeze(block)
torch.cuda.synchronize(device)
block.to(cpu, non_blocking=False)
torch.cuda.synchronize(device)
flush(garbage_collect=False)

Additional requested changes:

  1. Log block name, ordinal, and phase (H2D, quantize, freeze, D2H).
  2. Propagate the first submodule quantization failure; do not continue with a partially quantized model.
  3. Delete the state-dict mapping after load_state_dict(..., assign=True) so replaced BF16 storage can be reclaimed.
  4. Add exception-path cleanup/logging. If the CUDA status is GPU-lost/unknown, avoid repeated CUDA/NVML probing in the same process.

Local code-level validation of the CPU path

I implemented the CPU option locally and ran a CPU-only tiny-model test with CUDA_VISIBLE_DEVICES='' plus a guard that raises on any CUDA Module.to call. Two transformer blocks plus extra layers qfloat8-quantized successfully with:

cuda Module.to calls: 0
all parameters on CPU after quantization: true

This demonstrates that the CPU option removes the exact repeated GPU block-transfer/quantization path in code. It does not yet prove that the later single full-model H2D, training, or repeated load/unload cycles are safe on the affected system.

Safety / reproduction note

Because both occurrences caused display loss and required a full Windows reboot, I have not intentionally reproduced the original GPU path again. I can provide sanitized AI Toolkit logs, Windows event payload summaries, exact source hashes, and vendor traces. I would only run an A/B reproduction after NVIDIA/Microsoft or the maintainer specifies the required tracing and a safe procedure.

Suggested controlled A/B:

  • A: current GPU block quantization
  • B: blocking GPU transfers + synchronize + per-block flush
  • C: CPU block quantization + one final qfloat8 model transfer

Expected behavior

Quantization should either complete with bounded memory/residency or fail as a normal process error. A model-loading path should not repeatedly drive a GPU into a lost/TDR state, and an asynchronous failure should be attributed to an explicit block and phase.

kutaelee avatar Aug 01 '26 19:08 kutaelee

This issue has been automatically closed due to inactivity. It has had no activity for 3 months.

If this issue is still relevant, please feel free to reopen it with updated information or context. We apologize for any inconvenience.

github-actions[bot] avatar Nov 02 '26 00:11 github-actions[bot]