mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 12:54:11 +08:00
This reverts commit 74db92b21868b7e9e77cc966e5d57a8246723cbd.
Reverted https://github.com/pytorch/pytorch/pull/165216 on behalf of https://github.com/clee2000 due to I think this broke distributed/test_pg_wrapper.py::ProcessGroupNCCLWrapperTest::test_debug_level_detail_no_gloo [GH job link](https://github.com/pytorch/pytorch/actions/runs/18492765290/job/52693842750) [HUD commit link](74db92b218
), note to self: bad TD ([comment](https://github.com/pytorch/pytorch/pull/165216#issuecomment-3402838765))
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
import weakref
|
|
from typing import cast, Optional
|
|
|
|
import torch.nn as nn
|
|
|
|
|
|
class _State:
|
|
pass
|
|
|
|
|
|
_module_state_mapping: weakref.WeakKeyDictionary[
|
|
nn.Module, weakref.ReferenceType[_State]
|
|
] = weakref.WeakKeyDictionary()
|
|
|
|
|
|
def _insert_module_state(module: nn.Module, state: _State) -> None:
|
|
global _module_state_mapping
|
|
assert module not in _module_state_mapping, f"Inserting {module} more than once."
|
|
_module_state_mapping[module] = weakref.ref(state)
|
|
|
|
|
|
def _get_module_state(module: nn.Module) -> Optional[_State]:
|
|
"""
|
|
Return the ``_State`` in ``model``.
|
|
|
|
Given a ``module``, this API finds out if the module is also a ``_State``
|
|
instance or if the module is managed by a composable API. If the module
|
|
is also a ``_State``, ``module`` will be casted to ``_State` and returned.
|
|
If it is managed by a composable API, the corresponding ``_State`` will
|
|
be returned.
|
|
"""
|
|
global _module_state_mapping
|
|
if isinstance(module, _State):
|
|
# pyrefly: ignore # redundant-cast
|
|
return cast(_State, module)
|
|
else:
|
|
# https://github.com/pytorch/pytorch/issues/107054
|
|
if module in _module_state_mapping:
|
|
state_ref = _module_state_mapping[module]
|
|
state = state_ref()
|
|
if state is None:
|
|
raise AssertionError("State has already been garbage collected")
|
|
return state
|
|
else:
|
|
return None
|