Skip to content

[BUG] Compiled CPU fused GPTQ and AWQ forwards drop activation gradients after warmup #5369

Closed

Description

@Qubitium

Describe the bug

After a no-grad CPU warmup, the fused GPTQ/AWQ modules preserve activation gradients in a direct eager forward. Calling the same module through torch.compile(fullgraph=True) instead uses the inference path and returns an output without a gradient function.

To reproduce

Reproduced on CPU at 72cef9bac with PyTorch 2.15.0+cu132. This example uses the eager compiler backend to exercise the captured graph without code generation.

import torch
from gptqmodel.nn_modules.qlinear.torch_fused import TorchFusedLinear

def warmed_layer():
    layer = TorchFusedLinear(
        bits=4, group_size=32, in_features=32, out_features=16,
        sym=True, desc_act=False, register_buffers=True,
    ).eval()
    layer.qzero_format(2)
    with torch.no_grad():
        layer.qweight.fill_(0x77777777)
        layer.qzeros.fill_(-2004318072)  # packed zero code 8
        layer.scales.fill_(0.5)
    layer.post_init()
    with torch.no_grad():
        layer(torch.ones(1, 32, dtype=torch.bfloat16))
    return layer

x = torch.ones(1, 32, dtype=torch.bfloat16, requires_grad=True)
compiled = torch.compile(warmed_layer(), backend="eager", fullgraph=True)
compiled_output = compiled(x)
print(compiled_output.requires_grad)

eager = warmed_layer()
print(torch.autograd.grad(eager(x).sum(), x)[0][0, 0].item())
compiled_output.sum().backward()

The two paths use independently warmed instances because the eager gradient path restores the packed state. Eager returns the expected gradient -8. Compiled returns False and backward raises that the tensor has no grad_fn. Also reproduced with TorchAtenLinear, TorchFusedAwqLinear, and TorchAtenAwqLinear.

Additional context

The compile branches read the warmed inference mode and skip the gradient-sensitive fallback used in #5314. Fused GPTQ, ATen GPTQ, fused AWQ, ATen AWQ.

Suggested mitigation

In the fused and ATen GPTQ/AWQ forwards under gptqmodel/nn_modules/qlinear/, one option is a compile-compatible differentiable branch that can recover checkpoint-layout weights without mutating packed runtime state inside the trace. An input-gradient implementation for the packed op is another option. The existing eager path can serve grad-requiring calls as an interim workaround.

A regression could warm two modules independently, then compare direct and compiled backward against the scalar gradient. Keeping the warm states separate exposes the transition that triggered the bug.

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions