修复 cannot import name ‘_get_socket_with_port‘ from ‘torch.distributed.elastic.agent.server.api

#工作记录

一、问题描述

修复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 行:


五、验证修改
完成上述修改后,重新运行项目,如果该项目遇到其他报错输出,请翻看我之前的修复笔记。
————————————————

参考资料

[BUG] 无法从 'torch.distributed.elastic.agent.server.api' 导入名称 '_get_socket_with_port' ·问题 #5603 ·深度速度ai/深度速度

不能从'torch.distributed.elastic.agent.server.api' 引入模块'_get_socket_with_port' ·问题 #1 ·贾斯汀SmileDate/NFoxHead 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

love530love

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值