Skip to content

cuda: gate Flash Attention zero-fill workaround to NVCC 13.4 - #1143

Merged
0xShug0 merged 2 commits into
0xShug0:mainfrom
askmyteapot:codex/fix-cuda-flash-partial-tile
Oct 31, 2026
Merged

0xShug0 merged 2 commits into
0xShug0:mainfrom
askmyteapot:codex/fix-cuda-flash-partial-tile

Conversation

@askmyteapot

@askmyteapot askmyteapot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

BreezeTTS 2 voice design can produce flat PCM audio when NVCC 13.4 compiles the Flash Attention partial-tile loader with --split-compile=8. The standalone reproducer produces NaNs; disabling split compilation avoids the failure.

Scope the explicit valid-row copy / direct int4 zero-store workaround to NVIDIA NVCC 13.4 using GGML_CUDA_FA_NVCC_13_4_ZERO_FILL_FIX, as requested. Other compiler versions and HIP retain the original implementation. The gate applies to NVCC 13.4 builds regardless of whether split compilation is enabled. The precise faulty generated instruction has not been identified.

The final diff changes only external/ggml/src/ggml-cuda/fattn-mma-f16.cuh; no test files or CMake changes are included.

Validation

  • Recompiled the gated variant with CUDA 13.4, --split-compile=8, fast math, and sm_86/sm_120a targets. All 216/216 standalone cases passed against an independent CPU softmax reference (absolute tolerance 0.003) on RTX PRO 5000 Blackwell and RTX 3090. Coverage: head dimensions 64, 80, 96, 112, 128, 256; sequence lengths 1, 7, 17, 43, 86, 128, 212, 300, 325; causal-window and unmasked attention.
  • A separate BreezeTTS 2 BF16 CLI linked with the gated kernels, with all Flash Attention flags enabled, generated a normal 3.44-second waveform (PCM range -4788 to 5544). The earlier unconditional workaround was also confirmed working after a full server rebuild; the gated variant was validated with isolated kernel builds and the separate CLI.
  • Preprocessor checks select the workaround for NVCC 13.4 and the original path for simulated NVCC 13.3/13.5/14.0, HIP, and CUDA Clang configurations. These are conditional-selection checks, not runtime tests of those toolchains.
  • git diff --check passes. Tests were run locally and are not included in the PR.

The broader head-dimension results and traces document the original versus unconditional workaround comparison. The paragraph benchmark and timing logs showed no substantial end-to-end slowdown for that workload (6289.060 → 6283.628 ms median, -0.09%); that benchmark predates the compiler gate and was not rerun for this revision.

AI disclosure

OpenAI Codex assisted with the investigation, code changes, local validation, benchmark analysis, and PR text. Reported results come from tests run on the local hardware.

@0xShug0

0xShug0 commented Oct 30, 2026

Copy link
Copy Markdown
Owner

@askmyteapot Thanks for the PR. Can you test whether the change has any performance impact?

@0xShug0 0xShug0 added the ggml label Oct 30, 2026
@askmyteapot

Copy link
Copy Markdown
Contributor Author

@0xShug0 I tested this with a short paragraph on the RTX PRO 5000 Blackwell (sm_120a), Windows / CUDA 13.4, using BreezeTTS 2 BF16. No substantial end-to-end slowdown was observed for this workload.

Method

  • Compared the preserved original CLI executable with the same CLI linked against the separately compiled patched dimension-64 Flash Attention kernel instantiations. The patched kernels retained --split-compile=8, fast math, and the sm_86/sm_120a targets.
  • Same paragraph, seed 1234, voice instruction Australian female, offline vdes, and default perf_mode=off. Attention stayed auto; the traces confirm backbone, depth, encoder, and decoder Flash Attention all remained enabled. This is not an eager-vs-flash comparison.
  • Seven identical requests in one loaded session per executable, discarding the first two as warmups and taking medians of the remaining five. Patched ran first, original second, sequentially. Every request reported 19.92 seconds of output audio.
  • Used session.wall_ms for total generation and breeze_tts.speech_decoder.total_ms for the decoder, excluding model load and WAV file writing.

Results

Median, five measured requests Original Patched Change
Total generation 6289.060 ms 6283.628 ms -0.09%
Speech decoder 85.002 ms 86.066 ms +1.25%
Autoregressive generation 6167.188 ms 6166.540 ms -0.01%

Total-generation ranges were 6272.903–6303.645 ms original and 6259.115–6303.221 ms patched. Decoder ranges were 83.346–88.746 ms original and 85.067–92.413 ms patched. The differences are small and the observed ranges overlap; this supports negligible end-to-end impact for this paragraph, not a universal zero-overhead claim or a formal statistical conclusion.

For completeness, an earlier shorter sentence (3.44 seconds of output, ten measured requests after two warmups) gave 1154.893 → 1167.205 ms total (+1.07%), also with overlapping ranges.

Input and traces

Paragraph:

The morning sun lit the harbour as the first ferry pulled away from the wharf. Along the waterfront, a woman stopped to order a coffee and watch the boats pass. She had a busy day ahead, but for a few quiet minutes there was nothing to do except enjoy the warm breeze and the sound of the water.

Command shape, run once for each executable with the paragraph repeated on seven lines in paragraph-input.txt:

<cli> --task vdes --family breeze_tts --model <breeze-tts-2-bf16.gguf>
  --backend cuda --device 0 --batch-text-file paragraph-input.txt
  --instruct "Australian female" --seed 1234 --metrics
  --log-file <variant>-trace.log > <variant>-metrics.txt

Measured requests 3–7, extracted directly from the timing logs (milliseconds):

request  original session.wall_ms  patched session.wall_ms  original decoder  patched decoder
3        6299.0080                 6280.5286                83.5333           85.9470
4        6289.0595                 6303.2206                88.7456           87.7821
5        6277.0926                 6259.1155                83.3461           92.4132
6        6272.9025                 6287.8254                85.0020           86.0660
7        6303.6445                 6283.6283                85.2896           85.0666

The attached archive contains both complete timing/trace logs and metrics stdout, all seven requests including warmups, input text, CSV, analysis script, and build/method notes. Local model/cache paths are omitted; timing values and timestamps are unchanged. These are application timing traces, not Nsight/GPU profiler traces.

Separately, I rebuilt the full server and confirmed the original silent-audio problem is fixed. The timing comparison above still uses the diagnostic CLI pair described here. Other models and GPU architectures have not been performance-benchmarked.

AI disclosure: Codex assisted with running the benchmarks, analyzing the recorded timings, and drafting this reply.
breeze-paragraph-benchmark.zip

@askmyteapot
askmyteapot marked this pull request as draft October 30, 2026 05:14
@askmyteapot

Copy link
Copy Markdown
Contributor Author

Just converting this to draft while I investigate if other models are impacted by this.

@askmyteapot

Copy link
Copy Markdown
Contributor Author

@0xShug0 Follow-up on the broader-impact investigation: I tested the original and patched kernels at head dimensions 64, 80, 96, 112, 128, and 256. All 216 patched cases passed against an independent CPU softmax reference.

Head dimension Original passes / cases Patched passes / cases Patched maximum absolute error
64 30/36 36/36 0.00106208
80 36/36 36/36 0.00116485
96 36/36 36/36 0.00116835
112 36/36 36/36 0.00135682
128 36/36 36/36 0.00124153
256 36/36 36/36 0.00123871

Method: Windows / CUDA 13.4, with --split-compile=8, fast math, and sm_86/sm_120a targets retained. Both RTX PRO 5000 Blackwell and RTX 3090 were tested. Each dimension covered nine sequence lengths (1, 7, 17, 43, 86, 128, 212, 300, 325), both unmasked attention and a causal window of 25, batch 1, eight equal Q/K/V heads, F32 inputs, and F32 Flash Attention precision. Every valid output was checked against a CPU double-precision softmax reference with absolute tolerance 0.003. Kernel instantiations were compiled separately and linked ahead of the existing CUDA library; this supplemental matrix does not require model weights.

The original kernel reproduced NaNs only for dimension 64 on Blackwell, at lengths 17, 43, and 86 in both mask modes. The patch fixes those six cases. The other dimensions passed both before and after, so this establishes no regression in the tested cases rather than proving those dimensions previously had the same defect.

The loader is shared by other models, so the fix applies wherever that loader is instantiated. Code inspection identified potentially exposed paths in F5-TTS, GLM-TTS, Audio8 TTS, and Demucs, but I have not reproduced failures in those complete model pipelines. Their padding, dimensions, and kernel selection can affect exposure. This matrix also does not cover every dtype, grouped-query ratio, batch/layout, or unequal Q/K/V dimension.

Attached are the full original/patched results, test source, kernel-instantiation sources, summary, and analysis script. These are supplemental standalone tests; the committed regression test remains the dimension-64 case.

Returning this PR to ready for review now that the additional dimension checks have passed. The paragraph benchmark above still shows negligible end-to-end speed impact for the tested workload.

AI disclosure: Codex assisted with running and analyzing these checks and drafting this update.
flash-head-dimension-validation.zip

@askmyteapot
askmyteapot marked this pull request as ready for review October 30, 2026 05:24
@0xShug0

0xShug0 commented Oct 30, 2026

Copy link
Copy Markdown
Owner

@askmyteapot Thanks for the detailed investigation! I reproduced the issue with BreezeTTS2 on Linux using CUDA 13.4, while the other models I tested worked without the same corruption. Disabling split compilation also fixed it with the original kernel unchanged.

I'd prefer a narrowly scoped compiler workaround that preserves the original path, so we can remove it once NVIDIA fixes the 13.4 compiler issue. Gating it to affected builds would also avoid requiring broad regression testing on other CUDA versions and HIP, which shares this CUDA kernel source.

#if !defined(GGML_CUDA_FA_NVCC_13_4_ZERO_FILL_FIX)
    const half2 zero[4] = {{0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f}};
#endif
#if defined(GGML_CUDA_FA_NVCC_13_4_ZERO_FILL_FIX)
    // Workaround
#else
    // Original implementation.
    ggml_cuda_memcpy_1<16>(tile_KV + i*stride_tile + k*4,
                        !oob_check || i < i_sup ? KV + i*stride_KV + k*h2_per_chunk : zero);
#endif

@askmyteapot askmyteapot changed the title cuda: fix Flash Attention partial-tile zero filling cuda: gate Flash Attention zero-fill workaround to NVCC 13.4 Oct 31, 2026
@askmyteapot

Copy link
Copy Markdown
Contributor Author

@0xShug0 Updated in 5f6390c with the suggested GGML_CUDA_FA_NVCC_13_4_ZERO_FILL_FIX gate: the workaround is limited to NVIDIA NVCC 13.4, with the original implementation retained for other compiler versions and HIP. I also removed the added test and CMake registration, so the net PR diff is just the header change.

I recompiled the gated variant with CUDA 13.4 / --split-compile=8: all 216 CPU-reference cases across head dimensions 64, 80, 96, 112, 128, and 256 passed on the RTX PRO 5000 Blackwell and RTX 3090. A separate BreezeTTS 2 BF16 CLI using the gated kernels and all Flash Attention flags enabled generated normal audio (3.44 seconds, PCM range -4788 to 5544). Conditional-selection checks also confirmed the original path for the other compiler configurations; those were preprocessor checks, not runtime validation on other toolchains.

The description now reflects this revision and links the earlier detailed results and timing traces. The performance benchmark was not rerun after adding the gate. AI assistance: Codex helped implement, validate, and draft this update.

@0xShug0
0xShug0 merged commit 4f5afb6 into 0xShug0:main Oct 31, 2026
7 checks passed
@0xShug0

0xShug0 commented Oct 31, 2026

Copy link
Copy Markdown
Owner

@askmyteapot Thanks! PR merged!

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants