Reloading torch triggers RuntimeError

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

New contributor

jmpion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật