ComfyUI icon indicating copy to clipboard operation
ComfyUI copied to clipboard

TextGenerate with mtp=auto: CUDA illegal memory access during decode (Flux 3 prompt upsampler, RTX 3090 Ti) — crash also kills the prompt_worker thread

Open ToonBoy65 opened this issue 1 month ago • 7 comments

Custom Node Testing

Expected Behavior

TextGenerate running the Flux 3 prompt upsampler (qwen3.5_9b_flux3_prompt_upsampler_int8_convrot.safetensors) completes token generation — exactly as it does when the node's mtp input is set to off.

Actual Behavior

With mtp: auto (the node default), generation crashes reliably a few hundred tokens in (~8–12 s at ~84 it/s) with:

torch.AcceleratorError: CUDA error: an illegal memory access was encountered

The error is asynchronous, so the reported stack is the post-node cleanup (execution.py:554 → reset_cast_buffers → torch.cuda.synchronize), not the faulting kernel.

Secondary bug (makes it worse): right after the node fails, the queue worker dies too — main.py:422 (prompt_worker → comfy.model_management.soft_empty_cache() → torch.cuda.synchronize()) re-raises the same AcceleratorError outside any try, killing Thread (prompt_worker). The server then looks healthy (HTTP and the frontend still respond) but every new prompt stays pending forever until the process is restarted. Guarding that cleanup call would at least keep the failure visible and recoverable.

Reproduced 6/6 times (4× inside the full Flux 3 Klein t2i template, 2× with the minimal workflow below). A/B matrix — the only variable that changes the outcome is mtp:

mtp | --use-sage-attention | custom nodes | result -- | -- | -- | -- auto | yes | enabled | 💥 illegal memory access (×4) auto | yes | all disabled | 💥 illegal memory access auto | no | all disabled | 💥 illegal memory access off | yes | enabled | ✅ completes (full Klein t2i template runs end-to-end in 19 s)

Steps to Reproduce

  1. Load the attached minimal API workflow (5 core nodes): CLIPLoader (qwen3.5_9b_flux3_prompt_upsampler_int8_convrot.safetensors, type flux3) → TextGenerate → PreviewAny. The TextGenerate settings are the stock Flux 3 Klein template values: max_length 3584, sampling on (temperature 1.0, top_k 1, top_p 0.95, min_p 0.0, repetition_penalty 1.05, presence_penalty 1.5), thinking false, mtp auto, with the template's upsampler system prompt.
  2. Queue it. On this machine it crashes ~9 s into "Generating tokens", every time (seed does not matter).
  3. Set mtp: off on the same workflow → completes normally.

Workflow JSON: textgenerate_mtp_crash_repro.json (attached).

textgenerate_mtp_crash_repro.json

Debug Logs

** ComfyUI startup time: 2026-10-17 11:54:49
** Platform: Windows
** Python version: 3.13.12 (tags/v3.13.12:1cbe481, Feb  3 2026, 18:22:25) [MSC v.1944 64 bit (AMD64)]
** Python executable: C:\AI\ComfyUI_windows_portable\python_embeded\python.exe
Total VRAM 24564 MB, total RAM 32605 MB
pytorch version: 2.13.0+cu130
Set vram state to: NORMAL_VRAM
Device: cuda:0 NVIDIA GeForce RTX 3090 Ti : cudaMallocAsync
Using sage attention
aimdo: comfy-aimdo inited for GPU: NVIDIA GeForce RTX 3090 Ti (VRAM: 24563 MB)
DynamicVRAM support detected and enabled
ComfyUI version: 0.41.0
comfy-aimdo version: 0.5.6
comfy-kitchen version: 0.2.38
comfyui-frontend-package version: 1.55.17
Skipping loading of custom nodes
Starting server

