Skip to content

fix(qlinear): never return a reshape view from the trainable kernels (#4974) - #5003

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4974
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4974

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4974

Root cause

TorchLinear's wide-autograd path calls _CudaWideMatmul.apply(...) and TritonV2Linear calls QuantLinearFunction.apply(...). Both Functions ended with output.to(dtype).reshape(shape), and Tensor.reshape returns a view even when the shape is unchanged (t.reshape(t.shape)._is_view() == True). Autograd therefore treats the Function's output as a view created inside a custom Function, so any in-place op fails:

RuntimeError: Output 0 of View is a view and is being modified inplace. This view was created inside a custom Function ...

PEFT's AdaLoRA wrapper (peft/tuners/adalora/gptq.py) does result += output, so AdaLoRA could not train any GPTQ model.

Fix (gptqmodel/nn_modules/qlinear/_numerics.py)

  • _CudaWideMatmul.forward now returns the 2-D product (output.to(x.dtype)), never a reshape view.
  • New _reshape_reduced(output, shape) returns the tensor unchanged when output.shape == shape, else reshapes. Used at every reduce return: matmul_fp32_reduce (wide-autograd, main FP32-reduce, HIP fused-bias) and linear_fp32_reduce (the FP8/AWQ sibling entry point, which also called _CudaWideMatmul.apply directly).
  • Result: 2-D outputs are owned tensors; 3-D outputs are views created outside the Function, which autograd permits. Values, dtype rounding and backward are untouched; no clone is added.

Verification

  • Red on parent (fix reverted, _numerics.py from origin/main): 4 failed, 7 passed — the four CUDA contract cases (TorchLinear, TritonV2Linear, 2-D matmul_fp32_reduce, PEFT SVDQuantLinear), all with the issue's exact Output 0 of View ... error.
  • Green: tests/test_peft_adalora_training_4974.py tests/test_adapter_targets.py tests/test_auto_adapter_generate.py → 25 passed after merging current main.
  • Kernel/adapter sweep (test_pack_block_batched, test_gptq_static_group_order, test_gguf_bias_range, test_gptq_fallback_offset, test_fallback, test_adapter_config, test_adapter_unsupported_peft): 269 passed, 2 skipped, 1 failed. The one failure is tests/test_olora_finetuning_xpu.py::Test::test_peft, which hardcodes /monster/data/model/Meta-Llama-3.1-8B-Instruct-GPTQ-INT4 (absent on this host) and dies at load with HFValidationError — environmental, unrelated.
  • Inference cost: test_inference_path_does_not_clone counts 0 Tensor.clone calls across inference forwards; test_inference_output_is_unchanged is bit-exact against the manual epilogue on CPU and CUDA. The change removes a no-op reshape (metadata only) and adds no allocation.
  • ruff check gptqmodel tests: clean.

Residual risk

  • CPU does not reproduce the failure: _wants_fp32_reduce is CUDA-only, so the CPU path never enters a custom Function; out += 1 works there before and after. The red evidence is CUDA, which is where the reported bug lives. The CPU AdaLoRA case is kept as a regression guard.
  • TritonV2Linear was exercised on this box's CUDA via QuantLinearFunction; ROCm/XPU and the XPU-specific AdaLoRA integration test were not run.
  • peft is not installed in the venv; the PEFT tests use pytest.importorskip and were validated with peft 0.21.2 on PYTHONPATH (the issue's version). In CI without peft these cases skip rather than fail.
  • The 1B end-to-end checkpoint load was too slow on this host (110 s/tensor H2D), so the PEFT repro uses the real SVDQuantLinear wrapper over a real packed TorchLinear built by pack_block — same result += output code path, no model load.

…4974)

`TritonV2Linear` calls `QuantLinearFunction.apply` and `TorchLinear`'s
wide-autograd path calls `_CudaWideMatmul.apply`. Both Functions ended with
`output.to(dtype).reshape(shape)`, and `Tensor.reshape` returns a view even
when the requested shape equals the current one, so the Function's output was
a view. Autograd forbids in-place ops on a view created inside a custom
Function, so PEFT's AdaLoRA wrapper (`peft/tuners/adalora/gptq.py`, which does
`result += output`) failed on the first forward of every GPTQ model:

    RuntimeError: Output 0 of View is a view and is being modified inplace.

