#工作记录
一、问题描述
修复ImportError: cannot import name ‘log‘ from ‘torch.distributed.elastic.agent.server.api报错-CSDN博客
在运行CosyVoice_For_Windows项目时,虽然已修复“ImportError: cannot import name 'log' from 'torch.distributed.elastic.agent.server.api”错误,但又出现以下警告和错误:
File "D:\ProgramData\anaconda3\envs\CosyVoice\Lib\site-packages\deepspeed\elasticity\elastic_agent.py", line 9, in <module>
from torch.distributed.elastic.agent.server.api import logger, _get_socket_with_port
ImportError: cannot import name '_get_socket_with_port' from 'torch.distributed.elastic.agent.server.api' (D:\ProgramData\anaconda3\envs\CosyVoice\Lib\site-packages\torch\distributed\elastic\agent\server\api.py)
二、受影响的文件和行号
文件:
D:\ProgramData\anaconda3\envs\CosyVoice\Lib\site-packages\deepspeed\elasticity\elastic_agent.py
行号:第 9 行
原代码
from torch.distributed.elastic.agent.server.api import logger, _get_socket_with_port
三、修复过程
1. 删除“, _get_socket_with_port”
删除“, _get_socket_with_port”的代码片段。
2. 添加以下代码
import socket
def _get_socket_with_port() -> socket.socket:
"""Return a free port on localhost.
The free port is "reserved" by binding a temporary socket on it.
Close the socket before passing the port to the entity that
requires it. Usage example::
sock = _get_socket_with_port()
with closing(sock):
port = sock.getsockname()[1]
sock.close()
# there is still a race-condition that some other process
# may grab this port before func() runs
func(port)
"""
addrs = socket.getaddrinfo(
host="localhost", port=None, family=socket.AF_UNSPEC, type=socket.SOCK_STREAM
)
for addr in addrs:
family, type, proto, _, _ = addr
s = socket.socket(family, type, proto)
try:
s.bind(("localhost", 0))
s.listen(0)
return s
except OSError as e:
s.close()
log.info("Socket creation attempt failed.", exc_info=e)
raise RuntimeError("Failed to create a socket")
3. 测试修改后的代码
重新运行 python webui.py脚本,确认警告是否消失。
四、修改后的代码
elastic_agent.py 文件第 9 行 至 第 40 行:
五、验证修改
完成上述修改后,重新运行项目,如果该项目遇到其他报错输出,请翻看我之前的修复笔记。
————————————————
参考资料