Skip to content

Fix for normalize_function dataclass methods#8527

Merged
jrbourbeau merged 14 commits intodask:mainfrom
scharlottej13:fix-normalize-func
Feb 23, 2022
Merged

Fix for normalize_function dataclass methods#8527
jrbourbeau merged 14 commits intodask:mainfrom
scharlottej13:fix-normalize-func

Conversation

@scharlottej13
Copy link
Contributor

@scharlottej13 scharlottej13 changed the title change protocol to None fix for normalize_function dataclass methods Jan 3, 2022
@scharlottej13 scharlottej13 marked this pull request as ready for review January 13, 2022 00:49
@scharlottej13
Copy link
Contributor Author

The failed checks seem limited to python 3.7, so this change seems 'safe'? Would still love to know why protocol=0 was there in the first place!

Copy link
Collaborator

@ian-r-rose ian-r-rose left a comment

Choose a reason for hiding this comment

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

This seems fairly safe to me, but we should add a test for the original issue to prevent regressions.

@jsignell jsignell mentioned this pull request Jan 28, 2022
2 tasks
dask/base.py Outdated
import cloudpickle

return cloudpickle.dumps(func, protocol=0)
return cloudpickle.dumps(func, protocol=None)
Copy link
Member

Choose a reason for hiding this comment

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

We want to make sure the version of pickle used for tokenizing is identical across all versions of python that we care about. In this case specifying protocol=None means to use the default version of pickle for the provided python, which can change in a new release (creating an incompatibility). I'd rather we hardcode protocol 4 (supported for python >= 3.4) here so that if a new python release makes protocol 5 the default things don't implicitly change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, thanks for the explanation Jim! I'll change this and also add in a test per @ian-r-rose's suggestion (sorry for the delay on that!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From reading this thread on #8557 it seems this should be changed to protocol=4 in both cloudpickle.dumps calls

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually, I think this test Gabe added addresses the original issue

Copy link
Collaborator

Choose a reason for hiding this comment

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

@scharlottej13 does it? I had thought it wasn't quite the same thing #8557 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gjoseph92 the test I was going to add was to create a new DataClass and then call delayed on a function in that class, but really that'd be testing if it's tokenizing correctly, right? I guess the part that might still be missing is testing with pure=True, ie this snippet with pure=False works with the current protocol=0, but doesn't with pure=True:

from dask import delayed
from dataclasses import dataclass, field


@dataclass(frozen=True)
class A:
    param: float = field(repr=False)

    def get_param(self):
        return self.param

    def get_delayed_param(self, *args, **kwargs):
        return delayed(self.get_param)(*args, **kwargs)

(A(1).get_delayed_param() - A(0).get_delayed_param()).compute()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

summary from chat w/ @gjoseph92-- it does seem that the test in #8557 will also test the underlying reason for the original issue (tokenizing DataClasses), but I'll add in a simple test that more explicitly addresses the original issue since there is the additional layer of having a bound method.

@jsignell
Copy link
Member

Seems like you've got some genuine failures here.

@scharlottej13
Copy link
Contributor Author

Seems like you've got some genuine failures here.

Thanks for flagging @jsignell, I'll have a look!

@gjoseph92
Copy link
Collaborator

See some discussion in #8557 (comment). We'd have to change some tests for this to pass. I mostly agree with @ian-r-rose that these aren't very good tests. But they are explicitly testing that tokenization is stable across versions, which isn't a guarantee I knew we made. Since we'd be explicitly breaking that, we should be thoughtful about why there's a test for it first.

@gjoseph92
Copy link
Collaborator

Note @jcrist's comment on changing the pickle protocol: #8557 (comment). So we should be good to change the protocol and make those tests more sensible.

@scharlottej13
Copy link
Contributor Author

Note @jcrist's comment on changing the pickle protocol: #8557 (comment). So we should be good to change the protocol and make those tests more sensible.

Thanks @gjoseph92! Does this mean you're planning to change the tests in #8557, and we should wait for this PR until those are in?

@gjoseph92
Copy link
Collaborator

I was thinking we'd change the protocol and the tests here?

@scharlottej13
Copy link
Contributor Author

I was thinking we'd change the protocol and the tests here?

Ah ok, sounds good, sorry I misunderstood that!

@scharlottej13
Copy link
Contributor Author

@gjoseph92 the tests I'm seeing that need to be changed (from looking at CI failures) are:

Is this right? From @jcrist's comment it seems like there are more tests we'd want to change as well, but I'm not exactly sure which.

@gjoseph92
Copy link
Collaborator

@scharlottej13 those are the only ones I know of (and the only ones that seem to be failing right now). I'd say, if it's green, we're good.

@jsignell jsignell added bug Something is broken core labels Feb 4, 2022
@jsignell
Copy link
Member

It looks like there are maybe some test left to change on this and then it'll be good to go?

@scharlottej13
Copy link
Contributor Author

It looks like there are maybe some test left to change on this and then it'll be good to go?

yes! I can work on those today

@jrbourbeau jrbourbeau changed the title fix for normalize_function dataclass methods Fix for normalize_function dataclass methods Feb 22, 2022
Copy link
Member

@jrbourbeau jrbourbeau left a comment

Choose a reason for hiding this comment

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

Thanks @scharlottej13. One small style nit, but otherwise this looks good to go



def test_normalize_function_dataclass_field_no_repr():
from dataclasses import field, make_dataclass
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: Could you move these imports to the top of this module along with the other dataclasses imports we already have

from dataclasses import dataclass

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good!

Comment on lines +229 to +238
b"\x80\x04\x95\x1f\x00\x00\x00\x00\x00\x00\x00\x8c\x14dask.tests.test_base\x94\x8c\x02f3\x94\x93\x94.",
(
b"\x80\x04\x95\x1f\x00\x00\x00\x00\x00\x00\x00\x8c\x14dask.tests.test_base\x94\x8c\x02f2\x94\x93\x94.",
),
(
(
"c",
b"\x80\x04\x95\x1f\x00\x00\x00\x00\x00\x00\x00\x8c\x14dask.tests.test_base\x94\x8c\x02f1\x94\x93\x94.",
),
),
Copy link
Member

Choose a reason for hiding this comment

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

I agree it would be nice to make this (and other similar tests) less brittle, but it seems there aren't any suggestions at the moment on how to do that. In my mind what you have here is brittle in the same way as what's already on main. Given this, I'm inclined to merge this in as is and improve tests in a follow up PR if folks think of a nice way to do so.

Copy link
Member

@jrbourbeau jrbourbeau left a comment

Choose a reason for hiding this comment

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

Thanks @scharlottej13 -- this is in

@jrbourbeau jrbourbeau merged commit 61db08d into dask:main Feb 23, 2022
@scharlottej13 scharlottej13 deleted the fix-normalize-func branch October 4, 2022 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something is broken core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants