Skip to content

Estimate dict sizes properly - #6154

Merged
mrocklin merged 3 commits into
dask:masterfrom
fjetter:more_accurate_dictsizeof
May 4, 2020
Merged

mrocklin merged 3 commits into
dask:masterfrom
fjetter:more_accurate_dictsizeof

Conversation

@fjetter

@fjetter fjetter commented Apr 30, 2020 •

Copy link
Copy Markdown
Member

This would be the implementation alternative for #6149 which is supposed to address the issue that the size of intermediate shuffle tasks are not measured since they are usually dicts with > 10 keys.

This issue has been frequently discussed and there were some concerns about performance, most notably dask/distributed#1626

For the sampling I also tried to use (cy)toolz.itertoolz.random_sample but the builtin random performed the same so I chose to go this way.

Regarding mirco benchmarks of the function this code is useful

import sys
from distutils.version import LooseVersion
from dask.sizeof import sizeof

from cytoolz.itertoolz import map
getsizeof = sys.getsizeof

import random
def sizeof_python_collection(seq):
    
    num_items = len(seq)
    samples = 10
    if num_items > samples:
        return getsizeof(seq) + num_items / samples * sum(
            map(sizeof, random.sample(seq, samples))
        )
    else:
        return getsizeof(seq) + sum(map(sizeof, seq))


def sizeof_python_dict(d):
    return (
        getsizeof(d)
        + sizeof_python_collection(list(d.keys()))
        + sizeof_python_collection(list(d.values()))
        - 2 * sizeof_python_collection(list())
    )

def sizeof_python_dict_orig(d):
    if len(d) > 10:
        return getsizeof(d) + 1000 * len(d)
    else:
        return getsizeof(d) + sum(map(sizeof, d.keys())) + sum(map(sizeof, d.values()))

Benchmarks from https://github.com/dask/dask-benchmarks will follow

xref #2456

@fjetter

fjetter commented Apr 30, 2020 •

Copy link
Copy Markdown
Member Author

In terms of microbenchmarks of the new code, see the screenshot below.

What we can observe is that the new function is consistently slower (even for the sample_dict_very_small) case which is something I haven't understood, yet.

image

The dask-benchmarks for distributed stay the same within margin of error so I'll refrain from posting the results here. Not sure if the benchmarks include everything relevant

@mrocklin

Copy link
Copy Markdown
Member

This looks good to me. I suggest two small changes:

  1. unless using cytoolz.map makes this significantly faster than builtins.map let's just use builtins for simplicity
  2. Maybe add a test with a dictionary with 100 numpy arrays?

Thank you for doing this @fjetter

@fjetter

fjetter commented Apr 30, 2020

Copy link
Copy Markdown
Member Author

Regarding benchmarks the numpy arrays behave similar to the lists but a bit faster
image

Did you mean an actual unit test? Numpy arrays take another code path via the dispatch and should generally not be affected by this change. note that the benchmark code above is slightly different than the actual code in the PR. The PR code uses the proper dispatch, the benchmark code above doesn't to allow running both pieces in the same interpreter

@mrocklin

Copy link
Copy Markdown
Member

Did you mean an actual unit test? Numpy arrays take another code path via the dispatch and should generally not be affected by this change

Even if the dictionary is larger than ten elements?

@quasiben

Copy link
Copy Markdown
Member

I was curious about this PR when applied to GPU objects and in both cases, sizeof_python_dict_orig was fastest

In [3]: import cudf

In [5]: sr = cudf.Series([1, 2, 3])

In [6]: sample_dict = {k: sr for k in range(100)}

In [7]: %timeit sizeof_python_dict(sample_dict)
43.1 µs ± 226 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [8]: %timeit sizeof_python_dict_orig(sample_dict)
361 ns ± 0.826 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [10]: arr = cupy.arange(100)

In [11]: sample_dict = {c: arr for c in range(100)}

In [12]: %timeit sizeof_python_dict(sample_dict)
32.3 µs ± 122 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [13]: %timeit sizeof_python_dict_orig(sample_dict)
361 ns ± 3.55 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

@fjetter

fjetter commented May 4, 2020 •

Copy link
Copy Markdown
Member Author

Even if the dictionary is larger than ten elements?

@mrocklin Yes, the recursion order for a dict like { "key": np.array([1]) } looks like

sizeof_dict
-> sys.getsizeof(dict)
-> sizeof_python_collection( keys: List[str] )
-> sys.getsizeof (key: str) for 10 sampled keys
-> sizeof_python_collection( values: List[np.ndarray] )
-> sizeof_numpy_ndarray (value: np.ndarray) for 10 sampled values

In particular, the sampling within sizeof_python_collection is only applied to the list of values, not to the values themselves. This means that if the values are dataframes, arrays, etc. they are always using the proper dispatch but we're only evaluating 10 sampled dictionary values. Measuring a numpy array directly does not even cross paths with sizeof_python_collection.

The only change to previous functionality (other than dicts being more accurately estimated) is that now all python builtin collections are sampled instead of fully measured.

I was curious about this PR when applied to GPU objects and in both cases, sizeof_python_dict_orig was fastest

@quasiben The sizeof_python_dict_orig is always fastest since it doesn't inspect the structure of the dictionary values but estimates all values to be of constant size 1KiB. Therefore, the original is always faster but very inaccurate

@fjetter

fjetter commented May 4, 2020

Copy link
Copy Markdown
Member Author

Removed the cytoolz import again since in the end we loop over ten elements where there is no speedup

@mrocklin

mrocklin commented May 4, 2020

Copy link
Copy Markdown
Member

In particular, the sampling within sizeof_python_collection is only applied to the list of values, not to the values themselves. This means that if the values are dataframes, arrays, etc. they are always using the proper dispatch but we're only evaluating 10 sampled dictionary values. Measuring a numpy array directly does not even cross paths with sizeof_python_collection.

The only change to previous functionality (other than dicts being more accurately estimated) is that now all python builtin collections are sampled instead of fully measured.

Right, but I think that a dict of numpy arrays or pandas dataframes would measure differently now. My understanding is that this is your primary motivation for this change. You wanted dicts of dataframes to be measured somewhat accurately. It would be good to put that desired behavior into a test so that it doesn't get accidentally reverted.

@mrocklin

mrocklin commented May 4, 2020

Copy link
Copy Markdown
Member

I've added a small test

@fjetter

fjetter commented May 4, 2020

Copy link
Copy Markdown
Member Author

You wanted dicts of dataframes to be measured somewhat accurately. It would be good to put that desired behavior into a test so that it doesn't get accidentally reverted.

Got it. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants