算能RISC-V通用云编译飞桨paddlepaddle@openKylin留档

尝试一在riscv里编译飞桨。

先总结:

下载飞桨代码,参照pr修改代码

然后编译

cmake ../ -DWITH_GPU=OFF -DWITH_RISCV=ON
make -j 16 TARGET=RISCV64_GENERIC

编译好后安装:

pip install paddlepaddle-0.0.0-cp38-cp38-linux_riscv64.whl -i https://mirror.baidu.com/pypi/simple

先git下载源码

git clone https://github.com/paddlepaddle/padle

配置环境

pip3 install protobuf

还有一些库等,具体可以参考编译pytorch的文档:算能RISC-V通用云开发空间编译pytorch @openKylin留档-CSDN博客

然后进入paddle目录编译

mkdir build

cd build

cmake ../

我的天,除了protobuf,竟然一下子编译完成了!

python 复制代码
Automatic code generation for paddle/fluid/primitive succeed.
Automatic code generation for decomp interface succeed.
WITH_DLNNE:
-- Configuring done
-- Generating done
-- Build files have been written to: /root/github/paddle/build

是我肤浅了,后面还需要编译呢root@863c89a419ec:~/github/paddle/build# make

make -j 16

make -j 8 TARGET=RISCV64_GENERIC -dw

如果碰到报错,根据报错进行处理,比如哪个第三方库编译失败,就删除那个目录,然后在third_party目录执行:git submodule update --init --recursive

然后再make 即可。

困了,让它编译去吧。

报错了

通过官网issue,发现cmake指令为:

cmake ../ -DWITH_GPU=OFF -WITH_RISCV=ON

make 指令为;

make -j 16 TARGET=RISCV64_GENERIC

发现cmake指令有问题

拼写错误,应该是:

cmake ../ -DWITH_GPU=OFF -DWITH_RISCV=ON

然后再make

看MakeFile文件里面没有RISCV选项,不知道是不是cmake编译器那边支持,先执行看看。(后来知道,人家这个RISCV是对应了专门的pr的,目前这个pr还没有合并,所以需要手工改密码)

cmake结束之后提示WITH_RISCV没有生效

手工添加飞桨对riscv的支持

修改CMakeFile文件,在WITH_ARM的后面加上:

bash 复制代码
if(WITH_RISCV)
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
  set(WITH_XBYAK
      OFF
      CACHE STRING "Disable XBYAK when compiling WITH_RISCV=ON." FORCE)
  set(WITH_MKL
      OFF
      CACHE STRING "Disable MKL when compiling WITH_RISCV=ON." FORCE)
  set(WITH_AVX
      OFF
      CACHE STRING "Disable AVX when compiling WITH_AVX=OFF." FORCE)
  add_definitions(-DPADDLE_WITH_RISCV)
endif()

修改文件:cmake/flags.cmake

在ARM后面加上:AND NOT WITH_RISCV

同时找到了-m64的位置。因为加了这句,就不用手工删除-m64这句。

修改文件:paddle/fluid/operators/search_compute.h

一共需要修改四处,在四处加上defined(PADDLE_WITH_RISCV):

bash 复制代码
#pragma once

#if !defined(PADDLE_WITH_ARM) && !defined(PADDLE_WITH_SW) && \
    !defined(PADDLE_WITH_MIPS) && !defined(PADDLE_WITH_LOONGARCH)
    !defined(PADDLE_WITH_MIPS) && !defined(PADDLE_WITH_LOONGARCH) && \
	!defined(PADDLE_WITH_RISCV)
#include <immintrin.h>
#endif
#include <cfloat>
@@ -103,7 +104,8 @@ void call_gemm_batched(const framework::ExecutionContext& ctx,
}

#if !defined(PADDLE_WITH_ARM) && !defined(PADDLE_WITH_SW) && \
    !defined(PADDLE_WITH_MIPS) && !defined(PADDLE_WITH_LOONGARCH)
    !defined(PADDLE_WITH_MIPS) && !defined(PADDLE_WITH_LOONGARCH) && \
	!defined(PADDLE_WITH_RISCV)

#define __m256x __m256

@@ -144,7 +146,8 @@ inline void axpy(const T* x, T* y, size_t len, const T alpha) {
                      _mm256_mul_px(mm_alpha, _mm256_load_px(x + jjj))));
  }
#elif defined(PADDLE_WITH_ARM) || defined(PADDLE_WITH_SW) || \
    defined(PADDLE_WITH_MIPS) || defined(PADDLE_WITH_LOONGARCH)
    defined(PADDLE_WITH_MIPS) || defined(PADDLE_WITH_LOONGARCH) || \
	defined(PADDLE_WITH_RISCV)
  PADDLE_THROW(platform::errors::Unimplemented("axpy is not supported"));
#else
  lll = len & ~SSE_CUT_LEN_MASK;
@@ -174,7 +177,8 @@ inline void axpy_noadd(const T* x, T* y, size_t len, const T alpha) {
    _mm256_store_px(y + jjj, _mm256_mul_px(mm_alpha, _mm256_load_px(x + jjj)));
  }
#elif defined(PADDLE_WITH_ARM) || defined(PADDLE_WITH_SW) || \
    defined(PADDLE_WITH_MIPS) || defined(PADDLE_WITH_LOONGARCH)
    defined(PADDLE_WITH_MIPS) || defined(PADDLE_WITH_LOONGARCH) || \
	defined(PADDLE_WITH_RISCV)
  PADDLE_THROW(platform::errors::Unimplemented("axpy_noadd is not supported"));
#else
  lll = len & ~SSE_CUT_LEN_MASK;

修改文件:paddle/fluid/platform/denormal.cc

33行if那句后面加上RISCV,变成这样:

bash 复制代码
#if !defined(GCC_WITHOUT_INTRINSICS) && !defined(PADDLE_WITH_ARM) && \
    !defined(PADDLE_WITH_SW) && !defined(PADDLE_WITH_MIPS) &&        \
    !defined(_WIN32) && !defined(PADDLE_WITH_LOONGARCH) &&           \
        !defined(PADDLE_WITH_RISCV)
#define DENORM_USE_INTRINSICS

修改文件paddle/phi/backends/cpu/cpu_info.cc

bash 复制代码
  } else {
#if !defined(WITH_NV_JETSON) && !defined(PADDLE_WITH_ARM) &&  \
    !defined(PADDLE_WITH_SW) && !defined(PADDLE_WITH_MIPS) && \
    !defined(PADDLE_WITH_LOONGARCH) && !defined(PADDLE_WITH_RISCV)
    std::array<int, 4> reg;
    cpuid(reg.data(), 0);
    int nIds = reg[0];

修改文件paddle/phi/backends/cpu/cpu_info.h

bash 复制代码
#define cpuid(reg, x) __cpuidex(reg, x, 0)
#else
#if !defined(WITH_NV_JETSON) && !defined(PADDLE_WITH_ARM) &&  \
    !defined(PADDLE_WITH_SW) && !defined(PADDLE_WITH_MIPS) && \
    !defined(PADDLE_WITH_LOONGARCH) && !defined(PADDLE_WITH_RISCV)
#include <cpuid.h>
inline void cpuid(int reg[4], int x) {

感觉离曙光很近了。

重新cmake和make,希望不会碰到"pmmintrin.h"文件找不到的错误。

发现少修改一个文件

修改文件cmake/third_party.cmake

if(WITH_POCKETFFT)
  include(external/pocketfft)
  list(APPEND third_party_deps extern_pocketfft)
  add_definitions(-DPADDLE_WITH_POCKETFFT)
if(WITH_RISCV)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-maybe-uninitialized") # Warnings in pocketfft_hdronly.h
endif()
endif()

11点开始编译,计时开始!15:37分编译完成,耗时4小时37分钟!

哈哈,它静静的躺在那里:

root@863c89a419ec:~/github/paddle/build/python# ls dist/
paddlepaddle-0.0.0-cp38-cp38-linux_riscv64.whl
root@863c89a419ec:~/github/paddle/build/python# pip3 install dist/paddlepaddle-0.0.0-cp38-cp38-linux_riscv64.whl  -i https://mirror.baidu.com/pypi/simple
Processing ./dist/paddlepaddle-0.0.0-cp38-cp38-linux_riscv64.whl

安装:

bash 复制代码
pip install paddlepaddle-0.0.0-cp38-cp38-linux_riscv64.whl

安装后执行import paddle 报错。

总结:

目前已编译成功。

目前import paddle报错。

调试

报错:Could NOT find PY_google.protobuf (missing: PY_GOOGLE.PROTOBUF)

Could NOT find PY_google.protobuf (missing: PY_GOOGLE.PROTOBUF)

安装protobuf

pip3 install protobuf

编译时到100%报错

[ 97%] Building CXX object gloo/CMakeFiles/gloo.dir/transport/tcp/unbound_buffer.cc.o

[100%] Linking CXX static library libgloo.a

[100%] Built target gloo

[ 3%] Performing install step for 'extern_gloo'

[ 3%] Completed 'extern_gloo'

[ 3%] Built target extern_gloo

make: *** [Makefile:136: all] Error 2

再重新编译,到了protobuf这里报错:

-- Installing: /root/github/paddle/build/third_party/install/protobuf/lib/cmake/protobuf/protobuf-config.cmake

[ 4%] Completed 'extern_protobuf'

[ 4%] Built target extern_protobuf

make: *** [Makefile:136: all] Error 2

再重新编译,发现这里报错:

报错:cc1: error: '-march=loongson3a': ISA string must begin with rv32 or rv64

cc1: error: requested ABI requires '-march' to subsume the 'D' extension

cc1: error: ABI requires '-march=rv64'

make[4]: *** [Makefile:737: isamin.o] Error 1

cc1: error: '-march=loongson3a': ISA string must begin with rv32 or rv64

make[4]: *** [Makefile:611: sasum.o] Error 1

cc1: error: '-march=loongson3a': ISA string must begin with rv32 or rv64

cc1: error: '-march=loongson3a': ISA string must begin with rv32 or rv64

cc1: error: requested ABI requires '-march' to subsume the 'D' extension

cc1: error: requested ABI requires '-march' to subsume the 'D' extension

cc1: error: ABI requires '-march=rv64'

cc1: error: ABI requires '-march=rv64'

cc1: error: requested ABI requires '-march' to subsume the 'D' extension

cc1: error: ABI requires '-march=rv64'

make[4]: *** [Makefile:647: snrm2.o] Error 1

make[4]: *** [Makefile:629: ssum.o] Error 1

make[4]: *** [Makefile:701: smax.o] Error 1

make[4]: *** [Makefile:665: samax.o] Error 1

make[4]: *** [Makefile:755: ismax.o] Error 1

make[4]: *** [Makefile:773: sdsdot.o] Error 1

make[3]: *** [Makefile:164: libs] Error 1

make[2]: *** [CMakeFiles/extern_openblas.dir/build.make:86: third_party/openblas/src/extern_openblas-stamp/extern_openblas-build] Error 2

make[1]: *** [CMakeFiles/Makefile2:4001: CMakeFiles/extern_openblas.dir/all] Error 2

[ 5%] Built target eager_python_c_codegen

[ 5%] Built target op_map_codegen

[ 5%] Built target eager_codegen

make: *** [Makefile:136: all] Error 2

参考这里重新来过:

如何在RISC-V平台编译paddle - 知乎

编译报错:

Live child 0x2af978fd70 (paddle/fluid/eager/auto_code_generator/generator/CMakeFiles/eager_codegen) PID 115530

Reaping winning child 0x2af978fd70 PID 115530

Removing child 0x2af978fd70 PID 115530 from chain.

Considering target file 'paddle/fluid/eager/auto_code_generator/generator/CMakeFiles/eager_codegen.dir/build'.

File 'paddle/fluid/eager/auto_code_generator/generator/CMakeFiles/eager_codegen.dir/build' does not exist.

Considering target file 'eager_codegen'.

File 'eager_codegen' does not exist.

Considering target file 'paddle/fluid/eager/auto_code_generator/generator/CMakeFiles/eager_codegen'.

File 'paddle/fluid/eager/auto_code_generator/generator/CMakeFiles/eager_codegen' was considered already.

Considering target file 'paddle/fluid/eager/auto_code_generator/generator/CMakeFiles/eager_codegen.dir/build.make'.

File 'paddle/fluid/eager/auto_code_generator/generator/CMakeFiles/eager_codegen.dir/build.make' was considered already.

Finished prerequisites of target file 'eager_codegen'.

Must remake target 'eager_codegen'.

Successfully remade target file 'eager_codegen'.

Finished prerequisites of target file 'paddle/fluid/eager/auto_code_generator/generator/CMakeFiles/eager_codegen.dir/build'.

Must remake target 'paddle/fluid/eager/auto_code_generator/generator/CMakeFiles/eager_codegen.dir/build'.

Successfully remade target file 'paddle/fluid/eager/auto_code_generator/generator/CMakeFiles/eager_codegen.dir/build'.

make[2]: Leaving directory '/root/github/paddle/build'

Reaping winning child 0x2b00eb0510 PID 115266

Live child 0x2b00eb0510 (paddle/fluid/eager/auto_code_generator/generator/CMakeFiles/eager_codegen.dir/all) PID 115532

[ 4%] Built target eager_codegen

Reaping winning child 0x2b00eb0510 PID 115532

Removing child 0x2b00eb0510 PID 115532 from chain.

make[1]: Leaving directory '/root/github/paddle/build'

Reaping losing child 0x2ad05e7c50 PID 115118

make: *** [Makefile:136: all] Error 2

Removing child 0x2ad05e7c50 PID 115118 from chain.

make: Leaving directory '/root/github/paddle/build'

在paddle/build/third_party删除eigen3目录,然后执行:git submodule update --init --recursive

问题解决。

问题没有解决,还是4%这里报错:

[ 4%] Built target eager_codegen

Reaping winning child 0x2b167efa70 PID 117670

Removing child 0x2b167efa70 PID 117670 from chain.

make[1]: Leaving directory '/root/github/paddle/build'

Reaping losing child 0x2b0332dc20 PID 117206

make: *** [Makefile:136: all] Error 2

Removing child 0x2b0332dc20 PID 117206 from chain.

make: Leaving directory '/root/github/paddle/build'

重新设置make 后,cmake ../ -DWITH_GPU=OFF -WITH_RISCV=ON 编译报错

报错c++: error: unrecognized command line option '-m64'

按照文档,应该"查找makefile文件的生成逻辑,最终发现在Paddle.cmake文件中有这样一段逻辑",将该文件中的-64去掉。

但是没有找到这个文件。找到了,在这里:

cmake/flags.cmake

if(NOT WITH_NV_JETSON

AND NOT WITH_ARM

AND NOT WITH_RISCV

AND NOT WITH_SW

AND NOT WITH_MIPS

AND NOT WITH_LOONGARCH)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")

endif()

后期通过修改源码,添加RISCV选项的方法,使代码支持RISCV,并同时使用-m64失效

在cmake的时候加上DWITH_RISCV提示没有生效

WITH_DLNNE:

-- Configuring done

-- Generating done

CMake Warning:

Manually-specified variables were not used by the project:

WITH_RISCV

按照飞桨官网pr,修改飞桨make相关文件。

编译报错没有 pmmintrin.h文件

[ 7%] Building CXX object paddle/fluid/platform/CMakeFiles/denormal.dir/denormal.cc.o

/root/github/paddle/paddle/fluid/platform/denormal.cc:38:10: fatal error: pmmintrin.h: No such file or directory

38 | #include <pmmintrin.h>

| ^~~~~~~~~~~~~

compilation terminated.

make[2]: *** [paddle/fluid/platform/CMakeFiles/denormal.dir/build.make:76: paddle/fluid/platform/CMakeFiles/denormal.dir/denormal.cc.o] Error 1

make[1]: *** [CMakeFiles/Makefile2:4787: paddle/fluid/platform/CMakeFiles/denormal.dir/all] Error 2

make[1]: *** Waiting for unfinished jobs....

这个对应的就是paddle/fluid/platform/denormal.cc 这个文件,已经修改。

编译安装好后,

import paddle报错

oot@863c89a419ec:~/github/paddle/build# python3

Python 3.8.2 (default, Jan 18 2024, 07:05:37)

[GCC 9.3.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import paddle

Error: Can not import paddle core while this file exists: /usr/local/lib/python3.8/dist-packages/paddle/base/libpaddle.so

Traceback (most recent call last):

File "/usr/local/lib/python3.8/dist-packages/paddle/base/core.py", line 267, in <module>

from . import libpaddle

ImportError: /usr/local/lib/python3.8/dist-packages/paddle/base/libpaddle.so: undefined symbol: __atomic_exchange_1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "/usr/local/lib/python3.8/dist-packages/paddle/init.py", line 30, in <module>

from .base import core # noqa: F401

File "/usr/local/lib/python3.8/dist-packages/paddle/base/init.py", line 38, in <module>

from . import ( # noqa: F401

File "/usr/local/lib/python3.8/dist-packages/paddle/base/backward.py", line 25, in <module>

from . import core, framework, log_helper, unique_name

File "/usr/local/lib/python3.8/dist-packages/paddle/base/core.py", line 377, in <module>

if not avx_supported() and libpaddle.is_compiled_with_avx():

NameError: name 'libpaddle' is not defined

看文档,有人说是patchelf版本低的缘故,升级patchelf,再重新编译,问题依旧

https://github.com/PaddlePaddle/Paddle/issues/51536

已提issue :https://github.com/PaddlePaddle/Paddle/issues/62037

学习官网issue

risc-v芯片上编译paddle报错 · Issue #61770 · PaddlePaddle/Paddle · GitHub

学习官网改risc-v的pr

Adding a compile option to Paddle for Risc-V · PaddlePaddle/Paddle@d3db383 · GitHub

相关推荐
i嗑盐の小F29 分钟前
【IEEE 独立出版,快速EI检索】第四届人工智能、虚拟现实与可视化国际学术会议(AIVRV 2024)
大数据·人工智能·深度学习·算法·机器学习·vr
小王毕业啦2 小时前
全国832个贫困县名单及精准扶贫脱贫数据(2016-2020.11)
大数据·人工智能·数据挖掘·数据分析·社科数据
jndingxin2 小时前
OpenCV特征检测(8)检测图像中圆形的函数HoughCircles()的使用
人工智能·opencv·计算机视觉
富士达幸运星4 小时前
卷积神经网络(CNN):深度学习中的视觉奇迹
人工智能·深度学习·cnn
网络研究院7 小时前
由于安全风险,安全领导者考虑禁止人工智能编码
人工智能·安全·开源·开发·风险·技术·代码
hero_heart7 小时前
PointNet2(一)分类
人工智能·分类·数据挖掘
阿W呀8 小时前
MATLAB-最小二乘辨识
人工智能·算法·matlab
RedMery8 小时前
Ubuntu20.04配置NVIDIA+CUDA12.2+CUDNN【附所有下载资源】【亲测有效】【非常详细】
人工智能·windows
SQingL8 小时前
用OPenCV分割视频
人工智能·opencv·音视频
洋葱土豆和香菜8 小时前
图像处理与分析
图像处理·人工智能