Skip to content

check_estimator_internal_state_unchanged_by_inference: only compute per-attribute diagnostics when the check fails #35592

Backlog

Description

@betatim

Warning

This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.

#34729 added check_estimator_internal_state_unchanged_by_inference. Before and after each inference method, it pickles two things:

  1. the whole estimator, _pickled_bytes(estimator), which is what the assert compares;
  2. every value in estimator.__dict__, one at a time, used only to name the mismatching attributes in the error message.

Step 2 runs every time, not only when the check fails, and it pickles attribute values directly, bypassing the estimator's __getstate__. An estimator that holds unpicklable state (a native handle, a GPU index) and handles it in __getstate__ passes step 1 but errors in step 2. The check then fails even when inference changed nothing.

We hit this in cuML, where several estimators keep opaque GPU objects as attributes. Our current workaround monkeypatches the private _StatePickler in our test suite, which we'd like to avoid.

Minimal example:

import threading

import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.utils.estimator_checks import (
    check_estimator_internal_state_unchanged_by_inference,
)


class Est(ClassifierMixin, BaseEstimator):
    def fit(self, X, y):
        self.classes_ = np.unique(y)
        self._handle = threading.Lock()  # stand-in for a native resource/unpicklable thing
        return self

    def predict(self, X):
        return np.full(len(X), self.classes_[0])

    def __getstate__(self):
        state = self.__dict__.copy()
        state.pop("_handle")
        return state


check_estimator_internal_state_unchanged_by_inference("Est", Est())
# TypeError: cannot pickle '_thread.lock' object

Would it be possible to compare only the whole-estimator bytes? When they differ, compute the per-attribute pickles to build the error message, and skip any attribute that can't be pickled on its own (not sure what we do wrt error message if this happens). The contract being tested stays the same, I think.

cc @itamarst

Activity

  1. added
    Needs Triage
    on Dec 1, 2026
  2. itamarst commented on Dec 1, 2026

    @itamarst
    Contributor

    From memory, you need to get attributes (or the moral equivalent) initially because the whole problem is they might be mutated in inference; there's nothing to compare to if you only start looking once a difference has emerged.

    But that being said, this is all for debugging purposes, and really shouldn't break things. I'll open a PR to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs Triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions