-
Notifications
You must be signed in to change notification settings - Fork 22
Auto rechunk to enable blockwise reduction #380
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
25dfe8c
Auto rechunk to enable blockwise reduction
dcherian 8bae19e
Small fix.
dcherian b6ac7f7
Comment out chunk size factor
dcherian ed590ee
Add options
dcherian bb92309
Support descending
dcherian 0789c1f
fix init
dcherian 9896d4a
Merge branch 'main' into auto-blockwise-rechunk
dcherian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ | |
| ) | ||
| from .cache import memoize | ||
| from .lib import ArrayLayer, dask_array_type, sparse_array_type | ||
| from .options import OPTIONS | ||
| from .xrutils import ( | ||
| _contains_cftime_datetimes, | ||
| _to_pytimedelta, | ||
|
|
@@ -111,6 +112,7 @@ | |
| # _simple_combine. | ||
| DUMMY_AXIS = -2 | ||
|
|
||
|
|
||
| logger = logging.getLogger("flox") | ||
|
|
||
|
|
||
|
|
@@ -215,8 +217,11 @@ def identity(x: T) -> T: | |
| return x | ||
|
|
||
|
|
||
| def _issorted(arr: np.ndarray) -> bool: | ||
| return bool((arr[:-1] <= arr[1:]).all()) | ||
| def _issorted(arr: np.ndarray, ascending=True) -> bool: | ||
| if ascending: | ||
| return bool((arr[:-1] <= arr[1:]).all()) | ||
| else: | ||
| return bool((arr[:-1] >= arr[1:]).all()) | ||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def _is_arg_reduction(func: T_Agg) -> bool: | ||
|
|
@@ -299,7 +304,7 @@ def _collapse_axis(arr: np.ndarray, naxis: int) -> np.ndarray: | |
| def _get_optimal_chunks_for_groups(chunks, labels): | ||
| chunkidx = np.cumsum(chunks) - 1 | ||
| # what are the groups at chunk boundaries | ||
| labels_at_chunk_bounds = _unique(labels[chunkidx]) | ||
| labels_at_chunk_bounds = pd.unique(labels[chunkidx]) | ||
| # what's the last index of all groups | ||
| last_indexes = npg.aggregate_numpy.aggregate(labels, np.arange(len(labels)), func="last") | ||
| # what's the last index of groups at the chunk boundaries. | ||
|
|
@@ -317,6 +322,8 @@ def _get_optimal_chunks_for_groups(chunks, labels): | |
| Δl = abs(c - l) | ||
| if c == 0 or newchunkidx[-1] > l: | ||
| continue | ||
| f = f.item() # noqa | ||
| l = l.item() # noqa | ||
| if Δf < Δl and f > newchunkidx[-1]: | ||
| newchunkidx.append(f) | ||
| else: | ||
|
|
@@ -708,7 +715,9 @@ def rechunk_for_cohorts( | |
| return array.rechunk({axis: newchunks}) | ||
|
|
||
|
|
||
| def rechunk_for_blockwise(array: DaskArray, axis: T_Axis, labels: np.ndarray) -> DaskArray: | ||
| def rechunk_for_blockwise( | ||
| array: DaskArray, axis: T_Axis, labels: np.ndarray, *, force: bool = True | ||
| ) -> tuple[T_MethodOpt, DaskArray]: | ||
| """ | ||
| Rechunks array so that group boundaries line up with chunk boundaries, allowing | ||
| embarrassingly parallel group reductions. | ||
|
|
@@ -731,14 +740,47 @@ def rechunk_for_blockwise(array: DaskArray, axis: T_Axis, labels: np.ndarray) -> | |
| DaskArray | ||
| Rechunked array | ||
| """ | ||
| # TODO: this should be unnecessary? | ||
| labels = factorize_((labels,), axes=())[0] | ||
|
|
||
| chunks = array.chunks[axis] | ||
| newchunks = _get_optimal_chunks_for_groups(chunks, labels) | ||
| if len(chunks) == 1: | ||
| return "blockwise", array | ||
|
|
||
| # import dask | ||
| # from dask.utils import parse_bytes | ||
| # factor = parse_bytes(dask.config.get("array.chunk-size")) / ( | ||
| # math.prod(array.chunksize) * array.dtype.itemsize | ||
| # ) | ||
| # if factor > BLOCKWISE_DEFAULT_ARRAY_CHUNK_SIZE_FACTOR: | ||
| # new_constant_chunks = math.ceil(factor) * max(chunks) | ||
| # q, r = divmod(array.shape[axis], new_constant_chunks) | ||
| # new_input_chunks = (new_constant_chunks,) * q + (r,) | ||
| # else: | ||
| new_input_chunks = chunks | ||
|
|
||
| # FIXME: this should be unnecessary? | ||
| labels = factorize_((labels,), axes=())[0] | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: get rid of this line |
||
| newchunks = _get_optimal_chunks_for_groups(new_input_chunks, labels) | ||
| if newchunks == chunks: | ||
| return array | ||
| return "blockwise", array | ||
|
|
||
| Δn = abs(len(newchunks) - len(new_input_chunks)) | ||
| if pass_num_chunks_threshold := ( | ||
| Δn / len(new_input_chunks) < OPTIONS["rechunk_blockwise_num_chunks_threshold"] | ||
| ): | ||
| logger.debug("blockwise rechunk passes num chunks threshold") | ||
| if pass_chunk_size_threshold := ( | ||
| # we just pick the max because number of chunks may have changed. | ||
| (abs(max(newchunks) - max(new_input_chunks)) / max(new_input_chunks)) | ||
| < OPTIONS["rechunk_blockwise_chunk_size_threshold"] | ||
| ): | ||
| logger.debug("blockwise rechunk passes chunk size change threshold") | ||
|
|
||
| if force or (pass_num_chunks_threshold and pass_chunk_size_threshold): | ||
| logger.debug("Rechunking to enable blockwise.") | ||
| return "blockwise", array.rechunk({axis: newchunks}) | ||
| else: | ||
| return array.rechunk({axis: newchunks}) | ||
| logger.debug("Didn't meet thresholds to do automatic rechunking for blockwise reductions.") | ||
| return None, array | ||
|
|
||
|
|
||
| def reindex_numpy(array, from_: pd.Index, to: pd.Index, fill_value, dtype, axis: int): | ||
|
|
@@ -2704,6 +2746,11 @@ def groupby_reduce( | |
| has_dask = is_duck_dask_array(array) or is_duck_dask_array(by_) | ||
| has_cubed = is_duck_cubed_array(array) or is_duck_cubed_array(by_) | ||
|
|
||
| if method is None and is_duck_dask_array(array) and not any_by_dask and by_.ndim == 1 and _issorted(by_): | ||
| # Let's try rechunking for sorted 1D by. | ||
| (single_axis,) = axis_ | ||
| method, array = rechunk_for_blockwise(array, single_axis, by_, force=False) | ||
|
|
||
| is_first_last = _is_first_last_reduction(func) | ||
| if is_first_last: | ||
| if has_dask and nax != 1: | ||
|
|
@@ -2891,7 +2938,7 @@ def groupby_reduce( | |
|
|
||
| # if preferred method is already blockwise, no need to rechunk | ||
| if preferred_method != "blockwise" and method == "blockwise" and by_.ndim == 1: | ||
| array = rechunk_for_blockwise(array, axis=-1, labels=by_) | ||
| _, array = rechunk_for_blockwise(array, axis=-1, labels=by_) | ||
|
|
||
| result, groups = partial_agg( | ||
| array=array, | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| """ | ||
| Started from xarray options.py; vendored from cf-xarray | ||
| """ | ||
|
|
||
| import copy | ||
| from collections.abc import MutableMapping | ||
| from typing import Any | ||
|
|
||
| OPTIONS: MutableMapping[str, Any] = { | ||
| # Thresholds below which we will automatically rechunk to blockwise if it makes sense | ||
| # 1. Fractional change in number of chunks after rechunking | ||
| "rechunk_blockwise_num_chunks_threshold": 0.25, | ||
| # 2. Fractional change in max chunk size after rechunking | ||
| "rechunk_blockwise_chunk_size_threshold": 1.5, | ||
| # 3. If input arrays have chunk size smaller than `dask.array.chunk-size`, | ||
| # then adjust chunks to meet that size first. | ||
| # "rechunk.blockwise.chunk_size_factor": 1.5, | ||
| } | ||
|
|
||
|
|
||
| class set_options: # numpydoc ignore=PR01,PR02 | ||
| """ | ||
| Set options for cf-xarray in a controlled context. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| rechunk_blockwise_num_chunks_threshold : float | ||
| Rechunk if fractional change in number of chunks after rechunking | ||
| is less than this amount. | ||
| rechunk_blockwise_chunk_size_threshold: float | ||
| Rechunk if fractional change in max chunk size after rechunking | ||
| is less than this threshold. | ||
|
|
||
| Examples | ||
| -------- | ||
|
|
||
| You can use ``set_options`` either as a context manager: | ||
|
|
||
| >>> import flox | ||
| >>> with flox.set_options(rechunk_blockwise_num_chunks_threshold=1): | ||
| ... pass | ||
|
|
||
| Or to set global options: | ||
|
|
||
| >>> flox.set_options(rechunk_blockwise_num_chunks_threshold=1): | ||
| """ | ||
|
|
||
| def __init__(self, **kwargs): | ||
| self.old = {} | ||
| for k in kwargs: | ||
| if k not in OPTIONS: | ||
| raise ValueError(f"argument name {k!r} is not in the set of valid options {set(OPTIONS)!r}") | ||
| self.old[k] = OPTIONS[k] | ||
| self._apply_update(kwargs) | ||
|
|
||
| def _apply_update(self, options_dict): | ||
| options_dict = copy.deepcopy(options_dict) | ||
| OPTIONS.update(options_dict) | ||
|
|
||
| def __enter__(self): | ||
| return | ||
|
|
||
| def __exit__(self, type, value, traceback): | ||
| self._apply_update(self.old) |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.