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

RandomType pickling issues #638

Closed
ricardoV94 opened this issue Oct 29, 2021 · 1 comment · Fixed by #640
Closed

RandomType pickling issues #638

ricardoV94 opened this issue Oct 29, 2021 · 1 comment · Fixed by #640
Assignees
Labels
bug Something isn't working

Comments

@ricardoV94
Copy link
Contributor

ricardoV94 commented Oct 29, 2021

This problem was seen in pymc-devs/pymc#5090

import numpy as np
import aesara
# import cloudpickle as pickle  # either library fails
import pickle

# same problem with aesara.shared(np.random.RandomState())
var1 = aesara.shared(np.random.default_rng())
var2 = aesara.shared(np.random.default_rng())
print(var2)  # RandomGeneratorSharedVariable(<Generator(PCG64) at 0x7FC441E6EAC0>)

res = var1.type.filter_variable(var2)
print(res)  # RandomGeneratorSharedVariable(<Generator(PCG64) at 0x7FC441E6EAC0>)

var2 = pickle.loads(pickle.dumps(var2))
print(var2)  # RandomGeneratorSharedVariable(<Generator(PCG64) at 0x7FC441E853C0>)

res = var1.type.filter_variable(var2)  # ! raises TypeError
TypeError: Cannot convert Type RandomGeneratorType (of Variable <RandomGeneratorType>) into Type RandomGeneratorType. You can try to manually convert <RandomGeneratorType> into a RandomGeneratorType.
@ricardoV94 ricardoV94 changed the title RandomGeneratorType pickling issues RandomType pickling issues Oct 29, 2021
@ricardoV94 ricardoV94 added the question Further information is requested label Oct 29, 2021
@brandonwillard
Copy link
Member

brandonwillard commented Oct 29, 2021

The RandomGeneratorType instances are not equal, because the pickle/un-pickle process needs to construct a new one when it builds the last instance of var2 (i.e. it doesn't have a reference to the original instance).

These Type subclasses should probably be singletons (i.e. we need to implement custom __reduce__ methods that use the singleton instances). A cheaper solution is to implement type-only checking in custom __eq__ methods on those subclasses.

@brandonwillard brandonwillard added bug Something isn't working and removed question Further information is requested labels Oct 29, 2021
@brandonwillard brandonwillard self-assigned this Oct 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants