算能RISC-V通用云开发空间编译pytorch @openKylin留档

终于可以体验下risc-v了! 操作系统是openKylin,算能的云空间

尝试编译安装pytorch

首先安装git

apt install git

然后下载pytorch和算能cpu的库:

git clone https://github.com/sophgo/cpuinfo.git

git clone https://github.com/pytorch/pytorch

注意事项:

cd pytorch
# 确保子模块的远程仓库URL与父仓库中的配置一致
git submodule sync
# 确保获取并更新所有子模块的内容,包括初始化尚未初始化的子模块并递归地处理嵌套的子模块
git submodule update --init --recursive

将pytorch/third-parth目录的cpuinfo删除,换成算能的cpu库cpuinfo

cd pytorch

rm -rf cpuinfo

cp -rf ../cpuinfo .

安装相关库

apt install libopenblas-dev 报错,可以跳过

apt install libblas-dev m4 cmake cython3 ccache

手工编译安装openblas

git clone https://github.com/xianyi/OpenBLAS.git
cd OpenBLAS
make -j8
make PREFIX=/usr/local/OpenBLAS install

编译的时候是一堆warning啊

在/etc/profile最后一行添加:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/OpenBLAS/lib/

并执行:source  /etc/profile

修改代码

到pytorch目录,执行: vi aten/src/ATen/CMakeLists.txt

    aten/src/ATen/CMakeLists.txt

将语句:if(NOT MSVC AND NOT EMSCRIPTEN AND NOT INTERN_BUILD_MOBILE)
替换为:if(FALSE)

   vi caffe2/CMakeLists.txt

将语句:target_link_libraries(${test_name}_${CPU_CAPABILITY} c10 sleef gtest_main)
替换为:target_link_libraries(${test_name}_${CPU_CAPABILITY} c10 gtest_main)

   vi  test/cpp/api/CMakeLists.txt

在语句下:add_executable(test_api ${TORCH_API_TEST_SOURCES})
添加:target_compile_options(test_api PUBLIC -Wno-nonnull)

环境变量配置

# 直接在终端中输入即可,重启需要重新输入
export USE_CUDA=0
export USE_DISTRIBUTED=0
export USE_MKLDNN=0
export MAX_JOBS=16

配置原文链接:https://blog.csdn.net/m0_49267873/article/details/135670989

编译安装

执行:

python3 setup.py develop --cmake

或者python3.10 setup.py install

据说要gcc 13以上,自带的gcc版本:

gcc version 9.3.0 (Openkylin 9.3.0-ok12)

需要打patch:

# 若提示无patchelf命令,则执行下列语句
apt install patchelf

# path为存放libtorch_cpu.so的路径
patchelf --add-needed libatomic.so.1 /path/libtorch_cpu.so
 

对算能云的系统来说,命令为:patchelf --add-needed libatomic.so.1  /root/pytorch/build/lib/libtorch_cpu.so

编译前的准备

编译前还需要安装好这两个库:

pip3 install pyyaml typing_extensions

另外还要升级setuptools

pip3 install setuptools -U

最终编译完成

在pytorch目录执行:

python3 setup.py develop --cmake

整个编译过程大约需要3-4个小时

最终编译完成:

Installed /usr/lib/python3.8/site-packages/mpmath-1.3.0-py3.8.egg
Searching for typing-extensions==4.9.0
Best match: typing-extensions 4.9.0
Adding typing-extensions 4.9.0 to easy-install.pth file
detected new path './mpmath-1.3.0-py3.8.egg'

Using /usr/local/lib/python3.8/dist-packages
Finished processing dependencies for torch==2.3.0a0+git5c5b71b

测试

进入python3,执行import pytorch,报错没有pytorch。 执行import torch

看到没有报错,以为测试通过。其实是因为在pytorch目录,有子目录torch,误以为pass了

是我唐突了,因为使用的develop模式,就是这样用。

也就是必须在pytorch的目录,这样才能识别为develop的torch,在~/pytorch目录,执行python3,在命令交互方式下,把下面这段代码cp进去执行,测试通过

import torch
import torch.nn as nn
import torch.optim as optim
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"N,D_in,H,D_out = 64, 1000, 100, 10 # N: batch size, D_in:input size, H:hidden size, D_out: output size
x = torch.randn(N,D_in) # x = np.random.randn(N,D_in)
y = torch.randn(N,D_out) # y = np.random.randn(N,D_out)
w1 = torch.randn(D_in,H) # w1 = np.random.randn(D_in,H)
w2 = torch.randn(H,D_out) # w2 = np.random.randn(H,D_out)
learning_rate = 1e-6
for it in range(200):# forward passh = x.mm(w1) # N * H      h = x.dot(w1)h_relu = h.clamp(min=0) # N * H     np.maximum(h,0)y_pred = h_relu.mm(w2) # N * D_out     h_relu.dot(w2)  # compute lossloss = (y_pred - y).pow(2).sum() # np.square(y_pred-y).sum()print(it,loss.item()) #  print(it,loss)    # BP - compute the gradientgrad_y_pred = 2.0 * (y_pred-y)grad_w2 = h_relu.t().mm(grad_y_pred) # h_relu.T.dot(grad_y_pred)grad_h_relu = grad_y_pred.mm(w2.t())  # grad_y_pred.dot(w2.T)grad_h = grad_h_relu.clone() # grad_h_relu.copy()grad_h[h<0] = 0grad_w1 = x.t().mm(grad_h) # x.T.dot(grad_h)    # update weights of w1 and w2w1 -= learning_rate * grad_w1w2 -= learning_rate * grad_w2
0 29870438.0
1 26166322.0
2 25949932.0
3 25343224.0
4 22287072.0
5 16840522.0
6 11024538.0
7 6543464.5
8 3774165.25
9 2248810.5
10 1440020.25
11 1001724.5
12 749632.625
13 592216.6875
14 485451.34375
15 407586.65625
16 347618.4375
17 299686.625
18 260381.9375
19 227590.734375

怎样全环境可以用torch呢?

感觉是环境变量的问题,敬请期待

调试

安装libopenblas-dev报错

root@863c89a419ec:~/pytorch/third_party# apt install libopenblas-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package libopenblas-dev is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

竟然有人已经过了这个坑,可以跳过它,用编译安装openblas代替

编译pytorch的时候报错

python3 setup.py develop --cmake

Building wheel torch-2.3.0a0+git5c5b71b
-- Building version 2.3.0a0+git5c5b71b
Could not find any of CMakeLists.txt, Makefile, setup.py, LICENSE, LICENSE.md, LICENSE.txt in /root/pytorch/third_party/pybind11
Did you run 'git submodule update --init --recursive'?

进入third_parth目录执行下面命令解决:

rm -rf pthreadpool
# 执行下列指令前回退到pytorch目录
git submodule update --init --recursive

执行完还是报错:

root@863c89a419ec:~/pytorch# python3 setup.py develop --cmake
Building wheel torch-2.3.0a0+git5c5b71b
-- Building version 2.3.0a0+git5c5b71b
Could not find any of CMakeLists.txt, Makefile, setup.py, LICENSE, LICENSE.md, LICENSE.txt in /root/pytorch/third_party/QNNPACK
Did you run 'git submodule update --init --recursive'?

再次执行命令 git submodule update --init --recursive 照旧。

将QNNPACK目录删除,再执行一遍 git submodule update --init --recursive ,过了。

报错RuntimeError: Missing build dependency: Unable to `import yaml`.

python3 install pyyaml

报错:ModuleNotFoundError: No module named 'typing_extensions'

python3 install typing_extensions 搞定。

编译到78%报错

