Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize data classes based on their fields only (#2697)
Summary: Pull Request resolved: #2697 # Context: **I think the unit test is the easiest way to understand this!** There are several cases in Ax where an instance has an attribute that 1) can't or shouldn't be serialized; this is usually subclasses of `torch.nn.Module`, including BoTorch models, but can be other large and complex objects, 2) isn't typically passed at initialization, but rather constructed afterwards, and 3) should be an attribute rather than a property because it's too expensive to construct more than once These classes have thus required custom serialization logic so that such attributes are not serialized. Classes that have this issue include many benchmarking classes that work with surrogates or neural nets, such as `SurrogateRunner`, `PyTorchCNNTorchvisionRunner`, and `PyTorchCNNTorchvisionBenchmarkProblem`, as well as MBM classes. A simpler solution is to use dataclasses, by - specifying features that satisfy (1)-(3) with `InitVar`, and, if they are needed immediately, constructing them in the `__post_init__` - only serializing fields; `InitVar`s are not fields This gives more flexibility in what we serialize without taking any away: If an attribute is constructed in the post-init and *should* be serialized, that is still supported by marking it as a `field` and not an `InitVar`. Attributes that are constructed in the init will be serialized, even if they are modified elsewhere. ## Downside It is not quite the normal usage to use an `InitVar` to define a persistent non-field attribute; instead `InitVar` is intended for something that is needed only for initializing fields. So the usage pattern outlined in the unit test causes Pyre errors, and someone who uses an `InitVar` that way might be surprised that it can't be recovered. However, it might not need to be, since the other fields would be recovered. Also, `InitVar` is a rarely used feature. # This PR: * Changes Ax's JSON serialization for dataclasses to exclude non-fields * Adds a unit test Reviewed By: danielcohenlive Differential Revision: D61665461
- Loading branch information