Open jaheba opened 4 years ago
Thanks @jaheba.
I agree that the current mechanism is a bit strange, however, it does seem to work in practice and as a user I only have to know that I have to add @validated
and things magically work.
Could you give a few more examples for how this would look like for the user with you proposed change?
One thing we should support is serialization/deserialization of classes that are/contain gluon HybridBlocks and their weights.
I propose to re-work how we currently serialise and deserialise objects.
Our current design aims at solving these two issues:
For example:
There are two problems in my view:
validated
works.pickle API
We currently use pickle's
__getnewargs_ex__
to encode objects:I'm unsure if this is good, since we assume that we can also encode an object's parameters, which might not always be the case. Put differently, implementing
__getnewargs_ex__
promisies that the returned arguments can be pickled, but not that they can beencode
d. If we have our own interface, that contract is more explicit.Stateful Objects
We currently don't have a way to encode custom stateful objects.
Looking at pickle, it uses the contents of an objects
__dict__
to store the state by default.Pickle also has
__getstate__
and__setstate__
methods, which offer more control:Proposal
validated
for the common-usecase.Protocol
I think we should add bevaviour similar to
__getstate__
/__setstate__
.Further, I think we should use custom method-names to be more explicit and avoid confusion with pickle.
Better Interface
The main reason
validated
exists that way is that object initialisation is often more complicated than just assigning the arguments. For example, when some arguments depend on others:Instead we could use something like this: