Skip to content

Dataframe tree reductions - #1663

Merged
jcrist merged 8 commits into
dask:masterfrom
jcrist:dd-tree-reduction
Oct 18, 2016
Merged

jcrist merged 8 commits into
dask:masterfrom
jcrist:dd-tree-reduction

Conversation

@jcrist

@jcrist jcrist commented Oct 14, 2016 •

Copy link
Copy Markdown
Member

This adds tree reductions to dask.dataframe, allowing for more efficient reductions on dataframes with many partitions. All relevant methods now include an optional keyword split_every, which indicates the max number of intermediates to bring together before reducing. The default is 8. The reduction method (and the less publicly used apply_concat_apply function) also take an optional combine function, to do the intermediate aggregations, before passing to aggregate for the final reduction.

Fixes #1658.

Todo:

  • methods in groupby support tree reductions
  • tests

[ ] cov_corr supports tree reductions
[ ] quantile supports tree reductions (to be done later, see #1672)

Comment thread dask/dataframe/core.py Outdated
while k > split_every:
b = prefix + str(depth)
for part_i, inds in enumerate(partition_all(split_every, range(k))):
conc = (_concat, (list, [(a, i) for i in inds]))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We may no longer need the explicit call to (list, ...)

Comment thread dask/dataframe/core.py

@derived_from(pd.Series)
def nlargest(self, n=5):
def nlargest(self, n=5, split_every=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We will probably eventually want to put in some defaults for these. For operations like nlargest I suspect it'll be in the tens and for operations like value_counts or some of the groupby computations I suspect it'll be around 10.

@mrocklin

Copy link
Copy Markdown
Member

FWIW the times when this has come up it has always been about groupby-aggregations. If quantiles or covariance end up being tricky I would recommend putting them off until after groupby-aggregations.

@mrocklin

Copy link
Copy Markdown
Member

That's only if you find yourself under time pressure. I'm not sure how hard these things are.

@mrocklin

Copy link
Copy Markdown
Member

We should think about defaults. These seem to largely depend on the size of the intermediates. Presumably scalar-valued intermediates like sum, min, max can use split_every=None. For series or dataframe-valued intermediates such as in groupbys this can vary a bit. If there are very many groups then we might prefer a modest default, like 8. I think that we should design the default around worst-case.

@jreback if you have experience here with many-grouped groupbys and have the hardware to perform a fast benchmark on a small range of values (e.g. split_every=n for n in (2, 4, 8, 16, 32, 64)) that would be appreciated.

@jreback

jreback commented Oct 17, 2016

Copy link
Copy Markdown
Contributor

The 8 refers to the number of independent tasks you want to process?

When can you set these defaults? meaning at the start of the computation do we know the number of groups that this will generate? (so that can give you an idea of the size of the intermediates)

@mrocklin

Copy link
Copy Markdown
Member
df.groupby(...).sum(split_every=8)

The split_every keyword corresponds to the maximum number of children that we bundle together at once.

If we have 16 partitions and use split_every=2 then we'll aggregate the intermediate partitions to a set of 8, then a set of 4, then a set of 2, then finally a set of 1, which will be our result.

If we have 16 partitions and use split_every=4 then we'll aggregate the intermediate partitions to a set of 4 and then a final set of 1.

The lower the number is the safer (fewer intermediate partitions get concat-ed into one task) but also the more overhead there is.

@jcrist

jcrist commented Oct 17, 2016

Copy link
Copy Markdown
Member Author

Presumably scalar-valued intermediates like sum, min, max can use split_every=None.

Just a quick clarification: the default of split_every=None results in a default of split_every=8, mimicking the interface used in dask.bag. split_every=False results in using no tree-reductions. I'm not sure if this is the best interface, but it's consistent with the other collections.

@mrocklin

Copy link
Copy Markdown
Member

Ah, thanks for clarifying

@jreback

jreback commented Oct 17, 2016

Copy link
Copy Markdown
Contributor

So just to give some real-world (on the previous impl). I was effectively having split_every=1, IOW no-bundling, which works, just takes a lot of time and isn't very efficient. So happy with a default of 8.

I suppose this could somehow be dynamically calculated based on the sizes with some sort of cut-off (memory and/or intermediate size)?

@mrocklin

Copy link
Copy Markdown
Member

You were getting split_every=infinity before. It's great if the size of the intermediates is small but involves moving a lot of data to a single node if the size of the intermediates is considerable.

Dynamic calculation of the ideal value here would be very hard. I'm more inclined to use a default value that works well in the many-grouped partitions case, and swallow the overhead in the few-grouped partitions case.

@jreback

jreback commented Oct 17, 2016

Copy link
Copy Markdown
Contributor

ok sure. I can give this a try. To clarify, I would need to install this PR on: workers, scheduler, notebookenv? (all 3 or just on notebook)?

@mrocklin

Copy link
Copy Markdown
Member

All

@jreback

jreback commented Oct 17, 2016

Copy link
Copy Markdown
Contributor

I c. ok will give a try tomorrow then (of course free to merge when ready)

@jcrist
jcrist force-pushed the dd-tree-reduction branch from 659af94 to d53c5fc Compare October 17, 2016 21:03
@jcrist

jcrist commented Oct 17, 2016

Copy link
Copy Markdown
Member Author

Ok, this is ready for review. I'd prefer to finish the cov/corr/quantile work in a separate PR (unless it's necessary here).

A sensible default is still needed. Right now the default is 8 for all operations, and lives in apply_concat_apply. I think a sensible default for all operations should live here, and specific defaults for each operation should be added elsewhere. I'm not sure what the default on reduction should be - I'm apt to make it the same as apply_concat_apply, since it's just as general of an operation.

@mrocklin

Copy link
Copy Markdown
Member

Waiting on cov / corr / quantile sounds good to me. If you raised this as an issue with the introductory label and gave pointers to a similar problem (like var?) then this seems like the kind of task that might be picked up by a new developer.

The default of 8 seems sensible to me. We can tweak in the future if we need to.

@jcrist

jcrist commented Oct 17, 2016

Copy link
Copy Markdown
Member Author

Sounds good. I've fixed the failing test (due to a bug in pandas 0.18.0 that has since been fixed). Was there anything else that should be done here, or is everything else future work?

I'm not sure if the cov/corr stuff is a beginner issue, but I'll file an issue anyway. I might be able to get to it later this week.

@jcrist jcrist removed the scheduler label Oct 17, 2016
@mrocklin

Copy link
Copy Markdown
Member

It seems like an moderate-to-expert pandas issue but a beginner dask.dataframe issue perhaps? I might be overly optimistic here.

@jcrist jcrist changed the title [WIP] Dataframe tree reductions Dataframe tree reductions Oct 17, 2016
@mrocklin

Copy link
Copy Markdown
Member

I'd like to request that we change the default to False for all scalar reductions.

@jcrist
jcrist force-pushed the dd-tree-reduction branch from ae421ef to 0816f9c Compare October 18, 2016 15:45
@jcrist

jcrist commented Oct 18, 2016

Copy link
Copy Markdown
Member Author

Done. All scalar reductions (defined as operations that have scalars as intermediates) now have split_every=False for default.

@mrocklin mrocklin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that we should also use split_every=False in cases where the intermediates are series/dataframes of a fixed small number of rows.

Comment thread dask/dataframe/core.py Outdated

@derived_from(pd.DataFrame)
def sum(self, axis=None, skipna=True):
def sum(self, axis=None, skipna=True, split_every=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this be split_every=False?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Was assuming only for scalar intermediates, this is a row intermediate. Fixed.

Comment thread dask/dataframe/core.py Outdated
return super(Series, self).sum(axis=axis, skipna=skipna)
def sum(self, axis=None, skipna=True, split_every=False):
return super(Series, self).sum(axis=axis, skipna=skipna,
split_every=None)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should probably be split_every=split_every

Comment thread dask/dataframe/core.py Outdated
def nunique(self):
return self.drop_duplicates().count()
def nunique(self, split_every=None):
return self.drop_duplicates(split_every=None).count()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

split_every=split_every

Not integrated with rest of codebase yet, but added option to aca.
Except for ``cov``, ``corr``, and ``quantile``, all methods are
supported.
Still need to add tests.
Due to a bug in pandas 0.18.0, certain series groupby operations lose
the series name. Since this is fixed in 0.18.1 and 0.19.0, we'll keep
our code the same and just ignore the name checks in the test.
@jcrist
jcrist force-pushed the dd-tree-reduction branch from 0816f9c to bc5111d Compare October 18, 2016 18:46
@jcrist

jcrist commented Oct 18, 2016

Copy link
Copy Markdown
Member Author

Addressed all comments, and rebased to fix merge conflicts with the renaming PR.

@mrocklin

Copy link
Copy Markdown
Member

+1 to merge from me

@jreback

jreback commented Oct 18, 2016 •

Copy link
Copy Markdown
Contributor

@jcrist @mrocklin

so trying the tree-reduction PR: 0816f9c (most recent about 1 hr ago, doesn't include the very recent fix).
used memory much more evenly (IOW was not all on 1 process), so that's nice.

on my big dataset, about 1.5B rows, 50M result set (grouping on a non-categorized text column).
reducing on a float column, with a modest cluster (100+ cores).

result = dd.concat([g.sum(), g.mean(), g.count(), g.min(), g.max()], axis=1)

was about 37min (before tree-reduction)

with split_every (default) -> 15min

I am doing split_every=4 and will update in a couple of min (just tried on all ops).

result = (dd.concat([g.sum(split_every=4), 
                g.mean(split_every=4), 
                g.count(split_every=4), 
                g.min(split_every=4), 
                g.max(split_every=4)], 
            axis=1)
    )

18min

@jcrist
jcrist merged commit 0c11e81 into dask:master Oct 18, 2016
@jcrist
jcrist deleted the dd-tree-reduction branch October 18, 2016 19:28
@mrocklin

Copy link
Copy Markdown
Member

cc @hussainsultan I think you asked about this earlier

@jcrist

jcrist commented Oct 18, 2016

Copy link
Copy Markdown
Member Author

Thanks for the benchmark Jeff, that's a nice speedup :).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants