Repository navigation
Check a sentence-transformers module config on every version, and confirm the resolved class - #12444
Conversation
…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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
|
@codex security review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
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.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 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".
| FastSentenceTransformer._check_module_config_class_refs( | ||
| module_dir, saved_type or module_cls.__name__, root, False, module_cls |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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.
|
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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.
|
Correcting something I wrote on this branch. The merge commit
|
|
@codex review |
There was a problem hiding this comment.
💡 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".
| # 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. |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
#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.
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.
Two follow-ups to the
modules.jsontrust gate, both found while running the gate against older sentence-transformers releases.The config check ran on the wrong versions
_check_module_config_class_refscovers the dotted class paths a module's own config file carries, which some sentence-transformers loaders import with no trust gate of their own:Denseimports and then callsactivation_function,WordEmbeddingsimportstokenizer_class, andRouterandAsymimport every value intypes.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 exportsimport_module_classwhile itsWordEmbeddings.loadstill callsimport_from_string(config.pop("tokenizer_class")), so the check skipped exactly the versions that needed it.The prefixes it enforces,
torch.for the activation andsentence_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
WordEmbeddingsconfig 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'sDense, are all still accepted.An in-namespace type was trusted on its prefix alone
The other branch resolved any
sentence_transformers.*type straight throughimport_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_modulesgoes 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.Modulesubclass exposingloadand defined inside thesentence_transformerspackage.sentence_transformers.models.Poolingsentence_transformers.util.import_from_string.load()called on itsentence_transformers.util.http_getsentence_transformers.modelssentence_transformers.models..Poolingsentence_transformers. osI kept
import_from_stringrather than replacing it with a name table. A table of class names has to be maintained per release, since 5.x keepsTransformerinmodels/Transformer.pyand 6.x moved it tobase/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 thetrust_remote_code=Truefallback for sentence-transformers below 6, which has no dynamic resolver.Compatibility
tests/test_st_modules_json_trust_gate.pyis 19 tests. The version-fork case is covered by a parametrised test that pins the refusal withimport_module_classpresent, absent and set toNone, 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.Two behaviour changes worth stating plainly
Late-interaction models that declare a PyLate class.
lightonai/GTE-ModernColBERT-v1andlightonai/colbertv2.0declarepylate.models.Dense.Denseinmodules.json. That is third-party code by any reading, so the gate asks fortrust_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
Denseactivation outsidetorch.*, on 6.x only. Running the config check unconditionally means one key is now stricter than upstream: foractivation_function, sentence-transformers 6 warns and falls back to the default activation, where this refuses. The other two keys match upstream exactly, sincetokenizer_classandtypesgo throughimport_module_classand 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 nametorch.*.