Skip to content

fix(paro): address wide-prefill offsets and dispatch long prefills to cuBLAS (#5096) - #5164

Merged
Qubitium merged 1 commit into
mainfrom
fix/5096-paro-wide-prefill-offsets
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5096-paro-wide-prefill-offsets

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5096.

Two independent 32-bit assumptions in the Paro GPU prefill path faulted the
device (and, on the default CUDA path, wasted VRAM) once a single forward
produced more than 2^31 output elements (rows x out_features), or the
activation itself exceeded rows x in_features > 2^31.

Confirmed on current main before the change:

  • paro_gemm_kernel (gptqmodel/quantization/paro/modules/triton/gemm.py:255,
    :308) built K * offsets_am and N * offs_cm from int32 vectors, so the
    fused Triton GEMM wrapped the address and raised
    CUDA error: an illegal memory access was encountered /
    hipErrorIllegalAddress.
  • ParoLinear._forward_cuda_awq_kernel fed the native split-K AWQ GEMM for
    every row count. The underlying row_offset * OC store is already 64-bit
    (the AWQ wide-offset fix, fix(awq): address wide-prefill output and activation offsets in 64 bits #5149), so no crash remains there, but the kernel
    still allocates one FP32 partial slab per split: at 32k rows it peaked at
    3266 MiB for a 297 MiB output and was 2.2x slower than dequantize + cuBLAS.

What Changed

  • paro_gemm_kernel: build offsets_am and offs_cm from
    pid_m.to(tl.int64) so A and C row addresses stay 64-bit. Shapes below 2^31
    keep identical arithmetic.
  • ParoLinear: add _PARO_AWQ_DENSE_MIN_ROWS = 640 and the
    AwqGEMMLinear-style large-M dispatch. Above 640 rows the layer dequantizes
    the packed grid, runs cuBLAS with an FP32 reduction, adds the stored bias in
    FP32 and casts once. Below the threshold the native kernel is unchanged. The
    route is a module constant, not an environment switch, and the reconstructed
    weight exists only for the dispatched call (no retained dense copy).
  • docs/paro.md: new "Native prefill dispatch and wide offsets" mechanism
    page section with the old/new flow and the measured table.

Behaviour changes: a prefill above 640 rows uses a different (faster, smaller)
kernel. Accuracy is unchanged within output rounding (2e-5 relative against the
FP32 packed-grid oracle).

Tests

  • Unit tests executed and passed.
  • New targeted regressions added.

Red/green (both trees at the same base, tests copied into the pre-fix tree):

# pre-fix tree
python -m pytest tests/kernels/test_paro.py -q -k "int32 or long_prefill or short_prefill"
# 6 failed -- illegal memory access on both Triton boundary cases,
# AssertionError: the native split-K GEMM must not serve a long prefill (x2)

# fixed tree
python -m pytest tests/kernels/test_paro.py -q -rs
# 58 passed, 1 skipped (test_paro_rocm_skips_the_native_cuda_kernel: ROCm device required)

python -m pytest tests/kernels/test_awq_gemv_splitk.py tests/kernels/test_awq_cuda_fp32_reduce.py tests/kernels/test_awq_torch.py -q -rs
# 169 passed

python -m pytest tests/kernels/test_paro.py -rs -k "triton_prefill"   # gfx1201
# 2 passed (both boundary cases)

bash format/check.sh
# All checks passed!

New nodes: test_paro_triton_prefill_stores_rows_above_int32_element_count,
test_paro_triton_prefill_reads_activations_above_int32_row_stride_product,
test_paro_cuda_long_prefill_matches_the_dense_oracle[float16, bfloat16],
test_paro_cuda_short_prefill_keeps_the_native_kernel,
test_paro_cuda_long_prefill_stores_rows_above_int32_element_count.

Oracle: the independent FP32 packed-grid dequantization
(_fp32_awq_oracle). Long-prefill output is compared against the oracle
rounded to the same output dtype (bounded by 4 output ULPs) plus a visible
error bound against the FP32 oracle, so mandatory output rounding is measured
separately from kernel error.

Skipped/not verified: ROCm coverage is the two Triton boundary cases only; the
native CUDA dispatcher tests are CUDA-only by design. The Triton fix is not
exercised on Hopper/Blackwell tiles.

Model Support / Module Plan

Not applicable: no MODEL_MAP, definition, alias, or module-plan change.

Logic Flow Documentation

  • Change alters dispatch/selection, so diagrams are included.
  • New-flow Mermaid diagrams use shipped symbol names.
  • Removed mechanism is tabulated with file:line evidence.
  • Failure and fallback paths are marked.
  • docs/paro.md repeats the diagrams in the
    "Native prefill dispatch and wide offsets" section, titled for the
    mechanism. README.md already links docs/paro.md.
    mechanism. README.md already links docs/paro.md.

Old flow (main):

flowchart TD
    A["ParoLinear.forward"] --> B["_forward_cuda_awq_kernel (paro.py:753)"]
    B --> C{"native shape and device contract?"}
    C -- "No" --> Z["portable paths: AVX512 / ATen / dense"]
    C -- "Yes" --> F["native split-K AWQ GEMM for every row count"]
    F --> P["one FP32 partial slab per split"]
    P --> H["sum / fused reduce then store with row_offset * OC"]
Loading
flowchart LR
    A["ParoTritonLinear prefill"] --> B["paro_gemm_kernel (gemm.py:227)"]
    B --> C["offsets_am = pid_m * BLOCK_SIZE_M (int32)"]
    B --> D["offs_cm = pid_m * BLOCK_SIZE_M (int32)"]
    C --> E["a_ptrs = K * offsets_am + offsets_k"]
    D --> F["c_ptrs = N * offs_cm + offs_cn"]
    E --> G["wrapped A row address above 2^31"]
    F --> H["wrapped C row address above 2^31"]
    style G fill:#fce8e6,stroke:#a33,color:#111
    style H fill:#fce8e6,stroke:#a33,color:#111
Loading

New flow:

flowchart TD
    A["ParoLinear.forward"] --> B["_forward_cuda_awq_kernel(rotated)"]
    B --> C["_cuda_awq_kernel_supported(compute_dtype)"]
    C --> D{"native shape and device contract?"}
    D -- "No" --> Z["portable paths: AVX512 / ATen / dense"]
    D -- "Yes" --> E{"rows > _PARO_AWQ_DENSE_MIN_ROWS (640)?"}
    E -- "No" --> F["native split-K AWQ GEMM"]
    E -- "Yes" --> G["_forward_dense_batch: dequantize + cuBLAS, FP32 reduce, bias, one cast"]
    F --> H["output"]
    G --> H
    Z --> H
    classDef wide fill:#e8f4ff,stroke:#36c,color:#111
    class G wide
    style Z stroke-dasharray: 4 2
Loading
flowchart LR
    A["ParoTritonLinear prefill"] --> B["paro_gemm_kernel"]
    B --> C["offsets_am = pid_m.to(int64) * BLOCK_SIZE_M"]
    B --> D["offs_cm = pid_m.to(int64) * BLOCK_SIZE_M"]
    C --> E["a_ptrs = K * offsets_am + offsets_k"]
    D --> F["c_ptrs = N * offs_cm + offs_cn"]
    E --> G["64-bit A row address"]
    F --> H["64-bit C row address"]
Loading

Review Requirements

  • Reviewed this PR.
  • Matches existing project structure, APIs and conventions.
  • No new monkeypatching; the existing _forward_dense epilogue is reused.
  • Impacted surface minimized (one dispatch branch, two int64 casts).
  • Regression risk considered: the short-prefill control pins the native path.

Kernel Accuracy Requirements

  • Drift measured against an independent FP32 Torch oracle on both dtypes.
  • Fusion: not applicable (no fusion change).
  • Native-device qualification: the boundary cases ran on NVIDIA and AMD;
    the CUDA-only dispatcher tests ran on NVIDIA only, and that limit is
    stated above.

… cuBLAS (#5096)

Two independent 32-bit assumptions ended a single prefill once
rows x out_features passed 2^31, or the activation itself passed
rows x in_features > 2^31:

- paro_gemm_kernel computed K * offsets_am and N * offs_cm from int32
  row vectors, so the fused Triton GEMM wrapped the address and faulted
  with cudaErrorIllegalAddress / hipErrorIllegalAddress.
- ParoLinear called the native split-K AWQ GEMM for every row count.
  Its store is 64-bit since the AWQ wide-offset fix, but it still
  allocates one FP32 partial slab per split, so a long prefill paid
  split_k x the output in transient memory and was slower than
  dequantize + cuBLAS from ~640 rows.

Widen both Triton row vectors to int64, and give ParoLinear the
AwqGEMMLinear large-M dispatch: above 640 rows dequantize the packed
grid, run cuBLAS with an FP32 reduction and add the stored bias before
one output cast. Below the threshold the native kernel is unchanged.

Adds regressions in tests/kernels/test_paro.py that build rows x
out_features and rows x in_features just above 2^31 with only the tail
rows nonzero, assert the dispatched route and oracle agreement, and
keep a short-prefill control on the native kernel.

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

Labels

None yet

Projects

None yet

2 participants