Repository navigation
fix(qlinear): never return a reshape view from the trainable kernels (#4974) - #5003
Conversation
…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.
|
Coordination note: #5001 ( |
|
Follow-up (this PR already merged as |
…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
Fixes #4974
Root cause
TorchLinear's wide-autograd path calls_CudaWideMatmul.apply(...)andTritonV2LinearcallsQuantLinearFunction.apply(...). Both Functions ended withoutput.to(dtype).reshape(shape), andTensor.reshapereturns 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:PEFT's AdaLoRA wrapper (
peft/tuners/adalora/gptq.py) doesresult += output, so AdaLoRA could not train any GPTQ model.Fix (
gptqmodel/nn_modules/qlinear/_numerics.py)_CudaWideMatmul.forwardnow returns the 2-D product (output.to(x.dtype)), never a reshape view._reshape_reduced(output, shape)returns the tensor unchanged whenoutput.shape == shape, else reshapes. Used at every reduce return:matmul_fp32_reduce(wide-autograd, main FP32-reduce, HIP fused-bias) andlinear_fp32_reduce(the FP8/AWQ sibling entry point, which also called_CudaWideMatmul.applydirectly).Verification
_numerics.pyfromorigin/main):4 failed, 7 passed— the four CUDA contract cases (TorchLinear,TritonV2Linear, 2-Dmatmul_fp32_reduce, PEFTSVDQuantLinear), all with the issue's exactOutput 0 of View ...error.tests/test_peft_adalora_training_4974.py tests/test_adapter_targets.py tests/test_auto_adapter_generate.py→ 25 passed after merging currentmain.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 istests/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 withHFValidationError— environmental, unrelated.test_inference_path_does_not_clonecounts 0Tensor.clonecalls across inference forwards;test_inference_output_is_unchangedis 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
_wants_fp32_reduceis CUDA-only, so the CPU path never enters a custom Function;out += 1works 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.TritonV2Linearwas exercised on this box's CUDA viaQuantLinearFunction; ROCm/XPU and the XPU-specific AdaLoRA integration test were not run.peftis not installed in the venv; the PEFT tests usepytest.importorskipand were validated with peft 0.21.2 onPYTHONPATH(the issue's version). In CI withoutpeftthese cases skip rather than fail.SVDQuantLinearwrapper over a real packedTorchLinearbuilt bypack_block— sameresult += outputcode path, no model load.