When trying to reload the torch library in Python, by running the following lines of code, I encountered the error below:
import torch
import importlib
importlib.reload(torch)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-5-6781a72fd549> in <cell line: 3>()
1 import torch
2 import importlib
----> 3 importlib.reload(torch)
6 frames
/usr/lib/python3.10/importlib/__init__.py in reload(module)
167 if spec is None:
168 raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name)
--> 169 _bootstrap._exec(spec, module)
170 # The module may have replaced itself in sys.modules!
171 return sys.modules[name]
/usr/lib/python3.10/importlib/_bootstrap.py in _exec(spec, module)
/usr/lib/python3.10/importlib/_bootstrap_external.py in exec_module(self, module)
/usr/lib/python3.10/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)
/usr/local/lib/python3.10/dist-packages/torch/__init__.py in <module>
1951 from torch import compiler as compiler
1952
-> 1953 class _TritonLibrary:
1954 lib = torch.library.Library("triton", "DEF")
1955 ops_table: Dict[Tuple[str, str], Callable] = {}
/usr/local/lib/python3.10/dist-packages/torch/__init__.py in _TritonLibrary()
1952
1953 class _TritonLibrary:
-> 1954 lib = torch.library.Library("triton", "DEF")
1955 ops_table: Dict[Tuple[str, str], Callable] = {}
1956
/usr/local/lib/python3.10/dist-packages/torch/library.py in __init__(self, ns, kind, dispatch_key)
62 frame = traceback.extract_stack(limit=3)[0]
63 filename, lineno = frame.filename, frame.lineno
---> 64 self.m: Optional[Any] = torch._C._dispatch_library(kind, ns, dispatch_key, filename, lineno)
65 self.ns = ns
66 self._op_defs: Set[str] = set()
RuntimeError: Only a single TORCH_LIBRARY can be used to register the namespace triton; please put all of your definitions in a single TORCH_LIBRARY block. If you were trying to specify implementations, consider using TORCH_LIBRARY_IMPL (which can be duplicated). If you really intended to define operators for a single namespace in a distributed way, you can use TORCH_LIBRARY_FRAGMENT to explicitly indicate this. Previous registration of TORCH_LIBRARY was registered at /dev/null:1953; latest registration was registered at /dev/null:1953
Also, I got this error from different environments, one with torch 2.3.1 and another one with torch 2.4.0.
Why does reloading torch trigger this error? And what could be done to successfully reload torch?
Originally, to reload torch, I tried:
import torch
import sys
sys.modules.pop('torch')
import torch
but I got another error:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-cfb33825cf77> in <cell line: 3>()
1 # import sys
2 # sys.modules.pop('torch')
----> 3 import torch
/usr/local/lib/python3.10/dist-packages/torch/__init__.py in <module>
560 raise # If __file__ is not None the cause is unknown, so just re-raise.
561
--> 562 for name in dir(_C):
563 if name[0] != '_' and not name.endswith('Base'):
564 __all__.append(name)
NameError: name '_C' is not defined