You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
map_partitions or map_blocks with large objects eats up scheduler memory #7860
When passing some large objects as arguments to map_blocks / map_partitions, the scheduler memory can quickly be overwhelmed. Large objects should be wrapped in delayed to add them to graph and avoid this issue (see here) , but the way the object sizes are determined miss some large objects. In the cases I've encountered, it does not correctly compute the size of scikit-learn estimators.
This is because sys.getsizeof does not properly traverse object references to compute the "real" size of an object. This is a notoriously difficult thing to do in Python, so not sure what the best course of action here should be.
Thanks for the clear writeup @rikturr! Indeed it looks like we're underestimating the size of the RandomForestClassifier model in your example:
In [1]: fromsklearn.ensembleimportRandomForestClassifierIn [2]: rf=RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1)
In [3]: fromdask.sizeofimportsizeofIn [4]: sizeof(rf)
Out[4]: 48
One way to improve the situation is to register a custom sizeof implementation in dask/sizeof.py for scikit-learn estimators which more accurately captures the memory footprint of an estimator.
One thing that comes to mind is to include information from estimator.get_params()
Extra: The booster objects in xgboost and lightgbm would also need to define a __sizeof__. Since both libraries have a soft dependency on scikit-learn, they will get the benefit from Option 2a only when scikit-learn is installed.
Option 2b: To support xgboost/lightgbm better, a more generic solution would be to ducktype the estimator:
Activity
jrbourbeau commentedon Jul 2, 2021
Thanks for the clear writeup @rikturr! Indeed it looks like we're underestimating the size of the
RandomForestClassifiermodel in your example:One way to improve the situation is to register a custom
sizeofimplementation indask/sizeof.pyfor scikit-learn estimators which more accurately captures the memory footprint of an estimator.One thing that comes to mind is to include information from
estimator.get_params()though there may be other attributes which are stored on the class, but not captured by
get_params.cc @thomasjpfan as you might find this interesting
rikturr commentedon Jul 2, 2021
That would be great! I think there is more than in
get_params, if you train the model then pickle the object and check that size it is much larger:Another place this comes up is with xgboost and lightgbm, so would want to consider models from those libraries also
Speaking of pickle, one idea might be to check the size of
pickle.dumps(obj)stsievert commentedon Jul 2, 2021
I think scikit-learn/scikit-learn#8642 tracks this issue for Scikit-Learn.
thomasjpfan commentedon Jul 2, 2021
get_paramsonly shows the parameters in__init__which does not contain the fitted attributes in an estimator.I see a possible way forward to resolve this issue:
__sizeof__. For this specific issue, define it for scikit-learn's tree data structure.BaseEstimatorin https://github.com/dask/dask/blob/main/dask/sizeof.py with aregister_lazythat callssizeofonvars(estimator).__sizeof__. Since both libraries have a soft dependency on scikit-learn, they will get the benefit from Option 2a only when scikit-learn is installed.Option 2b: To support xgboost/lightgbm better, a more generic solution would be to ducktype the estimator:
(Would have been a little easier with Protocol but that requires Python>=3.8)
What does everyone think of either Option 2a or 2b?
jsignell commentedon Jul 19, 2021
I prefer Option 2b. It seems like a good idea to do as well as we can with the
sizeofestimation.