Repository navigation
fix(awq): run a stateless rotary on the replay device - #6634
Merged
Merged
Conversation
phi4mm (Phi-4-multimodal) AWQ quantization on CUDA crashed in the scale-search replay with `RuntimeError: Expected all tensors to be on the same device ... cuda:0 and cpu` in `apply_rotary_pos_emb`. Cause: `_prepare_module_forward` and `_refresh_forward_kwargs_from_cache` rebuild `position_embeddings` with the root rotary and move the replay input to the device `_get_rotary_device` reports. phi4mm's port rotary (`ports/phi4/modeling_phi4.py` `RotaryEmbedding`) is stateless: it has no parameters or buffers and derives its LongRoPE frequencies from the input on every call. `_get_rotary_device` fell through to `utils.device.get_device`, which returns CPU for a module without tensors, so the CUDA activation was copied to the CPU, cos/sin were built there, and the CUDA attention received CPU embeddings. Declaring `rotary_embedding` would not help: there is no tensor to move. Fix: `_get_rotary_device` returns the caller's fallback (the module/input device) when the rotary owns no parameters or buffers, as ParoQuant's copy of the helper already does. `_get_rotary_for_device` passes the target as that fallback, so a stateless rotary is used as-is instead of deep-copied per device. Rotaries with `inv_freq` or other tensors resolve exactly as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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
phi4mm (Phi-4-multimodal) AWQ quantization on CUDA crashes in the scale-search replay:
Root cause.
_prepare_module_forward(and_refresh_forward_kwargs_from_cache) rebuildposition_embeddingswith the root rotary and first move the replay input to the device
_get_rotary_devicereports. phi4mm's port rotary(
gptqmodel/models/ports/phi4/modeling_phi4.py:19RotaryEmbedding) is stateless: no parameters, no buffers; itderives its LongRoPE frequencies from the input on every call.
_get_rotary_devicefell through toutils.device.get_device, which returns CPU for a module without tensors. The CUDA activation was copied to the CPU,cos/sin were built there, and the CUDA attention received CPU embeddings.
The rotary is found correctly (
model.rotary_emb). Declaringrotary_embeddingon the definition, or moving the rotary as acalibration side module, would not help: it owns no tensor to move. The bug is in how the shared replay helper reads
the device of a module that has none.
What Changed
AWQProcessor._get_rotary_device: a rotary with no parameters or buffers now returns the caller's fallback (thereplayed module's device or the input's device) instead of CPU. ParoQuant's copy of this helper
(
paro_processor.py:1128) already returns the fallback. Rotaries withinv_freqor any other tensor resolve exactly as before.AWQProcessor._get_rotary_for_device: passes the target device as that fallback, so a stateless rotary is usedas-is. It is no longer deep-copied into the per-device cache.
MODEL_MAPfamily with a stateless decoder root rotary(
ports/deepseek_vl_v2/modeling_deepseek.py:72). Its AWQ replay crashes earlier on every device because the replaycalls its rotary without the required
seq_len: AWQ: DeepSeek-VL2 scale-search replay crashes (root rotary called without required seq_len) #6631. Once that is fixed, this change also keeps its CUDA replayon the device.
Stateless rotary scan (AST over
gptqmodel/models/portsandtransformers/models,nn.Module-based rotary classes withno
register_buffer/Buffer(/Parameter(/inv_freq):ports/phi4RotaryEmbedding(root, fixed here),ports/deepseek_vl_v2DeepseekV2RotaryEmbedding(root, #6631),ports/telechat2RotaryEmbedding(per-attention,and the attention takes no
position_embeddings, so the replay never rebuilds with it). No Transformers class matched.Tests
New regressions in
tests/test_awq_rotary_device.py(CPU,metastands in for the accelerator as in the existing teststhere; copying a meta tensor to the CPU raises, so the CPU move is visible without a GPU):
test_module_forward_runs_a_stateless_rotary_on_the_module_device: the scale-search replay path.test_refresh_forward_kwargs_runs_a_stateless_rotary_on_the_input_device: the cached-kwargs refresh path.The one failure,
tests/test_awq_weight_mean.py::test_awq_pseudo_quantize_nan_guard_uses_one_host_read(
assert 4 == 1scalar reads), fails the same way on origin/main when CUDA is hidden and passes with a GPU visible.It is unrelated to this change (pseudo-quantize NaN guard, not the rotary path).
bash format/check.sh:All checks passed!Measured Impact
RuntimeErrorcuda:0 vs cpu inapply_rotary_pos_embModel Support / Module Plan
Not applicable: no definition, alias, module tree or fusion change.
Model test node IDs and results (or "not applicable"): not applicable.
tests/test_phi4_local_port.pyran in the suite above.AWQ onboarding safety
non-quantized consumers, intervening operations, and complete replay scope.
layouts/configs, shared branches/routers, and nontrivial scales; passing
module-plan or width checks alone is not AWQ qualification.
behavior are tested where applicable; no-group modes still quantize all linears.
explicitly unsupported/unverified; exact test nodes, skips and limits are recorded.
AWQ evidence and per-variant policy: not applicable (boxes left unchecked on purpose): no grouping, fold, or search change. This is replay device placement only. The rebuilt
cos/sin are the same values computed on the replay device instead of the CPU. phi4mm's AWQ policy is unchanged (norm-sourced
groups with LoRA-adapter compensation,
_awq_allow_partial_replay). Its CPU output is bit-identical to main, and on CUDA it nowcompletes instead of crashing. The neutral-fold and chunk-parity rows are not affected by a device-placement change.
The existing phi4mm port tests above cover them.
Logic Flow Documentation
and docs page are included below.
Diagram summary: not applicable. Rotary resolution order and the rebuild decision are unchanged. Only the device reported for a
tensorless rotary changes, from CPU to the caller's fallback (
awq_processor.py_get_rotary_device).Review Requirements
Kernel Accuracy Requirements
Notes
exercises the same port rotary and replay path.
utils.device.get_devicestill returns CPU for a tensorless module. It has many callers, and changing its default is outof scope. The fix stays in the rotary helper, which turns that answer into an input move.
🤖 Generated with Claude Code