[2026-10-17 11:55:10.673] got prompt
[2026-10-17 11:55:10.717] Found quantization metadata version 1
[2026-10-17 11:55:10.718] Using MixedPrecisionOps for text encoder
[2026-10-17 11:55:11.266] Model storage policy: fast_disk=True paths=['...\\models\\text_encoders\\qwen3.5_9b_flux3_prompt_upsampler_int8_convrot.safetensors']
[2026-10-17 11:55:11.309] CLIP/text encoder model load device: cuda:0, offload device: cpu, current: cpu, dtype: torch.float16
[2026-10-17 11:55:11.331] Requested to load Qwen35TEModel_
[2026-10-17 11:55:11.484] Model Qwen35TEModel_ prepared for dynamic VRAM loading. 9295MB Staged. 0 patches attached. Force pre-loaded 222 weights: 822 KB.
[2026-10-17 11:55:19.426] Comfy model compiler graph breaks: 2, rogues: 0
[2026-10-17 11:55:19.636] !!! Exception during processing !!! CUDA error: an illegal memory access was encountered
Search for `cudaErrorIllegalAddress' in https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__TYPES.html for more information.
CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1

[2026-10-17 11:55:19.638] Traceback (most recent call last):
  File "C:\AI\ComfyUI_windows_portable\ComfyUI\execution.py", line 554, in execute
    comfy.model_management.reset_cast_buffers()
  File "C:\AI\ComfyUI_windows_portable\ComfyUI\comfy\model_management.py", line 1461, in reset_cast_buffers
    synchronize()
  File "C:\AI\ComfyUI_windows_portable\ComfyUI\comfy\model_management.py", line 2129, in synchronize
    torch.cuda.synchronize()
  File "C:\AI\ComfyUI_windows_portable\python_embeded\Lib\site-packages\torch\cuda\__init__.py", line 1219, in synchronize
    return torch._C._cuda_synchronize()
torch.AcceleratorError: CUDA error: an illegal memory access was encountered

[2026-10-17 11:55:19.639] Prompt executed in 8.96 seconds
[2026-10-17 11:55:19.815] Exception in thread Thread-11 (prompt_worker):
Traceback (most recent call last):
  File "threading.py", line 1044, in _bootstrap_inner
  File "threading.py", line 995, in run
  File "C:\AI\ComfyUI_windows_portable\ComfyUI\main.py", line 422, in prompt_worker
    comfy.model_management.soft_empty_cache()
  File "C:\AI\ComfyUI_windows_portable\ComfyUI\comfy\model_management.py", line 2145, in soft_empty_cache
    torch.cuda.synchronize()
  File "C:\AI\ComfyUI_windows_portable\python_embeded\Lib\site-packages\torch\cuda\__init__.py", line 1219, in synchronize
    return torch._C._cuda_synchronize()
torch.AcceleratorError: CUDA error: an illegal memory access was encountered

Other

  • Hardware: RTX 3090 Ti 24 GB (sm_86), driver 610.62, Windows 10, 32 GB RAM, portable build (embedded Python).
  • ComfyUI 0.41.0, torch 2.13.0+cu130, comfy-kitchen 0.2.38, comfy-aimdo 0.5.6, DynamicVRAM enabled. Normal launch args: --windows-standalone-build --enable-manager --use-sage-attention (crash also reproduces without sage, see matrix).
  • Only the 9.3 GB upsampler is loaded in the minimal repro — ~24 GB VRAM free at submit time, so this does not look like simple memory pressure.
  • Other heavy workflows (MiniMax H3 video a/v, Flux) run fine on this box; TextGenerate with mtp: off also runs fine, so it looks specific to the MTP speculative-decode path (qwen35.py _generate_mtp, CUDA-graphed draft loop) on this config.
  • I saw #17007 ("Performance improvements for qwen 3.x (Part 2)", merged after 0.41.0) rewrites this path and adds decode tests — happy to retest on a nightly or the next release if that helps.

ToonBoy65 avatar Oct 17 '26 09:10 ToonBoy65

I could not reproduce this on current master (ce1e922d, which already includes #17007), so it may be specific to your setup or fixed by that PR.

Setup: RTX 5090 (sm_120), torch 2.x cu130, comfy-kitchen 0.2.38. I ran your minimal workflow (CLIPLoader flux3 -> TextGenerate -> PreviewAny) with your exact settings and system prompt (max_length 3584, sampling on, temperature 1.0, top_k 1, top_p 0.95, repetition_penalty 1.05, presence_penalty 1.5, thinking off).

Difference: I only had the qwen3.5_9b_flux3_prompt_upsampler_w4a8.safetensors variant, not your int8_convrot file, so the quantized kernels are different.

Results:

  • mtp=auto with CUDA_LAUNCH_BLOCKING=1: completed, the model stopped by itself at about 369 tokens, no CUDA error. Peak allocation was 7.2 GB.
  • mtp=auto without CUDA_LAUNCH_BLOCKING: completed, no error.
  • mtp=off: completed.

If you can retest on a build that includes #17007 and it still crashes, a run with CUDA_LAUNCH_BLOCKING=1 would show the real faulting op instead of the cleanup stack. Also worth noting whether the crash needs the int8_convrot file specifically (sm_86 int8 path).

The second part (prompt_worker dying in soft_empty_cache after an async CUDA error) looks like the same problem as #16312 / #16317.

vangel76 avatar Oct 17 '26 18:10 vangel76

Follow-up: I also tested the exact file from the report, qwen3.5_9b_flux3_prompt_upsampler_int8_convrot.safetensors, with current master on a Blackwell RTX 5090 (sm_120), using your workflow (TextGenerate, flux3, max_length 3584, thinking off). With mtp=auto it finished 4 out of 4 runs (also with CUDA_LAUNCH_BLOCKING=1), about 393 tokens each at 34 to 57 tokens/s, peak VRAM about 10.3 GB. mtp=off also finished (427 tokens, about 36 tokens/s). No illegal memory access. So the crash is likely fixed by the MTP rewrite in #17007, or it is specific to Ampere (sm_86) or the older 0.41.0 build. Could you retest on the latest master?

vangel76 avatar Oct 17 '26 18:10 vangel76

I'm seeing the same crash on an Ampere card. My build already includes #17007, so that PR doesn't seem to fix it on sm_86.

Setup

  • RTX 3090 24 GB (sm_86), driver 610.74, Windows 10, portable build
  • ComfyUI 0.41.0 at master c9ea36b9d (includes 4d7b59459, #17007)
  • torch 2.14.0+cu130, comfy-kitchen 0.2.38, comfy-aimdo 0.5.6, DynamicVRAM enabled, cudaMallocAsync

Model: a different checkpoint from the original report. It's the MOSS-Image text encoder qwen3.5_9b_w4a8_gptq.safetensors running through TextGenerate (MOSSImageTEModel_, 7002 MB staged). So it happens with w4a8, not just int8_convrot. @vangel76's w4a8 test passed on an RTX 5090, which points to this being Ampere-specific.

What I tested (same workflow, same seed):

Launch flags Result
default 💥 illegal memory access
--disable-all-custom-nodes 💥 illegal memory access
--disable-cuda-graphs 💥 illegal memory access
--disable-comfy-compiler ✅ completes, full workflow runs end to end
default + CUDA_LAUNCH_BLOCKING=1 ✅ completes, compiler still active (graph breaks: 2, rogues: 0), ~69 tok/s

The crash happens when the token count reaches 205, which is where generation ends normally on the run that works. So for me the crash seems to be at or right after the end of decoding, not in the middle. The stack is the same post-node cleanup trace (execution.py:554 → reset_cast_buffers → torch.cuda.synchronize), and prompt_worker then dies in soft_empty_cache (#16312).

Because --disable-cuda-graphs alone still crashes and --disable-comfy-compiler doesn't, the fault looks like it comes from the compiler's malloc-graph side (model_prefetch.py, aimdo malloc_graph), not CUDA graph replay.

Update: with CUDA_LAUNCH_BLOCKING=1 and otherwise default flags, it no longer crashes. The whole workflow completes with the compiler active, so I couldn't capture the faulting kernel this way. Since making every launch synchronous hides the crash, it looks like a race or a missing stream sync, for example a buffer being reused or freed while a kernel on another stream is still reading it, rather than a deterministic out-of-bounds index.

rainlizard avatar Oct 18 '26 00:10 rainlizard

Also happening for me while generating with MOSS. As it is generating, if I stop the generation as it's partially through the KSampler, ComfyUI instantly crashes with "torch.AcceleratorError: CUDA error: an illegal memory access was encountered"

Gyramuur avatar Oct 18 '26 03:10 Gyramuur

Same. RTX 3090 Ti, driver 617.42, Python version: 3.13.12, pytorch version: 2.13.0+cu130, cudaErrorIllegalAddress after “prepared for dynamic VRAM loading”, fixed by --disable-dynamic-vram or CUDA_LAUNCH_BLOCKING=1, and the models involved (MOSSImage with int8_convrot quantization, Qwen3.5 9B text encoder, Flux2 VAE)

semyonanonov avatar Oct 18 '26 09:10 semyonanonov

I'm seeing a related failure in FLUX.3's Qwen3.5 9B Prompt Upsampler / TextGenerate, on AMD RDNA2 (gfx1030) rather than NVIDIA. There are two distinct findings: reproducible BF16/FP32 cache mismatches in the unmodified code, and an asynchronous GPU/driver failure after experimental local dtype harmonization. I have not confirmed that these have the same cause as the MTP crash in this issue.

Environment

  • Windows 11; Radeon RX 6800 16 GB (gfx1030)
  • ComfyUI v0.45.0, using the patientx native Windows ROCm distribution
  • PyTorch 2.14.0+rocm10.1.0; ROCm SDK 10.1; Triton Windows 3.7.0.post26
  • comfy-kitchen 0.2.41; comfy-aimdo 0.6.0 / DynamicVRAM
  • Qwen3.5 9B FLUX.3 Prompt Upsampler W4A8 (qwen3.5_9b_flux3_prompt_upsampler_w4a8.safetensors)
  • The ComfyUI-INT8-Fast-ROCM extension was activated in the isolated test instance to handle an independent gfx1030 hipBLASLt INT8 issue.

Observations (isolated TextGenerate tests)

  1. Unmodified v0.45.0: GatedAttention reaches basic attention with Q BF16, K FP32, V FP32, then fails in einsum (expected scalar type BFloat16 but found Float). Before KV-cache insertion Q/K were both BF16; the cache is allocated in FP32 on this device.
  2. With a temporary Q-to-K dtype alignment only, that failure is bypassed, but GatedDeltaNet then fails in the PyTorch fallback comfy_kitchen/gated_delta.py:_conv_deferred_torch: qkv_buf.index_copy_ has FP32 destination / BF16 source. Aligning proj to the FP32 deferred-buffer dtype before CastBiasWeightContext bypasses this.
  3. With those two temporary adjustments, decoding reaches 1 token but fails at FixedKVCache.write(): self.key.index_copy_(2, position, xk), again FP32 destination / BF16 source.
  4. With all three temporary, conditional harmonizations (Q against K, convolutional projection against FP32 deferred buffers, and K/V against the FP32 KV cache), TextGenerate advanced to 2 tokens and then failed with CUDA error: unspecified launch failure reported asynchronously at torch.cuda.mem_get_info. Windows also displayed an AMD driver timeout (TDR). The precise failing GPU kernel is unknown; the memory-management call may only be where the earlier asynchronous error surfaced. The MIOpen gfx1030 grouped-conv missing-library warning also appears, but there is no evidence it caused this failure.

All experiments were conducted on a separate ComfyUI instance and all modified Qwen3.5 files were restored afterward. No dependency, ROCm DLL, or driver changes were made.

Control: FLUX.3 Klein Base INT8, with the upsampler disabled and an ordinary text prompt, completed 50/50 sampling steps at 1024x1024 (CFG 3.5) and saved an image using the gfx1030 INT8 extension. So the image model's successful run does not establish that Qwen3.5 decoding is working.

I see that mtp=off, --disable-comfy-compiler, and --disable-dynamic-vram have been useful in related NVIDIA reports. The follow-up below confirms that mtp=off does not avoid the initial BF16/FP32 failure here; the other flags have not been tested on this RDNA2 environment. Has anyone investigated how Qwen3.5's W4A8 BF16 projection outputs interact with FP32 KV/deferred buffers under ROCm, or seen an MTP/compiler-related kernel failure on gfx1030?

I have the exact isolated workflow, original and experimental test logs, and observed tensor dtypes available if helpful.

2026-11-11 follow-up — MTP disabled, unmodified source

We tested an isolated core-only TextGenerate path (CLIPLoader → TextGenerateV2 → PreviewAny; no KSampler) with mtp: "off". Importantly, the saved minimal reproduction workflow already had MTP set to off; this was not a controlled A/B toggle from auto to off. The Qwen3.5 source was restored and its SHA-256 matched the original before testing.

Result: The test failed after 9.33 seconds, before emitting any prompt, at qwen35.py:539 → optimized_attention → einsum, reporting expected scalar type BFloat16 but found Float. The original BF16 query / FP32 cached K/V mismatch therefore still reproduces with mtp=off on gfx1030. This distinguishes our ROCm dtype issue from the NVIDIA MTP-specific failure described in the original report. No experimental Qwen fixes were applied in this follow-up and no new driver crash was observed.

Environment remained ROCm 10.1, ComfyUI 0.45.0, PyTorch 2.14.0+rocm10.1.0, gfx1030, DynamicVRAM enabled, with the gfx1030 INT8 patch active in the isolated instance. MIOpen gfx1030 warnings appeared again but the observed exception was the dtype mismatch. The test server was stopped, the stable server on port 8188 was left running, and all packages, source files, and launchers remain unchanged.

The full log is preserved locally as flux3-klein-qwen35-mtp-off-test-20261111.log and is available if maintainers would like it. --disable-comfy-compiler and --disable-dynamic-vram remain untested in this setup.

skullmonkey666 avatar Nov 11 '26 19:11 skullmonkey666

Same issue here. MOSS running on an RTX 5090 + Gemma4 QAT with mtp on an RTX 3090. When the run is stopped during MOSS sampling, it crashes with "CUDA error: an illegal memory access was encountered".

gammahurricane avatar Nov 29 '26 02:11 gammahurricane