Skip to content

Tree Reductions on Groupby-aggregations #1658

Closed

Description

@mrocklin

Several Dask.dataframe functions operate by applying a function across all partitions, bringing the results together, and then calling a function on the concatenated results. This applies to functions like groupby-aggregations:

df.groupby(...).column.sum()

If the individual partitions have many groups and there are many partitions, then we try to bring all of these together to one node, which now has to manage many ** 2 values. This is bad both if the node doesn't have enough memory or if there is a bandwidth problem (more likely).

To resolve this we should use tree-reductions. We do this in other parts of the dask codebase, including dask.array reductions and dask.bag reductions. It requires us to think a bit more about our algorithms (we now have a map, an intermediate reduction, and a final reduction) but these are often fairly doable.

The relevant function here is _Frame.reduction.

cc @jreback @jcrist

Activity

  1. mrocklin commented on Oct 13, 2016

    @mrocklin
    MemberAuthor

    If you search in dask.bag for def reduction and split_every you'll see that we took a simpler approach there where we only did the cases where the intermediate-aggregation function and the final-aggregation function were the same. This works well for functions like min, max, sum, distinct, and count, but not for things like mean, var, and std.

    For dask.dataframe we would probably want the full solution.

  2. mrocklin commented on Oct 13, 2016

    @mrocklin
    MemberAuthor

    For example when splitting something like groupby-var we may need to add another function to our current chunk and agg functions:

    def _var_chunk(df, index):
        if isinstance(df, pd.Series):
            df = df.to_frame()
        x = df.groupby(index).sum()
        x2 = (df**2).rename(columns=lambda c: c + '-x2')
        cols = [c + '-x2' for c in x.columns]
        x2 = pd.concat([df, x2], axis=1).groupby(index)[cols].sum()
        n = (df.groupby(index).count()
               .rename(columns=lambda c: c + '-count'))
    
        result = pd.concat([x, x2, n], axis=1)
        return result
    
    
    def _var_agg(g, ddof):
        g = g.groupby(level=0).sum()
        nc = len(g.columns)
        x = g[g.columns[:nc // 3]]
        x2 = g[g.columns[nc // 3:2 * nc // 3]].rename(columns=lambda c: c[:-3])
        n = g[g.columns[-nc // 3:]].rename(columns=lambda c: c[:-6])
    
        result = x2 - x ** 2 / n
        div = (n - ddof)
        div[div < 0] = 0
        result /= div
        result[(n - ddof) == 0] = np.nan
        assert isinstance(result, pd.DataFrame)
        return result
  3. added a commit that references this issue on Oct 18, 2016
    0c11e81
  4. added this to the 0.12.0 milestone on Nov 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions