Use __qualname__ in add_safe_globals and update Unpickling error raised for Unsupported GLOBAL (#146815)

- Fixes #146814

Change
```python
for f in _marked_safe_globals_set:
    module, name = f.__module__, f.__name__
```
to

```python
for f in _marked_safe_globals_set:
    module, name = f.__module__, f.__qualname__
```
for avoiding same key string overwrite.

A test is also added.
```
python test/test_serialization.py TestSerialization.test_serialization_nested_class
```

- Fixes #146886
Pull Request resolved: https://github.com/pytorch/pytorch/pull/146815
Approved by: https://github.com/mikaylagawarecki
This commit is contained in:
Hanson-HSChang
2025-02-21 18:04:59 +00:00
committed by PyTorch MergeBot
parent f4e4cfcb91
commit 2190ca7f47
3 changed files with 45 additions and 8 deletions

View File

@ -274,9 +274,9 @@ def add_safe_globals(safe_globals: list[Union[Callable, tuple[Callable, str]]])
(function/class, string) where string is the full path of the function/class.
Within the serialized format, each function is identified with its full
path as ``{__module__}.{__name__}``. When calling this API, you can provide this
path as ``{__module__}.{__qualname__}``. When calling this API, you can provide this
full path that should match the one in the checkpoint otherwise the default
``{fn.__module__}.{fn.__name__}`` will be used.
``{fn.__module__}.{fn.__qualname__}`` will be used.
Args:
safe_globals (List[Union[Callable, Tuple[Callable, str]]]): list of globals to mark as safe