Document setup and reproduction steps
本仓库包含两个可独立复现的 RISC-V C 算子:
matmul_ame/
build/models_ame_matmul_fpga.bin
softmax_rvv/
PASS
项目源码和辅助文件全部在本仓库内。外部只需要编译器、RISC-V sysroot/binutils 和 QEMU,不需要 Qwen 模型权重。
本仓库已验证的工具版本:
RuyiAI clang 23.0.0git, commit 926a1ea856cbb7978514c7c960aa16c3a835381c riscv64-linux-gnu-gcc 13.3.0 GNU binutils 2.42 qemu-riscv64 11.0.0
sudo apt update sudo apt install -y \ git make cmake ninja-build build-essential \ gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu \ qemu-user
确认发行版工具可用:
riscv64-linux-gnu-gcc --version riscv64-linux-gnu-objcopy --version riscv64-linux-gnu-readelf --version qemu-riscv64 --version test -d /usr/riscv64-linux-gnu && echo "RISC-V sysroot OK"
git clone https://gitlink.org.cn/michaelcjl/kernel_c.git cd kernel_c
AME matmul 使用 msettile*、mlce32、msce32、mqma.b.mm 等扩展指令。 Ubuntu 自带的 clang 不支持这些指令,不能用于构建 matmul。
msettile*
mlce32
msce32
mqma.b.mm
使用已验证的 RuyiAI LLVM fork:
mkdir -p "$HOME/opt" git clone https://github.com/RuyiAI-Stack/llvm-project.git \ "$HOME/opt/llvm-project-riscv" cd "$HOME/opt/llvm-project-riscv" git checkout 926a1ea856cbb7978514c7c960aa16c3a835381c cmake -S llvm -B build -G Ninja \ -DCMAKE_BUILD_TYPE=Release \ -DLLVM_ENABLE_PROJECTS="clang" \ -DLLVM_TARGETS_TO_BUILD="RISCV" ninja -C build clang
记录 clang 路径:
export AME_CLANG="$HOME/opt/llvm-project-riscv/build/bin/clang" "$AME_CLANG" --version
若已经拿到同一 fork 的预编译 clang,可跳过源码构建,直接让 AME_CLANG 指向该可执行文件。
AME_CLANG
cd /path/to/kernel_c/matmul_ame make clean make CLANG="$AME_CLANG" make verify CLANG="$AME_CLANG"
成功时会看到:
Generated: build/models_ame_matmul_fpga.bin Entry point address: 0x86400000
主要产物:
matmul_ame/build/models_ame_matmul_fpga.bin matmul_ame/build/models_ame_matmul_fpga.elf matmul_ame/build/models_ame_matmul_fpga.map
bin 的加载/入口地址为 0x86400000,UART MMIO 地址为 0x10000000。 该 bin 用于 FPGA,不在普通 QEMU 中执行。
0x86400000
0x10000000
如果提示 AME 指令需要 XBOSCAME,说明使用了错误的 clang,或使用的 RuyiAI LLVM 版本与上述 commit 不一致。
XBOSCAME
softmax 不需要定制 LLVM,使用发行版 RISC-V GCC 即可:
cd /path/to/kernel_c/softmax_rvv make clean make run make verify
make run 实际执行:
make run
qemu-riscv64 -cpu rv64,v=true,vlen=256,elen=64 build/rvv_softmax
程序会打印 512 行逐元素比较:
index | input | RVV output | reference | abs diff | status
结尾应为:
elements=512 passed=512 failed=0 PASS
softmax_rvv/build/rvv_softmax softmax_rvv/build/rvv_softmax_qemu.log
可测试其他向量长度:
make clean make QEMU_CPU='rv64,v=true,vlen=128,elen=64' run make QEMU_CPU='rv64,v=true,vlen=512,elen=64' run
clang-23: command not found
matmul Makefile 默认查找 clang-23。推荐始终显式指定:
clang-23
make CLANG="$HOME/opt/llvm-project-riscv/build/bin/clang"
instruction requires ... XBOSCAME
当前 clang 不包含或没有正确启用 RuyiAI AME 扩展。使用第 4 节指定的 fork 和 commit,不要使用 Ubuntu 官方 clang。
riscv64-linux-gnu-objcopy: command not found
sudo apt install binutils-riscv64-linux-gnu
cannot find -lgcc
sudo apt install gcc-riscv64-linux-gnu test -d /usr/riscv64-linux-gnu
确认使用的是 qemu-riscv64(用户态模拟器),并升级到支持 RVV 1.0 的 较新 QEMU。运行以下命令检查:
qemu-riscv64
qemu-riscv64 -cpu rv64,v=true,vlen=256,elen=64 \ softmax_rvv/build/rvv_softmax
matmul_ame/Makefile
softmax_rvv/Makefile
rvv_softmax.c
rvv_common.h
版权所有:中国计算机学会技术支持:开源发展技术委员会 京ICP备13000930号-9 京公网安备 11010802047560号
AME Matmul and RVV Softmax
本仓库包含两个可独立复现的 RISC-V C 算子:
matmul_ame/build/models_ame_matmul_fpga.binsoftmax_rvv/PASS项目源码和辅助文件全部在本仓库内。外部只需要编译器、RISC-V sysroot/binutils 和 QEMU,不需要 Qwen 模型权重。
1. 推荐环境
本仓库已验证的工具版本:
2. 安装基础依赖
确认发行版工具可用:
3. 获取仓库
4. 构建定制 LLVM(matmul 必需)
AME matmul 使用
msettile*、mlce32、msce32、mqma.b.mm等扩展指令。 Ubuntu 自带的 clang 不支持这些指令,不能用于构建 matmul。使用已验证的 RuyiAI LLVM fork:
记录 clang 路径:
若已经拿到同一 fork 的预编译 clang,可跳过源码构建,直接让
AME_CLANG指向该可执行文件。5. 生成 AME matmul FPGA bin
成功时会看到:
主要产物:
bin 的加载/入口地址为
0x86400000,UART MMIO 地址为0x10000000。 该 bin 用于 FPGA,不在普通 QEMU 中执行。如果提示 AME 指令需要
XBOSCAME,说明使用了错误的 clang,或使用的 RuyiAI LLVM 版本与上述 commit 不一致。6. 在 QEMU 运行 RVV softmax
softmax 不需要定制 LLVM,使用发行版 RISC-V GCC 即可:
make run实际执行:程序会打印 512 行逐元素比较:
结尾应为:
主要产物:
可测试其他向量长度:
7. 常见问题
clang-23: command not foundmatmul Makefile 默认查找
clang-23。推荐始终显式指定:instruction requires ... XBOSCAME当前 clang 不包含或没有正确启用 RuyiAI AME 扩展。使用第 4 节指定的 fork 和 commit,不要使用 Ubuntu 官方 clang。
riscv64-linux-gnu-objcopy: command not foundcannot find -lgcc或找不到 RISC-V 头文件QEMU 提示 CPU 属性或 RVV 指令不支持
确认使用的是
qemu-riscv64(用户态模拟器),并升级到支持 RVV 1.0 的 较新 QEMU。运行以下命令检查:8. 独立性说明
matmul_ame/Makefile的所有源码、头文件、启动文件和链接脚本均来自matmul_ame/内部。softmax_rvv/Makefile只读取本目录的rvv_softmax.c和rvv_common.h。