`_CudaWideMatmul.forward` now returns the 2-D product and `matmul_fp32_reduce`
reshapes outside the Function, via a helper that skips the reshape when the
shape already matches. `linear_fp32_reduce` (the FP8/AWQ sibling entry point)
gets the same treatment, so 2-D outputs are owned tensors and 3-D outputs are
views created outside the Function. Only view metadata changes: the reduction,
the dtype rounding, and the backward are untouched, and the inference path
still performs no clone (asserted by a `Tensor.clone` counter in the new
tests).

Tests: `tests/test_peft_adalora_training_4974.py` (11 cases) covers the
in-place + backward contract for `TorchLinear`, `TritonV2Linear`, and PEFT's
`SVDQuantLinear` wrapper, bit-equality against the manual FP32 epilogue on CPU
and CUDA, and the no-clone inference path. The four CUDA contract cases fail on
main and pass here.
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Coordination note: #5001 (root/adalora-view-4974) fixes the same #4974 view leak and additionally covers gemm_awq.py/gemm_awq_triton.py plus tests/test_training_kernel_inplace_4974.py. Two PRs cannot both land — one of them should be closed as a duplicate before merging. Recommend keeping #5001 (superset of the code change) and, if useful, porting tests/test_peft_adalora_training_4974.py into it.

@Qubitium
Qubitium merged commit 2be068c into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4974 branch November 17, 2026 21:54
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Follow-up (this PR already merged as 2be068cc6): three epilogues still returned a reshape view created inside a custom autograd.Function, so PEFT AdaLoRA still failed on them — linear_wide_bias (wide checkpoint bias, inside QuantLinearFunction under autocast and on ROCm), _fused_addmm (CPU/HIP fused epilogue), and AwqGemmFn/AwqGemmTritonFn (2-D and batched). Those, plus a broader trainable-kernel guard (all 9 buildable SUPPORTS_TRAINING kernels, train+eval, 2-D/3-D) and a fix for this file's PEFT probe in a source checkout (is_gptqmodel_available raises PackageNotFoundError when gptqmodel is not installed as a distribution, so both AdaLoRA nodes errored instead of running), are in #5001. Red on 8ab5fefd6: 7 nodes; green on #5001: 51 passed.

sLaffey pushed a commit to sLaffey/GPTQModel that referenced this pull request Nov 18, 2026
…MM (ModelCloud#4974) (ModelCloud#5001)

ModelCloud#5003 fixed the no-op reshape in the FP32-reduction epilogue. Three paths
still hand autograd a view created inside a custom `autograd.Function`, so
PEFT's AdaLoRA wrapper (`result += output`) still failed on them:

- `linear_wide_bias` (a checkpoint bias stored wider than the activations,
  reached inside `QuantLinearFunction` under autocast and on ROCm) ended in
  `output.to(dtype).reshape(shape)`;
- `_fused_addmm` (the CPU/HIP fused epilogue) reshaped its 2-D product;
- `AwqGemmFn` and `AwqGemmTritonFn` reshaped their batched result.

New `_reshape_reduced_owned()` keeps the allocation-free reshape for eager
calls and materializes it when it runs inside a Function forward (grad mode is
off there while the output is tracked), so only a batched, autograd-tracked
result pays for a copy.

`tests/test_training_kernel_inplace_4974.py` covers every `SUPPORTS_TRAINING`
kernel this host can build (TorchLinear, TritonV2Linear train+eval,
TorchFusedLinear, TorchAtenLinear, AwqTorchLinear, GGUFTorchLinear,
TorchFP8Linear, both AWQ GEMM Functions), the wide-bias epilogue through the
real `TritonV2Linear` Function, the storage-ownership invariant for the
epilogue helpers, and a real PEFT AdaLoRA step.

`tests/test_peft_adalora_training_4974.py` (from ModelCloud#5003) now tolerates a source
checkout: PEFT's `is_gptqmodel_available` probe resolves an installed
`gptqmodel` distribution and raises `PackageNotFoundError` without one, which
made both AdaLoRA nodes error instead of run.

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

Labels

None yet

Projects

None yet

1 participant