Skip to content

fix(exl3): skip zero-row hadamard launches (#4550) - #4587

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4550-exl3-zero-row-grid
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4550-exl3-zero-row-grid

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4550

Root cause

cbf5994 added contiguity checks and 64-bit row offsets to EXL3's had_r_128/had_r_128_dual but not the zero-batch return that the same commit added to gptqmodel_ext/hadamard. With rows == 0, both functions still built dim3 gridDim(0, cols / 128) and launched; the runtime rejected the zero-sized grid and cuda_check(cudaPeekAtLastError()) called exit().

LinearEXL3.forward reaches that path unconditionally on ROCm (torch_mode = True when torch.version.hip is set) and on CUDA when reconstruct is forced. hgemm happens to accept M=0, but the Hadamard launches do not.

Fix

Two entry-point guards; the M>0 code path is unchanged:

flowchart TD
    A["EXL3 forward / had_r_128(M)"] --> B{"rows == 0?"}
    B -- "yes (LinearEXL3.forward)" --> C["return torch.empty(out_shape, dtype, device)"]
    B -- "yes (had_r_128 / _dual)" --> D["validate output dtype, then return before gridDim/launch"]
    B -- "no" --> E["existing gridDim(rows, blocks) launch"]
    classDef fixed fill:#e8f6e8,stroke:#1b7f1b,color:#111;
    class C,D fixed;
Loading
Before After
had_r_128(rows=0) launches gridDim(0, blocks) -> invalid launch / process exit Returns after shape checks (output dtype still validated)
had_r_128_dual(rows=0) same Same early return for both outputs
LinearEXL3.forward(M=0) calls had_r_128 -> hgemm -> had_r_128 Returns a correctly shaped empty tensor before torch_mode dispatch

The Python guard uses bsz = x.numel() // x.shape[-1], so it also covers a leading zero extent such as (2, 0, K). The C++ guard sits after TORCH_CHECK_SHAPES_FULL/TORCH_CHECK_DIM/TORCH_CHECK_DIV/contiguity and keeps the existing output-dtype validation for empty operands.

Docs updated in docs/kernel-input-contiguity.md (empty-operand flow now includes EXL3 had_r_128/had_r_128_dual and LinearEXL3.forward).

Before / after evidence

Python entry-point test (tests/test_hadamard_empty_input.py::test_exl3_forward_guards_empty_batch_before_kernel_launch):

  • Before: rows=0 failed with AssertionError: assert ['had_r_128', ..., 'had_r_128'] == []; rows=2 passed.
  • After: both rows pass; M=2 still records exactly ["had_r_128", "hgemm", "had_r_128"].

Real extension test (test_exllamav3_had_r_128_accepts_an_empty_batch, CUDA RTX 4090):

  • Before: pytest exit 1 with GPU assert: invalid argument /root/stableabi/gptqmodel-kern2/gptqmodel_ext/exllamav3/quant/hadamard.cu 132 (the process exits in cuda_check).
  • After: passes; y.shape == (0, 128), dtype preserved. test_exllamav3_had_r_128_still_launches_for_nonempty_rows passes too (nonempty output is finite and differs from the input, proving the launch happened).

Tests (thread caps OMP/MKL/OPENBLAS/NUMEXPR_NUM_THREADS=4)

CUDA (/opt/venvs/cu130t, CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae):

  • pytest tests/test_hadamard_empty_input.py tests/test_exl3_wide_input.py tests/kernels/test_exllamav3_kernel.py -q -k "not addresses_rows_past_two" -> 46 passed, 1 deselected
  • pytest tests/kernels/test_exllamav3_kernel.py -q -k "hadamard_kernel_rejects_strided_operands" -> 1 passed

ROCm (/opt/venvs/rocm101t, HIP_VISIBLE_DEVICES=0 only):

  • pytest tests/test_hadamard_empty_input.py -q -> 13 passed (includes the real had_r_128 M=0 and M>0 extension tests)

Lint:

  • ruff check gptqmodel docs/eora tests setup.py -> All checks passed!

git fetch origin main && git merge --no-edit origin/main -> merged c280dfeba; lint and tests re-run after the merge.

Hardware

NVIDIA RTX 4090 (GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae), AMD R9700 (HIP_VISIBLE_DEVICES=0); torch 2.16.0.dev+cu130 and 2.16.0.dev+rocm10.1.

Limitations

  • had_r_128_dual is not exposed by the Python bindings, so its guard is compile-validated only; the runtime regression covers had_r_128 and LinearEXL3.forward.
  • tests/test_exl3_wide_input.py::test_native_exl3_row_boundary_uses_expected_half_kernel[32-bc] fails on ROCm at unmodified HEAD because HIP forces torch_mode=True and the test expects the NVIDIA-only bc path for 32 rows. Verified pre-existing; not caused by this change and not modified here.
  • The 2^32 row-offset test was excluded from the focused run because it allocates ~16 GiB; it is unrelated to the M=0 guard.
  • No numerical claim: M>0 executes the identical code path, and the new nonempty test only asserts that the launch occurs and the output is finite.

@Qubitium
Qubitium merged commit b2cd5eb into main Nov 17, 2026
5 checks passed
@Qubitium
Qubitium deleted the fix/issue-4550-exl3-zero-row-grid branch November 17, 2026 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] EXL3 had_r_128 launches a zero-row grid on M=0 inputs (always taken on ROCm); M=0 fix in cbf5994 incomplete

1 participant