Skip to content

[BUG] PEFT AdaLoRA cannot train any GPTQ model: TritonV2Linear/TorchLinear return a view created inside QuantLinearFunction, so AdaLoRA's in-place 'result += output' raises (test_olora_finetuning_xpu red since 12c1273c2) #4974

Description

@Qubitium

Found on main @ 3867c82, RTX 4090 (torch 2.15, peft 0.21.2, transformers 5.22). Also red at b225a43 (same error) and at 12c1273: this is an old bug, not a regression from this round.

What is wrong

The trainable GPTQ kernels return a tensor that is a view created inside a custom autograd.Function. Autograd forbids in-place ops on such a tensor. PEFT's AdaLoRA wrapper for GPTQ does exactly such an op (peft/tuners/adalora/gptq.py:66, result += output), so AdaLoRA fine-tuning of any GPTQ model fails on the first forward. Plain LoRA works because GPTQLoraLinear uses an out-of-place add.

Where the view comes from: TritonV2Linear.forward calls QuantLinearFunction.apply(...) (gptqmodel/nn_modules/triton_utils/dequant.py:360). Its forward returns quant_matmul(...), which ends in matmul_fp32_reduce() (gptqmodel/nn_modules/qlinear/_numerics.py:166, return output.to(x.dtype).reshape(shape)). reshape on a contiguous tensor returns a view even when the shape does not change, so the Function's output is a view of an internal tensor. TorchLinear (BACKEND.TORCH) gives the same result. The .reshape has been there since #3520.

tests/test_olora_finetuning_xpu.py::Test::test_peft (AdaLoRA on a GPTQ Llama-3.1-8B, BACKEND.AUTO_TRAINABLE) fails on CUDA with exactly this error. It has been red in every suite triage since at least 12c1273, but it was never filed:

peft/tuners/adalora/gptq.py:66: RuntimeError: Output 0 of View is a view and is being modified inplace. This view was created inside a custom Function ...

Minimal repro

import torch
from transformers import AutoModelForCausalLM, GPTQConfig
from gptqmodel import BACKEND
from peft import AdaLoraConfig, LoraConfig, get_peft_model

P = "Llama-3.2-1B-Instruct-GPTQ-4bits-gp128"   # any GPTQ 4-bit checkpoint
for name, cfg in [("LoRA", LoraConfig(r=8, target_modules=["q_proj", "v_proj"], task_type="CAUSAL_LM")),
                  ("AdaLoRA", AdaLoraConfig(r=8, target_modules=["q_proj", "v_proj"], task_type="CAUSAL_LM", total_step=10))]:
    model = AutoModelForCausalLM.from_pretrained(P, dtype=torch.bfloat16, device_map="cuda:0",
                                                 quantization_config=GPTQConfig(bits=4, backend=BACKEND.AUTO_TRAINABLE))
    pm = get_peft_model(model, cfg).train()
    ids = torch.randint(100, 1000, (2, 16), device="cuda")
    pm(input_ids=ids, labels=ids).loss.backward()

Output:

LoRA: wrapper=GPTQLoraLinear kernel=TritonV2Linear loss=12.259 backward OK
AdaLoRA: wrapper=SVDQuantLinear kernel=TritonV2Linear FAILED: Output 0 of View is a view and is being modified inplace. This view was created inside a custom Function ...

Directly on the kernel (GPTQModel.load(..., backend=AUTO_TRAINABLE or TORCH), q_proj, input with requires_grad): y = mod(x) gives y._is_view() == True, and y += 1.0 raises the same error for TritonV2Linear and TorchLinear, for both 2-D and 3-D inputs.

Suggested fix

Do not return a view from inside the Function:

  • in matmul_fp32_reduce, return output.to(x.dtype) when output.shape == shape, and reshape outside the autograd Function; or
  • reshape in TritonV2Linear.forward/TorchLinear after QuantLinearFunction.apply, and have the Function return the 2-D tensor it computed.

A view taken outside the Function (QuantLinearFunction.apply(...).reshape(...)) is fine for autograd.

Test that should have caught it

tests/test_olora_finetuning_xpu.py does catch it, but it needs a pre-quantized 8B checkpoint and the Hub, so it never runs in CI. A small unit test would cover the contract: for every SUPPORTS_TRAINING kernel, check that out = module(x.requires_grad_()) allows out += 1; out.sum().backward().

Severity

Medium: AdaLoRA (a documented PEFT method that GPT-QModel's AUTO_TRAINABLE path is supposed to serve) cannot train any GPTQ model, and any user code that adds in-place to the output hits the same error.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4974 is working on this issue now.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #5003

    Fix the PEFT AdaLoRA training failure: TritonV2Linear/TorchLinear return a view created inside QuantLinearFunction (autograd error); make the training forward return a tensor that does not alias the Function's saved output, with a CPU AdaLoRA smoke test.

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-root-4974 finished this issue.

    Pull request: #5001 (squash-merged as 24a46e337).

    #5003 landed the first half (_CudaWideMatmul returns the 2-D product, no-op
    reshape skipped in matmul_fp32_reduce). This PR finishes the paths that still
    returned a reshape view created inside a custom autograd.Function:

    path reached when
    linear_wide_bias checkpoint bias stored wider than the activations (FP32 bias + FP16/BF16 activations), inside QuantLinearFunction under autocast and on ROCm
    _fused_addmm the CPU/HIP fused bias epilogue in linear_fp32_reduce
    AwqGemmFn, AwqGemmTritonFn the two trainable AWQ GEMM kernels, 2-D and batched

    _reshape_reduced_owned() keeps the allocation-free reshape for eager calls and
    materializes the view only when it runs inside a Function forward (grad mode off
    there while the output is tracked), so only a batched autograd-tracked result
    pays for a copy. Numerics are unchanged (174 forward/gradient tensors bitwise
    equal to unmodified main).

    tests/test_training_kernel_inplace_4974.py guards every SUPPORTS_TRAINING
    kernel this host can build — TorchLinear, TritonV2Linear (train and
    eval), TorchFusedLinear, TorchAtenLinear, AwqTorchLinear,
    GGUFTorchLinear, TorchFP8Linear, AwqGemmFn, AwqGemmTritonFn — 2-D and
    3-D, plus a real PEFT SVDQuantLinear AdaLoRA step. Red on unmodified
    8ab5fefd6: 7 failed / 44 passed; green here: 51 passed (189 with the
    neighbouring AWQ/triton/torch epilogue suites; 17 passed / 34 skipped on CPU).
    Both guard files now run in the AWQ CPU unit CI lane.

    tests/test_peft_adalora_training_4974.py (from #5003) no longer errors in a
    source checkout: PEFT's is_gptqmodel_available probe resolves an installed
    gptqmodel distribution and raises PackageNotFoundError without one, which
    made both AdaLoRA nodes fail before reaching the assertion.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions