Skip to content

[PERF] Regression from #5001 (fix of #4974): AwqGemmFn/AwqGemmTritonFn clone every batched output even under inference_mode (112 extra copies per forward, +3-8%) #5086

Closed

Description

@Qubitium

Found on main @ e75181d, RTX 4090 (torch 2.15 + CUDA 13.2). Regression from #5001 (fix of #4974).

What is wrong

#5001 made AwqGemmFn.forward (gptqmodel/nn_modules/qlinear/gemm_awq.py:165) and AwqGemmTritonFn.forward (gptqmodel/nn_modules/qlinear/gemm_awq_triton.py:139) return out.reshape(out_shape).clone() whenever the input is batched (3-D). The clone has no condition. It runs on plain torch.inference_mode() inference too, where nothing is autograd-tracked and nothing can update the output in place.

Every HF forward passes 3-D hidden states [B, T, H], so every AWQ GEMM linear now does one extra full-output copy kernel per forward, for prefill and for each decode step.

The copy also does no work. Both callers already reshape outside the Function: AwqGEMMLinear.forward ends in return out.reshape(out_shape) (gemm_awq.py:393) and AwqGEMMTritonLinear.forward does the same (gemm_awq_triton.py:346). If the Function returned its 2-D GEMM result, autograd would get an owned tensor and the outer reshape would be a view created outside the Function. That is enough for #4974 (AdaLoRA result += output) without copying anything.

The commit message says "only a batched, autograd-tracked result pays for a copy". That is true for the new _reshape_reduced_owned() in _numerics.py, but not for these two Functions.

Repro

import torch
from gptqmodel import GPTQModel, BACKEND
for bk in (BACKEND.GEMM, BACKEND.GEMM_TRITON):
    m = GPTQModel.load("AWQ-Llama-3.2-1B-g128-gemm", backend=bk, device="cuda:0", dtype=torch.float16)
    n = [0]; orig = torch.Tensor.clone
    def cnt(self, *a, **k): n[0] += 1; return orig(self, *a, **k)
    ids = torch.randint(100, 10000, (1, 1), device="cuda")
    with torch.inference_mode():
        m.model(ids)
        torch.Tensor.clone = cnt; m.model(ids); torch.Tensor.clone = orig
    print(bk, "clones per inference forward:", n[0])

Output (main vs the round-18 base c2b0bf6):

main: GEMM 112, GEMM_TRITON 112   (16 layers x 7 linears: every AWQ linear, every forward)
c2b0bf64f: GEMM 0, GEMM_TRITON 0

torch.profiler, one forward of AWQ-Llama-3.2-1B on main, device time of the added aten::clone kernels:

backend decode (T=1) prefill (T=2048)
GEMM 112 launches, 105 us 112 launches, 1.14 ms
GEMM_TRITON 112 launches, 108 us 112 launches, 2.20 ms

Wall-clock per forward, min of 20 (GPU shared with other jobs, so treat these as indicative):

  • GEMM decode: 6.71 ms vs 6.21 ms (+8%)
  • GEMM prefill 2048: 49.4 ms vs 46.6 ms (+6%)
  • GEMM_TRITON decode: 12.0 ms vs 11.7 ms (+3%)
  • GEMM_TRITON prefill: 40.6 ms vs 39.0 ms (+4%)

Single up_proj module on main, 3-D vs 2-D input (the 2-D path has no clone): T=1 9.4 vs 8.0 us, T=2048 GEMM_TRITON 562 vs 428 us. On the old tree, 3-D and 2-D cost the same.

Suggested fix

In both Functions, return the 2-D GEMM output as-is and let the existing out.reshape(out_shape) in AwqGEMMLinear.forward / AwqGEMMTritonLinear.forward restore the leading dims. That reshape is a view created outside the Function, so in-place updates on it are legal. backward already flattens grad_output to 2-D, so it only needs ctx.input_shape for grad_input, which it already has. If a 3-D return must stay, clone only when the output is tracked, the same way _reshape_reduced_owned() does.

Test that should have caught it

tests/test_training_kernel_inplace_4974.py checks not out._is_view() but never counts allocations on the inference path. #5003's tests/test_peft_adalora_training_4974.py already has a Tensor.clone counter for the inference path of TorchLinear/TritonV2Linear. Extend it to AwqGEMMLinear / AwqGEMMTritonLinear with a 3-D input under torch.inference_mode().

Severity

Low-medium perf: 3-8% on every AWQ GEMM / GEMM_TRITON forward (the AWQ path on ROCm, pre-sm_80 GPUs, and explicit backends). Correctness is unaffected.

🤖 Generated with Claude Code

Activity

  1. ZX-ModelCloud commented on Nov 18, 2026

    @ZX-ModelCloud
    Collaborator

    Claimed. I'm working on this issue and will submit a fix PR. Please avoid duplicate work in the meantime.

  2. ZX-ModelCloud commented on Nov 18, 2026

    @ZX-ModelCloud
    Collaborator

    Agent claim released: agent-7f8403341687 finished this issue.

    Pull request: #5130

    Reproducing the unconditional batched-output clone under inference_mode and preparing a regression test

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

Metadata

Metadata

Assignees

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