[docs]defmake_serialization_function_for_dataclass(cls:type,supported_classes:Set[type])->Tuple[Callable,Callable,Callable]:""" Automatically creates serialization function for a class decorated with ``dataclasses.dataclass``. """defflatten_cls(obj:cls)->Tuple[List[Any],torch.utils._pytree.Context]:# type: ignore[valid-type]"""Serializes a ``%s`` with python objects."""returnlist(obj.values()),list(obj.keys())defflatten_with_keys_cls(obj:cls,# type: ignore[valid-type])->Tuple[List[Tuple[torch.utils._pytree.KeyEntry,Any]],torch.utils._pytree.Context]:"""Serializes a ``%s`` with python objects with keys."""values,context=list(obj.values()),list(obj.keys())return[(torch.utils._pytree.MappingKey(k),v)fork,vinzip(context,values)],contextdefunflatten_cls(values:List[Any],context:torch.utils._pytree.Context,output_type=None)->cls:# type: ignore[valid-type]"""Restores an instance of ``%s`` from python objects."""returncls(**dict(zip(context,values)))name=_lower_name_with_(cls.__name__)flatten_cls.__name__=f"flatten_{name}"flatten_with_keys_cls.__name__=f"flatten_with_keys_{name}"unflatten_cls.__name__=f"unflatten_{name}"flatten_cls.__doc__=flatten_cls.__doc__%cls.__name__flatten_with_keys_cls.__doc__=flatten_with_keys_cls.__doc__%cls.__name__unflatten_cls.__doc__=unflatten_cls.__doc__%cls.__name__supported_classes.add(cls)returnflatten_cls,flatten_with_keys_cls,unflatten_cls