Skip to content

Commit

Permalink
Fix yaml serialization in io mixin (#11106)
Browse files Browse the repository at this point in the history
* Fix yaml serialization in io mixin

Signed-off-by: Hemil Desai <[email protected]>

* Add docstring

Signed-off-by: Hemil Desai <[email protected]>

---------

Signed-off-by: Hemil Desai <[email protected]>
  • Loading branch information
hemildesai authored Oct 31, 2024
1 parent d441dca commit ab7b325
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions nemo/lightning/io/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,33 @@ def _partial_representer_with_defaults(dumper, data):


def _safe_object_representer(dumper, data):
if not inspect.isclass(data):
cls = data.__class__
call = True
else:
cls = data
"""
Represent a given object as YAML using the specified dumper.
This function is a fallback for objects that don't have specific representers.
If the object has __qualname__ attr, the __target__ is set to f"{inspect.getmodule(obj).__name__}.{obj.__qualname__}".
If the object does not have a __qualname__ attr, the __target__ is set from its __class__ attr.
The __call__ key is used to indicate whether the target should be called to create an instance.
Args:
dumper (yaml.Dumper): The YAML dumper to use for serialization.
data (Any): The data to serialize. This can be any Python object,
but if it's a class or a class instance, special handling will be applied.
Returns:
str: The YAML representation of the data.
"""
try:
obj = data
target = f"{inspect.getmodule(obj).__name__}.{obj.__qualname__}"
call = False
except AttributeError:
obj = data.__class__
target = f"{inspect.getmodule(obj).__name__}.{obj.__qualname__}"
call = True

value = {
"_target_": f"{inspect.getmodule(cls).__name__}.{cls.__qualname__}", # type: ignore
"_target_": target, # type: ignore
"_call_": call,
}
return dumper.represent_data(value)
Expand Down

0 comments on commit ab7b325

Please sign in to comment.