Skip to content

feat: add trust_remote_code option to AutoDocumentEncoder/AutoQueryEncoder - #2645

Open
St4r4x wants to merge 2 commits into
castorini:masterfrom
St4r4x:feat/auto-encoder-trust-remote-code
Open

St4r4x wants to merge 2 commits into
castorini:masterfrom
St4r4x:feat/auto-encoder-trust-remote-code

Conversation

@St4r4x

@St4r4x St4r4x commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Closes #2207

What

Adds an optional trust_remote_code: bool = False parameter to AutoDocumentEncoder and AutoQueryEncoder, threaded through to both AutoModel.from_pretrained() and the tokenizer loader, and further threaded through FaissSearcher/BinaryDenseFaissSearcher so it also reaches the "search a precomputed index" workflow the issue itself describes. This lets models that require custom code on the Hub (e.g. nomic-ai/nomic-embed-text-v1, named in the issue) load without a blocking Y/N terminal prompt or needing to patch pyserini locally.

Scope

Scoped to the two Auto*Encoder classes named in the issue (the generic AutoModel.from_pretrained(...)-based wrappers — model-specific encoders elsewhere in pyserini/encode/ like uniCOIL/SPLADE/DPR load fixed, known architectures and don't need this knob), plus FaissSearcher/BinaryDenseFaissSearcher/from_prebuilt_index — those are the actual entry points for "encoding queries and searching from a precomputed index" that the issue names as the motivating use case, and the fix wasn't reachable from there in the first commit.

load_auto_tokenizer() in _base.py already forwards **kwargs to AutoTokenizer.from_pretrained(), so no change was needed there beyond passing trust_remote_code through as a kwarg.

All new parameters default to False and are appended after existing params, so existing behavior/call sites are unchanged for everyone not opting in (checked every call site, including one that already passes 3 positional args to from_prebuilt_index).

Not included (happy to add if wanted)

Didn't wire this into the CLI scripts (scripts/encode_queries.py, pyserini/encode/__main__.py, pyserini/encode/query.py's CLI driver) — no --trust-remote-code flag there yet. Let me know if that'd be useful too.

Validation

Checked every call site of AutoDocumentEncoder/AutoQueryEncoder/FaissSearcher in the repo — all keyword-arg based (or safely appended after existing params), so this is fully backward compatible.

Added tests/base/search/test_faiss_searcher_trust_remote_code.py, a mock-based unit test on _init_encoder_from_str mirroring the existing SPLADE test's patch-and-assert pattern — no network or model download needed.

Couldn't run the full suite or even import pyserini.search for real: this repo needs a built/downloaded Anserini fatjar just to import that package (same wall as #2644), and there's no CI here to compare against either (no .github/workflows in this repo). Verified the new tests pass using a local, uncommitted stub of the JVM bridge (pyserini.pyclass) instead — confirms the logic, not a real end-to-end run.

Note

AI-assisted (Claude Code); verified against the source and a local test run before each push.

St4r4x added 2 commits August 17, 2026 14:10
…coder

Some HuggingFace models (e.g. nomic-ai/nomic-embed-text-v1) require
trust_remote_code=True to load, which previously required patching
pyserini or accepting a blocking Y/N terminal prompt every run.

Threads an optional trust_remote_code=False parameter through to both
AutoModel.from_pretrained() and load_auto_tokenizer() (which already
forwards **kwargs to AutoTokenizer.from_pretrained(), so no change
needed there). Defaults to False, matching current behavior.
AutoDocumentEncoder/AutoQueryEncoder gained trust_remote_code in the
prior commit, but FaissSearcher/BinaryDenseFaissSearcher never forwarded
it - the exact "encode queries and search a precomputed index" workflow
issue castorini#2207 describes, so that use case stayed blocked even after the
encoder classes were fixed.

Thread trust_remote_code through FaissSearcher.__init__,
from_prebuilt_index, the _init_encoder_from_str dispatcher, and
BinaryDenseFaissSearcher.__init__. All additions are appended after
existing params with a False default, so no caller breaks (checked
every call site, including the one that already passes 3 positional
args to from_prebuilt_index).

Add a mock-based unit test on _init_encoder_from_str mirroring the
existing SPLADE test's patch-and-assert pattern, since this repo has no
CI to lean on and building the Anserini fatjar needed to import
pyserini.search for real is out of scope here (same constraint noted on
castorini#2644).

AI-assisted (Claude Code); verified with a local test run before
committing (see CONTRIBUTION_NOTES.md, not part of this diff, for how).
@St4r4x

St4r4x commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Extended this to also cover `FaissSearcher`/`BinaryDenseFaissSearcher`/`from_prebuilt_index` (via `_init_encoder_from_str`) — the first commit only touched `AutoDocumentEncoder`/`AutoQueryEncoder` directly, but the issue's own motivating use case ("encoding queries and searching from a precomputed index") normally goes through `FaissSearcher`, which didn't have a way to pass `trust_remote_code` through. It does now, same append-only/default-False pattern as before. Added a mock-based test for the new dispatch logic. Updated the PR description above to match.

Note

AI-assisted (Claude Code) — should have flagged this in the original PR description too, apologies for the omission there.

This branch has not been deployed

No deployments
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.

Add trust_remote_code=True to load some specific models from huggingface

1 participant