Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH some steps to make cloudpickle dynamic function/classes more deterministic #524

Merged
merged 20 commits into from Nov 23, 2023

Conversation

tomMoral
Copy link
Contributor

@tomMoral tomMoral commented Nov 10, 2023

  • add some utilities to create pickle diff to check for determinist pickle.
  • add some tests to check if the same dynamic class, constructed from scratch or loaded from a cloudpickle stream, leads to the same pickle file:
    • Check for attribute order issues.
    • Check for some string interning issues.

One case seems unsolvable: when updating the class state, all attrname for a class are interned. This is why we are also interning the dynamic func names, to be consistent between their name and the method names (all interned).
But if some values in the pickle stream (anywhere) have the same value as one attrname, then these two strings which are the same in the original pickle file become different in the unpickled objects. Typically, pickling the following original dynamic class results in memorization of join between the method name and the argument value, while pickling the unpickled class leads to no memorization: arg_1 = "join" value is not interned. In contrast, the join method name is.

class A:
    '''Class with potential string interning issues.'''
    arg_1 = "join"

    def join(self, arg_1):
        pass

Not sure if there is a solution for this one.
Note that this test can be run locally with pytest -vv --runxfail -k determinist_subworker_str_interning

Related issues: #510, #453.

Copy link

codecov bot commented Nov 10, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (25aef95) 96.08% compared to head (e18678f) 96.12%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #524      +/-   ##
==========================================
+ Coverage   96.08%   96.12%   +0.04%     
==========================================
  Files           3        3              
  Lines         562      568       +6     
  Branches      116      123       +7     
==========================================
+ Hits          540      546       +6     
  Misses         11       11              
  Partials       11       11              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

Some early feedback.

tests/testutils.py Outdated Show resolved Hide resolved
tests/testutils.py Outdated Show resolved Hide resolved
tests/testutils.py Show resolved Hide resolved
cloudpickle/cloudpickle.py Outdated Show resolved Hide resolved
Copy link
Contributor

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

Here is a pass of feedback. I included some comments on my analysis of the xfailed test about string literals in class attributes. Interning is probably not a safe solution in this case...

cloudpickle/cloudpickle.py Outdated Show resolved Hide resolved
tests/cloudpickle_test.py Outdated Show resolved Hide resolved
tests/cloudpickle_test.py Outdated Show resolved Hide resolved
tests/cloudpickle_test.py Outdated Show resolved Hide resolved
tests/cloudpickle_test.py Outdated Show resolved Hide resolved
tests/cloudpickle_test.py Outdated Show resolved Hide resolved
tests/cloudpickle_test.py Outdated Show resolved Hide resolved
tests/cloudpickle_test.py Outdated Show resolved Hide resolved
tests/testutils.py Outdated Show resolved Hide resolved
tests/testutils.py Show resolved Hide resolved
@tomMoral
Copy link
Contributor Author

I found a better solution to string interning: prevent memoization for problematic strings that might be interned or not.
This is the reverse operation compared to the original proposal that tried to intern everything but this allows to only act on the problematic memoization.
Now all tests pass, except the one about the class hash that we will tackle in a follow-up PR.

For the use of difflib, I am not really convinced because it is useful to see the diff with the full payload to be able to debug (otherwise if is hard to tell from where BINGET 11 comes. This is possible with pytest -vv and the original solution, while difflib is not really telling the full story. So I propose to revert the difflib commit.

Copy link
Contributor

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

More feedback on the new strategy.

cloudpickle/cloudpickle.py Outdated Show resolved Hide resolved
cloudpickle/cloudpickle.py Outdated Show resolved Hide resolved
cloudpickle/cloudpickle.py Outdated Show resolved Hide resolved
tests/cloudpickle_test.py Outdated Show resolved Hide resolved
tests/cloudpickle_test.py Outdated Show resolved Hide resolved
cloudpickle/cloudpickle.py Outdated Show resolved Hide resolved
cloudpickle/cloudpickle.py Show resolved Hide resolved
Copy link
Contributor

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

Can you please add a changelog entry?

There seems to be a leftover call to sys.intern (see below).

Also, I would rather avoid duplicating variations of the same inline comment all over the place and instead make them all point to the inline comment of _class_setstate to limit redundancy and make it more maintainable.

cloudpickle/cloudpickle.py Outdated Show resolved Hide resolved
cloudpickle/cloudpickle.py Outdated Show resolved Hide resolved
cloudpickle/cloudpickle.py Outdated Show resolved Hide resolved
cloudpickle/cloudpickle.py Outdated Show resolved Hide resolved
cloudpickle/cloudpickle.py Show resolved Hide resolved
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
Copy link
Contributor

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

LGTM with the missing changelog entry.

@ogrisel ogrisel merged commit d003266 into cloudpipe:master Nov 23, 2023
21 checks passed
@ogrisel
Copy link
Contributor

ogrisel commented Nov 23, 2023

Thanks @tomMoral!

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.

None yet

2 participants