/usr/bin/ld: /root/pytorch/build/lib/libtorch_cpu.so: undefined reference to `__atomic_exchange_1'
collect2: error: ld returned 1 exit status
make[2]: *** [caffe2/CMakeFiles/NamedTensor_test.dir/build.make:101: bin/NamedTensor_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:3288: caffe2/CMakeFiles/NamedTensor_test.dir/all] Error 2
/usr/bin/ld: /root/pytorch/build/lib/libtorch_cpu.so: undefined reference to `__atomic_exchange_1'
collect2: error: ld returned 1 exit status
make[2]: *** [caffe2/CMakeFiles/cpu_profiling_allocator_test.dir/build.make:101: bin/cpu_profiling_allocator_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:3505: caffe2/CMakeFiles/cpu_profiling_allocator_test.dir/all] Error 2
[ 78%] Linking CXX executable ../bin/cpu_rng_test
/usr/bin/ld: /root/pytorch/build/lib/libtorch_cpu.so: undefined reference to `__atomic_exchange_1'
collect2: error: ld returned 1 exit status
make[2]: *** [caffe2/CMakeFiles/cpu_rng_test.dir/build.make:101: bin/cpu_rng_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:3536: caffe2/CMakeFiles/cpu_rng_test.dir/all] Error 2
make: *** [Makefile:146: all] Error 2

初步怀疑是cpu库有问题。看cpu库,没问题。

试试这个办法:

问题分析:对__atomic_exchange_1的未定义引用

解决方法:使用patchelf添加需要的动态库

# 若提示无patchelf命令,则执行下列语句
apt install patchelf

# path为存放libtorch_cpu.so的路径
patchelf --add-needed libatomic.so.1 /path/libtorch_cpu.so
 

存放libtorch_cpu.so的路径:/root/pytorch/build/lib/libtorch_cpu.so

因此命令为:patchelf --add-needed libatomic.so.1 /root/pytorch/build/lib/libtorch_cpu.so

果然运行完这条命令后,编译就能继续下去了。

编译100%报错

running develop
/usr/lib/python3/dist-packages/setuptools/command/easy_install.py:146: EasyInstallDeprecationWarning: easy_install command is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
Traceback (most recent call last):
  File "setup.py", line 1401, in <module>
    main()
  File "setup.py", line 1346, in main
    setup(
  File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 87, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib/python3/dist-packages/setuptools/_distutils/core.py", line 185, in setup
    return run_commands(dist)
  File "/usr/lib/python3/dist-packages/setuptools/_distutils/core.py", line 201, in run_commands
    dist.run_commands()
  File "/usr/lib/python3/dist-packages/setuptools/_distutils/dist.py", line 973, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 1217, in run_command
    super().run_command(command)
  File "/usr/lib/python3/dist-packages/setuptools/_distutils/dist.py", line 991, in run_command
    cmd_obj.ensure_finalized()
  File "/usr/lib/python3/dist-packages/setuptools/_distutils/cmd.py", line 109, in ensure_finalized
    self.finalize_options()
  File "/usr/lib/python3/dist-packages/setuptools/command/develop.py", line 52, in finalize_options
    easy_install.finalize_options(self)
  File "/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 231, in finalize_options
    self.config_vars = dict(sysconfig.get_config_vars())
UnboundLocalError: local variable 'sysconfig' referenced before assignment

尝试升级setuptools试试

root@863c89a419ec:~# pip3 install  setuptools -U
Collecting setuptools
  Using cached setuptools-69.1.0-py3-none-any.whl (819 kB)
Installing collected packages: setuptools
  Attempting uninstall: setuptools
    Found existing installation: setuptools 65.3.0
    Not uninstalling setuptools at /usr/lib/python3/dist-packages, outside environment /usr
    Can't uninstall 'setuptools'. No files were found to uninstall.
Successfully installed setuptools-69.1.0
然后再次编译,过了!

查看gcc版本

据说要gcc 13以上,自带的gcc版本:

gcc version 9.3.0 (Openkylin 9.3.0-ok12)

gcc version 9.3.0 (Openkylin 9.3.0-ok12)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2809754.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

比创达元启新程 共创新佳绩:2023年度总结暨迎新年晚会圆满收官!

新的一年&#xff0c;万象更新。回顾2023年&#xff0c;我们携手走过的岁月&#xff0c;喜悦伴着汗水&#xff0c;成功伴着艰辛&#xff0c;遗憾激励奋斗。在过去的一年时间里&#xff0c;每个行业都经历着前所未有的变革与困难。我们比创达人也凭借着人心齐泰山移的团结之力&a…

Spring Boot 项目集成camunda流程引擎

使用camunda开源工作流引擎有&#xff1a;通过docker运行、使用springboot集成、部署camunda发行包、基于源代码编译运行等多种方式。 其中&#xff0c;通过源代码编译运行的方式最为复杂&#xff0c;具体参考&#xff1a;https://lowcode.blog.csdn.net/article/details/1362…

图片录入设备、方式与质量对图片转Excel的影响

随着数字化时代的到来&#xff0c;图片已经成为人们日常生活中不可或缺的一部分。在各行各业中&#xff0c;图片的应用越发广泛&#xff0c;从而促使了图片处理技术的快速发展。然而&#xff0c;图片的质量对于后续数据处理和分析的准确性和可靠性有着至关重要的影响。本文将从…

Windows系统搭建Elasticsearch引擎结合内网穿透实现远程连接查询数据

文章目录 系统环境1. Windows 安装Elasticsearch2. 本地访问Elasticsearch3. Windows 安装 Cpolar4. 创建Elasticsearch公网访问地址5. 远程访问Elasticsearch6. 设置固定二级子域名 Elasticsearch是一个基于Lucene库的分布式搜索和分析引擎&#xff0c;它提供了一个分布式、多…

HTB-Bizness

一、信息收集 访问ip自动跳转域名&#xff0c;host绑定域名后访问 目录爆破 有一个登录目录&#xff0c;访问发现是apahce ofbiz登录页面 发现存在漏洞 二、漏洞利用 在github上找到了图形化利用工具 使用工具反弹shell 得到flag 三、权限提升 从本地利用python开启http服务…

Android RecyclerView 如何展示自定义列表 Kotlin

Android RecyclerView 如何展示自定义列表 Kotlin 一、前提 有这么一个对象 class DeviceDemo (val name: String, val type: String, val address: String)要展示一个包含这个对象的列表 bluetoothDevices.add(DeviceDemo("bb 9800", "LE", "32:…

蛇形矩阵3

题目描述 把数1&#xff0c;2&#xff0c;3&#xff0c;4&#xff0c;5&#xff0c;…&#xff0c;N*N按照“蛇形3”放入N*N矩阵的中&#xff0c;输出结果。 下面是N6的蛇形3的图示 输入格式 第一行1个正整数&#xff1a;N&#xff0c;范围在[1,100]。 输出格式 N行&#x…

docker 容器访问 GPU 资源使用指南

概述 nvidia-docker 和 nvidia-container-runtime 是用于在 NVIDIA GPU 上运行 Docker 容器的两个相关工具。它们的作用是提供 Docker 容器与 GPU 加速硬件的集成支持&#xff0c;使容器中的应用程序能够充分利用 GPU 资源。 nvidia-docker 为了提高 Nvidia GPU 在 docker 中的…

Linux系统前后端分离项目

目录 一.jdk安装 二.tomcat安装 三.MySQL安装 四.nginx安装 五.Nginx负载均衡tomcat 六.前端部署 一.jdk安装 1. 上传jdk安装包 jdk-8u151-linux-x64.tar.gz 进入opt目录&#xff0c;将安装包拖进去 2. 解压安装包 这里需要解压到usr/local目录下&#xff0c;在这里新建一个…

力扣思路题:丑数

此题的思路非常奇妙&#xff0c;可以借鉴一下 bool isUgly(int num){if(num0)return false;while(num%20)num/2;while(num%30)num/3;while(num%50)num/5;return num1; }

学会玩游戏,智能究竟从何而来?

最近在读梅拉妮米歇尔《AI 3.0》&#xff0c;第九章谈到学会玩游戏&#xff0c;智能究竟从何而来&#xff1f; 作者: [美] 梅拉妮米歇尔 出版社: 四川科学技术出版社湛庐 原作名: Artificial Intelligence: A Guide for Thinking Humans 译者: 王飞跃 / 李玉珂 / 王晓 / 张慧 …

Xcode与Swift开发小记

引子 鉴于React Native目前版本在iOS上开发遇到诸多问题&#xff0c;本以为搞RN只需理会Javascript开发&#xff0c;没想到冒出CocoaPod的一堆编译问题。所以横下一条心&#xff0c;决定直接进攻iOS本身。不管你是用React Native&#xff0c;还是用Flutter&#xff0c;iOS下的…

网站开发--详解Servlet

&#x1f495;"Echo"&#x1f495; 作者&#xff1a;Mylvzi 文章主要内容&#xff1a;网站开发–详解Servlet 一.基本介绍 tomcat是Java中开发服务器的重要的一个工具,任何开发的服务器都要部署在tomcat之上,可以说tomcat是所有服务器的底座,为了更好的操作http,to…

配置多个后端 API 代理

在开发 React 应用时&#xff0c;通常会涉及到与后端 API 的交互。而在开发过程中&#xff0c;我们经常需要在开发环境中使用代理来解决跨域请求的问题。Create React App 提供了一种简单的方式来配置代理&#xff0c;即通过创建一个名为 setupProxy.js 的文件来配置代理规则。…

Linux之安装jdk,tomcat,mysql,部署项目

目录 一、操作流程 1.1安装jdk 1.2安装tomcat&#xff08;加创建自启动脚本&#xff09; 1.3 安装mysql 1.4部署项目 一、操作流程 首先把需要用的包放进opt文件下 1.1安装jdk 把jdk解压到/usr/local/java里 在刚刚放解压包的文件夹打开vim /etc/profile编辑器&#xff0c…

高防服务器的原理

高防服务器的原理是什么?高防服务器的原理可以从哪些方面解读&#xff0c;小编为您整理发布高防服务器的工作原理主要基于以下几个方面&#xff1a; - **防火墙配置**&#xff1a;在服务器的节点上配置防火墙&#xff0c;能够自动过滤网络恶意攻击&#xff0c;提高网络安全性。…

Linux之部署前后端分离项目

Nginx配置安装 1.安装依赖 我们这里安装的依赖是有4个的 [rootlocalhost opt]# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel 2.上传解压安装包 [rootlocalhost opt]# tar -xvf nginx-1.13.7.tar.gz -C /usr/local/java/3.安装Nginx &#xff0…

聚观早报 | OPPO公布全新AI战略;华为P70 Art影像细节曝光

聚观早报每日整理最值得关注的行业重点事件&#xff0c;帮助大家及时了解最新行业动态&#xff0c;每日读报&#xff0c;就读聚观365资讯简报。 整理丨Cutie 2月22日消息 OPPO公布全新AI战略 华为P70 Art影像细节曝光 苹果正加速开发智能戒指 微软将大规模投资人工智能 …

openGauss学习笔记-229 openGauss性能调优-系统调优-配置Ustore

文章目录 openGauss学习笔记-229 openGauss性能调优-系统调优-配置Ustore229.1 设计原理229.2 核心优势229.3 使用指导 openGauss学习笔记-229 openGauss性能调优-系统调优-配置Ustore Ustore存储引擎&#xff0c;又名In-place Update存储引擎&#xff08;原地更新&#xff09…

北上广深数据分析岗位的薪资对比

目录 一、数据介绍及预处理 1、数据介绍 2、数据预处理 二、数据分析 1、岗位数量、薪资水平统计 3、企业维度岗位数量 4、top薪资岗位 三、划重点 少走10年弯路 之前跟大家分享过BOSS直聘上base北京的数据分析职位薪资数据分析&#xff0c;这次爬了北上广深四个城市的…