Skip to content

Check a sentence-transformers module config on every version, and confirm the resolved class - #12444

Merged
danielhanchen merged 9 commits into
mainfrom
st-config-refs-version-fork
Oct 1, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
st-config-refs-version-fork

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Two follow-ups to the modules.json trust gate, both found while running the gate against older sentence-transformers releases.

The config check ran on the wrong versions

_check_module_config_class_refs covers the dotted class paths a module's own config file carries, which some sentence-transformers loaders import with no trust gate of their own: Dense imports and then calls activation_function, WordEmbeddings imports tokenizer_class, and Router and Asym import every value in types.

It decided whether to run by asking hasattr(sentence_transformers.util, "import_module_class"), taking that to mean "6.0 or newer, which gates these itself". That is not what the attribute means. 5.5 exports import_module_class while its WordEmbeddings.load still calls import_from_string(config.pop("tokenizer_class")), so the check skipped exactly the versions that needed it.

The prefixes it enforces, torch. for the activation and sentence_transformers. for the rest, are upstream's own rules, so there is no reason to fork on version at all: running them unconditionally either agrees with a release that gates for itself, or covers one that does not. The fork is gone, and with it the feature-detection footgun.

Checked on 5.0.0, 5.5.0, 6.0.0 and 6.1.0: a WordEmbeddings config naming an outside class is refused on all four, where before it was refused only on 5.0.0. For the other direction, the 24 per-module config files the real embedders actually ship, which is every module directory of 20 repos including both embeddinggemma mirrors and LaBSE's Dense, are all still accepted.

An in-namespace type was trusted on its prefix alone

The other branch resolved any sentence_transformers.* type straight through import_from_string. The module part is pinned to the installed package, so the import itself only ever reaches trusted code, and that part was fine. What it did not establish is that the name denotes a module class: the same dotted syntax names any public attribute, and _load_modules goes straight on to call .load() on whatever comes back.

Resolution now rejects a path that is not a dotted identifier chain before importing anything, and afterwards confirms the result is a sentence-transformers module class, meaning a torch.nn.Module subclass exposing load and defined inside the sentence_transformers package.

type what it is before now
sentence_transformers.models.Pooling module class resolved resolved
sentence_transformers.util.import_from_string a function resolved, then .load() called on it refused
sentence_transformers.util.http_get a function resolved refused
sentence_transformers.models a submodule resolved refused
sentence_transformers.models..Pooling malformed import attempted refused before importing
sentence_transformers. os not an identifier import attempted refused before importing

I kept import_from_string rather than replacing it with a name table. A table of class names has to be maintained per release, since 5.x keeps Transformer in models/Transformer.py and 6.x moved it to base/modules/transformer.py, and a stale table refuses legitimate models. Confirming the resolved object costs nothing and cannot go stale. The one remaining call on an unconsented path is this one, on a prefix-pinned path; the only other is the trust_remote_code=True fallback for sentence-transformers below 6, which has no dynamic resolver.

Compatibility

  • All 62 module classes the installed library ships still resolve with the gate closed, and the 4 distinct types declared across 20 real embedder repos are unaffected.
  • The 24 real per-module config files are all accepted, so no published model is refused by the now-unconditional check.
  • tests/test_st_modules_json_trust_gate.py is 19 tests. The version-fork case is covered by a parametrised test that pins the refusal with import_module_class present, absent and set to None, which is the invariant that was missing: the earlier tests simulated an old release by deleting that attribute, which is why they could not see this.
  • 163 passed and 2 skipped across the sentence-transformers, remote-code and version-compat suites.

Two behaviour changes worth stating plainly

Late-interaction models that declare a PyLate class. lightonai/GTE-ModernColBERT-v1 and lightonai/colbertv2.0 declare pylate.models.Dense.Dense in modules.json. That is third-party code by any reading, so the gate asks for trust_remote_code=True, and stock sentence-transformers 6.1 refuses the identical reference with the identical reasoning, so on 6.x this is not a divergence. The people who see a change are on 5.x with PyLate installed, reaching these through the fallback module assembly rather than the fast encoder route: for them the model now needs the flag, which upstream will ask of them as soon as they move to 6.x. PyLate is not a dependency here and there is no ColBERT notebook or test in the repo, so no supported flow regresses. Worth knowing rather than discovering.

