Skip to content

armgit / Soup

Public Imported from GitHub
0

Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.

fix: thread --trust-remote-code to every load it was silently dropped from (#1456)

Y.B.5068bb6cea49
diff --git a/changelog.d/0.75.1/1456.fixed.md b/changelog.d/0.75.1/1456.fixed.md
new file mode 100644
index 00000000..5d38b6a0
--- /dev/null
+++ b/changelog.d/0.75.1/1456.fixed.md
@@ -0,0 +1,16 @@
+- **`--trust-remote-code` is now threaded through on the five paths that dropped it (#1449 by @drakeo338 in #1456).**
+ `soup data download` forwards the flag to `load_dataset` and only prints the
+ "Remote Code Warning" panel when it is set. `soup eval auto` forwards it to
+ both `benchmark()` (appended to the lm-eval `model_args` from the resolved
+ flag only, never from `adapter_config.json`) and `custom()`, which now
+ passes it to the `pipeline(...)` load. `soup train --find-lr`'s live sweep
+ loads the tokenizer and model with the resolved flag instead of a hardcoded
+ `False`. `soup draft distill` and `soup shrink` (heal step) now append
+ `--trust-remote-code` to the child `soup train` argv when it was resolved
+ true, instead of asking the user to re-run with a flag they already passed.
+ On `datasets>=4`, which dropped `trust_remote_code` support upstream and
+ now just logs an error and ignores it, `soup data download
+ --trust-remote-code` is refused outright instead of silently forwarding a
+ flag that would do nothing.
+ `soup eval benchmark` and `soup eval custom` gain their own
+ `--trust-remote-code` option (off by default).
diff --git a/docs/performance-and-quantization.md b/docs/performance-and-quantization.md
index 522a0e69..2e8d8686 100644
--- a/docs/performance-and-quantization.md
+++ b/docs/performance-and-quantization.md
@@ -866,6 +866,9 @@ Coverage:
- `soup train` (every task — SFT, DPO, GRPO, KTO, ORPO, SimPO, IPO, PPO, Reward Model, Pretrain, Embedding, BCO, and the unified Preference dispatcher)
- `soup chat`, `soup serve`, `soup data download`, `soup eval auto`
- `soup diff`, `soup export`, `soup merge`, `soup infer`, `soup data generate`
+- `soup eval benchmark`, `soup eval custom`, `soup train --find-lr`, `soup draft distill`, `soup shrink`
+
+`soup data download --trust-remote-code` is refused when the installed `datasets` is 4 or newer, because those versions no longer support remote dataset code and would silently ignore the flag.
```bash
soup train --config soup.yaml --trust-remote-code
diff --git a/src/soup_cli/commands/data.py b/src/soup_cli/commands/data.py
index c8ed3963..65d692fc 100644
--- a/src/soup_cli/commands/data.py
+++ b/src/soup_cli/commands/data.py
@@ -1225,17 +1225,32 @@ def _hf_dataset_info(dataset_id: str) -> dict:
}
+def _datasets_major_version() -> int | None:
+ """Return the installed ``datasets`` package's major version, or None if unreadable."""
+ import re
+
+ try:
+ import datasets
+ except ImportError:
+ return None
+
+ match = re.match(r"(\d+)", str(getattr(datasets, "__version__", "")))
+ return int(match.group(1)) if match else None
+
+
def _hf_download_dataset(
dataset_id: str,
split: str = "train",
samples: int | None = None,
+ trust_remote_code: bool = False,
) -> list[dict]:
"""Download a dataset from HuggingFace Hub and return as list of dicts."""
from datasets import load_dataset
try:
ds = load_dataset(
- dataset_id, split=split, streaming=True, trust_remote_code=False,
+ dataset_id, split=split, streaming=True,
+ trust_remote_code=trust_remote_code,
)
except Exception as exc:
raise ValueError(f"Failed to load dataset {dataset_id}: {exc}") from exc
@@ -1521,20 +1536,34 @@ def download_dataset(
)
raise typer.Exit(1)
- from rich.panel import Panel
+ if trust_remote_code:
+ datasets_major = _datasets_major_version()
+ if datasets_major is not None and datasets_major >= 4:
+ console.print(
+ "[red]--trust-remote-code is refused: the installed "
+ f"datasets package (v{datasets_major}.x) dropped "
+ "trust_remote_code support upstream, so it would be silently "
+ "ignored rather than doing what you asked. Install "
+ "datasets<4 if this dataset needs its remote loading "
+ "script, or drop --trust-remote-code if it doesn't.[/]"
+ )
+ raise typer.Exit(1)
- console.print(Panel(
- "[bold yellow]Warning:[/] Downloading this dataset may execute a "
- "remote dataset loading script from HuggingFace Hub.\n\n"
- "Only download datasets from sources you trust.",
- title="Remote Code Warning",
- border_style="yellow",
- ))
+ from rich.panel import Panel
+
+ console.print(Panel(
+ "[bold yellow]Warning:[/] Downloading this dataset may execute a "
+ "remote dataset loading script from HuggingFace Hub.\n\n"
+ "Only download datasets from sources you trust.",
+ title="Remote Code Warning",
+ border_style="yellow",
+ ))
console.print(f"[dim]Downloading {dataset_id} (split={split})...[/]")
try:
data = _hf_download_dataset(
dataset_id, split=split, samples=samples,
+ trust_remote_code=trust_remote_code,
)
except ValueError as exc:
console.print(f"[red]{exc}[/]")
diff --git a/src/soup_cli/commands/draft.py b/src/soup_cli/commands/draft.py
index 612b2f88..f115ffc0 100644
--- a/src/soup_cli/commands/draft.py
+++ b/src/soup_cli/commands/draft.py
@@ -513,6 +513,8 @@ def _run_distill(
str(config_path),
"--yes",
]
+ if trc:
+ argv.append("--trust-remote-code")
try:
result = subprocess.run( # noqa: S603 — argv list, no shell.
argv,
diff --git a/src/soup_cli/commands/eval.py b/src/soup_cli/commands/eval.py
index 3b6fb86e..859f761f 100644
--- a/src/soup_cli/commands/eval.py
+++ b/src/soup_cli/commands/eval.py
@@ -77,6 +77,14 @@ def benchmark(
None, "--device",
help="Device: cuda, mps, cpu. Auto-detected if not set.",
),
+ trust_remote_code: bool = typer.Option(
+ False,
+ "--trust-remote-code",
+ help=(
+ "Allow loading models that ship custom Python via auto_map. "
+ "Default deny (v0.36.0). Only enable if you trust the source."
+ ),
+ ),
):
"""Evaluate on standard benchmarks (wraps lm-evaluation-harness)."""
model_path = Path(model)
@@ -108,6 +116,12 @@ def benchmark(
else:
model_arg = f"pretrained={model_path}"
+ if trust_remote_code:
+ # Appended only from the resolved --trust-remote-code flag, never
+ # from adapter_config.json: _reject_lm_eval_injection above already
+ # refused a base_model_name_or_path smuggling its own ','/'=' pair.
+ model_arg = f"{model_arg},trust_remote_code=True"
+
benchmark_list = [b.strip() for b in benchmarks.split(",")]
console.print(f"[dim]Evaluating on: {', '.join(benchmark_list)}[/]")
@@ -336,6 +350,14 @@ def custom(
"--attach-to-registry (v0.40.1 / G10)."
),
),
+ trust_remote_code: bool = typer.Option(
+ False,
+ "--trust-remote-code",
+ help=(
+ "Allow loading models that ship custom Python via auto_map. "
+ "Default deny (v0.36.0). Only enable if you trust the source."
+ ),
+ ),
):
"""Run custom evaluation tasks from a JSONL file."""
from soup_cli.eval.custom import load_eval_tasks
@@ -379,7 +401,9 @@ def custom(
from soup_cli.eval.custom import _create_default_generator, score_task
console.print("[dim]Loading model...[/]")
- generate_fn = _create_default_generator(str(model_path))
+ generate_fn = _create_default_generator(
+ str(model_path), trust_remote_code=trust_remote_code,
+ )
with progress:
task_bar = progress.add_task(
@@ -692,6 +716,7 @@ def auto(
batch_size=8,
run_id=None,
device=None,
+ trust_remote_code=trust_remote_code,
)
except (typer.Exit, SystemExit):
# benchmark() signals failure with typer.Exit (a RuntimeError, NOT
@@ -715,6 +740,7 @@ def auto(
run_id=None,
attach_to_registry=None,
output=None,
+ trust_remote_code=trust_remote_code,
)
except SystemExit:
console.print("[yellow]Custom eval skipped (see above).[/]")
diff --git a/src/soup_cli/commands/shrink.py b/src/soup_cli/commands/shrink.py
index 881f7421..1ac4dea0 100644
--- a/src/soup_cli/commands/shrink.py
+++ b/src/soup_cli/commands/shrink.py
@@ -577,6 +577,8 @@ def _run_heal(
str(config_path),
"--yes",
]
+ if trc:
+ argv.append("--trust-remote-code")
try:
result = subprocess.run( # noqa: S603 — argv list, no shell.
argv, capture_output=True, check=False, timeout=_HEAL_TIMEOUT_SECONDS, env=env
diff --git a/src/soup_cli/commands/train.py b/src/soup_cli/commands/train.py
index 0087d3e0..42947fe2 100644
--- a/src/soup_cli/commands/train.py
+++ b/src/soup_cli/commands/train.py
@@ -715,7 +715,7 @@ def train(
# --find-lr-steps (a short dataset, or a loss that went non-finite).
try:
lrs, losses_for_report = _run_live_lr_sweep(
- config_path, schedule,
+ config_path, schedule, trust_remote_code=trust_remote_code,
)
except (SweepTooShortError, LrSweepUnavailableError) as exc:
console.print(f"[red]{markup_escape(str(exc))}[/]")
@@ -2332,7 +2332,7 @@ def _resolve_resume_or_exit(resume: str, cfg: "SoupConfig") -> str | None:
def _run_live_lr_sweep(
- config_path: str, schedule: list[float],
+ config_path: str, schedule: list[float], trust_remote_code: bool = False,
) -> tuple[list[float], list[float]]:
"""v0.33.0 #56 — run the in-process LR sweep over the config's own model.
@@ -2357,7 +2357,9 @@ def _run_live_lr_sweep(
) from exc
try:
- return _live_lr_sweep_from_config(cfg, schedule)
+ return _live_lr_sweep_from_config(
+ cfg, schedule, trust_remote_code=trust_remote_code,
+ )
except SweepTooShortError:
raise
except Exception as exc: # noqa: BLE001 — the cause is the useful half
@@ -2378,7 +2380,7 @@ def _lr_finder_dataset_path(train) -> str:
def _live_lr_sweep_from_config(
- cfg, schedule: list[float],
+ cfg, schedule: list[float], trust_remote_code: bool = False,
) -> tuple[list[float], list[float]]:
"""Build a tiny in-process loop: load model + tokenizer + a slice of
the train dataset, then call :func:`run_lr_sweep`. Returns the
@@ -2420,12 +2422,12 @@ def _live_lr_sweep_from_config(
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained(
- cfg.base, trust_remote_code=False,
+ cfg.base, trust_remote_code=trust_remote_code,
)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(
- cfg.base, trust_remote_code=False,
+ cfg.base, trust_remote_code=trust_remote_code,
).to(device)
model.train()
diff --git a/src/soup_cli/eval/custom.py b/src/soup_cli/eval/custom.py
index 201405c5..bf4bbbbe 100644
--- a/src/soup_cli/eval/custom.py
+++ b/src/soup_cli/eval/custom.py
@@ -348,7 +348,7 @@ def run_eval(
return eval_results
-def _create_default_generator(model_path: str):
+def _create_default_generator(model_path: str, trust_remote_code: bool = False):
"""Create a default text generation function using transformers pipeline."""
from rich.console import Console
from rich.panel import Panel
@@ -367,6 +367,7 @@ def _create_default_generator(model_path: str):
model=model_path,
max_new_tokens=512,
do_sample=False,
+ trust_remote_code=trust_remote_code,
)
def generate(prompt: str) -> str:
diff --git a/src/soup_cli/monitoring/callback.py b/src/soup_cli/monitoring/callback.py
index 32c29fbf..a63793e7 100644
--- a/src/soup_cli/monitoring/callback.py
+++ b/src/soup_cli/monitoring/callback.py
@@ -578,6 +578,7 @@ class _SoupTrainerCallback_body: # noqa: N801
batch_size=8,
run_id=self.run_id,
device=None,
+ trust_remote_code=False,
)
except Exception as exc:
logger.exception("Auto-eval benchmark failed")
@@ -594,6 +595,7 @@ class _SoupTrainerCallback_body: # noqa: N801
run_id=self.run_id,
attach_to_registry=None,
output=None,
+ trust_remote_code=False,
)
except Exception as exc:
logger.exception("Auto-eval custom failed")
diff --git a/tests/test_issue1449_trust_remote_code_dropped.py b/tests/test_issue1449_trust_remote_code_dropped.py
new file mode 100644
index 00000000..babf3c04
--- /dev/null
+++ b/tests/test_issue1449_trust_remote_code_dropped.py
@@ -0,0 +1,556 @@
+"""#1449 — ``--trust-remote-code`` is accepted but dropped on five paths.
+
+``soup data download`` and ``soup eval auto`` never read the parameter at all.
+``soup train --find-lr`` hardcodes ``trust_remote_code=False`` in the live-sweep
+loaders. ``soup draft distill`` and ``soup shrink`` (heal step) resolve the flag
+in the parent but never pass it to the child ``soup train`` subprocess. In every
+case, opting in changes nothing: the load that needs the flag never receives it.
+
+Each test below mirrors the issue's own stub-based repro: the real load call is
+replaced with a stub that records the kwargs/argv it received, so the assertion
+is on what actually reaches the consumer, not on whether a real model loads.
+"""
+
+from __future__ import annotations
+
+import json
+import subprocess
+import sys
+import types
+
+import pytest
+from typer.testing import CliRunner
+
+from tests.conftest import strip_ansi
+
+runner = CliRunner()
+
+
+def _flat(text: str) -> str:
+ return " ".join(strip_ansi(text).split())
+
+
+# ─── 1. soup data download -> _hf_download_dataset -> load_dataset ───
+
+
+class TestDataDownloadTrust:
+ def test_trust_remote_code_reaches_load_dataset(self, tmp_path, monkeypatch):
+ import datasets
+
+ import soup_cli.commands.data as data_cmd
+ from soup_cli.cli import app
+
+ monkeypatch.chdir(tmp_path)
+ # datasets<4 still honors trust_remote_code, so the version gate
+ # (below) must let it through and the flag must reach load_dataset.
+ monkeypatch.setattr(datasets, "__version__", "3.6.0")
+ seen = []
+ monkeypatch.setattr(
+ datasets, "load_dataset",
+ lambda *a, **k: seen.append(k) or iter([{"text": "x"}]),
+ )
+ result = runner.invoke(
+ app,
+ ["data", "download", "org/ds", "-n", "1", "-o", "out.jsonl",
+ "--trust-remote-code"],
+ )
+ assert result.exit_code == 0, result.output
+ assert seen, "load_dataset was never called"
+ assert seen[0].get("trust_remote_code") is True, seen[0]
+ assert data_cmd._datasets_major_version() == 3
+
+ def test_without_flag_load_dataset_gets_false(self, tmp_path, monkeypatch):
+ import datasets
+
+ from soup_cli.cli import app
+
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setattr(datasets, "__version__", "3.6.0")
+ seen = []
+ monkeypatch.setattr(
+ datasets, "load_dataset",
+ lambda *a, **k: seen.append(k) or iter([{"text": "x"}]),
+ )
+ result = runner.invoke(
+ app, ["data", "download", "org/ds", "-n", "1", "-o", "out2.jsonl"],
+ )
+ assert result.exit_code == 0, result.output
+ assert seen[0].get("trust_remote_code") is False, seen[0]
+
+ def test_trust_remote_code_is_refused_on_datasets_v4_plus(
+ self, tmp_path, monkeypatch,
+ ):
+ """On datasets>=4, `load_dataset` pops `trust_remote_code` and only
+ logs an error (it no longer supports it at all) — so forwarding the
+ flag there would silently do nothing, same bug as before this fix,
+ one layer down. The CLI must refuse instead of forwarding it."""
+ import datasets
+
+ from soup_cli.cli import app
+
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setattr(datasets, "__version__", "5.0.1")
+ seen = []
+ monkeypatch.setattr(
+ datasets, "load_dataset",
+ lambda *a, **k: seen.append(k) or iter([{"text": "x"}]),
+ )
+ result = runner.invoke(
+ app,
+ ["data", "download", "org/ds", "-n", "1", "-o", "out3.jsonl",
+ "--trust-remote-code"],
+ )
+ assert result.exit_code != 0, result.output
+ assert "refused" in result.output.lower(), result.output
+ assert not seen, "load_dataset must not be called when the flag is refused"
+
+ def test_without_flag_datasets_v4_plus_is_unaffected(self, tmp_path, monkeypatch):
+ """The version gate only fires when --trust-remote-code is actually
+ requested; a plain download on datasets>=4 must still work."""
+ import datasets
+
+ from soup_cli.cli import app
+
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setattr(datasets, "__version__", "5.0.1")
+ seen = []
+ monkeypatch.setattr(
+ datasets, "load_dataset",
+ lambda *a, **k: seen.append(k) or iter([{"text": "x"}]),
+ )
+ result = runner.invoke(
+ app, ["data", "download", "org/ds", "-n", "1", "-o", "out4.jsonl"],
+ )
+ assert result.exit_code == 0, result.output
+ assert seen[0].get("trust_remote_code") is False, seen[0]
+
+
+# ─── 2. soup eval auto -> benchmark()/custom() -> lm-eval model_args / pipeline ───
+
+
+class TestEvalAutoTrust:
+ def _setup(self, tmp_path, monkeypatch):
+ import soup_cli.commands.eval as eval_cmd
+ import soup_cli.eval.custom as custom
+
+ monkeypatch.chdir(tmp_path)
+ out = tmp_path / "out"
+ out.mkdir()
+ (out / "adapter_config.json").write_text(
+ json.dumps({"base_model_name_or_path": "org/custom-code-model"})
+ )
+ (tmp_path / "tasks.jsonl").write_text(
+ json.dumps({"prompt": "2+2?", "expected": "4", "scoring": "exact"}) + "\n"
+ )
+ (tmp_path / "train.jsonl").write_text(
+ json.dumps({"instruction": "i", "output": "o"}) + "\n"
+ )
+ (tmp_path / "soup.yaml").write_text(
+ "base: org/custom-code-model\ntask: sft\ndata:\n train: train.jsonl\n"
+ "output: out\neval:\n benchmarks: [mmlu]\n custom_tasks: tasks.jsonl\n"
+ )
+ monkeypatch.setitem(sys.modules, "lm_eval", types.ModuleType("lm_eval"))
+
+ seen_model_args = []
+ seen_generator_calls = []
+ monkeypatch.setattr(
+ eval_cmd, "_run_lm_eval",
+ lambda model_arg, *a, **k: seen_model_args.append(model_arg) or {"results": {}},
+ )
+ monkeypatch.setattr(eval_cmd, "_save_benchmark_results", lambda *a, **k: None)
+ monkeypatch.setattr(eval_cmd, "_save_custom_results", lambda *a, **k: None)
+ monkeypatch.setattr(
+ custom, "_create_default_generator",
+ lambda *a, **k: seen_generator_calls.append((a, k)) or (lambda prompt: "4"),
+ )
+ return seen_model_args, seen_generator_calls
+
+ def test_trust_remote_code_reaches_lm_eval_model_args_and_generator(
+ self, tmp_path, monkeypatch,
+ ):
+ from soup_cli.cli import app
+
+ seen_model_args, seen_generator_calls = self._setup(tmp_path, monkeypatch)
+ result = runner.invoke(
+ app, ["eval", "auto", "-c", "soup.yaml", "--trust-remote-code"],
+ )
+ assert result.exit_code == 0, result.output
+ assert seen_model_args, "benchmark() was never reached"
+ assert "trust_remote_code=True" in seen_model_args[0], seen_model_args[0]
+ assert seen_generator_calls, "custom() was never reached"
+ _, gen_kwargs = seen_generator_calls[0]
+ assert gen_kwargs.get("trust_remote_code") is True, seen_generator_calls[0]
+
+ def test_lm_eval_model_args_still_reject_injection_from_adapter_metadata(
+ self, tmp_path, monkeypatch,
+ ):
+ """The `,`/`=` guard on `base_model_name_or_path` must survive: the only
+ source of `trust_remote_code=True` is the resolved CLI flag, never
+ adapter_config.json (#1449 acceptance criteria)."""
+ from soup_cli.cli import app
+
+ out = tmp_path / "out"
+ out.mkdir()
+ (out / "adapter_config.json").write_text(
+ json.dumps({"base_model_name_or_path": "org/model,trust_remote_code=True"})
+ )
+ (tmp_path / "soup.yaml").write_text(
+ "base: org/model\ntask: sft\ndata:\n train: train.jsonl\n"
+ "output: out\neval:\n benchmarks: [mmlu]\n"
+ )
+ monkeypatch.chdir(tmp_path)
+ result = runner.invoke(
+ app, ["eval", "auto", "-c", "soup.yaml", "--trust-remote-code"],
+ )
+ # benchmark() must refuse the smuggled base model, not silently swallow it.
+ assert "Refusing to evaluate" in result.output, result.output
+
+
+class TestCreateDefaultGeneratorTrust:
+ """Unit-level coverage of `_create_default_generator` itself (`eval.auto`'s
+ fallback generator, and every other caller of it), stubbing
+ `transformers.pipeline` directly rather than going through the CLI."""
+
+ def test_trust_remote_code_reaches_pipeline_call(self, monkeypatch):
+ import transformers
+
+ # Reading `transformers.pipeline` for the first time in a process can
+ # replace `sys.modules["transformers"]` with a new module object
+ # (this transformers version's lazy-loading backend check does a
+ # fresh re-import under the hood) — so touch it once here and
+ # re-fetch the *live* module before patching. Patching the
+ # `transformers` reference obtained above would silently patch a
+ # now-stale object and the real `pipeline` would still run.
+ _ = transformers.pipeline
+ live_transformers = sys.modules["transformers"]
+
+ from soup_cli.eval.custom import _create_default_generator
+
+ seen = []
+
+ def fake_pipeline(*args, **kwargs):
+ seen.append(kwargs.get("trust_remote_code"))
+ return lambda *a, **k: [{"generated_text": "x"}]
+
+ monkeypatch.setattr(live_transformers, "pipeline", fake_pipeline)
+
+ _create_default_generator("org/model", trust_remote_code=True)
+ assert seen == [True], seen
+
+ _create_default_generator("org/model", trust_remote_code=False)
+ assert seen == [True, False], seen
+
+
+# ─── 3. soup train --find-lr -> _live_lr_sweep_from_config -> from_pretrained ───
+
+
+class TestFindLrTrust:
+ """Drives the real `soup train --find-lr` CLI path (like the issue's own
+ repro), stubbing only `transformers.Auto*.from_pretrained` so the sweep
+ reaches the loaders without needing a real model. `torch` is stubbed too
+ only when it is not actually installed, with just enough surface
+ (`cuda.is_available`) for `_live_lr_sweep_from_config` to reach the loads
+ it makes before any real tensor math.
+ """
+
+ def _invoke(self, tmp_path, monkeypatch, *, requested_flag: bool):
+ import importlib.util
+ from types import SimpleNamespace
+
+ import transformers
+
+ # Force the lazy Auto* attributes to resolve now, while `torch`'s
+ # real availability is still whatever this interpreter actually has.
+ # Do this BEFORE any fake `torch` goes into sys.modules: once a fake
+ # is there, transformers' own availability check can see it, decide
+ # torch "is" available, and cache a real (but broken, half-fake)
+ # class under these names — poisoning every other test in this
+ # process that imports transformers afterward.
+ _ = transformers.AutoTokenizer
+ _ = transformers.AutoModelForCausalLM
+
+ if importlib.util.find_spec("torch") is None:
+ fake_torch = types.ModuleType("torch")
+ fake_torch.cuda = types.SimpleNamespace(is_available=lambda: False)
+ monkeypatch.setitem(sys.modules, "torch", fake_torch)
+
+ seen = []
+
+ def fake_tokenizer_from_pretrained(*args, **kwargs):
+ seen.append(kwargs.get("trust_remote_code"))
+ return SimpleNamespace(pad_token="x", eos_token="x")
+
+ def fake_model_from_pretrained(*args, **kwargs):
+ seen.append(kwargs.get("trust_remote_code"))
+ raise ValueError("custom code: set trust_remote_code=True")
+
+ # `transformers.Auto*` classes gate attribute ACCESS to
+ # `from_pretrained` behind a metaclass backend check (raises
+ # ImportError without torch), so `monkeypatch.setattr(cls,
+ # "from_pretrained", ...)` cannot even read the old value to patch
+ # it. Replace the module-level class names instead — that's a plain
+ # module attribute, no metaclass involved — so
+ # `_live_lr_sweep_from_config`'s `from transformers import
+ # AutoModelForCausalLM, AutoTokenizer` picks up the fakes. Separate
+ # fakes for each class so both from_pretrained calls are actually
+ # reached (the tokenizer must succeed for the model load to happen
+ # at all).
+ class _FakeAutoTokenizer:
+ from_pretrained = staticmethod(fake_tokenizer_from_pretrained)
+
+ class _FakeAutoModel:
+ from_pretrained = staticmethod(fake_model_from_pretrained)
+
+ monkeypatch.setattr(transformers, "AutoTokenizer", _FakeAutoTokenizer)
+ monkeypatch.setattr(transformers, "AutoModelForCausalLM", _FakeAutoModel)
+
+ monkeypatch.chdir(tmp_path)
+ (tmp_path / "rows.jsonl").write_text(
+ "".join(json.dumps({"text": f"row {i}"}) + "\n" for i in range(8))
+ )
+ (tmp_path / "lr.yaml").write_text(
+ "base: org/custom-code-model\ntask: sft\n"
+ "data:\n train: rows.jsonl\n format: plaintext\noutput: out2\n"
+ )
+
+ from soup_cli.cli import app
+
+ argv = ["train", "-c", "lr.yaml", "--find-lr", "--find-lr-steps", "8"]
+ if requested_flag:
+ argv.append("--trust-remote-code")
+ result = runner.invoke(app, argv)
+ return seen, result
+
+ def test_trust_remote_code_reaches_both_from_pretrained_calls(
+ self, tmp_path, monkeypatch,
+ ):
+ seen, result = self._invoke(tmp_path, monkeypatch, requested_flag=True)
+ assert len(seen) == 2, (
+ "expected both AutoTokenizer and AutoModelForCausalLM."
+ f"from_pretrained to be reached, got {seen}: {result.output}"
+ )
+ assert all(v is True for v in seen), (seen, result.output)
+
+ def test_without_flag_from_pretrained_gets_false(self, tmp_path, monkeypatch):
+ seen, result = self._invoke(tmp_path, monkeypatch, requested_flag=False)
+ assert len(seen) == 2, (
+ "expected both AutoTokenizer and AutoModelForCausalLM."
+ f"from_pretrained to be reached, got {seen}: {result.output}"
+ )
+ assert all(v is False for v in seen), (seen, result.output)
+
+
+# ─── 4./5. soup draft distill and soup shrink --heal: child `soup train` argv ───
+
+
+class TestChildTrainArgvTrust:
+ def test_draft_distill_appends_trust_remote_code_when_trc(self, tmp_path, monkeypatch):
+ import soup_cli.commands.draft as draft
+
+ monkeypatch.chdir(tmp_path)
+ argvs = []
+
+ def fake_run(argv, **kwargs):
+ argvs.append(argv)
+ return subprocess.CompletedProcess(argv, 1, b"", b"")
+
+ monkeypatch.setattr(subprocess, "run", fake_run)
+ data_path = tmp_path / "d.jsonl"
+ data_path.write_text(
+ "".join(
+ json.dumps({"instruction": f"q{i}", "output": f"a{i}"}) + "\n"
+ for i in range(8)
+ )
+ )
+ try:
+ draft._run_distill(
+ draft_base="org/draft", target="org/target", data=str(data_path),
+ out_dir="draft_out", steps=8, data_rows=8, trc=True,
+ )
+ except Exception:
+ pass # the stubbed child "fails" (rc=1); only its argv matters here
+ assert argvs, "subprocess.run (child `soup train`) was never called"
+ assert "--trust-remote-code" in argvs[-1], argvs[-1]
+
+ def test_draft_distill_omits_flag_when_not_trc(self, tmp_path, monkeypatch):
+ import soup_cli.commands.draft as draft
+
+ monkeypatch.chdir(tmp_path)
+ argvs = []
+
+ def fake_run(argv, **kwargs):
+ argvs.append(argv)
+ return subprocess.CompletedProcess(argv, 1, b"", b"")
+
+ monkeypatch.setattr(subprocess, "run", fake_run)
+ data_path = tmp_path / "d.jsonl"
+ data_path.write_text(
+ "".join(
+ json.dumps({"instruction": f"q{i}", "output": f"a{i}"}) + "\n"
+ for i in range(8)
+ )
+ )
+ try:
+ draft._run_distill(
+ draft_base="org/draft", target="org/target", data=str(data_path),
+ out_dir="draft_out2", steps=8, data_rows=8, trc=False,
+ )
+ except Exception:
+ pass
+ assert argvs
+ assert "--trust-remote-code" not in argvs[-1], argvs[-1]
+
+ def test_shrink_heal_appends_trust_remote_code_when_trc(self, tmp_path, monkeypatch):
+ import soup_cli.commands.shrink as shrink
+
+ monkeypatch.chdir(tmp_path)
+ argvs = []
+
+ def fake_run(argv, **kwargs):
+ argvs.append(argv)
+ return subprocess.CompletedProcess(argv, 1, b"", b"")
+
+ monkeypatch.setattr(subprocess, "run", fake_run)
+ data_path = tmp_path / "d.jsonl"
+ data_path.write_text(
+ "".join(
+ json.dumps({"instruction": f"q{i}", "output": f"a{i}"}) + "\n"
+ for i in range(8)
+ )
+ )
+ pruned_dir = tmp_path / "pruned"
+ pruned_dir.mkdir()
+ try:
+ shrink._run_heal(
+ pruned_dir=str(pruned_dir), teacher="org/target", heal_data=str(data_path),
+ steps=8, out_dir="heal_out", heal_rows=8, trc=True,
+ )
+ except Exception:
+ pass
+ assert argvs, "subprocess.run (child `soup train`) was never called"
+ assert "--trust-remote-code" in argvs[-1], argvs[-1]
+
+ def test_shrink_heal_omits_flag_when_not_trc(self, tmp_path, monkeypatch):
+ import soup_cli.commands.shrink as shrink
+
+ monkeypatch.chdir(tmp_path)
+ argvs = []
+
+ def fake_run(argv, **kwargs):
+ argvs.append(argv)
+ return subprocess.CompletedProcess(argv, 1, b"", b"")
+
+ monkeypatch.setattr(subprocess, "run", fake_run)
+ data_path = tmp_path / "d.jsonl"
+ data_path.write_text(
+ "".join(
+ json.dumps({"instruction": f"q{i}", "output": f"a{i}"}) + "\n"
+ for i in range(8)
+ )
+ )
+ pruned_dir = tmp_path / "pruned"
+ pruned_dir.mkdir()
+ try:
+ shrink._run_heal(
+ pruned_dir=str(pruned_dir), teacher="org/target", heal_data=str(data_path),
+ steps=8, out_dir="heal_out2", heal_rows=8, trc=False,
+ )
+ except Exception:
+ pass
+ assert argvs, "subprocess.run (child `soup train`) was never called"
+ assert "--trust-remote-code" not in argvs[-1], argvs[-1]
+
+
+# ─── Default-deny: without the flag, no site may opt in to remote code ───
+
+
+class TestDefaultsStayDeny:
+ def test_eval_auto_without_flag_trusts_nothing(self, tmp_path, monkeypatch):
+ from soup_cli.cli import app
+
+ seen_model_args, seen_generator_calls = TestEvalAutoTrust()._setup(tmp_path, monkeypatch)
+ result = runner.invoke(app, ["eval", "auto", "-c", "soup.yaml"])
+ assert result.exit_code == 0, result.output
+ assert seen_model_args and "trust_remote_code" not in seen_model_args[0], seen_model_args
+ assert seen_generator_calls[0][1].get("trust_remote_code") is False, seen_generator_calls
+
+ @pytest.mark.parametrize(("flag", "expected"), [([], False), (["--trust-remote-code"], True)])
+ def test_eval_benchmark_cli_model_args(self, tmp_path, monkeypatch, flag, expected):
+ from soup_cli.cli import app
+
+ seen_model_args, _ = TestEvalAutoTrust()._setup(tmp_path, monkeypatch)
+ result = runner.invoke(app, ["eval", "benchmark", "-m", "out", "-b", "mmlu", *flag])
+ assert result.exit_code == 0, result.output
+ assert ("trust_remote_code=True" in seen_model_args[0]) is expected, seen_model_args
+
+ @pytest.mark.parametrize(("flag", "expected"), [([], False), (["--trust-remote-code"], True)])
+ def test_eval_custom_cli_generator_kwargs(self, tmp_path, monkeypatch, flag, expected):
+ from soup_cli.cli import app
+
+ _, seen_generator_calls = TestEvalAutoTrust()._setup(tmp_path, monkeypatch)
+ result = runner.invoke(app, ["eval", "custom", "-t", "tasks.jsonl", "-m", "out", *flag])
+ assert result.exit_code == 0, result.output
+ assert seen_generator_calls[0][1].get("trust_remote_code") is expected, seen_generator_calls
+
+ def test_training_auto_eval_callback_never_opts_in(self, monkeypatch):
+ import soup_cli.commands.eval as ce
+ from soup_cli.monitoring.callback import SoupTrainerCallback
+
+ calls: dict = {}
+ monkeypatch.setattr(ce, "benchmark", lambda **kw: calls.__setitem__("benchmark", kw))
+ monkeypatch.setattr(ce, "custom", lambda **kw: calls.__setitem__("custom", kw))
+ callback = SoupTrainerCallback.__new__(SoupTrainerCallback)
+ callback.eval_config = type(
+ "EvalCfg", (),
+ {"auto_eval": True, "benchmarks": ["mmlu"], "custom_tasks": "tasks.jsonl"},
+ )()
+ callback.output_dir = "out"
+ callback.run_id = "run-1"
+ callback._run_auto_eval()
+ assert calls["benchmark"]["trust_remote_code"] is False, calls
+ assert calls["custom"]["trust_remote_code"] is False, calls
+
+ @pytest.mark.parametrize(("flag", "panel"), [([], False), (["--trust-remote-code"], True)])
+ def test_data_download_warning_panel_only_with_flag(self, tmp_path, monkeypatch, flag, panel):
+ import datasets
+
+ from soup_cli.cli import app
+
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setattr(datasets, "__version__", "3.6.0")
+ monkeypatch.setattr(datasets, "load_dataset", lambda *a, **k: iter([{"text": "x"}]))
+ result = runner.invoke(
+ app, ["data", "download", "org/ds", "-n", "1", "-o", "o.jsonl", *flag],
+ )
+ assert result.exit_code == 0, result.output
+ assert ("Remote Code Warning" in _flat(result.output)) is panel, _flat(result.output)
+
+ def test_default_generator_defaults_to_false(self, monkeypatch):
+ """Callers that omit the kwarg must get an explicit False, not None."""
+ import transformers
+
+ _ = transformers.pipeline
+ live_transformers = sys.modules["transformers"]
+
+ from soup_cli.eval.custom import _create_default_generator
+
+ seen = []
+
+ def fake_pipeline(*args, **kwargs):
+ seen.append(kwargs.get("trust_remote_code", "missing"))
+ return lambda *a, **k: [{"generated_text": "x"}]
+
+ monkeypatch.setattr(live_transformers, "pipeline", fake_pipeline)
+ _create_default_generator("org/model")
+ assert seen == [False], seen
+
+ def test_flag_without_datasets_installed_gets_the_install_hint(self, tmp_path, monkeypatch):
+ from soup_cli.cli import app
+
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setitem(sys.modules, "datasets", None) # `import datasets` raises ImportError
+ result = runner.invoke(
+ app, ["data", "download", "org/ds", "-n", "1", "-o", "o.jsonl", "--trust-remote-code"],
+ )
+ assert result.exit_code == 1, (result.output, repr(result.exception))
+ assert "datasets library not available" in _flat(result.output)
diff --git a/tests/test_issue752_cli_commands_called_as_functions.py b/tests/test_issue752_cli_commands_called_as_functions.py
index 3fb0b9d4..733ba863 100644
--- a/tests/test_issue752_cli_commands_called_as_functions.py
+++ b/tests/test_issue752_cli_commands_called_as_functions.py
@@ -207,7 +207,10 @@ def _stub_eval_internals(monkeypatch):
import soup_cli.commands.eval as ce
import soup_cli.eval.custom as ec
- monkeypatch.setattr(ec, "_create_default_generator", lambda path: (lambda p: "4"))
+ monkeypatch.setattr(
+ ec, "_create_default_generator",
+ lambda path, trust_remote_code=False: (lambda p: "4"),
+ )
monkeypatch.setattr(ce, "_save_custom_results", lambda *a, **k: None)
diff --git a/tests/test_typer_defaults_do_not_leak.py b/tests/test_typer_defaults_do_not_leak.py
index c0117b53..e37450f0 100644
--- a/tests/test_typer_defaults_do_not_leak.py
+++ b/tests/test_typer_defaults_do_not_leak.py
@@ -305,7 +305,8 @@ def test_a_partial_positional_call_names_only_the_unfilled_parameters():
)
assert _leaking_calls([caller], commands) == [
- "caller.py:2 calls custom() without ['attach_to_registry', 'output', 'run_id']"
+ "caller.py:2 calls custom() without "
+ "['attach_to_registry', 'output', 'run_id', 'trust_remote_code']"
]