Repository navigation
fix(security): gate checkpoint code imports on trust_remote_code (#3968) - #3981
Merged
Merged
Conversation
trust_remote_code=False was bypassed on load, save and dequantize because auto_map data, the compressed-tensors format, and the writer's re-save path all imported Python from the snapshot. - prepare_remote_model_init_compat() and normalize_hf_config_compat() now require the caller's trust_remote_code; auto_map alone no longer enables remote-code imports or compat patches. Loader call sites forward the flag. - get_model_with_quantize() uses self.trust_remote_code instead of True. - dequantize_model() takes trust_remote_code=False and threads it through the compressed-tensors discovery path; the CLI exposes it. - Processor loads use self.trust_remote_code; Kimi K3 reference loading requires trust_remote_code=True. Adds a checkpoint-based regression test whose auto_map modules write a marker file; the marker must stay absent after load, save and dequantize.
8 tasks done
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.
Fixes #3968
TL;DR
trust_remote_code=Falseno longer executes checkpoint Python on load, save or dequantize.auto_map,quant_method=compressed-tensorsand the re-save path are treated as checkpoint data, not consent. Net: 28 files, +434/-52 (204 lines are the new regression test and docs).1. The problem
A checkpoint could carry a native
model_type(so it loads as a built-in config and a native QModel) plus anauto_mapentry. The existingrequire_trust_remote_codegate never fired because the native class was selected, but the snapshot module was still imported:auto_mapimportprepare_remote_model_init_compat()calledget_class_from_dynamic_module(class_ref, model_path)with no trust check (gptqmodel/utils/hf.py:1900-1923); reached fromloader.py:1789/1801and1307/1313auto_mapcompat patchesnormalize_hf_config_compat()enabled remote-code shims wheneverauto_mapexisted (gptqmodel/utils/hf.py:1729-1735)get_model_with_quantize()calledAutoConfig.from_pretrained(..., trust_remote_code=True)(gptqmodel/models/writer.py:2140-2147)True(gptqmodel/utils/model_dequant.py:335,354)base.py:746usedself.require_trust_remote_code; severalload_processor()overrides hard-codedTrueload_calibration_reference()execed snapshot files with no opt-in (gptqmodel/utils/kimi_k3.py:24-57)2. The new flow
flowchart TD A["GPTQModel.load / from_quantized"] --> B["resolve_trust_remote_code(model_local_path)"] B --> C["AutoConfig.from_pretrained(trust_remote_code=trust_remote_code)"] C --> D["normalize_hf_config_compat(config, trust_remote_code)"] D --> E{"prepare_remote_model_init_compat:<br/>trust_remote_code?"} E -- "False" --> F["return before auto_map import"] E -- "True" --> G["get_class_from_dynamic_module(class_ref, model_path)"] G --> H["remote model init compat patches"] F --> I["native QModel / built-in config"] H --> I E -. "fail-closed path<br/>auto_map is data only" .-> F style E fill:#ffe0b2,stroke:#e65100 style F fill:#ffcdd2,stroke:#b71c1cSave:
flowchart TD A["save() / save_quantized()"] --> B{"self.load_quantized_model?"} B -- "yes" --> C["get_model_with_quantize(qcfg, model_local_path)"] C --> D["AutoConfig.from_pretrained(trust_remote_code=self.trust_remote_code)"] D --> E{"trust_remote_code?"} E -- "False" --> F["built-in config; auto_map AutoConfig not imported"] E -- "True" --> G["auto_map AutoConfig imported"] B -- "no" --> H["write saved config and shards"] F --> H G --> H style E fill:#ffe0b2,stroke:#e65100 style F fill:#ffcdd2,stroke:#b71c1cDequantize:
flowchart TD A["dequantize_model(..., trust_remote_code=False)"] --> B["detect_format(model_path, config)"] B --> C{"format == compressed-pack?"} C -- "no" --> D["shard converters"] C -- "yes" --> E["_prepare_compressed_tensors_context(trust_remote_code)"] E --> F["_discover_compressed_tensors_module_schemes(trust_remote_code)"] F --> G["AutoConfig.from_pretrained(trust_remote_code)"] G --> H{"trust_remote_code?"} H -- "False" --> I["built-in config; auto_map not imported"] H -- "True" --> J["auto_map AutoConfig imported"] I --> K["loader.from_config(trust_remote_code=False)"] J --> L["loader.from_config(trust_remote_code=True)"] style H fill:#ffe0b2,stroke:#e65100 style I fill:#ffcdd2,stroke:#b71c1c3. The fix
prepare_remote_model_init_compat(..., *, trust_remote_code=False)returns beforeget_class_from_dynamic_module()unless trust is on. This also covers transformers' cross-reporepo--module.Classform.normalize_hf_config_compat()returns after the RoPE backfill when trust is off;auto_mapalone no longer enables remote-code compat patches.gptqmodel/models/loader.py:1313,1803).get_model_with_quantize()usesself.trust_remote_code(gptqmodel/models/writer.py:2140-2147).dequantize_model(..., trust_remote_code=False)threads the flag through_prepare_compressed_tensors_context()/_discover_compressed_tensors_module_schemes()(gptqmodel/utils/model_dequant.py:1766).scripts/dequantize_model.pyexposes--trust-remote-code.BaseQModel.__init__()and everyload_processor()override useself.trust_remote_code(gptqmodel/models/base.py:746).KimiK3Source/load_calibration_reference()requiretrust_remote_code=True(gptqmodel/utils/kimi_k3.py:24,92);KimiK31Sourcedoes the same.4. Before/after evidence
Regression checkpoint: native
model_type=llama+auto_map.AutoConfigandauto_map.AutoModelForCausalLMmodules that writeEXECUTEDinto the checkpoint directory.trust_remote_code=Falsethroughout.Unmodified
origin/main(93d3ae7, clean worktree/tmp/gptq-base-3968awith only the new test copied in):This branch (
771a45e2):The five tests cover load, save, dequantize (marker absent with trust off) plus opt-in import for the loader and dequantize paths (marker present with
trust_remote_code=True).5. Related tests
CUDA (
cu130t), branch:The same six failures and one teardown error reproduce on unmodified
origin/mainwith the identical selection (6 failed, 121 passed, 14 skipped, 1 error):test_normalize_hf_config_compat_restores_legacy_cache_length_helpers,test_resolve_trust_remote_code_overrides_when_native_support_exists, threetest_generic_wrapper_crops_padded_output_and_preserves_input_dtypecases,test_ignored_layers_are_honored_by_non_fp8_converters, and thetest_locateanything_prepare_dataset_uses_remote_processorteardown error. They are pre-existing and unrelated to this diff.ROCm (
rocm101t), branch:Same pre-existing failure names as above.
tests/test_kimi_k3_support.py(16 passed),tests/test_kimi_k31.py(10 passed), andtests/test_dequant_launch_config.py(14 passed) are clean.6. Documentation
The committed reference page is
docs/trust-remote-code-isolation.md, linked from the inference section ofREADME.md. It carries the same three diagrams and the fail-closed contract.7. Hardware and limitation
GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae(RTX 4090),/opt/venvs/cu130t/bin/python(torch 2.16.0.dev+cu130, Python 3.14t).HIP_VISIBLE_DEVICES=0(R9700),/opt/venvs/rocm101t/bin/python(torch 2.16.0.dev+rocm10.1).compressed_tensorsis not installed in either venv, so the dequantize regression injects the dependency boundary (_get_compressed_tensors_dependencies) while using the real transformersAutoConfig/ model classes. The trust gate, theauto_mapimport sink, and thefrom_configcall are the shipped ones; only the compressed-tensors plugin objects are fakes. The CLI flag is verified with--help.