This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
Serializing/Deserializing type Any: bugfix and simplification #1344
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary:
Current serialization and de-serialization of type
Any
in PyText has a bug. This diff:Any
.====================
PyText Config Serialization for
Any
PyText config serializes type
Any
by prefixing the real type of the object before the actual object.This type prepending is important for PyText specific types (like
Model
). However, for a generic type likeAny
, this is unnecessary, and instead causes a bug.Eg: if a config looks like this:
class AConfig(ConfigBase):
a_param: Any
And at run-time, the input is:
Pytext will serialize this config by converting
a_param
into adict
, and pre-pending the type of the input:a_config: {
a_param: {'list': [1,2,3]}
}
====================
Bug
When de-serializing a config, we handle this type prepending for
Any
something like this:def _any_from_json(json_obj):
if(_is_dict(json_obj)):
# aha, this must be type pre-pending!
assert(len(json_obj) == 1
# return the first value
return list(json_obj.values())[0]
This creates a bug if something of type
Any
is actually aDict
. E.g.:Assume at run-time, the input is:
a_config: {
"param_2" : {
"nested_param1": 10,
"nested_param2": 2,
}
}
Parsing this config generates an error:
{F235368935}
====================
Fix and simplification
This bug is fixed by simpler serialization of
Any
: type doesn't need to prepended at all====================
Unit Test
Added a unit test that exposes the bug above by taking a json input, and checks that
deserialized(serialized(input)) == input
====================
Who does this affect
AFAIK, only Federated Learning trainers use type
Any
(for information hiding). This doesn't affect anyone other than people working on Federated Learning.Reviewed By: jessemin, psuzhanhy
Differential Revision: D21244134