Repository navigation
fix(awq): skip the batched output copy for untracked inference (#5086) - #5130
Merged
Merged
Conversation
#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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5086.
#5001 made
AwqGemmFn.forwardandAwqGemmTritonFn.forwardreturnout.reshape(out_shape).clone()for every batched (3-D) input. The clone wasunconditional, so ordinary inference under
torch.inference_mode()paid for afull-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.Functioncannot be updated inplace (#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:AwqGemmFnreturns the batchedreshape view for untracked results and clones only when
any(ctx.needs_input_grad).gptqmodel/nn_modules/qlinear/gemm_awq_triton.py: the same condition forAwqGemmTritonFn.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):
Red evidence on the unmodified base (same test file, isolated checkout): the
six inference clone-count assertions failed at
assert clones["n"] == 0withassert 1 == 0; the two tracked-output control tests passed.Affected families on a CUDA build:
Triton AWQ path on a ROCm build:
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): thefixed 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.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
Kernel Accuracy Requirements
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 reportedinference path passes inputs that do not require grad, so it skips the copy.