Skip to content

partition_quantiles finds incorrect minimum with large unsigned integers #7304

Closed

Description

@kylebarron

What happened:

dask.dataframe.partitionquantiles.partition_quantiles finds an incorrect minimum and maximum value with large integer inputs.

What you expected to happen:

For partition_quantiles to find correct minimum and maximum.

Minimal Complete Verifiable Example:

In [1]: import numpy as np
   ...: import pandas as pd
   ...: import dask.dataframe as dd
   ...: from dask.dataframe.partitionquantiles import partition_quantiles

In [2]: pandas_df = pd.DataFrame({
   ...:     'a': np.array([612509347682975743, 616762138058293247], dtype=np.uint64)
   ...: })
   ...: dask_df = dd.from_pandas(pandas_df, npartitions=1)

In [3]: partition_quantiles(dask_df.a, npartitions=1).compute()
Out[3]:
0.0    612509347682975744
1.0    616762138058293248
Name: a, dtype: uint64

In [4]: dask_df.a.min().compute()
Out[4]: 612509347682975743

Anything else we need to know?:

As more commentary on this bug: this is the simplest example I could come up with, but I've been having related issues with set_index on a uint64 column. Specifically, if I load data from an unsorted CSV, and the minimum value is towards the end of the dataset, then calling set_index leaves the minimum value in the last partition!

I can't share the full dataset but this is really interesting:

df = dd.read_csv(path, dtype={'uint_col': np.uint64})
df = df.set_index('uint_col')

df_min_value = df.index.min().compute()
# 612509347682975743
partition_min_value = df.partitions[-1].compute().index.min()
# 612509347682975743
divisions_min_value = df.divisions[0]
# 612509347682975744

df_min_value < divisions_min_value
# True
partition_min_value < divisions_min_value
# False
partition_min_value < np.uint64(divisions_min_value)
# True
df_min_value == partition_min_value
# True

type(df_min_value)
# numpy.int64
type(partition_min_value)
# numpy.uint64
type(divisions_min_value)
# int

🤯

So I'm guessing that there are some buffer overflow issues with uints?

Edit: It seems like it might be helpful to store more things in the same datatype as the index, and prevent unnecessary coercion? For example, this seems like a bug:

df.index.min().dtype
# dtype('uint64')
df.index.min().compute().dtype
# dtype('int64')

I don't understand why the dtype should change there.

Environment:

  • Dask version: 2021.2.0
  • Python version: 3.8.3
  • Operating System: Mac OSX 11.1
  • Install method (conda, pip, source): pip

Activity

  1. eriknw commented on Mar 2, 2021

    @eriknw
    Member

    Thanks for the report @kylebarron!

    I can reproduce. I believe the issue arises from using linear interpolation when operating on integer dtypes. Observe:

    In [1]: import numpy as np
    
    In [2]: a = np.array([612509347682975743, 616762138058293247], dtype=np.uint64)
    
    In [3]: np.percentile(a, [0, 100], interpolation='linear').astype(np.uint64)
    Out[3]: array([612509347682975744, 616762138058293248], dtype=uint64)  # Bad
    
    In [4]: np.percentile(a, [0, 100], interpolation='nearest')
    Out[4]: array([612509347682975743, 616762138058293247], dtype=uint64)  # Good

    This could be solved by a few different ways:

    1. don't perform perform linear interpolation for integer dtypes (always do nearest)
    2. check the min/max value and do linear if the values can safely be cast to/from float64
    3. let the user choose the interpolation method.

    (1) is simple and probably good enough, but may change behavior for existing code in unexpected ways.
    (2) more complicated and introduces an extra min/max computation, but computing the min/max should be very fast.
    (3) is my least favorite choice.

    I lean towards (2).

  2. eriknw commented on Mar 2, 2021

    @eriknw
    Member

    Always using interpolation="nearest" for integer dtypes is probably a very reasonable thing to do. It will help the compression step when combining quantiles from blocks. See relevant comment here: #1261 (comment) (if only we considered domain more carefully back then!).

    Another option is to see if the min/max value from the percentile calculation isn't safe to cast to float, and to move the the next closest floating point value. This may have the least impact to existing code:

    In [1]: import numpy as np
    
    In [2]: a = np.array([612509347682975743, 616762138058293247], dtype=np.uint64)
    
    In [3]: qs = np.percentile(a, [0, 100], interpolation='linear')
    
    In [4]: qs.astype(np.uint64)
    Out[4]: array([612509347682975744, 616762138058293248], dtype=uint64)
    
    In [5]: qs[0] = np.nextafter(qs[0], -np.inf)
    
    In [6]: qs[-1] = np.nextafter(qs[-1], np.inf)
    
    In [7]: qs.astype(np.uint64)
    Out[7]: array([612509347682975616, 616762138058293376], dtype=uint64)
    
    In [8]: qs.astype(np.uint64)[0] < a[0]
    Out[8]: True
    
    In [9]: qs.astype(np.uint64)[-1] > a[-1]
    Out[9]: True
  3. kylebarron commented on Mar 2, 2021

    @kylebarron
    ContributorAuthor

    Glad it's reproducible! It looks like the relevant code is here?

    data = df.values
    interpolation = "linear"
    if is_categorical_dtype(data):
    data = data.codes
    interpolation = "nearest"
    vals, n = _percentile(data, qs, interpolation=interpolation)
    if interpolation == "linear" and np.issubdtype(data.dtype, np.integer):
    vals = np.round(vals).astype(data.dtype)

    So it looks like the float output is being rounded and coerced back to integer as you mentioned above.

    I don't know Dask super well so I don't have a strong opinion on the best fix. Using interpolation="nearest" for integer types seems fine to me.

    Another option is to see if the min/max value from the percentile calculation isn't safe to cast to float, and to move the the next closest floating point value. This may have the least impact to existing code

    One small comment here is that it can be beneficial to know tight bounds on your partition, and it seems like this approach necessarily creates larger bounds than necessary? But maybe that's better to prevent a backwards-incompatible change?

  4. eriknw commented on Mar 2, 2021

    @eriknw
    Member

    Yup, you're understanding of all of the above is correct.

    If you want to make a PR to use nearest interpolation for integer dtypes, @kylebarron, that would be great! We're here to help if you need it.

    But just to be clear, you don't need to make a PR if you don't want to. I can take care of it if you don't. The reproducible bug report is already super-valuable.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions