[施工中] cupy与torch的导入顺序不同对计算结果的影响

Overview

背景

公司内部的基于torch的toolbox发现某个版本之后,结果发生了偏移. 通过一系列排查,发现当导入cupy和torch的顺序不同时,计算结果会有所差异。 也就是说,如下两段代码会导致模型训练等环节的计算得到不同的结果.

1import cupy as cp
2import torch
1import torch
2import cupy as cp
3

最小复现代码

经过一番努力,把问题从内部框架中剥离了出来. 如下是得到的最小复现代码. 通过调整import cupy与import torch的相对顺序,会得到不同的结果.

 1# import cupy as cp
 2import torch
 3import torch.nn as nn
 4import cupy as cp
 5import random
 6import numpy as np
 7
 8# 设置随机种子
 9def set_random_seed(seed: int):
10    random.seed(seed)
11    np.random.seed(seed)
12    torch.manual_seed(seed)
13    torch.cuda.manual_seed(seed)
14    torch.cuda.manual_seed_all(seed)
15    cp.random.seed(seed)
16
17# 定义一个简单的MLP模型
18class MLP(nn.Module):
19    def __init__(self):
20        super(MLP, self).__init__()
21        net = nn.Sequential()
22        net.add_module("linear0", nn.Linear(16, 4))
23        net.add_module("linear1", nn.Linear(4, 1))
24        self.net = net
25
26    def forward(self, x):
27        x = self.net(x)
28        return x
29
30# 设置随机种子
31set_random_seed(0)
32
33# 创建MLP模型实例
34model = MLP()
35
36# 将模型移动到CUDA设备上
37model = model.to('cuda')
38model.train()
39
40# 生成随机的输入张量并移动到CUDA设备上
41input_tensor = torch.randn(10, 16).to('cuda')
42
43# 使用MLP模型进行预测
44output_tensor = model(input_tensor).detach().cpu().numpy()
45
46print("输出张量:", output_tensor)

测试环境

1cupy-cuda11x 12.0.0
2NVIDIA-SMI 460.73.01 Driver Version: 460.73.01 CUDA Version: 11.2
3torch 1.13.0+cu117
4numpy                              1.24.4
5numpy-groupies                     0.9.22
6numpydoc                           1.1.0

nvcc版本

1./nvcc --version
2nvcc: NVIDIA (R) Cuda compiler driver
3Copyright (c) 2005-2021 NVIDIA Corporation
4Built on Sun_Feb_14_21:12:58_PST_2021
5Cuda compilation tools, release 11.2, V11.2.152
6Build cuda_11.2.r11.2/compiler.29618528_0
7

完整conda list

  1_ipyw_jlab_nb_ext_conf    0.1.0                    py38_0  
  2_libgcc_mutex             0.1                        main  
  3alabaster                 0.7.12             pyhd3eb1b0_0  
  4anaconda                  2021.04                  py38_0  
  5anaconda-client           1.7.2                    py38_0  
  6anaconda-navigator        2.0.3                    py38_0  
  7anaconda-project          0.9.1              pyhd3eb1b0_1  
  8anyio                     2.2.0            py38h06a4308_1  
  9appdirs                   1.4.4                      py_0  
 10argh                      0.26.2                   py38_0  
 11argon2-cffi               20.1.0           py38h27cfd23_1  
 12asn1crypto                1.4.0                      py_0  
 13astroid                   2.5              py38h06a4308_1  
 14astropy                   4.2.1            py38h27cfd23_1  
 15async_generator           1.10               pyhd3eb1b0_0  
 16atomicwrites              1.4.0                      py_0  
 17attrs                     20.3.0             pyhd3eb1b0_0  
 18autopep8                  1.5.6              pyhd3eb1b0_0  
 19babel                     2.9.0              pyhd3eb1b0_0  
 20backcall                  0.2.0              pyhd3eb1b0_0  
 21backports                 1.0                pyhd3eb1b0_2  
 22backports.functools_lru_cache 1.6.4              pyhd3eb1b0_0  
 23backports.shutil_get_terminal_size 1.0.0              pyhd3eb1b0_3  
 24backports.tempfile        1.0                pyhd3eb1b0_1  
 25backports.weakref         1.0.post1                  py_1  
 26beautifulsoup4            4.9.3              pyha847dfd_0  
 27bitarray                  1.9.2            py38h27cfd23_1  
 28bkcharts                  0.2                      py38_0  
 29black                     19.10b0                    py_0  
 30blas                      1.0                         mkl  
 31bleach                    3.3.0              pyhd3eb1b0_0  
 32blosc                     1.21.0               h8c45485_0  
 33bokeh                     2.3.1            py38h06a4308_0  
 34boto                      2.49.0                   py38_0  
 35bottleneck                1.3.2            py38heb32a55_1  
 36brotlipy                  0.7.0           py38h27cfd23_1003  
 37build                     0.10.0                   pypi_0    pypi
 38bzip2                     1.0.8                h7b6447c_0  
 39c-ares                    1.17.1               h27cfd23_0  
 40ca-certificates           2021.4.13            h06a4308_1  
 41cairo                     1.16.0               hf32fb01_1  
 42certifi                   2020.12.5        py38h06a4308_0  
 43cffi                      1.14.5           py38h261ae71_0  
 44chardet                   4.0.0           py38h06a4308_1003  
 45click                     7.1.2              pyhd3eb1b0_0  
 46cloudpickle               1.6.0                      py_0  
 47clyent                    1.2.2                    py38_1  
 48colorama                  0.4.4              pyhd3eb1b0_0  
 49conda                     4.10.1           py38h06a4308_1  
 50conda-build               3.21.4           py38h06a4308_0  
 51conda-content-trust       0.1.1              pyhd3eb1b0_0  
 52conda-env                 2.6.0                         1  
 53conda-package-handling    1.7.3            py38h27cfd23_1  
 54conda-repo-cli            1.0.4              pyhd3eb1b0_0  
 55conda-token               0.3.0              pyhd3eb1b0_0  
 56conda-verify              3.4.2                      py_1  
 57contextlib2               0.6.0.post1                py_0  
 58cryptography              3.4.7            py38hd23ed53_0  
 59curl                      7.71.1               hbc83047_1  
 60cycler                    0.10.0                   py38_0  
 61cython                    0.29.23          py38h2531618_0  
 62cytoolz                   0.11.0           py38h7b6447c_0  
 63dask                      2021.4.0           pyhd3eb1b0_0  
 64dask-core                 2021.4.0           pyhd3eb1b0_0  
 65dbus                      1.13.18              hb2f20db_0  
 66decorator                 5.0.6              pyhd3eb1b0_0  
 67defusedxml                0.7.1              pyhd3eb1b0_0  
 68diff-match-patch          20200713                   py_0  
 69distributed               2021.4.0         py38h06a4308_0  
 70docutils                  0.17             py38h06a4308_1  
 71entrypoints               0.3                      py38_0  
 72et_xmlfile                1.0.1                   py_1001  
 73expat                     2.3.0                h2531618_2  
 74fastcache                 1.1.0            py38h7b6447c_0  
 75filelock                  3.0.12             pyhd3eb1b0_1  
 76flake8                    3.9.0              pyhd3eb1b0_0  
 77flask                     1.1.2              pyhd3eb1b0_0  
 78fontconfig                2.13.1               h6c09931_0  
 79freetype                  2.10.4               h5ab3b9f_0  
 80fribidi                   1.0.10               h7b6447c_0  
 81fsspec                    0.9.0              pyhd3eb1b0_0  
 82future                    0.18.2                   py38_1  
 83get_terminal_size         1.0.0                haa9412d_0  
 84gevent                    21.1.2           py38h27cfd23_1  
 85glib                      2.68.1               h36276a3_0  
 86glob2                     0.7                pyhd3eb1b0_0  
 87gmp                       6.2.1                h2531618_2  
 88gmpy2                     2.0.8            py38hd5f6e3b_3  
 89graphite2                 1.3.14               h23475e2_0  
 90greenlet                  1.0.0            py38h2531618_2  
 91gst-plugins-base          1.14.0               h8213a91_2  
 92gstreamer                 1.14.0               h28cd5cc_2  
 93h5py                      2.10.0           py38h7918eee_0  
 94harfbuzz                  2.8.0                h6f93f22_0  
 95hdf5                      1.10.4               hb1b8bf9_0  
 96heapdict                  1.0.1                      py_0  
 97html5lib                  1.1                        py_0  
 98icu                       58.2                 he6710b0_3  
 99idna                      2.10               pyhd3eb1b0_0  
100imageio                   2.9.0              pyhd3eb1b0_0  
101imagesize                 1.2.0              pyhd3eb1b0_0  
102importlib-metadata        3.10.0           py38h06a4308_0  
103importlib_metadata        3.10.0               hd3eb1b0_0  
104iniconfig                 1.1.1              pyhd3eb1b0_0  
105intel-openmp              2021.2.0           h06a4308_610  
106intervaltree              3.1.0                      py_0  
107ipykernel                 5.3.4            py38h5ca1d4c_0  
108ipython                   7.22.0           py38hb070fc8_0  
109ipython_genutils          0.2.0              pyhd3eb1b0_1  
110ipywidgets                7.6.3              pyhd3eb1b0_1  
111isort                     5.8.0              pyhd3eb1b0_0  
112itsdangerous              1.1.0              pyhd3eb1b0_0  
113jbig                      2.1                  hdba287a_0  
114jdcal                     1.4.1                      py_0  
115jedi                      0.17.2           py38h06a4308_1  
116jeepney                   0.6.0              pyhd3eb1b0_0  
117jinja2                    2.11.3             pyhd3eb1b0_0  
118joblib                    1.0.1              pyhd3eb1b0_0  
119jpeg                      9b                   h024ee3a_2  
120json5                     0.9.5                      py_0  
121jsonschema                3.2.0                      py_2  
122jupyter                   1.0.0                    py38_7  
123jupyter-packaging         0.7.12             pyhd3eb1b0_0  
124jupyter_client            6.1.12             pyhd3eb1b0_0  
125jupyter_console           6.4.0              pyhd3eb1b0_0  
126jupyter_core              4.7.1            py38h06a4308_0  
127jupyter_server            1.4.1            py38h06a4308_0  
128jupyterlab                3.0.14             pyhd3eb1b0_1  
129jupyterlab_pygments       0.1.2                      py_0  
130jupyterlab_server         2.4.0              pyhd3eb1b0_0  
131jupyterlab_widgets        1.0.0              pyhd3eb1b0_1  
132keyring                   22.3.0           py38h06a4308_0  
133kiwisolver                1.3.1            py38h2531618_0  
134krb5                      1.18.2               h173b8e3_0  
135lazy-object-proxy         1.6.0            py38h27cfd23_0  
136lcms2                     2.12                 h3be6417_0  
137ld_impl_linux-64          2.33.1               h53a641e_7  
138libarchive                3.4.2                h62408e4_0  
139libcurl                   7.71.1               h20c2e04_1  
140libedit                   3.1.20210216         h27cfd23_1  
141libev                     4.33                 h7b6447c_0  
142libffi                    3.3                  he6710b0_2  
143libgcc-ng                 9.1.0                hdf63c60_0  
144libgfortran-ng            7.3.0                hdf63c60_0  
145liblief                   0.10.1               he6710b0_0  
146libllvm10                 10.0.1               hbcb73fb_5  
147libpng                    1.6.37               hbc83047_0  
148libsodium                 1.0.18               h7b6447c_0  
149libspatialindex           1.9.3                h2531618_0  
150libssh2                   1.9.0                h1ba5d50_1  
151libstdcxx-ng              9.1.0                hdf63c60_0  
152libtiff                   4.2.0                h85742a9_0  
153libtool                   2.4.6             h7b6447c_1005  
154libuuid                   1.0.3                h1bed415_2  
155libuv                     1.40.0               h7b6447c_0  
156libwebp-base              1.2.0                h27cfd23_0  
157libxcb                    1.14                 h7b6447c_0  
158libxml2                   2.9.10               hb55368b_3  
159libxslt                   1.1.34               hc22bd24_0  
160lightgbm                  3.3.5.99                 pypi_0    pypi
161llvmlite                  0.40.1                   pypi_0    pypi
162locket                    0.2.1            py38h06a4308_1  
163lxml                      4.6.3            py38h9120a33_0  
164lz4-c                     1.9.3                h2531618_0  
165lzo                       2.10                 h7b6447c_2  
166markupsafe                1.1.1            py38h7b6447c_0  
167matplotlib                3.3.4            py38h06a4308_0  
168matplotlib-base           3.3.4            py38h62a2d02_0  
169mccabe                    0.6.1                    py38_1  
170mistune                   0.8.4           py38h7b6447c_1000  
171mkl                       2021.2.0           h06a4308_296  
172mkl-service               2.3.0            py38h27cfd23_1  
173mkl_fft                   1.3.0            py38h42c9631_2  
174mkl_random                1.2.1            py38ha9443f7_2  
175mock                      4.0.3              pyhd3eb1b0_0  
176more-itertools            8.7.0              pyhd3eb1b0_0  
177mpc                       1.1.0                h10f8cd9_1  
178mpfr                      4.0.2                hb69a4c5_1  
179mpmath                    1.2.1            py38h06a4308_0  
180msgpack-python            1.0.2            py38hff7bd54_1  
181multipledispatch          0.6.0                    py38_0  
182mypy_extensions           0.4.3                    py38_0  
183navigator-updater         0.2.1                    py38_0  
184nbclassic                 0.2.6              pyhd3eb1b0_0  
185nbclient                  0.5.3              pyhd3eb1b0_0  
186nbconvert                 6.0.7                    py38_0  
187nbformat                  5.1.3              pyhd3eb1b0_0  
188ncurses                   6.2                  he6710b0_1  
189nest-asyncio              1.5.1              pyhd3eb1b0_0  
190networkx                  2.5                        py_0  
191nltk                      3.6.1              pyhd3eb1b0_0  
192nose                      1.3.7           pyhd3eb1b0_1006  
193notebook                  6.3.0            py38h06a4308_0  
194numba                     0.57.1                   pypi_0    pypi
195numexpr                   2.7.3            py38h22e1b3c_1  
196numpy                     1.24.4                   pypi_0    pypi
197numpydoc                  1.1.0              pyhd3eb1b0_1  
198olefile                   0.46                       py_0  
199openpyxl                  3.0.7              pyhd3eb1b0_0  
200openssl                   1.1.1k               h27cfd23_0  
201packaging                 20.9               pyhd3eb1b0_0  
202pandas                    1.2.4            py38h2531618_0  
203pandoc                    2.12                 h06a4308_0  
204pandocfilters             1.4.3            py38h06a4308_1  
205pango                     1.45.3               hd140c19_0  
206parso                     0.7.0                      py_0  
207partd                     1.2.0              pyhd3eb1b0_0  
208patchelf                  0.12                 h2531618_1  
209path                      15.1.2           py38h06a4308_0  
210path.py                   12.5.0                        0  
211pathlib2                  2.3.5            py38h06a4308_2  
212pathspec                  0.7.0                      py_0  
213patsy                     0.5.1                    py38_0  
214pcre                      8.44                 he6710b0_0  
215pep8                      1.7.1                    py38_0  
216pexpect                   4.8.0              pyhd3eb1b0_3  
217pickleshare               0.7.5           pyhd3eb1b0_1003  
218pillow                    8.2.0            py38he98fc37_0  
219pip                       21.0.1           py38h06a4308_0  
220pixman                    0.40.0               h7b6447c_0  
221pkginfo                   1.7.0            py38h06a4308_0  
222pluggy                    0.13.1           py38h06a4308_0  
223ply                       3.11                     py38_0  
224prometheus_client         0.10.1             pyhd3eb1b0_0  
225prompt-toolkit            3.0.17             pyh06a4308_0  
226prompt_toolkit            3.0.17               hd3eb1b0_0  
227psutil                    5.8.0            py38h27cfd23_1  
228ptyprocess                0.7.0              pyhd3eb1b0_2  
229py                        1.10.0             pyhd3eb1b0_0  
230py-lief                   0.10.1           py38h403a769_0  
231pybind11                  2.10.4                   pypi_0    pypi
232pycodestyle               2.6.0              pyhd3eb1b0_0  
233pycosat                   0.6.3            py38h7b6447c_1  
234pycparser                 2.20                       py_2  
235pycurl                    7.43.0.6         py38h1ba5d50_0  
236pydocstyle                6.0.0              pyhd3eb1b0_0  
237pyerfa                    1.7.3            py38h27cfd23_0  
238pyflakes                  2.2.0              pyhd3eb1b0_0  
239pygments                  2.8.1              pyhd3eb1b0_0  
240pylint                    2.7.4            py38h06a4308_1  
241pyls-black                0.4.6                hd3eb1b0_0  
242pyls-spyder               0.3.2              pyhd3eb1b0_0  
243pymysql                   1.1.0                    pypi_0    pypi
244pyodbc                    4.0.30           py38he6710b0_0  
245pyopenssl                 20.0.1             pyhd3eb1b0_1  
246pyparsing                 2.4.7              pyhd3eb1b0_0  
247pyproject-hooks           1.0.0                    pypi_0    pypi
248pyqt                      5.9.2            py38h05f1152_4  
249pyrsistent                0.17.3           py38h7b6447c_0  
250pysocks                   1.7.1            py38h06a4308_0  
251pytables                  3.6.1            py38h9fd0a39_0  
252pytest                    6.2.3            py38h06a4308_2  
253python                    3.8.8                hdb3f193_5  
254python-dateutil           2.8.1              pyhd3eb1b0_0  
255python-jsonrpc-server     0.4.0                      py_0  
256python-language-server    0.36.2             pyhd3eb1b0_0  
257python-libarchive-c       2.9                pyhd3eb1b0_1  
258pytz                      2021.1             pyhd3eb1b0_0  
259pywavelets                1.1.1            py38h7b6447c_2  
260pyxdg                     0.27               pyhd3eb1b0_0  
261pyyaml                    5.4.1            py38h27cfd23_1  
262pyzmq                     20.0.0           py38h2531618_1  
263qdarkstyle                2.8.1                      py_0  
264qt                        5.9.7                h5867ecd_1  
265qtawesome                 1.0.2              pyhd3eb1b0_0  
266qtconsole                 5.0.3              pyhd3eb1b0_0  
267qtpy                      1.9.0                      py_0  
268readline                  8.1                  h27cfd23_0  
269regex                     2021.4.4         py38h27cfd23_0  
270requests                  2.25.1             pyhd3eb1b0_0  
271ripgrep                   12.1.1                        0  
272rope                      0.18.0                     py_0  
273rtree                     0.9.7            py38h06a4308_1  
274ruamel_yaml               0.15.100         py38h27cfd23_0  
275scikit-image              0.18.1           py38ha9443f7_0  
276scikit-learn              0.24.1           py38ha9443f7_0  
277scipy                     1.10.1                   pypi_0    pypi
278seaborn                   0.11.1             pyhd3eb1b0_0  
279secretstorage             3.3.1            py38h06a4308_0  
280send2trash                1.5.0              pyhd3eb1b0_1  
281setuptools                52.0.0           py38h06a4308_0  
282simplegeneric             0.8.1                    py38_2  
283singledispatch            3.6.1           pyhd3eb1b0_1001  
284sip                       4.19.13          py38he6710b0_0  
285six                       1.15.0           py38h06a4308_0  
286sniffio                   1.2.0            py38h06a4308_1  
287snowballstemmer           2.1.0              pyhd3eb1b0_0  
288sortedcollections         2.1.0              pyhd3eb1b0_0  
289sortedcontainers          2.3.0              pyhd3eb1b0_0  
290soupsieve                 2.2.1              pyhd3eb1b0_0  
291sphinx                    3.5.3              pyhd3eb1b0_0  
292sphinxcontrib             1.0                      py38_1  
293sphinxcontrib-applehelp   1.0.2              pyhd3eb1b0_0  
294sphinxcontrib-devhelp     1.0.2              pyhd3eb1b0_0  
295sphinxcontrib-htmlhelp    1.0.3              pyhd3eb1b0_0  
296sphinxcontrib-jsmath      1.0.1              pyhd3eb1b0_0  
297sphinxcontrib-qthelp      1.0.3              pyhd3eb1b0_0  
298sphinxcontrib-serializinghtml 1.1.4              pyhd3eb1b0_0  
299sphinxcontrib-websupport  1.2.4                      py_0  
300spyder                    4.2.5            py38h06a4308_0  
301spyder-kernels            1.10.2           py38h06a4308_0  
302sqlalchemy                1.4.7            py38h27cfd23_0  
303sqlite                    3.35.4               hdfb4753_0  
304statsmodels               0.12.2           py38h27cfd23_0  
305sympy                     1.8              py38h06a4308_0  
306tblib                     1.7.0                      py_0  
307terminado                 0.9.4            py38h06a4308_0  
308testpath                  0.4.4              pyhd3eb1b0_0  
309textdistance              4.2.1              pyhd3eb1b0_0  
310threadpoolctl             2.1.0              pyh5ca1d4c_0  
311three-merge               0.1.1              pyhd3eb1b0_0  
312tifffile                  2020.10.1        py38hdd07704_2  
313tk                        8.6.10               hbc83047_0  
314toml                      0.10.2             pyhd3eb1b0_0  
315tomli                     2.0.1                    pypi_0    pypi
316toolz                     0.11.1             pyhd3eb1b0_0  
317torch                     1.13.0+cu117             pypi_0    pypi
318tornado                   6.1              py38h27cfd23_0  
319tqdm                      4.59.0             pyhd3eb1b0_1  
320traitlets                 5.0.5              pyhd3eb1b0_0  
321treelite                  0.93                     pypi_0    pypi
322typed-ast                 1.4.2            py38h27cfd23_1  
323typing_extensions         3.7.4.3            pyha847dfd_0  
324ujson                     4.0.2            py38h2531618_0  
325unicodecsv                0.14.1                   py38_0  
326unixodbc                  2.3.9                h7b6447c_0  
327urllib3                   1.26.4             pyhd3eb1b0_0  
328watchdog                  1.0.2            py38h06a4308_1  
329wcwidth                   0.2.5                      py_0  
330webencodings              0.5.1                    py38_1  
331werkzeug                  1.0.1              pyhd3eb1b0_0  
332wheel                     0.36.2             pyhd3eb1b0_0  
333widgetsnbextension        3.5.1                    py38_0  
334wrapt                     1.12.1           py38h7b6447c_1  
335wurlitzer                 2.1.0            py38h06a4308_0  
336xlrd                      2.0.1              pyhd3eb1b0_0  
337xlsxwriter                1.3.8              pyhd3eb1b0_0  
338xlwt                      1.3.0                    py38_0  
339xmltodict                 0.12.0                     py_0  
340xz                        5.2.5                h7b6447c_0  
341yaml                      0.2.5                h7b6447c_0  
342yapf                      0.31.0             pyhd3eb1b0_0  
343zeromq                    4.3.4                h2531618_0  
344zict                      2.0.0              pyhd3eb1b0_0  
345zipp                      3.4.1              pyhd3eb1b0_0  
346zlib                      1.2.11               h7b6447c_3  
347zope                      1.0                      py38_1  
348zope.event                4.5.0                    py38_0  
349zope.interface            5.3.0            py38h27cfd23_0  
350zstd                      1.4.5                h9ceee32_0 

