Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhancement] check that all sklearnex estimators are centrally tested #2037

Merged
merged 45 commits into from
Oct 10, 2024

Conversation

icfaust
Copy link
Contributor

@icfaust icfaust commented Sep 9, 2024

Description

This adds BasicStatistics and IncrementalBasicStatistics to the SPECIAL_INSTANCES since they cannot be added easily to the patch_map. This adds a test to tests/test_common.py which will check that all estimators which inherit from sklearn's BaseEstimator without a leading underscore are in either PATCHED_MODELS or SPECIAL_INSTANCES such that they are centrally tested via sklearnex/tests

This works by monkeypatching sklearn's all_estimators which is a function sklearn uses internally to discover all estimators in sklearn. This is patched to yield all sklearn-style estimators in sklearnex without using a patch map. This required modifying all sklearn-imported estimators to follow python private variable conventions (leading underscore), which is the bulk of the changes. This is a reasonable change, since we actually would like the sklearn estimators to be private in the various sklearnex modules.

A changes was necessary in BasicStatistics in order for it to be added to SPECIAL_INSTANCES, where it currently cannot be cloned(), it required naming the options attribute to result_options to match the kwarg on __init__. (fixed in a separate PR #2038)

This fixes an issue with IncrementalBasicStatistics where validate_data is called too often for a fit call. It follows the conventions of IncrementalPCA and IncrementalEmpericalCovariance by adding a check_input kwarg boolean.

This PR has no-performance impact as it adds to testing or is renaming variables.

BasicStatistics is shown by stability testing to not be deterministic, this will be added to the documentation.


Checklist to comply with before moving PR from draft:

PR completeness and readability

  • I have reviewed my changes thoroughly before submitting this pull request.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation to reflect the changes or created a separate PR with update and provided its number in the description, if necessary.
  • Git commit message contains an appropriate signed-off-by string (see CONTRIBUTING.md for details).
  • I have added a respective label(s) to PR if I have a permission for that.
  • I have resolved any merge conflicts that might occur with the base branch.

Testing

  • The unit tests pass successfully.
  • I have run it locally and tested the changes extensively.

Performance

  • I have measured performance for affected algorithms using scikit-learn_bench and provided at least summary table with measured data, if performance change is expected.
  • I have provided justification why performance has changed or why changes are not expected.

@icfaust icfaust marked this pull request as ready for review September 10, 2024 04:49
@icfaust icfaust removed the request for review from ahuber21 September 10, 2024 04:49
@icfaust
Copy link
Contributor Author

icfaust commented Sep 10, 2024

@icfaust could you please also include some statistics about the PR change

All in comparison to last main CI run

runner number of tests sklearnex runtime
github py3.9sk1.1 lnx 9086 (+101) 10 min 15 s (+9 s)
github py3.9sk1.1 win 11763 (+123) 20 min 13 s (-10 s)
github py3.10sk1.2 lnx 6309 (+71) 7 min 31 s (+8 s)
github py3.10sk1.2 win 6309 (+71) 13 min 4 s (-54 s)
github py3.11sk1.3 lnx 9154 (+101) 9 min 55 s (+26 s)
github py3.11sk1.3 win 11853 (+123) 19 min 41 s (+16 s)

This adds roughly 1% more testing, and the change in runtime is the variance in the run-to-run times.

sklearnex/tests/test_common.py Outdated Show resolved Hide resolved
print(estimators)
for name, obj in estimators:
# do nothing if defined in preview
if "preview" not in obj.__module__:
Copy link
Contributor

Choose a reason for hiding this comment

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

Just for my understanding why the preview is skipped?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Preview has not been centrally tested up to this point. It would also conflict with the PATCHED_MODELS, as we would need to then bookkeep for two versions of the same estimator throughout testing. Individual preview tests are discovered by pytest, but not discovered in a meaningful way by sklearnex.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can enable preview via env variable in this case they should be centrally tested for patched models don't they?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, luckily so far we don't do that in any of the CI systems. I guess it raises questions about what defines a preview estimator, I assumed it was because of code-quality and/or performance.

Co-authored-by: Samir Nasibli <samir.nasibli@intel.com>
@icfaust
Copy link
Contributor Author

icfaust commented Sep 10, 2024

/intelci: run

@icfaust
Copy link
Contributor Author

icfaust commented Sep 18, 2024

/intelci: run

@icfaust
Copy link
Contributor Author

icfaust commented Sep 20, 2024

/intelci: run

Copy link
Contributor

@samir-nasibli samir-nasibli left a comment

Choose a reason for hiding this comment

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

Overall looks good to me! Thank you @icfaust

Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest highlight this in squashed PR commit message this changes or just move this into separate PR

print(estimators)
for name, obj in estimators:
# do nothing if defined in preview
if "preview" not in obj.__module__:
Copy link
Contributor

Choose a reason for hiding this comment

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

We can enable preview via env variable in this case they should be centrally tested for patched models don't they?

Comment on lines 87 to 91
estimators = all_estimators()
print(estimators)
for name, obj in estimators:
# do nothing if defined in preview
if "preview" not in obj.__module__:
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use estimator as test parameter and pytest.skip preview ones?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question, I would have to move the all_estimators monkeypatch into a fixture and then do an indirect parametrization. Objects like BaseSVM could also show up in the list. It will make sure that multiple failures will both show up, not just the first. If you want me to do it let me know. @Alexsandruss

Copy link
Contributor

Choose a reason for hiding this comment

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

@icfaust, yes, it makes same to do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately using fixtures at collection time is something specifically not supported: pytest-dev/pytest#7140 (comment) In order to do this, I would need to manually monkeypatch instead of using the pytest monkeypatch fixture, which has implications on test isolation. What I will do is change the logic to collect all missing estimators and display them all as a single fail assert.

@icfaust
Copy link
Contributor Author

icfaust commented Sep 25, 2024

/intelci: run

@icfaust
Copy link
Contributor Author

icfaust commented Oct 10, 2024

/intelci: run

@icfaust icfaust merged commit 8883b39 into uxlfoundation:main Oct 10, 2024
25 checks passed
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.

3 participants