Skip to content

fix(writer): stop copying remote-code .py files when a native definition owns the config - #6287

Merged
Qubitium merged 2 commits into
mainfrom
fix/save-python-copy
Nov 29, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/save-python-copy

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Defect

save() kept the source checkpoint's auto_map and copied every source *.py file whenever the saved model_type was not registered in Transformers' CONFIG_MAPPING. That predicate ignores GPT-QModel definitions: local_model_loader_for_config() already selects the package config/model for Baichuan, MiniCPM and the other ports, so checkpoint Python can never replace the implementation. The exported Baichuan/MiniCPM checkpoint therefore carried a stale checkpoint_python.py and advertised trust_remote_code, letting the dropped remote class shadow the native one on reload.

Reproduced at 89dbcf91 (CPU):

tests/test_baichuan_tokenizer.py::test_real_tokenizer_public_awq_pack_save_reload  (4 cases)
E  AssertionError: assert not [PosixPath('.../awq-0/checkpoint_python.py')]
tests/test_minicpm_local_port.py::test_batched_gptq_save_and_reload_ignores_checkpoint_python

Predicate reused

local_model_loader_for_config() — the same definition-selection predicate used by load_model_config() and resolve_trust_remote_code(). A returned loader means the package owns the config/model, so the export must drop auto_map and must not copy checkpoint Python. The existing #6134 carve-out is retained for self-describing multimodal/processor-bearing ports (modality/require_load_processor), which keep the released metadata.

Changes

  • gptqmodel/models/writer.py
    • Add _remote_code_export_preserved() / _definition_remote_code_export() around the reused predicate.
    • _preserve_remote_code_config() now drops auto_map for definition-owned text exports even when trust_remote_code=True.
    • _save_model_configs_without_weights() returns the policy; save_quantized() uses it instead of self.trust_remote_code for the extra .py copy.
    • save_quantized_embeddings() uses the same policy and excludes .py from _copy_missing_checkpoint_files() when remote code is not exported.
  • tests/test_config_only_export.py
    • test_native_definition_save_drops_checkpoint_python_even_with_trust (negative).
    • test_unknown_remote_code_save_still_copies_checkpoint_python (positive control).

Red/green

Red at HEAD for the new negative node (same intended reason as the public repro):

E  AssertionError: assert not [PosixPath('/tmp/.../out/checkpoint_python.py')]

Green with the fix:

test_baichuan_tokenizer.py::test_real_tokenizer_public_awq_pack_save_reload 4 passed
test_minicpm_local_port.py::test_batched_gptq_save_and_reload_ignores_checkpoint_python 1 passed
tests/test_config_only_export.py 10 passed (both new nodes included)
tests/test_baichuan_tokenizer.py 89 passed
tests/test_minicpm_local_port.py 129 passed
other `ignores_checkpoint_python` local-port nodes 46 passed + 21 passed
writer/save suites 101 passed, 3 skipped (GPU-only lm_head quantization)
tests/test_vl_legacy_checkpoint_resave_4802.py 6 passed
ruff check gptqmodel/models/writer.py tests/test_config_only_export.py: All checks passed

No merge; draft only.

…ion owns the config

Reuse the loader's local_model_loader_for_config() predicate so a definition-owned text export drops the source auto_map and never copies checkpoint Python. Multimodal/processor-bearing ports keep the released metadata required by #6134. The unknown-model remote-code path still copies .py under explicit trust.
@Qubitium
Qubitium force-pushed the fix/save-python-copy branch from ca4e085 to e7309a0 Compare November 29, 2026 05:42
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Review: the core export fix is correct; two non-blocking gaps in the new policy helper.

Non-blocking

  • gptqmodel/models/writer.py:502-504: getattr(definition, "modality", None) and getattr(definition, "require_load_processor", False) assume plain class attributes. Qwen2_5_VLQModel defines both as @property (gptqmodel/models/definitions/qwen2_5_vl.py:27-33), so getattr on the class returns the property object: tuple(...) raises TypeError: 'property' object is not iterable, and the require_load_processor check would be truthy even for the text-only variant. Repro:

    from gptqmodel.models.writer import _definition_remote_code_export
    _definition_remote_code_export({"model_type": "qwen2_5_vl"})
    # TypeError: 'property' object is not iterable

    This is currently masked because both qwen2_5_vl types are native and _remote_code_export_preserved() returns at line 524 before calling the helper; a non-native alias or a reordered branch would break save() for that family.

  • gptqmodel/models/writer.py:524-525: the native short-circuit runs before the definition predicate, so a definition-owned native type with trust_remote_code=True still keeps auto_map and copies .py. Repro: Lfm2MoeForCausalLM + a source config.json with auto_map + _save_model_configs_without_weights(..., trust_remote_code=True) keeps auto_map and copies checkpoint_python.py. The public resolve_trust_remote_code() currently forces trust False for definition-owned configs, so this is not reachable through GPTQModel.load; but it contradicts the PR's stated rule ("a returned loader means ... must not copy checkpoint Python") and the negative test only covers a non-native type (minicpm).

Verified by running: the minicpm save node is red at origin/main (hostile.py copied) and green at the PR; the module-replacement path (save_quantized_embeddings) is red at main / green at PR; the unknown-type positive control exercises the trust branch; the new test_config_only_export.py suite passes.

…native trust

- Read modality/require_load_processor from plain class values, falling back
  to the base definition default when the subclass exposes an instance
  property. Qwen2.5-VL declares both as properties, so the export metadata
  helper raised TypeError before its native short-circuit masked it.
- Evaluate the definition-owned predicate before the Transformers-native
  branch so a native type with a package definition still drops checkpoint
  auto_map and .py files under trust_remote_code=True. Unknown model types
  keep the trust-controlled positive-control path.
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Addressed both non-blocking findings in a8ad15c (pushed to fix/save-python-copy).

  • Property metadata (writer.py:502-504): added _definition_class_value(), which walks the definition MRO, skips properties/other descriptors, and uses the nearest plain class value — so modality/require_load_processor resolve to the base definition defaults without constructing a model instance. _definition_remote_code_export({"model_type": "qwen2_5_vl"}) now returns (True, True) (the base Qwen2-VL multimodal/processor defaults) instead of raising TypeError: 'property' object is not iterable.
  • Native definition rule (writer.py:524-525): the definition-owned predicate is now evaluated first. A definition-owned type returns self_describing regardless of trust_remote_code, so Lfm2MoeForCausalLM with a source auto_map and trust_remote_code=True drops auto_map and copies no .py. Types with no package definition stay trust-controlled, so the unknown-type positive control still passes.

Tests: added tests/test_writer_remote_code_policy.py covering the property metadata, plain-attribute definitions, and the Lfm2Moe native-definition-wins repro. test_config_only_export.py (the PR's own suite) plus the new file: 13 passed. ruff check clean. Red evidence on e7309a0: the property repro raised TypeError, and the new tests were 2 failed / 1 passed.

@Qubitium

Copy link
Copy Markdown
Collaborator Author

Cross-validation note: this PR also fixes the four test_baichuan_tokenizer.py nodes that are red on origin/main.

origin/main 89dbcf9 this PR (a8ad15c9)
tests/test_baichuan_tokenizer.py 4 failed, 85 passed 89 passed
FAILED test_baichuan_tokenizer.py::test_real_tokenizer_public_awq_pack_save_reload[Baichuan-7B]
FAILED test_baichuan_tokenizer.py::test_real_tokenizer_public_awq_pack_save_reload[Baichuan-13B-Chat]
FAILED test_baichuan_tokenizer.py::test_real_tokenizer_public_awq_pack_save_reload[Baichuan2-7B-Chat]
FAILED test_baichuan_tokenizer.py::test_real_tokenizer_public_awq_pack_save_reload[Baichuan2-13B-Chat]

Same mechanism as the minicpm node: the hostile checkpoint ships auto_map + checkpoint_python.py, and a native definition now owns the config, so the export must not copy the checkpoint's Python. Baichuan is a native definition, so the new predicate covers it. No code change was needed for this — it falls out of the fix as written.

Command:

cd tests && CUDA_VISIBLE_DEVICES="" PYTHONPATH=<clone>:<clone>/tests \
  /root/venv/bin/python -m pytest test_baichuan_tokenizer.py -q -p no:cacheprovider

@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 08:10
@Qubitium
Qubitium merged commit 477f69d into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/save-python-copy branch November 29, 2026 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant