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

Parallelize the TableVectorizer column-wise #592

Merged
merged 62 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
eb0a020
fall back to pandas if no datetime format is found
LeoGrin Jun 10, 2023
f813fbb
change changelog
LeoGrin Jun 10, 2023
5d5e862
Merge branch 'main' of https://github.com/skrub-data/skrub into table…
LeoGrin Jun 12, 2023
d9a374f
first working version
LeoGrin Jun 12, 2023
ffa9755
add tests
LeoGrin Jun 12, 2023
5bcb6dd
update changelog
LeoGrin Jun 12, 2023
c005a49
typo
LeoGrin Jun 12, 2023
85b7f9c
copy transformer when split between columns to avoid conflict
LeoGrin Jun 13, 2023
26e66bd
Merge branch 'main' into table_vec_parallelism
LeoGrin Jun 13, 2023
c3706c7
update changelog
LeoGrin Jun 12, 2023
4147549
fix bug with repeated transformer
LeoGrin Jun 13, 2023
7771eb6
split and merge
LeoGrin Jun 22, 2023
8acca83
more tests
LeoGrin Jun 22, 2023
8dd62cb
merge with main
LeoGrin Jun 22, 2023
b997d28
fix test
LeoGrin Jun 23, 2023
f1ec2dd
also split self.transformers_ to parallelize transform
LeoGrin Jun 23, 2023
198fb04
merging with latest change
LeoGrin Jun 26, 2023
4780dfc
add _split to minhashencoder
LeoGrin Jun 26, 2023
744e881
cleaning
LeoGrin Jun 27, 2023
856892b
clean test
LeoGrin Jun 27, 2023
4cb44a5
merge with main
LeoGrin Jul 17, 2023
e50323a
fix wrong merge
LeoGrin Jul 17, 2023
6d007cf
fix wrong merge
LeoGrin Jul 17, 2023
f2b4266
Merge branch 'main' into table_vec_parallelism
LeoGrin Jul 18, 2023
970a73a
Apply suggestions from code review
LeoGrin Jul 19, 2023
d15e7e5
Update skrub/_utils.py
LeoGrin Jul 19, 2023
4ae9ae2
apply Lilian's suggestions
LeoGrin Jul 19, 2023
b5848e7
add future annotation to avoid circular import in type hints
LeoGrin Jul 19, 2023
6f645fc
add docstring
LeoGrin Jul 19, 2023
9753bd0
Update skrub/_table_vectorizer.py
LeoGrin Jul 20, 2023
6e48fab
type hint
LeoGrin Jul 20, 2023
76ea8a9
Merge branch 'table_vec_parallelism' of https://github.com/LeoGrin/sk…
LeoGrin Jul 20, 2023
63e5afd
revert change
LeoGrin Jul 20, 2023
3b768ea
Merge branch 'main' into table_vec_parallelism
LeoGrin Jul 20, 2023
20682dd
Merge remote-tracking branch 'upstream/main' into table_vec_parallelism
LeoGrin Jul 27, 2023
2fc38b9
Merge branch 'table_vec_parallelism' of https://github.com/LeoGrin/sk…
LeoGrin Jul 27, 2023
5e6966a
first batch of Vincent's suggestions
LeoGrin Aug 4, 2023
da06783
use tags
LeoGrin Aug 4, 2023
3671fb6
Merge branch 'main' into table_vec_parallelism
LeoGrin Aug 4, 2023
f8b177a
compare fitted transformers better
LeoGrin Aug 4, 2023
e1a98ea
Merge branch 'table_vec_parallelism' of https://github.com/LeoGrin/sk…
LeoGrin Aug 4, 2023
02bf26e
talk about tags in changelog
LeoGrin Aug 4, 2023
bb7ad81
run precommit checks
LeoGrin Aug 4, 2023
9dd62bb
get rid of _transformers_original
LeoGrin Aug 8, 2023
d6939f5
clean tests
LeoGrin Aug 18, 2023
9157451
Merge remote-tracking branch 'upstream/main' into table_vec_parallelism
LeoGrin Aug 25, 2023
99b3a9b
Apply suggestions from code review
LeoGrin Aug 26, 2023
769df3a
_parallel_on_columns
LeoGrin Aug 28, 2023
28cfb4c
explain transformers vs transformers_
LeoGrin Aug 29, 2023
e338fe1
Merge branch 'table_vec_parallelism' of https://github.com/LeoGrin/sk…
LeoGrin Aug 29, 2023
9aec058
split merge into two functions
LeoGrin Aug 29, 2023
72dd3d4
add tests to check that splitting doesn't prevent resetting transformers
LeoGrin Aug 29, 2023
2d5fd3a
Apply suggestions from code review
LeoGrin Aug 30, 2023
2eb6f5b
combine merge_unfitted and merge_fitted and move _split outside of class
LeoGrin Aug 30, 2023
7f7b524
don't return empty new_transformer_to_input_indices
LeoGrin Aug 30, 2023
ebcca20
remove future warning
LeoGrin Aug 30, 2023
d8e10ce
remove future warning
LeoGrin Sep 1, 2023
25d854b
add docstrings
LeoGrin Sep 1, 2023
0d381d0
fix merge
LeoGrin Sep 1, 2023
1cef43d
fix test
LeoGrin Sep 1, 2023
eabdb8b
Update skrub/_table_vectorizer.py
LeoGrin Sep 1, 2023
134a413
fix type hints
LeoGrin Sep 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ Major changes
* Parallelized the :class:`GapEncoder` column-wise. Parameters `n_jobs` and `verbose`
added to the signature. :pr:`582` by :user:`Lilian Boulard <LilianBoulard>`


Minor changes
-------------

* :class:`TableVectorizer` is now able to apply parallelism at the column level rather than the transformer level. This is the default for univariate transformers, like :class:`MinHashEncoder`, and :class:`GapEncoder`.
:pr:`592` by :user:`Leo Grinsztajn <LeoGrin>`

* Parallelized the :func:`deduplicate` function. Parameter `n_jobs`
added to the signature. :pr:`618` by :user:`Jovan Stojanovic <jovan-stojanovic>`
and :user:`Lilian Boulard <LilianBoulard>`
Expand Down Expand Up @@ -174,6 +181,8 @@ Minor changes
:pr:`543` by :user:`Leo Grinsztajn <LeoGrin>`
:pr:`587` by :user:`Leo Grinsztajn <LeoGrin>`



Dirty-cat Release 0.4.0
=========================

Expand Down
44 changes: 43 additions & 1 deletion skrub/_gap_encoder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Implements the GapEncoder: a probabilistic encoder for categorical variables.
"""
from __future__ import annotations
Vincent-Maladiere marked this conversation as resolved.
Show resolved Hide resolved

from collections.abc import Generator
from copy import deepcopy
Expand All @@ -13,7 +14,7 @@
from numpy.random import RandomState
from numpy.typing import ArrayLike, NDArray
from scipy import sparse
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.base import BaseEstimator, TransformerMixin, clone
from sklearn.cluster import KMeans, kmeans_plusplus
from sklearn.decomposition._nmf import _beta_divergence
from sklearn.feature_extraction.text import CountVectorizer, HashingVectorizer
Expand Down Expand Up @@ -739,6 +740,43 @@ class GapEncoder(TransformerMixin, BaseEstimator):
fitted_models_: list[GapEncoderColumn]
column_names_: list[str]

@classmethod
def _merge(cls, transformers_list: list[GapEncoder]):
"""
Merge GapEncoders fitted on different columns
into a single GapEncoder. This is useful for parallelization
over columns in the TableVectorizer.
"""
full_transformer = clone(transformers_list[0])
# assert rho_ is the same for all transformers
rho_ = transformers_list[0].rho_
full_transformer.rho_ = rho_
full_transformer.fitted_models_ = []
for transformers in transformers_list:
full_transformer.fitted_models_.extend(transformers.fitted_models_)
if hasattr(transformers_list[0], "column_names_"):
full_transformer.column_names_ = []
for transformers in transformers_list:
full_transformer.column_names_.extend(transformers.column_names_)
return full_transformer

def _split(self):
"""
Split a GapEncoder fitted on multiple columns
into a list of GapEncoders fitted on one column each.
This is useful for parallelizing transform over columns
in the TableVectorizer.
"""
check_is_fitted(self)
transformers_list = []
for i, model in enumerate(self.fitted_models_):
transformer = clone(self)
transformer.rho_ = model.rho_
transformer.fitted_models_ = [model]
transformer.column_names_ = [self.column_names_[i]]
transformers_list.append(transformer)
return transformers_list

def __init__(
self,
*,
Expand Down Expand Up @@ -1025,6 +1063,10 @@ def _more_tags(self):
),
"check_estimators_dtypes": "We only support string dtypes.",
},
"univariate": True, # whether the estimator is univariate and can be
# applied column by column. This is useful for the TableVectorizer,
# to decide whether to apply the transformer on each column separately
# and thus improve the parallelization when the transformer is slow enough.
}


Expand Down
50 changes: 48 additions & 2 deletions skrub/_minhash_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
Implements the MinHashEncoder, which encodes string categorical features by
applying the MinHash method to n-gram decompositions of strings.
"""
from __future__ import annotations
Vincent-Maladiere marked this conversation as resolved.
Show resolved Hide resolved

from collections.abc import Callable, Collection
from typing import Literal

import numpy as np
from joblib import Parallel, delayed, effective_n_jobs
from numpy.typing import ArrayLike, NDArray
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.base import BaseEstimator, TransformerMixin, clone
from sklearn.utils import gen_even_slices, murmurhash3_32
from sklearn.utils.validation import _check_feature_names_in, check_is_fitted

from ._fast_hash import ngram_min_hash
from ._string_distances import get_unique_ngrams
from ._utils import LRUDict, check_input
from ._utils import LRUDict, check_input, combine_lru_dicts

NoneType = type(None)

Expand Down Expand Up @@ -120,6 +121,47 @@ class MinHashEncoder(TransformerMixin, BaseEstimator):

_capacity: int = 2**10

@classmethod
def _merge(cls, transformers_list: list[MinHashEncoder]):
LeoGrin marked this conversation as resolved.
Show resolved Hide resolved
"""
Merge MinHashEncoders fitted on different columns
into a single MinHashEncoder. This is useful for parallelization
over columns in the TableVectorizer.
"""
full_transformer = clone(transformers_list[0])
capacity = transformers_list[0]._capacity
full_transformer.hash_dict_ = combine_lru_dicts(
capacity, *[transformer.hash_dict_ for transformer in transformers_list]
)
full_transformer.n_features_in_ = sum(
transformer.n_features_in_ for transformer in transformers_list
)
full_transformer.feature_names_in_ = np.concatenate(
[transformer.feature_names_in_ for transformer in transformers_list]
)
return full_transformer

def _split(self):
"""
Split a MinHashEncoder fitted on multiple columns
into a list of MinHashEncoders (one for each column).
This is useful for parallelizing transform over columns
in the TableVectorizer.
"""
check_is_fitted(self)
transformer_list = []
for i in range(self.n_features_in_):
trans = clone(self)
attributes = ["hash_dict_", "_capacity"]
for a in attributes:
if hasattr(self, a):
setattr(trans, a, getattr(self, a))
# TODO; do we want to deepcopy hash_dict_
trans.n_features_in_ = 1
trans.feature_names_in_ = np.array([self.feature_names_in_[i]])
transformer_list.append(trans)
return transformer_list

def __init__(
self,
*,
Expand Down Expand Up @@ -395,4 +437,8 @@ def _more_tags(self):
),
"check_estimators_dtypes": "We only support string dtypes.",
},
"univariate": True, # whether the estimator is univariate and can be
# applied column by column. This is useful for the TableVectorizer,
# to decide whether to apply the transformer on each column separately
# and thus improve the parallelization when the transformer is slow enough.
}
Loading
Loading