Skip to content

Inconsistent Tokenization due to Lazy Registration #8111

Closed

Description

@vikshanker

I am running into a case where objects are inconsistently tokenized due to lazy registration of pandas dispatch handlers.

Here is a minimal, reproducible example:

import dask
import pandas as pd


class CustomDataFrame(pd.DataFrame):
    pass

custom_df = CustomDataFrame(data=[1,2,3,4])
pandas_df = pd.DataFrame(data = [4,5,6])
custom_hash_before = dask.base.tokenize(custom_df)
dask.base.tokenize(pandas_df)
custom_hash_after = dask.base.tokenize(custom_df)

print("before:", custom_hash_before)
print("after:", custom_hash_after)
assert custom_hash_before == custom_hash_after

The bug requires a subclass of a pandas (or other lazily registered module) to be tokenized before and then after an object in the module.

It occurs because the Dispatcher used by tokenize decides whether to lazily register modules by checking the toplevel module of the class. Therefore, it will not attempt to lazily register a module of a subclass of an object from the module. However, if the module is already registered, those handlers will be used before the generic object handler when applicable (due to MRO).

The same issue exists for all lazily loaded modules.

I think a better behavior would be for the lazy registration check to walk the MRO of the object and make sure that any lazy-loaded modules are triggered

Activity

  1. jsignell commented on Sep 16, 2021

    @jsignell
    Member

    Thanks for writing this up @v1shanker! I looked at it at the time but forgot to write down what I found 🤦 Looking at it again now I can't quite figure out what the behavior should be. Regardless of whether I've tokenized a pandas object or not, I get a different hash every time I call dask.base.tokenize(custom_df). Is that what you see?

  2. vikshanker commented on Sep 16, 2021

    @vikshanker
    Author

    Yes I do see this behavior now (although I can't remember if I tested this particular case when I wrote this up).

    So it seems there are two different issues here:

    1. How subclasses of DataFrames (and likely other types) are tokenized
    2. The inconsistent dispatching described in the initial issue.

    point 1 likely hides observation of point 2 in most (if not all) cases.

    Thanks for taking a look! Happy to help however I can.

  3. added a commit that references this issue on Sep 27, 2021
    1d6fa74
  4. jcrist commented on Sep 27, 2021

    @jcrist
    Member

    Thanks @v1shanker, I've pushed a patch for this hereat #8185.

    In [1]: import dask
       ...: import pandas as pd
       ...: 
       ...: 
       ...: class CustomDataFrame(pd.DataFrame):
       ...:     pass
       ...: 
       ...: custom_df = CustomDataFrame(data=[1,2,3,4])
       ...: pandas_df = pd.DataFrame(data = [4,5,6])
       ...: custom_hash_before = dask.base.tokenize(custom_df)
       ...: dask.base.tokenize(pandas_df)
       ...: custom_hash_after = dask.base.tokenize(custom_df)
       ...: 
       ...: print("before:", custom_hash_before)
       ...: print("after:", custom_hash_after)
       ...: assert custom_hash_before == custom_hash_after
    before: 3eec44d119a2704d4a7efb951dfd7145
    after: 3eec44d119a2704d4a7efb951dfd7145
  5. added a commit that references this issue on Sep 27, 2021
    8bc0b08
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