* Scan auto_map for GGUF-only repo ids in the consent gate The trust_remote_code consent gate treated any repo classified GGUF-only (ships .gguf, no transformers-loadable weight) as having no remote code, so _config_has_auto_map returned False even when a config declared an auto_map and the repo shipped the referenced .py. The evaluator then skipped the scan/fingerprint for that target entirely. GGUF-inertness is a property of the loader, not the repo. A GGUF selection loads via llama.cpp, which never reads config.json/auto_map, and that case is already short-circuited upstream by the caller's is_gguf check (the inference route skips the remote-code preflight for a GGUF load). Every path that reaches this helper (export, training, non-GGUF inference) loads through transformers/Unsloth from_pretrained, which DOES import auto_map even for a repo that only ships .gguf weights: the custom module runs before from_pretrained fails on the missing transformers weights. The export path has no is_gguf guard and passes the source straight to FastLanguageModel.from_pretrained(trust_remote_code=True), so the in-helper GGUF skip let a repo with config.json (auto_map) + modeling_x.py + only a .gguf run unreviewed code during export. Drop the redundant repo-level GGUF short-circuit (and the now-unused _is_gguf_repo helper). A direct .gguf file reference stays inert via _is_direct_gguf_file_ref because that genuinely is a single-file llama.cpp load; repo ids are always scanned. A GGUF repo whose auto_map ships no .py still allows via the existing empty-code path, so legitimate GGUF loads are unaffected (and GGUF inference never reaches this helper at all). Only a repo that actually contains a .gguf can change behavior here; non-GGUF repos (safetensors, MLX) are byte-identical before and after. Update the GGUF auto_map test to expect a scan, and add two regression tests: a GGUF-only repo shipping auto_map Python is scanned and blocked, and a transformers-style repo (safetensors / MLX .npz) with auto_map stays scanned and blocked. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove trust_remote_code config defaults; consent dialog is the only enabler trust_remote_code is a per-load decision that must go through the remote-code consent dialog, which scans the auto_map code and pins the exact version. Two pre-set paths could still enable it without the user reviewing any code, and the GGUF consent bypass rode one of them into the export flow: - 4 model_defaults YAMLs shipped trust_remote_code: true (GLM-4.7-Flash, Nemotron-3-Nano-30B-A3B, PaddleOCR-VL, ERNIE-4.5-VL). - The frontend consent hook silently enabled trust_remote_code on a clean scan whenever the caller flagged the model as needing it. Remove every trust_remote_code key from the model_defaults YAMLs (the loaders already default to False when the key is absent) and delete the frontend silent auto-enable, so trust_remote_code is only turned on after the user approves the scanned code in the dialog. The three models that genuinely run custom code ship auto_map, which the consent gate detects on its own via _config_has_auto_map, so the dialog still fires for them in inference, training, and export (Nemotron is also re-granted by the trusted-org auto-enable in the workers). GLM-4.7-Flash has no auto_map: glm4_moe_lite is native in transformers 5.0+ and it loads with trust_remote_code=False, so its YAML flag was a no-op. Adds test_yaml_trust_remote_code_removed.py. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Drop YAML sections emptied by trust_remote_code removal Removing trust_remote_code from a model YAML whose section had no other key left a bare `inference:` header, which PyYAML parses as None; load_inference_config() then does `model_config.get("inference", {}).get(...)` and crashes on the None. Drop those now-empty section headers (24 model defaults, all the `inference:` section) so callers fall back to family/default inference params, which is the same result those models had before (their only inference override was trust_remote_code). Strengthens test_yaml_trust_remote_code_removed.py to forbid any empty/None top-level section and to load the affected models' inference config end to end. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add sweep asserting every model YAML loads via training + inference paths Loads all model_defaults YAMLs through load_model_defaults (training) and load_inference_config (inference) with the exact .get() access patterns the routes use, so a malformed/None section that crashes either loader is caught. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Assert ex-TRC auto_map models still surface the consent dialog Removing the trust_remote_code YAML default must not suppress the dialog for the models that genuinely run custom code. The dialog is driven by the repo's auto_map (via preflight_remote_code_consent_for_targets -> _config_has_auto_map), not the YAML flag, so Nemotron/PaddleOCR-VL/ERNIE-4.5-VL still require consent; GLM-4.7-Flash (no auto_map) takes no dialog and loads natively. Mocks only the Hub config + .py reader. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Tighten comments in consent-gate changes * Trim comments to be more succinct --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han <michaelhan2050@gmail.com>
163 lines
6.8 KiB
Python
163 lines
6.8 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
|
|
|
|
"""Regression: model-default YAMLs must not pre-set trust_remote_code.
|
|
|
|
It is a per-load decision made through the consent dialog (which scans and pins the
|
|
auto_map code), never a config default -- a YAML flag would re-open the no-review
|
|
bypass. Models that run custom code ship auto_map, so the dialog still fires without it.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
_CONFIGS = Path(__file__).resolve().parent.parent / "assets" / "configs"
|
|
_MODEL_DEFAULTS = _CONFIGS / "model_defaults"
|
|
|
|
|
|
def test_no_model_default_yaml_sets_trust_remote_code():
|
|
offenders = []
|
|
for f in _MODEL_DEFAULTS.rglob("*.yaml"):
|
|
doc = yaml.safe_load(f.read_text()) or {}
|
|
if not isinstance(doc, dict):
|
|
continue
|
|
for section, body in doc.items():
|
|
if isinstance(body, dict) and "trust_remote_code" in body:
|
|
offenders.append(
|
|
f"{f.relative_to(_CONFIGS)} [{section}={body['trust_remote_code']}]"
|
|
)
|
|
assert not offenders, (
|
|
"trust_remote_code must not be pre-set in model defaults; it is enabled only via "
|
|
f"the consent dialog. Remove it from: {offenders}"
|
|
)
|
|
|
|
|
|
def test_no_model_default_yaml_has_empty_or_none_section():
|
|
# A bare `inference:` header (no keys) parses to None and crashes the .get() loaders.
|
|
offenders = []
|
|
for f in _MODEL_DEFAULTS.rglob("*.yaml"):
|
|
doc = yaml.safe_load(f.read_text())
|
|
if not isinstance(doc, dict):
|
|
offenders.append(f"{f.relative_to(_CONFIGS)} (not a mapping)")
|
|
continue
|
|
for section, body in doc.items():
|
|
if body is None or (isinstance(body, dict) and not body):
|
|
offenders.append(f"{f.relative_to(_CONFIGS)} [{section}]")
|
|
assert not offenders, (
|
|
"empty/None YAML section would crash the config loaders; drop the bare section "
|
|
f"header instead. Offending: {offenders}"
|
|
)
|
|
|
|
|
|
def test_formerly_flagged_models_load_inference_config_without_crash():
|
|
# Models whose inference section was emptied by the TRC removal must still load.
|
|
from utils.inference import load_inference_config
|
|
for model in (
|
|
"tiiuae/Falcon-H1-0.5B-Instruct",
|
|
"unsloth/Llama-3.2-1B-Instruct",
|
|
"unsloth/Qwen2.5-7B",
|
|
):
|
|
cfg = load_inference_config(model)
|
|
assert isinstance(cfg, dict)
|
|
assert cfg.get("trust_remote_code", False) is False
|
|
|
|
|
|
def test_all_model_yamls_load_for_training_and_inference():
|
|
# Every YAML must load through both config paths (training + inference) as the routes do.
|
|
from utils.inference import load_inference_config
|
|
from utils.models.model_config import load_model_defaults
|
|
|
|
infer_keys = {
|
|
"temperature",
|
|
"top_p",
|
|
"top_k",
|
|
"min_p",
|
|
"presence_penalty",
|
|
"trust_remote_code",
|
|
}
|
|
failures = []
|
|
for f in sorted(_MODEL_DEFAULTS.rglob("*.yaml")):
|
|
stem = f.stem
|
|
try:
|
|
md = load_model_defaults(stem)
|
|
assert isinstance(md, dict), f"load_model_defaults -> {type(md).__name__}"
|
|
assert not [k for k, v in md.items() if v is None], "has a None section"
|
|
# the dict sections the loaders read via .get('sect', {}).get(...)
|
|
for sect in ("training", "inference", "lora", "logging"):
|
|
assert isinstance(md.get(sect, {}), dict), f"{sect!r} is not a mapping"
|
|
md.get("training", {}).get("trust_remote_code", False) # routes/training.py:263
|
|
cfg = load_inference_config(stem)
|
|
assert infer_keys <= set(cfg), f"inference config missing {infer_keys - set(cfg)}"
|
|
except Exception as e: # noqa: BLE001 - aggregate so one failure does not hide others
|
|
failures.append(f"{f.relative_to(_CONFIGS)}: {type(e).__name__}: {e}")
|
|
assert not failures, "YAML config loaders crashed on: " + "; ".join(failures)
|
|
|
|
|
|
def test_base_templates_have_no_trust_remote_code():
|
|
for name in ("full_finetune.yaml", "lora_text.yaml", "vision_lora.yaml"):
|
|
doc = yaml.safe_load((_CONFIGS / name).read_text()) or {}
|
|
flat = yaml.safe_dump(doc)
|
|
assert "trust_remote_code" not in flat, f"{name} should not set trust_remote_code"
|
|
|
|
|
|
def test_loader_defaults_trust_remote_code_off_for_formerly_flagged_models():
|
|
# The 4 models that used to ship trust_remote_code: true must now report no default.
|
|
from utils.models.model_config import load_model_defaults
|
|
for model in (
|
|
"unsloth/GLM-4.7-Flash",
|
|
"unsloth/Nemotron-3-Nano-30B-A3B",
|
|
"unsloth/PaddleOCR-VL",
|
|
"unsloth/ERNIE-4.5-VL-28B-A3B-PT",
|
|
):
|
|
d = load_model_defaults(model)
|
|
for section in ("training", "inference"):
|
|
assert not (d.get(section) or {}).get(
|
|
"trust_remote_code", False
|
|
), f"{model} [{section}] still carries a trust_remote_code default"
|
|
|
|
|
|
def test_formerly_flagged_auto_map_models_still_require_consent_dialog():
|
|
# Crux: an auto_map model must STILL surface the dialog (driven by auto_map, not the
|
|
# YAML flag). Real backend path, mocking only the Hub json + .py fetch.
|
|
from unittest.mock import patch
|
|
from utils.security import consent, preflight_remote_code_consent_for_targets
|
|
|
|
auto_map_cfg = [
|
|
{
|
|
"auto_map": {
|
|
"AutoConfig": "configuration_x.XConfig",
|
|
"AutoModelForCausalLM": "modeling_x.XForCausalLM",
|
|
}
|
|
}
|
|
]
|
|
benign_py = {"modeling_x.py": "class XForCausalLM:\n pass\n"}
|
|
for model in (
|
|
"unsloth/Nemotron-3-Nano-30B-A3B",
|
|
"unsloth/PaddleOCR-VL",
|
|
"unsloth/ERNIE-4.5-VL-28B-A3B-PT",
|
|
):
|
|
with (
|
|
patch.object(consent, "_load_remote_code_configs", return_value = auto_map_cfg),
|
|
patch.object(consent, "repo_remote_code_files", return_value = benign_py),
|
|
):
|
|
decision = preflight_remote_code_consent_for_targets([model], hf_token = None)
|
|
# routes/models.py opens the dialog from decision.has_remote_code.
|
|
assert decision.has_remote_code is True, (
|
|
f"{model} ships auto_map but the consent scan did not flag it -> dialog would "
|
|
"not fire"
|
|
)
|
|
|
|
|
|
def test_no_auto_map_model_takes_no_dialog():
|
|
# Flip side: GLM-4.7-Flash ships no auto_map -> no dialog; its old YAML flag was a no-op.
|
|
from unittest.mock import patch
|
|
from utils.security import consent, preflight_remote_code_consent_for_targets
|
|
|
|
with patch.object(
|
|
consent, "_load_remote_code_configs", return_value = [{"model_type": "glm4_moe_lite"}]
|
|
):
|
|
decision = preflight_remote_code_consent_for_targets(
|
|
["unsloth/GLM-4.7-Flash"], hf_token = None
|
|
)
|
|
assert decision.has_remote_code is False
|