Repository navigation
refactor(nlp): give NLP backends their model at fit() instead of construction - #834
Open
milton-minervino wants to merge 5 commits into
Open
milton-minervino wants to merge 5 commits into
milton-minervino wants to merge 5 commits into
Conversation
…nd contract The text backends never use it, and the tabular BaseBackend keeps its own implementation. The shared contract is now run_explainer alone.
…truction A backend now holds settings only. NlpShapBackend, NlpLimeBackend and NlpCaptumLigBackend are scikit-learn estimators: the constructor stores its arguments unchanged, and fit(model, label_names=None) builds the explainer. NlpExplainer owns the model and binds a fresh copy of the backend it is given, built from get_params() rather than sklearn's clone, which would deep-copy maskers and tokenizers. The model is no longer passed twice, and the caller's instance is never modified and can be reused. - Values fit() derives end in "_" (model_, explainer_, masking_, baseline_token_). Reading one before fit raises. - NlpBackend.run_explainer checks the backend is fitted, then calls the subclass's _run_explainer. - The cache key is the backend name plus get_params(), with nested dicts sorted. Display-only settings (display_params: show_progress) are left out. - Removed: the ignored preprocessing parameter, label_names from the constructors (it now arrives through fit), the unused explainer_args of NlpCaptumLigBackend, NlpBackend.get_local_contributions, and the explainer_args / explainer_compute_args parameters of NlpExplainer. - NlpLimeBackend applies num_features="all" in fit, so the caller's explainer_compute_args stay as given. xpl.backend is now a fitted copy, not the instance passed in.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #834 +/- ##
===========================================
+ Coverage 86.37% 86.40% +0.03%
===========================================
Files 115 115
Lines 11958 11978 +20
Branches 2176 2175 -1
===========================================
+ Hits 10329 10350 +21
+ Misses 996 995 -1
Partials 633 633
🚀 New features to boost your workflow:
|
milton-minervino
marked this pull request as ready for review
November 16, 2026 10:12
milton-minervino
requested
a balanced review from Copilot
and removed request for
Copilot
November 16, 2026 10:13
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
NLP backends now hold settings only and receive the model at
fit(), so you no longer pass the model twice.NlpExplainerowns the model and fits the backend.Before
After
Changes
NlpShapBackend,NlpLimeBackendandNlpCaptumLigBackendare scikit-learnBaseEstimators. Constructors store their arguments unchanged;fit(model, label_names=None)builds the explainer and returnsself.fitend in_(model_,label_names_,explainer_,masker_,masking_,baseline_token_, …). Settings keep plain names.NlpBackend.run_explainerchecks that the backend is fitted (NotFittedErrorotherwise), then calls the subclass's_run_explainer.NlpExplainerfits a fresh copy,type(backend)(**backend.get_params(deep=False)). It doesn't usesklearn.clone, which would deep-copy maskers and tokenizers.get_params(), with nested dicts sorted. Before, the key listed a few attributes by hand, so a setting outside that list (for exampleNlpLimeBackend(mask_string=...)) didn't change it and could reload a stale cached explanation. Display-only settings (display_params, currentlyshow_progress) are left out of the key, so turning on a progress bar doesn't invalidate the cache.NlpLimeBackendaddsnum_features="all"infitand leaves the caller'sexplainer_compute_argsdict untouched.compare_backends.ipynb), demo (serve_nlp.py) and wiki (NLP-Explainability.md) are updated to the new constructors.Breaking changes
The NLP path hasn't been released yet, so this only affects code written against
develop.model,label_namesorpreprocessing(it was ignored).NlpCaptumLigBackendalso loses its unusedexplainer_args.NlpExplainerno longer takesexplainer_args/explainer_compute_args. Pass them to the backend instead:NlpShapBackend(explainer_args=...).get_local_contributionsis gone fromNlpBackendand from the sharedBackendcontract. The tabularBaseBackendkeeps its own, andSmartExplaineris unaffected.xpl.backendis now a fitted copy, not the instance you passed in. The instance you pass in is never modified, so you can reuse one settings object across several explainers. Code that inspected its own backend object after construction should readxpl.backendinstead.Testing
run_explainerbeforefitraises.ruff checkis clean. Coverage on the changed modules is 92–99%.compare_backendsnotebook was edited and re-run.