V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
111qqz
V2EX  ›  Python

[求助] import cupy 和 import torch 的顺序不同导致了不同的计算结果,如何进一步排查?

  •  
  •   111qqz · 132 天前 · 1372 次点击
    这是一个创建于 132 天前的主题,其中的信息可能已经有所发展或是发生改变。

    公司模型训练框架突然精度出现问题,排查了一番,发现原因和 cupy, torch 的 import 顺序有关.

    简单来说,发现

    import cupy as cp
    import torch
    
    

    import torch
    import cupy as cp
    
    

    两种顺序会得到不一样的计算结果. 目前怀疑是 torch 和 cupy 在初始化时各自有一些关于 cuda 的设置,这些设置之间冲突.

    不知道思路是否正确. 还是比较想知道造成这个现象的原因的.

    具体的背景以及复现代码在这里:

    https://111qqz.com/2023/12/cupy-torch-import-order-impact/

    第 1 条附言  ·  131 天前

    环境信息:

    1. cupy-cuda11x 12.0.0
    2. NVIDIA-SMI 460.73.01 Driver Version: 460.73.01 CUDA Version: 11.2
    3. torch 1.13.0+cu117
    numpy                              1.24.4
    numpy-groupies                     0.9.22
    numpydoc                           1.1.0
    
    cat /etc/lsb-release                                                                    
    DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=20.04
    DISTRIB_CODENAME=focal
    DISTRIB_DESCRIPTION="Ubuntu 20.04.6 LTS"
    
    第 2 条附言  ·  131 天前
    目前一个怀疑是,cuda 版本会不同.
    `/usr/local/cuda` 版本是 11.2.
    torch 是`1.13.0+cu117`的版本.
    因此可能会有差别
    20 条回复    2024-01-22 11:28:49 +08:00
    nagisaushio
        1
    nagisaushio  
       132 天前 via Android
    像浮点误差,你同一个脚本跑多次的结果一样吗
    111qqz
        2
    111qqz  
    OP
       132 天前 via Android
    @nagisaushio 一样的。同样的代码结果可以稳定出现,没有任何误差
    NoOneNoBody
        3
    NoOneNoBody  
       132 天前
    去 torch 提个 issue ,这么高深的问题,不详细研究过代码的人恐怕也不知道原因
    thinkershare
        4
    thinkershare  
       131 天前
    怀疑是这 2 个库使用了不同的种子,因此后面一个覆盖了前面一个的,导致结果始终不同,不过这应该对最终结果不会造成影响。
    thinkershare
        5
    thinkershare  
       131 天前
    我来给你测试一下。
    thinkershare
        6
    thinkershare  
       131 天前
    @111qqz 库的版本信息呢?
    111qqz
        7
    111qqz  
    OP
       131 天前
    @thinkershare #6 感谢,已补充
    111qqz
        8
    111qqz  
    OP
       131 天前
    @thinkershare #4 最开始我也怀疑是种子. 但是种子应该已经设置相同了,而且种子对结果的差异应该比这个要大
    thinkershare
        9
    thinkershare  
       131 天前
    @111qqz 抱歉,我无法复现你的问题,我在 3090/4090 都无法复现此问题,我手头暂时没有 H100/A100 的卡。不同机器上的结果竟然都不一致。但是调换后引用后的值,并不会有变化。可能是底层 CUDA 的 A100/H100 的底层模块不同导致的?你还有其它同事有其它型号的型卡吗? 或者切换以下版本。我用的 cupy-cuda111.
    [[0.23727967]
    [0.8440665 ]
    [0.5425058 ]
    [0.24246126]
    [0.12167222]
    [0.57418287]
    [0.5093833 ]
    [0.40845215]
    [0.32752675]
    [0.31734812]

    [0.23727967]
    [0.8440665 ]
    [0.5425058 ]
    [0.24246126]
    [0.12167222]
    [0.57418287]
    [0.5093833 ]
    [0.40845215]
    [0.32752675]
    [0.31734812]
    thinkershare
        10
    thinkershare  
       131 天前
    感觉你可能需要更详细的用 conda list 获取所有包的详细版本和签名(或者用 conda-lock),然后获取你的硬件信息(主要是 GPU 相关的,驱动版本和 CUDA 版本),并提供一个复现的的 github 仓库地址,然后发给 pytorch 官方,让它们的人去找找原因。
    thinkershare
        11
    thinkershare  
       131 天前
    cupy-cuda117 我也试过,其它环境都和你的一样,只有显卡和驱动版本和型号同你的不一致。

    NVIDIA-SMI 470.161.03 Driver Version: 470.161.03 CUDA Version: 11.4
    thinkershare
        12
    thinkershare  
       131 天前
    输出张量:
    [
    [0.23729032]
    [0.84413356]
    [0.5424989 ]
    [0.24236819]
    [0.1216919 ]
    [0.574163 ]
    [0.5093746 ]
    [0.40847316]
    [0.32745278]
    [0.3172678 ]
    ]

    输出张量:
    [
    [0.23729032]
    [0.84413356]
    [0.5424989 ]
    [0.24236819]
    [0.1216919 ]
    [0.574163 ]
    [0.5093746 ]
    [0.40847316]
    [0.32745278]
    [0.3172678 ]
    ]
    111qqz
        13
    111qqz  
    OP
       131 天前
    @thinkershare #9 非常感谢协助排查.
    不同机器上的结果似乎确实是会不一致的, 之前遇到过使用两种不同型号的 A100GPU 时(A100-PCIE-40GB vs A100-SXM4-40GB),Adam 优化器的结果会有明显区别.

    我打算先看看对齐下 CUDA 版本来验证下,确保 cupy 和 torch 使用的是同一个 cuda 版本.
    如果还是不行, 打算尝试下你后面提到的步骤
    有了更新会 at 你

    再次感谢
    AirCrusher
        14
    AirCrusher  
       131 天前
    非常有意思,在 V100 上面没复现出来这个错。
    如果只使用一个 torch.matmul 能否复现问题?
    以及,设定 https://pytorch.org/docs/stable/notes/randomness.html 是否有帮助?
    111qqz
        15
    111qqz  
    OP
       131 天前
    @AirCrusher #14
    1. 只用一个 torch.matmul 无法复现问题.
    甚至两个 linear 的参数也会影响问题的复现.
    基本上第二个 linear 的参数的输出是 1 才会复现,如果是其他值,先后 import 不会对结果有影响.

    2. 随机种子和`torch.use_deterministic_algorithms(True)` 都设置了.
    以及设置了如下这些:

    ```python

    def set_random_seed(seed: int):
    """Set random seed.

    Args:
    seed (int): Seed to be used.
    """
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True # type: ignore
    torch.backends.cudnn.benchmark = False # type: ignore
    torch.backends.cudnn.enabled = False # type: ignore
    cp.random.seed(seed)


    ```

    问题还是可以复现.
    thinkershare
        16
    thinkershare  
       104 天前
    @111qqz 最近搞到了 A100 显卡,你能发给详细的 conda list 信息给我妈,还有 nvcc 的版本和你显卡的驱动版本出来。
    或者你现在找到原因 了嘛?
    111qqz
        17
    111qqz  
    OP
       103 天前
    @thinkershare #16 周来忙别的就暂时没有再跟进这个问题了
    稍晚一点我把 conda list 还有相关的版本也更新一下
    thinkershare
        19
    thinkershare  
       102 天前
    @111qqz 好的,我明天来看看。
    thinkershare
        20
    thinkershare  
       96 天前   ❤️ 1
    @111qqz 抱歉,我无法复现你的问题,我使用 A100 80GB 的,除了显卡驱动版本和你的不一致,其它都一致(包版本,cuda, cudatookit),得到的结果如下:
    显卡驱动是:NVIDIA-SMI 545.23.06 Driver Version: 545.23.06 CUDA Version: 12.3

    输出张量: [[0.23729031]
    [0.84413356]
    [0.5424989 ]
    [0.24236819]
    [0.12169194]
    [0.5741631 ]
    [0.5093746 ]
    [0.40847313]
    [0.32745278]
    [0.31726775]]
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   947 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:29 · PVG 03:29 · LAX 12:29 · JFK 15:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.