Skip to content

fix(awq): skip the batched output copy for untracked inference (#5086) - #5130

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-5086-awq-clone
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-5086-awq-clone

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5086.

#5001 made AwqGemmFn.forward and AwqGemmTritonFn.forward return
out.reshape(out_shape).clone() for every batched (3-D) input. The clone was
unconditional, so ordinary inference under torch.inference_mode() paid for a
full-output copy on every AWQ GEMM / GEMM_TRITON linear even though no autograd
graph exists and nothing can update the result in place.

The copy is only required when the Function output is autograd-tracked: a
reshape view created inside a custom autograd.Function cannot be updated in
place (#4974). An untracked result can keep that view, so this change
materializes it only when an input requires grad.

What Changed

  • gptqmodel/nn_modules/qlinear/gemm_awq.py: AwqGemmFn returns the batched
    reshape view for untracked results and clones only when
    any(ctx.needs_input_grad).
  • gptqmodel/nn_modules/qlinear/gemm_awq_triton.py: the same condition for
    AwqGemmTritonFn.
  • tests/test_awq_rank_and_empty.py: regression coverage for both backends and both modules.

No API change: the Functions still return the requested batched shape, and the
tracked path still returns an owned tensor. The 2-D path is unchanged.

Tests

New nodes (CPU-runnable, monkeypatched GEMM boundary; the module-level JIT case
is gated on the native CUDA AWQ extension):

cd tests
python -m pytest test_awq_rank_and_empty.py -q -rs \
  -k "does_not_clone or tracked_batched or module_inference"
# 8 passed

Red evidence on the unmodified base (same test file, isolated checkout): the
six inference clone-count assertions failed at assert clones["n"] == 0 with
assert 1 == 0; the two tracked-output control tests passed.

Affected families on a CUDA build:

cd tests
python -m pytest test_awq_rank_and_empty.py test_training_kernel_inplace_4974.py \
  test_peft_adalora_training_4974.py test_awq_fp32_output.py \
  kernels/test_awq_triton_bias_epilogue.py test_awq_fp16_matmul_heuristic.py \
  -q -rs
# 138 passed, 54 skipped (benchmark env gate)

Triton AWQ path on a ROCm build:

cd tests
python -m pytest test_awq_rank_and_empty.py kernels/test_awq_triton_bias_epilogue.py \
  test_awq_fp16_matmul_heuristic.py -q -rs
# 82 passed, 55 skipped (1 native-CUDA-only module case, 54 benchmark env gate)

Lint:

bash format/check.sh
# ruff 0.14.2; All checks passed!

Real-kernel A/B (3-D input under torch.inference_mode(), both backends): the
fixed tree reports 0 clones, the unmodified base reports 1 clone per forward,
and the outputs are bit-identical. A tracked 3-D forward still materializes an
owned tensor, accepts out += 1, and backpropagates.

  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.

Model Support / Module Plan

Not applicable: no model registry, alias, definition, module-plan or fusion
change.

Logic Flow Documentation

Not applicable: this is a local epilogue-ownership fix. It does not change
dispatch, plan selection, control flow, or a decision mechanism; it only
removes an unconditional tensor copy. No diagram is needed to verify the flow.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • For kernel-related changes, accuracy drift was measured against an applicable independent Torch FP32/FP64 oracle, or the reason this is not applicable is documented.
  • Fusion support changes preserve AUTO's native class and add actual-device FP16/BF16 tests where supported, native shape/workspace/descriptor checks, mutation, direct source calls, exact restoration and packed-storage checks. Not applicable: no fusion change.
  • The complete kernel inventory/device report lists unavailable dependencies, architecture restrictions and unqualified formats; skips are not validation. Not applicable: no new kernel, format, or device is added.

Kernel accuracy note: this change only chooses between a view and a copy of the
same values; it does not alter arithmetic. The existing independent-oracle tests
above pass unchanged, and the real-kernel A/B comparison is bit-identical.

Notes

The clone is still taken when an input requires grad (including a grad-requiring
input under a disabled-grad context), matching _reshape_reduced_owned() in
_numerics.py; that is the condition the Function can observe. The reported
inference path passes inputs that do not require grad, so it skips the copy.

#5001 materialized the leading-dimension reshape in AwqGemmFn and
AwqGemmTritonFn unconditionally, so every batched AWQ GEMM forward paid
for a full-output copy even under inference_mode. Materialize the reshape
only when an input needs grad, which is the condition that makes the
Function output autograd-tracked. Untracked results can keep the
Function's reshape view and accept in-place updates, so the #4974
guarantee is preserved without the inference-time copy.

Add regression coverage for both backends and both modules: a batched
inference forward allocates no clone, while a tracked batched output is
still an owned tensor that supports in-place accumulation and backward.
@ZX-ModelCloud
ZX-ModelCloud marked this pull request as ready for review November 18, 2026 05:20
@Qubitium
Qubitium merged commit 3de2c3c into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-5086-awq-clone branch November 18, 2026 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants