Repository navigation
refactor(definitions): declare the vision-input cast instead of per-model move_input_capture_example overrides - #6469
Merged
Conversation
…odel move_input_capture_example overrides
Five definitions (deepseek_ocr2, deepseek_vl, deepseek_vl_v2, llava,
locateanything) overrode move_input_capture_example with the same body: call
super(), then, if the pixel key holds a tensor, cast it to the vision tower's
dtype (tower.dtype, falling back to the first parameter's dtype) on the
tower's first-parameter device (unchanged when the tower has no parameters).
BaseQModel now owns that cast, after finalize_input_capture_example exactly
where the overrides ran it, driven by two class attributes:
calibration_vision_module: Optional[str] = None # path under self.model
calibration_vision_input_keys = ("pixel_values",)
The tower is still resolved only when a declared key holds a tensor, so
text-only rows never touch it. LocateAnything keeps a short override for its
int32 image_grid_hws, now placed on the cast pixel_values device (the same
device the old code used). ModuleLooper._model_initial_capture_is_generic
treats a declared tower like an override, so first-capture reuse eligibility
is unchanged.
The other 13 overrides use a different rule (dtype only, first-parameter
dtype with a floating-only gate, eager tower lookup, list items, or nested
moves) and are left as they are.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qubitium
marked this pull request as ready for review
November 30, 2026 02:59
Qubitium
added a commit
that referenced
this pull request
Nov 30, 2026
…d teardown Rebased fix/6148-issue onto origin/main 94ceaf7 (after #6464, #6465, #6467, #6469, #6470). Three conflicts: - gptqmodel/looper/forward_executor.py: import block. Main dropped the unused resolve_calibration_keep_mask import; kept only the PR's replay_records import. - gptqmodel/looper/module_looper.py: #6465 moved the failed-run teardown into InflightLayerTracker (inflight_layer.py). Kept main's structure and re-applied the PR's release_looped_capture() as a ModuleLooper override of _release_failed_run_processors that calls super() first. - gptqmodel/models/definitions/inkling2_loop.py: #6464 reduced pre_quantize_generate_hook_start to the batch-index/bank reset plus super(); the PR removes that reset, so the override is deleted. Earlier rebase (onto 81ac176): Rebased fix/6148-issue onto origin/main 81ac176. Two conflicts: - gptqmodel/models/definitions/inkling2_loop.py: import block. Kept the PR's replay_records imports; dropped the PR's get_op_name import because main removed its last use and the rebased file does not use it. - tests/module_tree/test_inkling2_loop_plan.py: main added the AWQ declaration block where the PR deletes _capture_inkling2_forward. Kept all of main's AWQ tests, removed only the helper the PR replaced with record-owned head banks. Tests: 135 passed, 1 skipped on CPU (main baseline for the pre-existing files: 124 passed). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 30, 2026
Merged
Qubitium
added a commit
that referenced
this pull request
Dec 1, 2026
…d teardown Rebased fix/6148-issue onto origin/main 94ceaf7 (after #6464, #6465, #6467, #6469, #6470). Three conflicts: - gptqmodel/looper/forward_executor.py: import block. Main dropped the unused resolve_calibration_keep_mask import; kept only the PR's replay_records import. - gptqmodel/looper/module_looper.py: #6465 moved the failed-run teardown into InflightLayerTracker (inflight_layer.py). Kept main's structure and re-applied the PR's release_looped_capture() as a ModuleLooper override of _release_failed_run_processors that calls super() first. - gptqmodel/models/definitions/inkling2_loop.py: #6464 reduced pre_quantize_generate_hook_start to the batch-index/bank reset plus super(); the PR removes that reset, so the override is deleted. Earlier rebase (onto 81ac176): Rebased fix/6148-issue onto origin/main 81ac176. Two conflicts: - gptqmodel/models/definitions/inkling2_loop.py: import block. Kept the PR's replay_records imports; dropped the PR's get_op_name import because main removed its last use and the rebased file does not use it. - tests/module_tree/test_inkling2_loop_plan.py: main added the AWQ declaration block where the PR deletes _capture_inkling2_forward. Kept all of main's AWQ tests, removed only the helper the PR replaced with record-owned head banks. Tests: 135 passed, 1 skipped on CPU (main baseline for the pre-existing files: 124 passed). 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
Five definitions overrode
move_input_capture_examplewith the same body. Each calledsuper(), then cast the pixel tensor to the vision tower's dtype on the tower's device.BaseQModelnow runs that cast at the same point (right afterfinalize_input_capture_example), and each definition declares only data:This is a behavior-preserving refactor: no change to numerics, checkpoint format or public API. The structural A/B over all 210
MODEL_MAPtypes shows no differences. The GPU A/B checkpoints (llava-interleave-qwen-0.5b plus a Qwen2.5-0.5B text control) are bit-identical.git diff --shortstat origin/main:10 files changed, 127 insertions(+), 82 deletions(-).gptqmodel/:7 files changed, 40 insertions(+), 82 deletions(-)(net −42).Scope is deliberately strict. On current
main, 18 definitions overridemove_input_capture_example. Only these 5 use the identical rule:tower.dtype, else the first parameter's dtype;finalize_input_capture_example.The other 13 are listed under "Kept as-is" with the rule that differs.
What Changed
BaseQModel(gptqmodel/models/base.py):calibration_vision_module: Optional[str] = Noneandcalibration_vision_input_keys: Tuple[str, ...] = ("pixel_values",).move_input_capture_examplenow ends with the cast that the five overrides each carried. It runs afterfinalize_input_capture_example, as before.getattr(self, ..., None), likecalibration_inputs, so test doubles that borrow the unbound method keep working.model.vision_towermodel.vision_modelvision, keyimagesmodel.vision_towerelse: pixel_values.to(device=vision_device)branch was dead code. It runs only when no dtype resolves, which means there is no first parameter, sovision_deviceis the tensor's own device.vision_model. It keeps its int32image_grid_hwsconversion and places it onpixel_values.deviceafter the base cast.vision_deviceheld in every branch:vision_device;vision_devicewas the tensor's own device.ModuleLooper._model_initial_capture_is_generic(gptqmodel/looper/module_looper.py) returns False when a class declarescalibration_vision_module.calibration_device_modulesguard from refactor(definitions): declare calibration side modules instead of per-model hook pairs #6464.docs/vlm-calibration-input-normalization.md. That page is already linked fromREADME.md.Kept as-is (a different cast rule; converting would change behavior):
tower.dtype); casts floating tensors onlyvision_modelis tolerated (no cast); tower looked up for every row; device falls back todata_devicenext(parameters())with no default, looked up for every row (text rows included); casts any value present under the keyvision_model.dtype or model.dtypevision_backbonevisual_tokenizer.dtypeinside the generic move, before finalizenested_move_tobodies replace the generic moveFollow-ups, not in this PR:
tower.dtype, else the first parameter". On real towersPreTrainedModel.dtypeis the first floating parameter's dtype. The only behavior change would be for a non-floating tensor under a pixel key, which processors never emit.nested_move_tobodies could become a separate declared flag.Tests
New tests (CPU):
tests/test_calibration_vision_input_cast.py::test_only_the_converted_definitions_declare_a_vision_towerMODEL_MAP.(module, keys)pairs equalEXPECTED, which is the tower path and key each removed override read onorigin/main.tests/test_calibration_vision_input_cast.py::test_declared_tower_casts_its_key_and_text_rows_skip_the_tower[deepseek_ocr2|deepseek_vl|deepseek_vl_v2|llava|locateanything]tests/test_gptaq_foem_first_capture_5831.py::test_declared_vision_tower_disables_reuse(CPU, real tiny Qwen2 quantize).assert 6 == (2 * 6), meaning the capture was reused.Affected-family run: 45 test files, i.e. every test file that references
move_input_capture_example, the capture-reuse guard, or a converted definition, plustest_stage_inputs_capture.pyandtest_mixed_modality_calibration_rows_6355.py. The seventests/models/*::Test*full-model eval classes are deselected; the GPU A/B below covers end to end.origin/mainand are unrelated:models/test_llava.py::test_llava_model_type_selects_image_text_definition:gptqmodel.models.autohas noAutoConfig.test_unlimited_ocr_support.py::test_unlimited_ocr_prepares_native_image_layout_and_retries: a cpu/cuda index tensor mismatch when a GPU is visible.test_calibration_data_device.pycases need at least 2 CUDA devices.test_locateanything_cleanroom_fixtures.pycases are HF greedy-id references recorded for sdpa on non-sliding variants only..agents/and docs.bash format/check.sh # ruff 0.14.2: All checks passed! (temporary no-op pip shim; venv ruff 0.14.2)Structural A/B 1: every MODEL_MAP definition, synthetic towers, CPU and CUDA
record_cast.pycovers all 210MODEL_MAPtypes; the unconverted ones act as controls.move_input_capture_examplewith a stubself.modelthat has a tower at the path the old override read. Tower variants:nn.Moduleand plain fp16nn.Module(nodtypeattribute);PreTrainedModel-like tower whose first parameter is int8 and whosedtypeproperty returns the first floating parameter;data_devicecpu and cuda:0.pixel_values/images/pixel_values_local/pixel_values_videos/input_features;image_grid_thw/video_grid_thw/image_sizes, a listimage_grid_hws, bool masks, a 1-Dposition_ids;Structural A/B 2: real vendored towers
record_real.pybuilds each converted model onmetafrom its repository fixture:native_parity/{llava,deepseek_vl,deepseek_ocr2}.json;tests/test_deepseek_vl_v2_local_port.py::tiny_config;checkpoint_config.json.It allocates only the old tower (
to_empty, then a bf16 or fp16 cast) on cpu or cuda:0. Then it feeds image, text-only and uint8-pixel rows withdata_devicecpu or cuda:0. Towers:CLIPVisionModel,SiglipVisionModel,DeepseekOcr2VisionModel,MoonViTPreTrainedModel(allPreTrainedModel), and deepseek_vl_v2's plain-nn.ModuleVisionTransformer.GPU A/B: bit-identical saved checkpoints
Setup:
CUDA_DEVICE_ORDER=PCI_BUS_ID,trust_remote_code=False.origin/main8d52eb9 and on this branch with an identical config (GPTQConfig(bits=4, group_size=128), FP16), seed and calibration rows.gptqmodel.__file__was printed in every log.quant_log.csv, compared as sorted rows withouttime.LlavaQModel, converted)On both trees, a probe of the same llava calibration row shows:
pixel_valuestorch.float32on cpu, shape(1, 3, 384, 384);move_input_capture_example, with the tower staged on cuda:0:torch.float16oncuda:0.So the declared cast is the one that runs in the GPU A/B.
Measured Impact
Not applicable. This is a behavior-preserving refactor and no issue is claimed. The GPU A/B is bit-identical.
Model Support / Module Plan
Not applicable. No
MODEL_MAPentry, module tree, checkpoint ownership or fusion declaration changes. Only the capture-input cast moves from five definitions intoBaseQModeldata.Model test node IDs and results: see Tests.
tests/models/test_{deepseek_ocr2,deepseek_vl,deepseek_vl_v2,llava,locateanything}.pynon-eval nodes passed on both trees, except the pre-existing llavaAutoConfigfailure.AWQ onboarding safety
Not applicable. No AWQ grouping, search, replay or fold change. The cast acts only on capture inputs, and AWQ sees the same capture.
Logic Flow Documentation
file:lineevidence.docs/repeats the diagrams, titled for the mechanism, and is linked fromREADME.md.flowchart TD A["StageInputsCapture / LoopedLayerCaptureMixin"] --> B["BaseQModel.move_input_capture_example"] B --> C["move tensors to data_device<br/>unsqueeze 1-D, tensor_kwargs cast"] C --> D["finalize_input_capture_example"] D --> E{"calibration_vision_module set and<br/>a calibration_vision_input_keys value is a tensor?"} E -- no --> H["example unchanged"] E -- yes --> F["resolve tower by attribute path<br/>dtype: tower.dtype, else first parameter"] F --> G{"dtype resolved?"} G -- yes --> I["value.to(first-parameter device<br/>or own device, dtype)"] G -- "no: no parameters, no dtype" --> H F -.->|"path missing: AttributeError, as before"| X["raise"]calibration_vision_module/calibration_vision_input_keysread inBaseQModel.move_input_capture_example(base.py:4591)finalize_input_capture_examplesuper()(which finalizes), then casttower.dtype, else first parameter; first-parameter device, else own device; any tensor; no dtype means no castimage_grid_hwsvision_devicepixel_values.device(same value)Review Requirements
I or an AI agent (such as Codex or Claude) have:
Kernel Accuracy Requirements
Not applicable. No kernel, packing or dispatch change.
Notes
gptqmodel/(−42). With tests and docs it is +45, because the strict same-rule scope converts 5 of the 18 overrides. The follow-up above would fold 4 more, roughly −60 lines, if the floating-only rule is accepted.git merge-treeagainst the stalerefactor/defs-declared-side-modulesbranch reports a conflict intests/test_gptaq_foem_first_capture_5831.py. That is only because refactor(definitions): declare calibration side modules instead of per-model hook pairs #6464 was squash-merged and this branch already contains it. Against currentorigin/mainthe branch is a fast-forward descendant.🤖 Generated with Claude Code