完整pip list

  1Package                            Version
  2---------------------------------- -----------------
  3addict                             2.4.0
  4ai-queue-client                    1.14.0
  5alabaster                          0.7.12
  6alembic                            1.11.3
  7anaconda-client                    1.7.2
  8anaconda-navigator                 2.0.3
  9anaconda-project                   0.9.1
 10annotated-types                    0.5.0
 11anyio                              2.2.0
 12appdirs                            1.4.4
 13argh                               0.26.2
 14argon2-cffi                        20.1.0
 15asn1crypto                         1.4.0
 16astroid                            2.5
 17astropy                            4.2.1
 18asttokens                          2.2.1
 19async-generator                    1.10
 20async-timeout                      4.0.3
 21atomicwrites                       1.4.0
 22attrs                              20.3.0
 23autopep8                           1.5.6
 24Babel                              2.9.0
 25backcall                           0.2.0
 26backports.functools-lru-cache      1.6.4
 27backports.shutil-get-terminal-size 1.0.0
 28backports.tempfile                 1.0
 29backports.weakref                  1.0.post1
 30backports.zoneinfo                 0.2.1
 31beautifulsoup4                     4.9.3
 32bitarray                           1.9.2
 33bkcharts                           0.2
 34black                              22.10.0
 35bleach                             3.3.0
 36bokeh                              2.3.1
 37boto                               2.49.0
 38boto3                              1.28.80
 39botocore                           1.31.80
 40Bottleneck                         1.3.2
 41brotlipy                           0.7.0
 42build                              0.10.0
 43certifi                            2020.12.5
 44cffi                               1.14.5
 45cfgv                               3.4.0
 46chardet                            4.0.0
 47charset-normalizer                 3.2.0
 48click                              8.1.7
 49cloudpickle                        1.6.0
 50clyent                             1.2.2
 51colorama                           0.4.4
 52coloredlogs                        15.0.1
 53comm                               0.1.4
 54conda                              4.10.1
 55conda-build                        3.21.4
 56conda-content-trust                0+unknown
 57conda-package-handling             1.7.3
 58conda-repo-cli                     1.0.4
 59conda-token                        0.3.0
 60conda-verify                       3.4.2
 61contextlib2                        0.6.0.post1
 62cramjam                            2.5.0
 63cryptography                       3.4.7
 64cupy-cuda11x                       12.0.0
 65cycler                             0.10.0
 66Cython                             0.29.23
 67cytoolz                            0.11.0
 68dask                               2021.4.0
 69data-record                        0.0.2
 70databricks-cli                     0.17.7
 71dateparser                         1.1.8
 72debugpy                            1.8.0
 73decorator                          5.0.6
 74defusedxml                         0.7.1
 75devtools                           0.12.1
 76diff-match-patch                   20200713
 77distlib                            0.3.7
 78distributed                        2021.4.0
 79docker                             6.1.3
 80docutils                           0.17
 81dominate                           2.8.0
 82entrypoints                        0.3
 83et-xmlfile                         1.0.1
 84executing                          1.2.0
 85fastcache                          1.1.0
 86fastrlock                          0.8.1
 87filelock                           3.12.2
 88flake8                             5.0.4
 89Flask                              1.1.2
 90flatbuffers                        23.5.26
 91fsspec                             0.9.0
 92future                             0.18.2
 93gevent                             21.1.2
 94gitdb                              4.0.10
 95GitPython                          3.1.32
 96glob2                              0.7
 97gmpy2                              2.0.8
 98greenlet                           1.0.0
 99grpcio                             1.37.0
100gunicorn                           20.1.0
101h5py                               2.10.0
102hdf5plugin                         4.3.0
103HeapDict                           1.0.1
104HFML                               3.8.9
105hiredis                            2.2.3
106horology                           1.2.0
107html5lib                           1.1
108httmock                            1.4.0
109humanfriendly                      10.0
110identify                           2.5.27
111idna                               2.10
112imageio                            2.9.0
113imagesize                          1.2.0
114importlib-metadata                 6.11.0
115importlib-resources                6.0.1
116iniconfig                          1.1.1
117intervaltree                       3.1.0
118ipaddress                          1.0.23
119ipykernel                          6.25.2
120ipython                            8.12.3
121ipython-genutils                   0.2.0
122ipywidgets                         7.6.3
123isort                              5.12.0
124itsdangerous                       1.1.0
125jdcal                              1.4.1
126jedi                               0.17.2
127jeepney                            0.6.0
128Jinja2                             2.11.3
129jmespath                           1.0.1
130joblib                             1.3.2
131json5                              0.9.5
132jsonschema                         3.2.0
133jupyter                            1.0.0
134jupyter-client                     6.1.12
135jupyter-console                    6.4.0
136jupyter-core                       4.7.1
137jupyter-packaging                  0.7.12
138jupyter-server                     1.4.1
139jupyterlab                         3.0.14
140jupyterlab-pygments                0.1.2
141jupyterlab-server                  2.4.0
142jupyterlab-widgets                 1.0.0
143keyring                            22.3.0
144kiwisolver                         1.3.1
145kunpeng-client                     0.0.1rc2
146lazy-object-proxy                  1.6.0
147libarchive-c                       2.9
148lightgbm                           3.3.5.99
149line-profiler                      3.5.0
150lleaves                            1.1.0
151llvmlite                           0.40.1
152locket                             0.2.1
153loguru                             0.7.0
154lxml                               4.6.3
155Mako                               1.2.4
156Markdown                           3.4.4
157markdown-it-py                     3.0.0
158MarkupSafe                         1.1.1
159matplotlib                         3.3.4
160matplotlib-inline                  0.1.6
161mccabe                             0.7.0
162mdurl                              0.1.2
163mistune                            0.8.4
164mkl-fft                            1.3.0
165mkl-random                         1.2.1
166mkl-service                        2.3.0
167mlflow                             2.8.0
168mmengine                           0.10.1
169mock                               4.0.3
170more-itertools                     8.7.0
171mpmath                             1.2.1
172msgpack                            1.0.2
173multipledispatch                   0.6.0
174mypy-extensions                    0.4.3
175navigator-updater                  0.2.1
176nbclassic                          0.2.6
177nbclient                           0.5.3
178nbconvert                          6.0.7
179nbformat                           5.1.3
180nest-asyncio                       1.5.1
181networkx                           2.5
182nltk                               3.6.1
183nodeenv                            1.8.0
184nose                               1.3.7
185notebook                           6.3.0
186numba                              0.57.1
187numexpr                            2.7.3
188numpy                              1.24.4
189numpy-groupies                     0.9.22
190numpydoc                           1.1.0
191oauthlib                           3.2.2
192olefile                            0.46
193onnx                               1.13.0
194onnxconverter-common               1.12.2
195onnxmltools                        1.11.2
196onnxruntime                        1.16.1
197onnxsim                            0.4.35
198opencv-python                      4.8.0.76
199openpyxl                           3.0.7
200packaging                          20.9
201pandas                             1.5.3
202pandocfilters                      1.4.3
203parso                              0.7.0
204partd                              1.2.0
205path                               15.1.2
206pathlib2                           2.3.5
207pathspec                           0.11.2
208patsy                              0.5.1
209pep8                               1.7.1
210pexpect                            4.8.0
211pickleshare                        0.7.5
212Pillow                             10.0.0
213pip                                23.3.1
214pkginfo                            1.7.0
215platformdirs                       3.10.0
216pluggy                             0.13.1
217ply                                3.11
218pre-commit                         3.3.3
219prometheus-api-client              0.5.4
220prometheus-client                  0.10.1
221prompt-toolkit                     3.0.39
222protobuf                           3.20.3
223psutil                             5.8.0
224ptyprocess                         0.7.0
225pure-eval                          0.2.2
226py                                 1.10.0
227pyarrow                            12.0.1
228pybind11                           2.10.4
229pycodestyle                        2.9.1
230pycosat                            0.6.3
231pycparser                          2.20
232pycurl                             7.43.0.6
233pydantic                           1.10.13
234pydantic_core                      2.6.3
235pydocstyle                         6.0.0
236pyerfa                             1.7.3
237pyflakes                           2.5.0
238Pygments                           2.16.1
239PyJWT                              2.8.0
240pylint                             2.7.4
241pyls-black                         0.4.6
242pyls-spyder                        0.3.2
243PyMySQL                            1.1.0
244pynvml                             11.5.0
245pyodbc                             4.0.0-unsupported
246pyOpenSSL                          20.0.1
247pyparsing                          2.4.7
248pyproject_hooks                    1.0.0
249pyright                            1.1.337
250pyrsistent                         0.17.3
251PySocks                            1.7.1
252pytest                             6.2.3
253python-dateutil                    2.8.1
254python-dotenv                      1.0.0
255python-jsonrpc-server              0.4.0
256python-language-server             0.36.2
257pytz                               2021.1
258PyWavelets                         1.1.1
259pyxdg                              0.27
260PyYAML                             6.0
261pyzmq                              20.0.0
262QDarkStyle                         2.8.1
263QtAwesome                          1.0.2
264qtconsole                          5.0.3
265QtPy                               1.9.0
266querystring-parser                 1.2.4
267redis                              5.0.0
268regex                              2021.4.4
269requests                           2.31.0
270rich                               13.5.2
271rope                               0.18.0
272Rtree                              0.9.7
273ruamel-yaml-conda                  0.15.100
274s3transfer                         0.7.0
275schema                             0.7.5
276scikit-image                       0.18.1
277scikit-learn                       0.24.1
278scipy                              1.10.1
279seaborn                            0.11.1
280SecretStorage                      3.3.1
281Send2Trash                         1.5.0
282setuptools                         68.2.2
283simplegeneric                      0.8.1
284singledispatch                     0.0.0
285sip                                4.19.13
286six                                1.16.0
287skl2onnx                           1.15.0
288smmap                              5.0.0
289sniffio                            1.2.0
290snowballstemmer                    2.1.0
291sortedcollections                  2.1.0
292sortedcontainers                   2.3.0
293soupsieve                          2.2.1
294Sphinx                             3.5.3
295sphinxcontrib-applehelp            1.0.2
296sphinxcontrib-devhelp              1.0.2
297sphinxcontrib-htmlhelp             1.0.3
298sphinxcontrib-jsmath               1.0.1
299sphinxcontrib-qthelp               1.0.3
300sphinxcontrib-serializinghtml      1.1.4
301sphinxcontrib-websupport           1.2.4
302spyder                             4.2.5
303spyder-kernels                     1.10.2
304SQLAlchemy                         1.4.7
305sqlparse                           0.4.4
306stack-data                         0.6.3
307statsmodels                        0.12.2
308sympy                              1.8
309tables                             3.6.1
310tabulate                           0.9.0
311tblib                              1.7.0
312termcolor                          2.3.0
313terminado                          0.9.4
314testpath                           0.4.4
315textdistance                       4.2.1
316threadpoolctl                      2.1.0
317three-merge                        0.1.1
318tifffile                           2020.10.1
319tokenize-rt                        5.2.0
320toml                               0.10.2
321tomli                              2.0.1
322toolz                              0.11.1
323torch                              1.13.0+cu117
324torchaudio                         0.12.1+cu113
325torchvision                        0.13.1+cu113
326tornado                            6.1
327tqdm                               4.27.0
328traitlets                          5.11.2
329treelite                           0.93
330typed-ast                          1.4.2
331typing_extensions                  4.8.0
332tzlocal                            5.0.1
333ujson                              4.0.2
334unicodecsv                         0.14.1
335urllib3                            1.26.16
336virtualenv                         20.24.3
337watchdog                           1.0.2
338wcwidth                            0.2.5
339webencodings                       0.5.1
340websocket-client                   1.6.2
341Werkzeug                           1.0.1
342wheel                              0.36.2
343widgetsnbextension                 3.5.1
344wrapt                              1.12.1
345wurlitzer                          2.1.0
346wy-cms-api                         1.0.18
347wy-feature-selector                2.1.0
348wy-model-optimizer                 0.1.1
349wy-utils                           2.5.2
350wyai-common                        1.0.6
351wyai-inference-sdk                 0.0.1rc3
352wyai-toolbox                       1.0.3
353wymrc                              0.0.2
354xlrd                               2.0.1
355XlsxWriter                         1.3.8
356xlwt                               1.3.0
357xmltodict                          0.12.0
358yapf                               0.31.0
359zict                               2.0.0
360zipp                               3.4.1
361zope.event                         4.5.0
362zope.interface                     5.3.0
363

结果举例

测试GPU为: A100-PCIE-40GB
import cupy,后import torch,得到的结果为:

 1输出张量: [[0.23729032]
 2 [0.84413356]
 3 [0.5424989 ]
 4 [0.24236819]
 5 [0.1216919 ]
 6 [0.574163  ]
 7 [0.5093746 ]
 8 [0.40847316]
 9 [0.32745278]
10 [0.3172678 ]]
11

而先import torch, 后import cupy,得到的结果是

 1[[0.23729031]
 2 [0.84413356]
 3 [0.5424989 ]
 4 [0.24236819]
 5 [0.12169194]
 6 [0.5741631 ]
 7 [0.5093746 ]
 8 [0.40847313]
 9 [0.32745278]
10 [0.31726775]]
11

一些猜测

模型和代码在cpu上没有任何问题,因此问题一定出在gpu上 怀疑是cupy初始化时某个cuda有关的设置和torch初始化时这个设置冲突

但是具体是是什么设置..还需要继续探究.