A Dense activation outside torch.*, on 6.x only. Running the config check unconditionally means one key is now stricter than upstream: for activation_function, sentence-transformers 6 warns and falls back to the default activation, where this refuses. The other two keys match upstream exactly, since tokenizer_class and types go through import_module_class and raise there too. Refusing is the safer direction, because silently swapping a model's activation changes its embeddings while still looking like a successful load, and no published model is affected: the 24 per-module config files checked all name torch.*.

…firm the class

Two follow-ups to the modules.json trust gate.

The config check keyed off util.import_module_class to mean "sentence-transformers
6, which gates these itself". That is not what the attribute means: 5.5 exports it
while its WordEmbeddings.load still calls import_from_string on the config's
tokenizer_class, so the check skipped exactly the versions that need it. The
prefixes are upstream's own rules, so run them unconditionally, where they either
agree with a version that gates for itself or cover one that does not. Verified
refused on 5.0, 5.5, 6.0 and 6.1, and the 24 per-module config files the real
embedders ship are all still accepted.

The in-namespace branch also trusted the prefix alone. The module part is pinned to
the installed package so the import reaches only trusted code, but the same dotted
syntax names any public attribute, and the caller goes straight on to .load() it.
Resolution now rejects a malformed path before importing and confirms the result is
a sentence-transformers module class, so a type naming a function or a submodule is
refused instead of being handed a load call. All 62 module classes the library ships
and the 4 types the real embedders declare still resolve.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-01T19:29:20.199274Z abdfdf8 Manual request
🔒 Security Review ✅ Completed 2026-10-01T14:28:49.091292Z d8bf230 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: d8bf23036d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: d8bf23036d

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

config_file_name only exists from sentence-transformers 5. On 3.x and 4.x
WordEmbeddings opens wordembedding_config.json by name and Router/Asym open
router_config.json, so the attribute lookup found nothing to check on those
versions. Map the known legacy names by class.

Checkpoint resume is a second caller of the same shape: the class comes from the
live model, so it is trusted, but load() still reads the checkpoint's own config
files and some versions import dotted paths out of them. Run the same check first.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: eaa8a49aab

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth/models/sentence_transformer.py Outdated
Comment on lines +2569 to +2570
FastSentenceTransformer._check_module_config_class_refs(
module_dir, saved_type or module_cls.__name__, root, False, module_cls

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve consent when validating checkpoint configs

When a PEFT model was originally loaded with trust_remote_code=True and a non-transformer module config legitimately references a third-party activation, tokenizer, or nested type, resuming its saved checkpoint now always fails because this call hard-codes the trust argument to False. _unsloth_load_from_checkpoint accepts only self and checkpoint_path, so the resulting error's advice to pass trust_remote_code=True provides no usable escape hatch even though the live model was already created with that consent; preserve the original trust setting or otherwise provide a way to consent during checkpoint reload.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in bbd733b: the consent the model was loaded with is now recorded on it by each route and read back at reload, defaulting to False when absent so an object from an older build still fails closed. You are right that the advice was useless there, since the patched reload takes only a checkpoint path, so the message named a flag the caller had no way to pass. Covered both directions in tests, plus one asserting every route that returns a model records the flag, so a new route cannot silently skip it.

The config check on checkpoint resume passed False unconditionally, so a model
that legitimately needed trust_remote_code could no longer resume, and the
error advised a flag the reload cannot accept, since it takes only a checkpoint
path. Record the consent the model was loaded with on each route and read it
back there, defaulting to False when it is absent so an older object still
fails closed.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: bbd733b1c2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The gate's tests errored in Repo tests (CPU, rest) with ModuleNotFoundError:
sentence-transformers is an extra, not a core dependency, and that shard installs
the core set. The file now skips when the package is absent, so it reports a skip
rather than an error, and the shard installs the package so it runs rather than
skipping everywhere, which would leave the gate unfailable in CI.

The install rides the existing transformers pin on one line, so resolving
sentence-transformers cannot lift transformers past the <5.5 ceiling.
@danielhanchen

Copy link
Copy Markdown
Member Author

pre-commit.ci run

main added the same pytest.importorskip in #12447, so the conflict is only in the
comment above it. Kept this branch's, corrected: that PR's note says
version-compat-ci's zoo-imports job runs this file, and it does not. That job
installs sentence-transformers but runs two named tests/vllm_compat files. The only
shard that collects this one is Repo tests (CPU, rest), which is why this branch
also installs the package there. Without that, the importorskip leaves the gate
skipped in every job, where it cannot fail and so proves nothing.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 6c6e297fbf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The merge commit on this branch asserted that version-compat-ci's zoo-imports job does
not run tests/test_st_modules_json_trust_gate.py, and used that to justify the
sentence-transformers install added here. The assertion is wrong: that job names the file
on its pytest line, at version-compat-ci.yml:240, and installs the package for it. #12447
was right and my note contradicting it was not.

The install itself stands on its own, which is why this only touches the two comments.
studio-backend-ci's Repo tests shard collects the file by discovery rather than by name,
and with no sentence-transformers there the importorskip leaves it skipped for good, which
is the same reason the pinned-ruff step directly above it exists. So the file runs in two
places for two different reasons: the version matrix under the CUDA spoof in
version-compat-ci, and the core dependency set here. Both comments now say that.
@danielhanchen

Copy link
Copy Markdown
Member Author

Correcting something I wrote on this branch. The merge commit 6c6e297fb claimed that version-compat-ci's zoo-imports job does not run tests/test_st_modules_json_trust_gate.py, and used that to justify the sentence-transformers install here. That claim is false. The job names the file on its pytest line at version-compat-ci.yml:240 and installs the package for it. #12447 was right and my note contradicting it was not.

66817b62e corrects both comments and changes nothing else. The install itself stands on its own merits: studio-backend-ci's Repo tests shard collects the file by discovery rather than by name, and with no sentence-transformers there the importorskip would leave it skipped permanently, which is the same failure mode the pinned-ruff step immediately above it exists to prevent. So the file runs in two places for two different reasons, the version matrix under the CUDA spoof and the core dependency set here, and both comments now say so.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 66817b62ea

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1301 to +1303
# some versions, so an allowed in-namespace "type" reaches them. Prefixes are upstream's own
# rules, applied unconditionally: util.import_module_class is not a version test, since 5.5
# exports it while its WordEmbeddings.load still calls import_from_string on tokenizer_class.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict activation checks to loaders that import it

Applying these key rules to every module misclassifies legitimate built-in configurations. For example, sentence_transformers.sparse_encoder.models.SpladePooling saves activation_function as the literal enum "relu" or "log1p_relu" and consumes it directly rather than importing it; with trust_remote_code=False, the now-unconditional check rejects either value for not starting with torch., so affected sparse/Router models cannot load unless users unnecessarily enable remote code. Dispatch this validation by the resolved loader/class (such as Dense) rather than solely by matching a config key name.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, and confirmed against the 5.1.0 source rather than from the diff. SpladePooling lists activation_function in its own config_keys, saves "relu" or "log1p_relu" there, and then does if self.activation_function == "log1p_relu". Nothing is imported, and the check refused both values, so no SPLADE sparse model could load without a user turning on remote code for a name no loader resolves.

Fixed in abdfdf8. The rules are keyed by the class that resolves them and a class that imports nothing out of its config returns before any file is read. I enumerated the loaders instead of guessing: the only config-driven import_from_string calls in the package are Dense.load on activation_function, WordEmbeddings.load on tokenizer_class, Router.load on types, and SentenceTransformer's own modules.json type, which _resolve_module_class already covers. CrossEncoder.load resolves an activation path too, but it is not loaded through modules.json and does not reach here. Asym has been an alias of Router since 5.0 (Asym = Router) so it arrives as Router; I kept its entry for versions where it is its own class, and the rules follow the bases so a subclass stays gated.

Five tests. The SpladePooling one raises on the previous head with exactly the message a user would have seen. The Dense and WordEmbeddings cases are negative controls that pass on the previous head as well, which is deliberate: they are what proves the scoping did not buy the fix by dropping coverage. One test asserts the set of covered classes, so a version that adds another config-driven import surfaces as a missing key rather than as a silently unchecked path.

The check keyed on the key name alone, so any module config carrying an
activation_function or a tokenizer_class was refused for a value outside torch.* or
sentence_transformers.*. SpladePooling lists activation_function in its own config_keys,
saves the literal "relu" or "log1p_relu" there and then does
`if self.activation_function == "log1p_relu"`: nothing is imported, and the check refused
both values. No SPLADE sparse model could load without the user turning on remote code
for a name no loader resolves.

The rules are now keyed by the class that resolves them, and a class that imports nothing
out of its config returns before any file is read. Verified against the 5.1.0 source
rather than inferred: the only config-driven import_from_string calls in the package are
Dense.load on activation_function, WordEmbeddings.load on tokenizer_class, Router.load on
types, and SentenceTransformer's own modules.json type, which _resolve_module_class
already covers. CrossEncoder.load resolves an activation path too, but it is not loaded
through modules.json and does not reach here. Asym has been an alias of Router since 5.0
(`Asym = Router`) so it arrives as Router; its entry is kept for versions where it is its
own class, and the rules follow the bases so a subclass stays gated.

Five tests. The SpladePooling one raises on the previous head with exactly the message a
user would have hit. The Dense and WordEmbeddings cases are negative controls that pass
on the previous head too, which is the point: they prove the scoping did not buy the fix
by dropping coverage. One test asserts the set of covered classes, so a version that adds
another config-driven import shows up as a missing key rather than as a silently
unchecked path.

28 pass in this file, 85 across the three sentence-transformers suites, 65 under the CUDA
spoof alongside the vllm_compat files.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: abdfdf85f1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 6102d2c into main Oct 1, 2026
35 checks passed
@danielhanchen
danielhanchen deleted the st-config-refs-version-fork branch October 1, 2026 20:27
danielhanchen added a commit that referenced this pull request Oct 1, 2026
#12444 landed, so the config check no longer keys its early return off
util.import_module_class and the dependency this branch was stacked behind is gone.

Conflict was in tests/test_st_modules_json_trust_gate.py, where both branches appended
tests to the same file. Resolved by taking this branch's file, taking main's version of
the three tests #12444 rewrote for the per-class dispatch, and appending the ten tests
only main has. No test was dropped as a merge artefact.

Two deliberate changes on top of the resolution.

_simulate_pre_six no longer deletes util.import_module_class. That deletion was how these
tests passed at all while the check keyed off the attribute, which meant they passed for a
reason production did not have and hid exactly the 5.5 gap that made the attribute a bad
test in the first place. Setting only the version is what makes them exercise the real
condition.

test_the_installed_sentence_transformers_gate_is_not_second_guessed is removed. It
asserted the check is a no-op on sentence-transformers 6, which was true when the early
return existed. #12444 applies the prefix rules on every version instead, so the test now
encodes behaviour that was deliberately changed, and main's
test_the_check_does_not_key_off_the_import_module_class_attribute asks the same question
the right way round.

Also folded the two stacked comments above _MODULE_CONFIG_CLASS_REFS into one. The merge
left #12444's explanation of why the rules are unconditional directly above this branch's
explanation of why they are keyed by class, which is one decision described twice.

63 pass in this file, 120 across the three sentence-transformers suites, 100 under the
CUDA spoof.
danielhanchen added a commit that referenced this pull request Oct 1, 2026
Lint CI was failing for a real reason, not a flake. The check runs against main merged
with this branch, and #12444 has since landed changes to sentence_transformer.py, so the
three reviewed sinks in that file no longer matched their allowances: the enclosing
functions changed, and the enclosing function's digest is part of the key by design, so
editing around a reviewed sink reopens it. That is the gate behaving as intended, pointed
at its own baseline.

Merged cleanly, no conflicts. Regenerating moves two entries and adds one.

The addition is _import_sentence_transformers_class, which #12444 introduced and nothing
had looked at. Reviewed rather than waved through: every dotted part is checked to be an
identifier before anything is imported, callers only reach it for a class_ref already
starting with "sentence_transformers.", and the resolved object has to be a torch.nn.Module
subclass defined inside that package and carrying a load attribute. The import itself does
run before that post-check, which is why it is reported, but the prefix pins it to the
installed package, so what can execute is module-level code inside sentence_transformers.

Worth noting for whoever merges this last: a baseline keyed on the enclosing function
means this branch has to be re-merged and regenerated after any PR that touches a file
with a reviewed sink in it. #12446 is still open and touches this same file, so this will
need one more pass once it lands.

69 tests pass, the self-test passes, and the gate exits 0 on the merged tree.
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