diff --git "a/sig/Hygon Arch/content/2-CSV\346\265\213\350\257\225\346\226\207\346\241\243/3-\350\231\232\346\213\237\346\234\272/4-\346\265\213\350\257\225CSV\350\231\232\346\213\237\346\234\272\347\233\264\351\200\232DCU.md" "b/sig/Hygon Arch/content/2-CSV\346\265\213\350\257\225\346\226\207\346\241\243/3-\350\231\232\346\213\237\346\234\272/4-\346\265\213\350\257\225CSV\350\231\232\346\213\237\346\234\272\347\233\264\351\200\232DCU.md" index f50d04374c8c54314c688664b12e1ddc4276838a..a627802e00ad93c9f12618875282c83630fe0349 100644 --- "a/sig/Hygon Arch/content/2-CSV\346\265\213\350\257\225\346\226\207\346\241\243/3-\350\231\232\346\213\237\346\234\272/4-\346\265\213\350\257\225CSV\350\231\232\346\213\237\346\234\272\347\233\264\351\200\232DCU.md" +++ "b/sig/Hygon Arch/content/2-CSV\346\265\213\350\257\225\346\226\207\346\241\243/3-\350\231\232\346\213\237\346\234\272/4-\346\265\213\350\257\225CSV\350\231\232\346\213\237\346\234\272\347\233\264\351\200\232DCU.md" @@ -8,15 +8,15 @@ * DCU直通CSV虚拟机 - 将DCU直通到CSV虚拟机,在CSV的机密环境中运行DCU。 + 将DCU直通到CSV虚拟机,在CSV的机密环境中运行DCU。 * 全机密模式 - 作为DCU直通CSV的增强版,CSV虚拟机和DCU卡之间通过PCIE总线传输数据,传输的数据被DCU软件栈加密,密钥由安全处理器管理。DCU支持显存隔离功能,用户数据放在隔离显存中处理,SDMA无法访问。 + 作为DCU直通CSV的增强版,CSV虚拟机和DCU卡之间通过PCIE总线传输数据,传输的数据被DCU软件栈加密,密钥由安全处理器管理。DCU支持显存隔离功能,用户数据放在隔离显存中处理,SDMA无法访问。 * DCU attestation - DCU attestation用来向CSV用户证明DCU卡是真实的海光DCU,而不是伪造的DCU。 + DCU attestation用来向CSV用户证明DCU卡是真实的海光DCU,而不是伪造的DCU。 我们使用qemu启动CSV虚拟机,借助vfio将DCU直通给CSV虚拟机,就可以在CSV虚拟机中直接使用DCU设备。 @@ -32,140 +32,139 @@ * Host上安装DCU的fixup header插件 - 请确认Host上是否已安装DCU fixup header插件。 - - ```sh - $ cat /sys/module/hycu_pci_fixup_header/version - 0.4.4 - ``` - - 如果上面命令有打印输出,表明Host已经安装了,就不用再次安装了。 - - 从`光合开发社区`主页的`工具包`页面,下载DCU的fixup header插件,文件名为`hycu_pci_fixup_header_{version}.bin`,并在Host上安装。 - - 以`hycu_pci_fixup_header_0.4.4.bin`为例: - - ```sh - $ chmod +x hycu_pci_fixup_header_0.4.4.bin - $ sudo ./hycu_pci_fixup_header_0.4.4.bin - ``` - - 安装完成后,验证当前fixup header版本,显示为0.4.4: - - ```sh - $ cat /sys/module/hycu_pci_fixup_header/version - 0.4.4 - ``` - - > **注意:** - > - > 1. 老版本的fixup header名为hydcu_pci_fixup_header_{version}.bin,可以从`光合开发社区->DCU直通插件->previous_tool`中下载到。 - > 2. 如果更改host kernel,如使用其他版本的Kernel,或者重新编译host kernel,必须要重新安装fixup header插件才能生效。 - + 请确认Host上是否已安装DCU fixup header插件。 + + ```sh + $ cat /sys/module/hycu_pci_fixup_header/version + 0.4.4 + ``` + + 如果上面命令有打印输出,表明Host已经安装了,就不用再次安装了。 + + 从`光合开发社区`主页的`工具包`页面,下载DCU的fixup header插件,文件名为`hycu_pci_fixup_header_{version}.bin`,并在Host上安装。 + + 以`hycu_pci_fixup_header_0.4.4.bin`为例: + + ```sh + $ chmod +x hycu_pci_fixup_header_0.4.4.bin + $ sudo ./hycu_pci_fixup_header_0.4.4.bin + ``` + + 安装完成后,验证当前fixup header版本,显示为0.4.4: + + ```sh + $ cat /sys/module/hycu_pci_fixup_header/version + 0.4.4 + ``` + + > **注意:** + > + > 1. 老版本的fixup header名为hydcu_pci_fixup_header_{version}.bin,可以从`光合开发社区->DCU直通插件->previous_tool`中下载到。 + > 2. 如果更改host kernel,如使用其他版本的Kernel,或者重新编译host kernel,必须要重新安装fixup header插件才能生效。 + * 查看DCU设备的Vendor ID和Device ID - 通过下面命令找到DCU设备的Device ID和Vendor ID: - - ```sh - $ lspci -nn | grep -e "Display" -e "Co-processor" | grep "Haiguang" - 03:00.0 Display controller [0380]: Chengdu Haiguang IC Design Co., Ltd. Device [1d94:53b7] (rev 01) - ``` - - 可以看到,示例设备的VendorID为**1d94**,Device Id为**53b7**。 - - > 注意:不同的DCU卡的Device ID不同,以实际为准。下面操作中用DCU实际的Device Id替换53b7。 + 通过下面命令找到DCU设备的Device ID和Vendor ID: + + ```sh + $ lspci -nn | grep -e "Display" -e "Co-processor" | grep "Haiguang" + 03:00.0 Display controller [0380]: Chengdu Haiguang IC Design Co., Ltd. Device [1d94:53b7] (rev 01) + ``` + + 可以看到,示例设备的VendorID为**1d94**,Device Id为**53b7**。 + + > 注意:不同的DCU卡的Device ID不同,以实际为准。下面操作中用DCU实际的Device Id替换53b7。 * Host加载VFIO驱动 - 执行下面命令,在Host加载VFIO驱动: - - ```sh - $ sudo modprobe vfio_virqfd - $ sudo modprobe vfio_pci - $ sudo modprobe vfio_iommu_type1 - $ sudo modprobe vfio - ``` - - > **注意**:每次重启host后,启动虚拟机前,需要加载vfio驱动。 + 执行下面命令,在Host加载VFIO驱动: + + ```sh + $ sudo modprobe vfio_virqfd + $ sudo modprobe vfio_pci + $ sudo modprobe vfio_iommu_type1 + $ sudo modprobe vfio + ``` + > **注意**:每次重启host后,启动虚拟机前,需要加载vfio驱动。 * 将DCU绑定到VFIO驱动 - 对于DCU直通虚拟机,在Host上,DCU使用VFIO驱动,我们需要将DCU绑定到VFIO驱动。 - - 如果在Host上已经加载的DCU驱动,那么在将DCU绑定VFIO驱动之前,还需要将DCU和DCU驱动解绑。 - - 可以通过下面命令查看DCU当前绑定的驱动: - - ```sh - $ lspci -s 03:00.0 -v | grep "Kernel driver" - ``` - - 如果输出位空,那么表明当前Host上DCU没有绑定到到任何驱动,可以直接将DCU绑定到VFIO驱动。 - - 如果DCU已经绑定到DCU驱动,上面命令的输出为: - - ```sh - $ lspci -s 03:00.0 -v | grep "Kernel driver" - Kernel driver in use: hydcu - ``` - - 表明DCU当前在使用`hydcu`驱动。我们将DCU和`hydcu`驱动解绑: - - ```sh - $ echo 0000:03:00.0 | sudo tee /sys/bus/pci/devices/0000:03:00.0/driver/unbind - ``` - - 执行命令后,可以再次通过命令`lspci -s 03:00.0 -v | grep "Kernel driver"`验证,此时该命令输出为空。 - - 可以使用命令` sudo systemctl disable hymgr.service`永久禁止Host自动加载DCU驱动,需重启host才能生效。 - - 最后,执行下面命令将DCU绑定到VFIO驱动,再次查看DCU的当前驱动为`vfio-pci`: - - ```sh - $ echo 1d94 53b7 | sudo tee /sys/bus/pci/drivers/vfio-pci/new_id - $ lspci -s 03:00.0 -v | grep "Kernel driver" - Kernel driver in use: vfio-pci - ``` - - 同时也可以看到,在目录`/dev/vfio`下会产生名为DCU设备所在iommu group的ID文件,例如,本机上DCU的iommu group为16,显示如下: - - ```sh - $ ls /dev/vfio - 16 vfio - ``` - - > **注意**:每次重启host后,启动虚拟机前,需要重新绑定DCU到VFIO驱动。 + 对于DCU直通虚拟机,在Host上,DCU使用VFIO驱动,我们需要将DCU绑定到VFIO驱动。 + + 如果在Host上已经加载的DCU驱动,那么在将DCU绑定VFIO驱动之前,还需要将DCU和DCU驱动解绑。 + + 可以通过下面命令查看DCU当前绑定的驱动: + + ```sh + $ lspci -s 03:00.0 -v | grep "Kernel driver" + ``` + + 如果输出位空,那么表明当前Host上DCU没有绑定到到任何驱动,可以直接将DCU绑定到VFIO驱动。 + + 如果DCU已经绑定到DCU驱动,上面命令的输出为: + + ```sh + $ lspci -s 03:00.0 -v | grep "Kernel driver" + Kernel driver in use: hydcu + ``` + + 表明DCU当前在使用`hydcu`驱动。我们将DCU和`hydcu`驱动解绑: + + ```sh + $ echo 0000:03:00.0 | sudo tee /sys/bus/pci/devices/0000:03:00.0/driver/unbind + ``` + + 执行命令后,可以再次通过命令`lspci -s 03:00.0 -v | grep "Kernel driver"`验证,此时该命令输出为空。 + + 可以使用命令` sudo systemctl disable hymgr.service`永久禁止Host自动加载DCU驱动,需重启host才能生效。 + + 最后,执行下面命令将DCU绑定到VFIO驱动,再次查看DCU的当前驱动为`vfio-pci`: + + ```sh + $ echo 1d94 53b7 | sudo tee /sys/bus/pci/drivers/vfio-pci/new_id + $ lspci -s 03:00.0 -v | grep "Kernel driver" + Kernel driver in use: vfio-pci + ``` + + 同时也可以看到,在目录`/dev/vfio`下会产生名为DCU设备所在iommu group的ID文件,例如,本机上DCU的iommu group为16,显示如下: + + ```sh + $ ls /dev/vfio + 16 vfio + ``` + + > **注意**:每次重启host后,启动虚拟机前,需要重新绑定DCU到VFIO驱动。 * 启动CSV虚拟机 - 经过前面的准备工作,我们在Host上将DCU设备绑定到VFIO驱动,现在可以通过qemu启动CSV虚拟机,将DCU直通到虚拟机中。 - - 执行下面命令启动CSV虚拟机: - - ```sh - $ sudo qemu-system-x86_64 -name csv-vm --enable-kvm -cpu host -m 6G -nographic \ - -hda /opt/hygon/csv/vm.qcow2 \ - -drive if=pflash,format=raw,unit=0,file=/opt/hygon/csv/OVMF_CODE.fd,readonly=on \ - -object sev-guest,id=sev0,policy=0x1,cbitpos=47,reduced-phys-bits=5 \ - -machine memory-encryption=sev0 \ - -qmp tcp:127.0.0.1:2222,server,nowait -vnc 0.0.0.0:1 \ - -netdev bridge,br=virbr0,id=net0 -device virtio-net-pci,netdev=net0,romfile= \ - -device vfio-pci,host=03:00.0 \ - -fw_cfg name=opt/ovmf/X-PciMmio64Mb,string=131072 - ``` - - 其中: - - * `-device vfio-pci,host=03:00.0` 指定DCU的PCI地址,例程中为`03:00.0`,用户需要更改自己DCU的实际PCI地址 - * `-fw_cfg name=opt/ovmf/X-PciMmio64Mb,string=131072`指定vfio mmio bar的大小。 - - > **注意**: - > - > 1. 不同的DCU的VRAM大小可能不同,此处对于64G的VRAM的DCU,指定`sring=131072`;32G的VRAM的DCU,需要指定更改为`string=65536`。 - > 2. 如果有多个DCU卡需要直通到CSV虚拟机,在上述命令中添加一行`-device vfio-pci,host=xx:yy.z` ,xx:yy.z为新增DCU卡的设备地址。同时将参数`-fw_cfg name=opt/ovmf/X-PciMmio64Mb,string=131072`按照线性比例调整。 - - 虚拟机启动后,登录虚拟机,可以通过命令`lspci`查看到DCU设备,说明DCU直通CSV虚拟机成功。 - + 经过前面的准备工作,我们在Host上将DCU设备绑定到VFIO驱动,现在可以通过qemu启动CSV虚拟机,将DCU直通到虚拟机中。 + + 执行下面命令启动CSV虚拟机: + + ```sh + $ sudo qemu-system-x86_64 -name csv-vm --enable-kvm -cpu host -m 6G -nographic \ + -hda /opt/hygon/csv/vm.qcow2 \ + -drive if=pflash,format=raw,unit=0,file=/opt/hygon/csv/OVMF_CODE.fd,readonly=on \ + -object sev-guest,id=sev0,policy=0x1,cbitpos=47,reduced-phys-bits=5 \ + -machine memory-encryption=sev0 \ + -qmp tcp:127.0.0.1:2222,server,nowait -vnc 0.0.0.0:1 \ + -netdev bridge,br=virbr0,id=net0 -device virtio-net-pci,netdev=net0,romfile= \ + -device vfio-pci,host=03:00.0 \ + -fw_cfg name=opt/ovmf/X-PciMmio64Mb,string=131072 + ``` + + 其中: + + * `-device vfio-pci,host=03:00.0` 指定DCU的PCI地址,例程中为`03:00.0`,用户需要更改自己DCU的实际PCI地址 + * `-fw_cfg name=opt/ovmf/X-PciMmio64Mb,string=131072`指定vfio mmio bar的大小。 + + > **注意**: + > + > 1. 不同的DCU的VRAM大小可能不同,此处对于64G的VRAM的DCU,指定`sring=131072`;32G的VRAM的DCU,需要指定更改为`string=65536`。 + > 2. 如果有多个DCU卡需要直通到CSV虚拟机,在上述命令中添加一行`-device vfio-pci,host=xx:yy.z` ,xx:yy.z为新增DCU卡的设备地址。同时将参数`-fw_cfg name=opt/ovmf/X-PciMmio64Mb,string=131072`按照线性比例调整。 + + 虚拟机启动后,登录虚拟机,可以通过命令`lspci`查看到DCU设备,说明DCU直通CSV虚拟机成功。 + ### 在CSV虚拟机中安装DCU环境 @@ -175,74 +174,73 @@ * 安装驱动 - 参考光合组织`https://cancon.hpccube.com:65024/6/main/`页面`README.md`文件指导安装驱动。这里以安装`rock-5.7.1-6.2.31-V1.6.3.aio.run`为例。 - - 进入`光合开发者社区->驱动->dtk-24.04驱动`,下载`rock-5.7.1-6.2.31-V1.6.3.aio.run`。 - - 进行安装: - - ```sh - $ chmod +x rock-5.7.1-6.2.31-V1.6.3.aio.run - $ sudo ./rock-5.7.1-6.2.31-V1.6.3.aio.run - ``` - - 安装完成后,禁止虚拟机的selinux。打开`/etc/selinux/config`文件,修改`SELINUX`为`SELINUX=disabled`。 - - 最后在虚拟机中执行`reboot`,重启虚拟机。 - - 虚拟机重启后,在虚拟中执行命令验证驱动正确加载: - - ```sh - $ /opt/hyhal/bin/hy-smi - - ============================ System Management Interface ============================= - ====================================================================================== - DCU Temp AvgPwr Perf PwrCap VRAM% DCU% Mode - 0 47.0C 15.0W auto 450.0W 0% 0.0% Normal - ====================================================================================== - =================================== End of SMI Log =================================== - ``` + 参考光合组织`https://cancon.hpccube.com:65024/6/main/`页面`README.md`文件指导安装驱动。这里以安装`rock-5.7.1-6.2.31-V1.6.3.aio.run`为例。 + + 进入`光合开发者社区->驱动->dtk-24.04驱动`,下载`rock-5.7.1-6.2.31-V1.6.3.aio.run`。 + + 进行安装: + + ```sh + $ chmod +x rock-5.7.1-6.2.31-V1.6.3.aio.run + $ sudo ./rock-5.7.1-6.2.31-V1.6.3.aio.run + ``` + + 安装完成后,禁止虚拟机的selinux。打开`/etc/selinux/config`文件,修改`SELINUX`为`SELINUX=disabled`。 + + 最后在虚拟机中执行`reboot`,重启虚拟机。 + + 虚拟机重启后,在虚拟中执行命令验证驱动正确加载: + + ```sh + $ /opt/hyhal/bin/hy-smi + + ============================ System Management Interface ============================= + ====================================================================================== + DCU Temp AvgPwr Perf PwrCap VRAM% DCU% Mode + 0 47.0C 15.0W auto 450.0W 0% 0.0% Normal + ====================================================================================== + =================================== End of SMI Log =================================== + ``` * 安装DTK软件栈 - 在光合组织DTK下载页面`https://cancon.hpccube.com:65024/1/main`下载DTK软件。 - - 这里我们选择与上面驱动`dtk-24.04驱动/rock-5.7.1-6.2.31-V1.6.3.aio.run`对应的DTK版本`DTK-24.04.3`为例进行演示。如果要安装其他版本的DTK,请确保DTK和驱动rock的版本匹配。 - 在`https://cancon.hpccube.com:65024/1/main/DTK-24.04.3`页面选择适合虚拟机OS的DTK软件。如虚拟机的OS为ubuntu20.04,选择`Ubuntu20.04.1`,进入下载页面下载。 - - 将DTK解压到/opt目录: - - ```sh - $ sudo tar zxvf DTK-24.04.3-Ubuntu20.04.1-x86_64.tar.gz -C /opt - ``` - - 安装完成后,在虚拟机的`/opt`目录下生成`/opt/dtk-24.04.3`目录,其中的文件就是DTK安装后生成的文件。 + 在光合组织DTK下载页面`https://cancon.hpccube.com:65024/1/main`下载DTK软件。 + + 这里我们选择与上面驱动`dtk-24.04驱动/rock-5.7.1-6.2.31-V1.6.3.aio.run`对应的DTK版本`DTK-24.04.3`为例进行演示。如果要安装其他版本的DTK,请确保DTK和驱动rock的版本匹配。 + 在`https://cancon.hpccube.com:65024/1/main/DTK-24.04.3`页面选择适合虚拟机OS的DTK软件。如虚拟机的OS为ubuntu20.04,选择`Ubuntu20.04.1`,进入下载页面下载。 + + 将DTK解压到/opt目录: + + ```sh + $ sudo tar zxvf DTK-24.04.3-Ubuntu20.04.1-x86_64.tar.gz -C /opt + ``` + + 安装完成后,在虚拟机的`/opt`目录下生成`/opt/dtk-24.04.3`目录,其中的文件就是DTK安装后生成的文件。 * 配置环境 - 在CSV虚拟机上,需要使能CSV虚拟机相关的环境变量。执行下面命令: - - ```sh - $ echo "hy-virtual -enable-encrypted" | sudo tee -a /opt/dtk-24.04.3/env.sh - ``` - - 最后使能dtk的环境变量,执行: - - ```sh - $ source /opt/dtk-24.04.3/env.sh - ``` - - 可以将上面使能dtk环境变量的命令添加的shell的初始化文件。例如,对于bash: - - ```sh - $ echo "source /opt/dtk-24.04.3/env.sh" >> ~/.bashrc - $ source ~/.bashrc - ``` - - 其他的shell,如zsh, csh请自行添加到对应的shell初始化文件中。 - + 在CSV虚拟机上,需要使能CSV虚拟机相关的环境变量。执行下面命令: + + ```sh + $ echo "hy-virtual -enable-encrypted" | sudo tee -a /opt/dtk-24.04.3/env.sh + ``` + + 最后使能dtk的环境变量,执行: + + ```sh + $ source /opt/dtk-24.04.3/env.sh + ``` + + 可以将上面使能dtk环境变量的命令添加的shell的初始化文件。例如,对于bash: + + ```sh + $ echo "source /opt/dtk-24.04.3/env.sh" >> ~/.bashrc + $ source ~/.bashrc + ``` + + 其他的shell,如zsh, csh请自行添加到对应的shell初始化文件中。 ## 在CSV虚拟机中运行DCU测试程序 @@ -276,182 +274,181 @@ PASSED! * 安装go -```sh -$ wget https://golang.google.cn/dl/go1.23.4.linux-amd64.tar.gz -$ tar -C /usr/local -xzf go1.23.4.linux-amd64.tar.gz -$ export PATH=$PATH:/usr/local/go/bin -``` + ```sh + $ wget https://golang.google.cn/dl/go1.23.4.linux-amd64.tar.gz + $ tar -C /usr/local -xzf go1.23.4.linux-amd64.tar.gz + $ export PATH=$PATH:/usr/local/go/bin + ``` * 安装gcc-10 -```sh -$ sudo apt-get update -$ sudo apt-get -y install gcc-10 g++-10 -``` - -安装完成后,需要设置默认的gcc、g++为gcc-10、g++-10。 - -```sh -$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10 -$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10 -``` - -验证默认gcc为gcc-10 - -```sh -$ gcc --version -$ gcc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0 -``` + ```sh + $ sudo apt-get update + $ sudo apt-get -y install gcc-10 g++-10 + ``` + + 安装完成后,需要设置默认的gcc、g++为gcc-10、g++-10。 + + ```sh + $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10 + $ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10 + ``` + + 验证默认gcc为gcc-10 + + ```sh + $ gcc --version + $ gcc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0 + ``` * 安装python3.10 -如果系统中已经安装了Python3.10或者python3.11,可以跳过安装python3.10。 - -此处我们使用python venv环境,和主机的python环境隔离开来。 - -```sh -sudo add-apt-repository ppa:deadsnakes/ppa -sudo apt-get update -sudo apt-get -y install python3.10 python3.10-venv -mkdir ~/py_venv -python3.10 -m venv ~/py_venv -``` - -激活python3.10 venv环境 - -```sh -$ source ~/py_venv/bin/activate -``` - -> 注意: 如果使用python venv环境,每次需要在bash中激活venv环境。可以将`source ~/py_venv/bin/activate`放在~/.bashrc中,新开终端自动激活。 + 如果系统中已经安装了Python3.10或者python3.11,可以跳过安装python3.10。 + + 此处我们使用python venv环境,和主机的python环境隔离开来。 + + ```sh + sudo add-apt-repository ppa:deadsnakes/ppa + sudo apt-get update + sudo apt-get -y install python3.10 python3.10-venv + mkdir ~/py_venv + python3.10 -m venv ~/py_venv + ``` + + 激活python3.10 venv环境 + + ```sh + $ source ~/py_venv/bin/activate + ``` + + > 注意: 如果使用python venv环境,每次需要在bash中激活venv环境。可以将`source ~/py_venv/bin/activate`放在~/.bashrc中,新开终端自动激活。 * 安装pytorch -在`光合开发者社区->AI生态包`页面`https://cancon.hpccube.com:65024/4/main`选择与DTK、python版本一直的`pytorch`,此处我们选择适合我们Python3.10的版本`torch-2.1.0+das.opt2.dtk24043-cp310-cp310-manylinux_2_28_x86_64.whl`,在`DAS1.3`中。 - -```sh -$ python3.10 -m pip install https://download.sourcefind.cn:65024/directlink/4/pytorch/DAS1.3/torch-2.1.0+das.opt2.dtk24043-cp310-cp310-manylinux_2_28_x86_64.whl -``` - -> 注意: -> -> 1. 如果使用python3.11,请选择对应python3.11版本的pytorch,并使用python3.11命令替换python3.10。 -> -> 2. pytorch的版本:例如,pytorch安装包名称中有`dtk24043`字样,表明该pytorch适用于DTK 24.04.3版本。安装包名称中有`cp310`字样,表明该pytorch适用于python3.10。 - -安装其他依赖库 - -```sh -$ sudo apt-get -y install libgl1 -$ python3.10 -m pip install --force-reinstall -v "numpy==1.25.2" -``` - -测试torch可以识别到DCU卡 - -```sh -$ python3.10 # Enter -Python 3.10.17 (main, Apr 9 2025, 08:54:15) [GCC 9.4.0] on linux -Type "help", "copyright", "credits" or "license" for more information. ->>> import torch ->>> torch.cuda.is_available() -True -``` + 在`光合开发者社区->AI生态包`页面`https://cancon.hpccube.com:65024/4/main`选择与DTK、python版本一直的`pytorch`,此处我们选择适合我们Python3.10的版本`torch-2.1.0+das.opt2.dtk24043-cp310-cp310-manylinux_2_28_x86_64.whl`,在`DAS1.3`中。 + + ```sh + $ python3.10 -m pip install https://download.sourcefind.cn:65024/directlink/4/pytorch/DAS1.3/torch-2.1.0+das.opt2.dtk24043-cp310-cp310-manylinux_2_28_x86_64.whl + ``` + + > 注意: + > + > 1. 如果使用python3.11,请选择对应python3.11版本的pytorch,并使用python3.11命令替换python3.10。 + > + > 2. pytorch的版本:例如,pytorch安装包名称中有`dtk24043`字样,表明该pytorch适用于DTK 24.04.3版本。安装包名称中有`cp310`字样,表明该pytorch适用于python3.10。 + + 安装其他依赖库 + + ```sh + $ sudo apt-get -y install libgl1 + $ python3.10 -m pip install --force-reinstall -v "numpy==1.25.2" + ``` + + 测试torch可以识别到DCU卡 + + ```sh + $ python3.10 # Enter + Python 3.10.17 (main, Apr 9 2025, 08:54:15) [GCC 9.4.0] on linux + Type "help", "copyright", "credits" or "license" for more information. + >>> import torch + >>> torch.cuda.is_available() + True + ``` * 编译ollama -```sh -$ git clone http://developer.sourcefind.cn/codes/OpenDAS/ollama.git -b 0.5.7+dtk24.03 --depth 1 -$ cd ollama -$ export LIBRARY_PATH=/opt/dtk-24.04.3/lib:$LIBRARY_PATH -$ make -j${nproc} -$ go build . -``` - -> 注意:LIBRARY_PATH调整为实际的dtk的路径。 - -编译时间比较久,请耐心等待。 + ```sh + $ git clone http://developer.sourcefind.cn/codes/OpenDAS/ollama.git -b 0.5.7+dtk24.03 --depth 1 + $ cd ollama + $ export LIBRARY_PATH=/opt/dtk-24.04.3/lib:$LIBRARY_PATH + $ make -j${nproc} + $ go build . + ``` + + > 注意:LIBRARY_PATH调整为实际的dtk的路径。 + + 编译时间比较久,请耐心等待。 * 单卡运行Qwen大模型 -在ollama目录中,运行ollama serve - -```sh -$ cd ollama -$ export HSA_OVERRIDE_GFX_VERSION=9.0.6 # Z100 gfx906对应9.0.6, K100 gfx926对应9.2.6, K100AI gfx928对应9.2.8 -$ export ROCR_VISIBLE_DEVICES=0 # DCU卡的编号,可以通过hy-smi命令查看DCU卡编号。此处以单卡为例,多卡请看多卡运行Qwen大模型一节 -$ ./ollama serve -``` - -另外再开启一个终端,拉取Qwen大模型。此处以qwen:7b为例。 - -```sh -$ ./ollama pull qwen:7b -``` - -> 提示:ollama支持的其他模型,可以在`https://ollama.com/search`页面查找。 - -下载完成后,可以通过`ollama list`命令查看到: - -```sh -$ ./ollama list -NAME ID SIZE MODIFIED -qwen:7b 2091ee8c8d8f 4.5 GB 4 days ago -``` - -最后运行模型: - -```sh -$ ./ollama run qwen:7b ->>> hello -Hello! How can I assist you today? - ->>> Send a message (/? for help) -``` - -通过查看DCU的VRAM显存占用率,确认qwen模型已经在DCU上运行起来了。 - -```sh -$ hy-smi -============================ System Management Interface ============================= -====================================================================================== -DCU Temp AvgPwr Perf PwrCap VRAM% DCU% Mode -0 44.0C 15.0W auto 450.0W 28% 0% Normal -====================================================================================== -=================================== End of SMI Log =================================== -``` + 在ollama目录中,运行ollama serve + + ```sh + $ cd ollama + $ export HSA_OVERRIDE_GFX_VERSION=9.0.6 # Z100 gfx906对应9.0.6, K100 gfx926对应9.2.6, K100AI gfx928对应9.2.8 + $ export ROCR_VISIBLE_DEVICES=0 # DCU卡的编号,可以通过hy-smi命令查看DCU卡编号。此处以单卡为例,多卡请看多卡运行Qwen大模型一节 + $ ./ollama serve + ``` + + 另外再开启一个终端,拉取Qwen大模型。此处以qwen:7b为例。 + + ```sh + $ ./ollama pull qwen:7b + ``` + + > 提示:ollama支持的其他模型,可以在`https://ollama.com/search`页面查找。 + + 下载完成后,可以通过`ollama list`命令查看到: + + ```sh + $ ./ollama list + NAME ID SIZE MODIFIED + qwen:7b 2091ee8c8d8f 4.5 GB 4 days ago + ``` + + 最后运行模型: + + ```sh + $ ./ollama run qwen:7b + >>> hello + Hello! How can I assist you today? + + >>> Send a message (/? for help) + ``` + + 通过查看DCU的VRAM显存占用率,确认qwen模型已经在DCU上运行起来了。 + + ```sh + $ hy-smi + ============================ System Management Interface ============================= + ====================================================================================== + DCU Temp AvgPwr Perf PwrCap VRAM% DCU% Mode + 0 44.0C 15.0W auto 450.0W 28% 0% Normal + ====================================================================================== + =================================== End of SMI Log =================================== + ``` * 多卡运行qwen大模型 -在ollama目录中,运行ollama serve - -```sh -$ cd ollama -$ export HSA_OVERRIDE_GFX_VERSION=9.0.6 # Z100 gfx906对应9.0.6, K100 gfx926对应9.2.6, K100AI gfx928对应9.2.8 -$ export ROCR_VISIBLE_DEVICES=0,1 # DCU卡的编号,可以通过hy-smi命令查看DCU卡编号。选择编号为0和1的DCU卡 -$ export OLLAMA_SCHED_SPREAD=True # 允许ollama在多卡之间调度 -$ ./ollama serve -``` - -另外再开启一个终端,运行qwen大模型: - -```sh -$ ./ollama run qwen:7b ->>> -``` - -通过查看DCU的VRAM显存占用率,确认qwen模型已经在DCU上运行起来了。 - -```sh -$ hy-smi -============================ System Management Interface ============================= -====================================================================================== -DCU Temp AvgPwr Perf PwrCap VRAM% DCU% Mode -0 45.0C 15.0W auto 450.0W 16% 0% Normal -1 42.0C 14.0W auto 450.0W 15% 0% Normal -====================================================================================== -=================================== End of SMI Log =================================== -``` - + 在ollama目录中,运行ollama serve + + ```sh + $ cd ollama + $ export HSA_OVERRIDE_GFX_VERSION=9.0.6 # Z100 gfx906对应9.0.6, K100 gfx926对应9.2.6, K100AI gfx928对应9.2.8 + $ export ROCR_VISIBLE_DEVICES=0,1 # DCU卡的编号,可以通过hy-smi命令查看DCU卡编号。选择编号为0和1的DCU卡 + $ export OLLAMA_SCHED_SPREAD=True # 允许ollama在多卡之间调度 + $ ./ollama serve + ``` + + 另外再开启一个终端,运行qwen大模型: + + ```sh + $ ./ollama run qwen:7b + >>> + ``` + + 通过查看DCU的VRAM显存占用率,确认qwen模型已经在DCU上运行起来了。 + + ```sh + $ hy-smi + ============================ System Management Interface ============================= + ====================================================================================== + DCU Temp AvgPwr Perf PwrCap VRAM% DCU% Mode + 0 45.0C 15.0W auto 450.0W 16% 0% Normal + 1 42.0C 14.0W auto 450.0W 15% 0% Normal + ====================================================================================== + =================================== End of SMI Log =================================== + ``` # 测试全机密模式 @@ -531,7 +528,7 @@ DCU驱动从 rock-kernel-refactory-rock-5.7.1-6.2.26-V1.5.aio.run 开始支持At ## 认证机制 - 每一颗DCU芯片拥有唯一的芯片ID(ChipID),在芯片生产时烧入,以后无法更改。芯片内置一SM2密钥对(DCEK, DCU Chip Endorsement Key),私钥作为DCU芯片的身份象征,只保存在DCU芯片,永不外泄。公钥被HDSK(海光DCU签名密钥)/HRK(海光根密钥)签名后,存放于海光证书系统中,可公开下载。 +每一颗DCU芯片拥有唯一的芯片ID(ChipID),在芯片生产时烧入,以后无法更改。芯片内置一SM2密钥对(DCEK, DCU Chip Endorsement Key),私钥作为DCU芯片的身份象征,只保存在DCU芯片,永不外泄。公钥被HDSK(海光DCU签名密钥)/HRK(海光根密钥)签名后,存放于海光证书系统中,可公开下载。 ![](../../../assets/dcu_attestation_arch.png) @@ -553,53 +550,53 @@ DCU驱动从 rock-kernel-refactory-rock-5.7.1-6.2.26-V1.5.aio.run 开始支持At * 在线远程认证 - 如果您的CSV虚拟机能够直接连接海光证书服务器(https://cert.hygon.cn/),请直接编运行,示例程序将自动完成DCU认证过程。 + 如果您的CSV虚拟机能够直接连接海光证书服务器(https://cert.hygon.cn/),请直接编运行,示例程序将自动完成DCU认证过程。 - ```sh - $ cd attestation - $ make - $./dcu_attestation_demo - ``` + ```sh + $ cd attestation + $ make + $./dcu_attestation_demo + ``` * 手动认证 - 如果您的CSV虚拟机无法连接海光证书服务器,则需要手动下载证书。将下载的证书放置于dcu_attestation_demo程序的所在的目录下,执行下面步骤: - - 1. 运行demo程序,获取DCU ChipID - - ```sh - $ ./dcu_attestation_demo - ``` - - 该程序会打印出DCU ChipID,并打印证书下载失败的日志,然后退出。 - - ```sh - chip_id - T6N69800002080601 - ``` + 如果您的CSV虚拟机无法连接海光证书服务器,则需要手动下载证书。将下载的证书放置于dcu_attestation_demo程序的所在的目录下,执行下面步骤: - 如上日志所示,该DCU的ChipID为”T6N6980002080601”。 + 1. 运行demo程序,获取DCU ChipID - 2. 在attestation下,手动下载该芯片的HSK_CEK证书 + ```sh + $ ./dcu_attestation_demo + ``` + + 该程序会打印出DCU ChipID,并打印证书下载失败的日志,然后退出。 + + ```sh + chip_id + T6N69800002080601 + ``` + + 如上日志所示,该DCU的ChipID为”T6N6980002080601”。 - ```sh - $ curl -s -f -o hsk_cek.cert https://cert.hygon.cn/hsk_cek?snumber=T6N6980002080601 - ``` + 2. 在attestation下,手动下载该芯片的HSK_CEK证书 - 命令完成后,在当前目录下生成hsk_cek.cert文件,文件包含HSK证书和CEK证书。 + ```sh + $ curl -s -f -o hsk_cek.cert https://cert.hygon.cn/hsk_cek?snumber=T6N6980002080601 + ``` + + 命令完成后,在当前目录下生成hsk_cek.cert文件,文件包含HSK证书和CEK证书。 - 3. 在attestation下,手动下载HRK证书 + 3. 在attestation下,手动下载HRK证书 - ```sh - $ curl -s -f -o ./hrk.cert https://cert.hygon.cn/hrk - ``` + ```sh + $ curl -s -f -o ./hrk.cert https://cert.hygon.cn/hrk + ``` + + 命令完成后,在当前目录下生成hrk.cert。 - 命令完成后,在当前目录下生成hrk.cert。 + 4. 再次运行attestation demo程序,完成DCU身份验证 - 4. 再次运行attestation demo程序,完成DCU身份验证 + ```sh + $ ./dcu_attestation_demo + ``` - ```sh - $ ./dcu_attestation_demo - ``` - diff --git "a/sig/Hygon Arch/content/2-CSV\346\265\213\350\257\225\346\226\207\346\241\243/4-KATA-3/4-\346\265\213\350\257\225\347\233\264\351\200\232DCU.md" "b/sig/Hygon Arch/content/2-CSV\346\265\213\350\257\225\346\226\207\346\241\243/4-KATA-3/4-\346\265\213\350\257\225\347\233\264\351\200\232DCU.md" index cd30774b5fec0a82cd8d76973f5eac135c3a5cca..93376249c25539c9c7dcc46bf78d20db8afe1b1e 100644 --- "a/sig/Hygon Arch/content/2-CSV\346\265\213\350\257\225\346\226\207\346\241\243/4-KATA-3/4-\346\265\213\350\257\225\347\233\264\351\200\232DCU.md" +++ "b/sig/Hygon Arch/content/2-CSV\346\265\213\350\257\225\346\226\207\346\241\243/4-KATA-3/4-\346\265\213\350\257\225\347\233\264\351\200\232DCU.md" @@ -11,32 +11,33 @@ ### IOMMU设置 -#### 1. BIOS打开IOMMU +* BIOS打开IOMMU -使用DCU直通功能必须打开BIOS的IOMMU功能,具体请参考[1-0-安全功能配置要求](https://openanolis.cn/sig/Hygon-Arch/doc/1076446632387394260?lang=zh)中**BIOS安装及设置要求->DCU直通功能** 章节。 + 使用DCU直通功能必须打开BIOS的IOMMU功能,具体请参考[1-0-安全功能配置要求](https://openanolis.cn/sig/Hygon-Arch/doc/1076446632387394260?lang=zh)中**BIOS安装及设置要求->DCU直通功能** 章节。 -#### 2. 检查IOMMU设置成功 +* 检查IOMMU设置成功 -```sh -$ dmesg | grep -i IOMMU -``` - -得到类似如下的输出,表示IOMMU设置成功: - -``` -[ 0.626887] iommu: Default domain type: Passthrough (set via kernel command line) -[ 0.873056] pci 0000:00:00.2: AMD-Vi: IOMMU performance counters supported -[ 0.873112] pci 0000:40:00.2: AMD-Vi: IOMMU performance counters supported -[ 0.873157] pci 0000:00:01.0: Adding to iommu group 0 -[ 0.873172] pci 0000:00:01.2: Adding to iommu group 1 -[ 0.873187] pci 0000:00:01.4: Adding to iommu group 2 -[ 0.873207] pci 0000:00:02.0: Adding to iommu group 3 -``` + ```sh + $ dmesg | grep -i IOMMU + ``` + + 得到类似如下的输出,表示IOMMU设置成功: + + ```sh + [ 0.626887] iommu: Default domain type: Passthrough (set via kernel command line) + [ 0.873056] pci 0000:00:00.2: AMD-Vi: IOMMU performance counters supported + [ 0.873112] pci 0000:40:00.2: AMD-Vi: IOMMU performance counters supported + [ 0.873157] pci 0000:00:01.0: Adding to iommu group 0 + [ 0.873172] pci 0000:00:01.2: Adding to iommu group 1 + [ 0.873187] pci 0000:00:01.4: Adding to iommu group 2 + [ 0.873207] pci 0000:00:02.0: Adding to iommu group 3 + ``` ## 功能描述 把DCU直通进CSV机密容器中,使其处于可信执行环境中,在可信执行环境中借助DCU加速机密计算workload。 + ## 测试用例 ### 修改kata配置文件 @@ -45,124 +46,123 @@ $ dmesg | grep -i IOMMU > > 如果需要使用该功能,请参考[6-使用加密镜像来部署workload](https://openanolis.cn/sig/Hygon-Arch/doc/865622244851586866)。 -- 关闭pre-attestation +* 关闭pre-attestation ```sh $ sudo sed -i -e 's/^\(guest_pre_attestation =\).*$/\1 false/g' /opt/confidential-containers/share/defaults/kata-containers/configuration-qemu-csv-dcu.toml ``` -- initrd指向支持offline_fs_kbc模式的initrd(其他initrd为社区增强特性准备) +* initrd指向支持offline_fs_kbc模式的initrd(其他initrd为社区增强特性准备) ```sh $ sudo sed -i -e 's#^\(initrd\).*=.*$#\1 = \"/opt/confidential-containers/share/kata-containers/kata-containers-initrd-csv-hygon-gpu.img.offline_fs_kbc\"#g' /opt/confidential-containers/share/defaults/kata-containers/configuration-qemu-csv-dcu.toml ``` -- 禁用sev_kbc模式 +* 禁用sev_kbc模式 由于国内网络目前访问不了dockerhub 需要使用代理,代理参数agent.https_proxy=http://ip:port agent.no_proxy=10.*.*.*,172.*.*.* http://ip:port 为代理地址 ```sh $ sudo sed -i -e 's#^\(kernel_params\).*=.*$#\1 = \"agent.https_proxy=http://ip:port agent.no_proxy=10.*.*.*,172.*.*.* agent.aa_kbc_params=offline_fs_kbc::null agent.enable_signature_verification=false \"#g' /opt/confidential-containers/share/defaults/kata-containers/configuration-qemu-csv-dcu.toml ``` -- 设置内存 +* 设置内存 ```sh $ sudo sed -i "s/^default_memory *=.*\$/default_memory = 10240/g" /opt/confidential-containers/share/defaults/kata-containers/configuration-qemu-csv-dcu.toml ``` -### 部署DCU直通环境 -#### 1. 查找DCU设备 - -找到DCU设备的PCI 设备标识地址,后面会用到。 - -- 找到DCU设备 +### 部署DCU直通环境 - ```sh - $ lspci -nn | grep -e "Display" -e "Co-processor" | grep "Haiguang" - ``` +* 查找DCU设备 -- 结果如下: - 如果是Z100卡,那么显示的是Display,如果是K100/K100-AI卡,显示的是Co-processor” + 找到DCU设备的PCI 设备标识地址,后面会用到。 - ```sh - $ lspci -nn | grep -e "Display" -e "Co-processor" | grep "Haiguang" - 03:00.0 Display controller: Chengdu Haiguang IC Design Co., Ltd. Device [1d94:53b7] (rev 01) - ``` + * 找到DCU设备 - `03:00.0`为该设备的设备标识,其完整信息为: `0000:03:00.0` + ```sh + $ lspci -nn | grep -e "Display" -e "Co-processor" | grep "Haiguang" + ``` -#### 2. 使用一键部署脚本,部署DCU环境 + * 结果如下: + 如果是Z100卡,那么显示的是Display,如果是K100/K100-AI卡,显示的是Co-processor” -```sh -$ cd /opt/hygon/csv/confidential-containers/scripts -# 参数请替换为上一步实际得到的id -$ sudo ./setup_dcu_passthrough.sh -d 0000:03:00.0 -``` + ```sh + $ lspci -nn | grep -e "Display" -e "Co-processor" | grep "Haiguang" + 03:00.0 Display controller: Chengdu Haiguang IC Design Co., Ltd. Device [1d94:53b7] (rev 01) + ``` -该脚本主要实现以下功能: + `03:00.0`为该设备的设备标识,其完整信息为: `0000:03:00.0` -1. 把指定的DCU设备绑定到VFIO -2. 安装`vfio-device-plugin`,该插件负责与k8s交互,将vfio device直通进CSV机密容器 -3. 下载DTK,DTK文件较大,直接安装进容器镜像中,会导致镜像太大,本测试中,通过将DTK mount到容器中的方式,来使用 -4. 下载DCU驱动,安装到host上。这个步骤的目的是后面将`/opt/hyhal`目录挂载到容器中。 +* 使用一键部署脚本,部署DCU环境 -> 注意: 第4步安装驱动的过程中,如果询问是否更新rock、hyhal、vbios,一律选择'Y'或者'y'。 + ```sh + $ cd /opt/hygon/csv/confidential-containers/scripts + # 参数请替换为上一步实际得到的id + $ sudo ./setup_dcu_passthrough.sh -d 0000:03:00.0 + ``` -#### 3. (可选)上一步的设置,设备重启失效,想要永久生效,请做以下设置: + 该脚本主要实现以下功能: + + 1. 把指定的DCU设备绑定到VFIO + 2. 安装`vfio-device-plugin`,该插件负责与k8s交互,将vfio device直通进CSV机密容器 + 3. 下载DTK,DTK文件较大,直接安装进容器镜像中,会导致镜像太大,本测试中,通过将DTK mount到容器中的方式,来使用 + 4. 下载DCU驱动,安装到host上。这个步骤的目的是后面将`/opt/hyhal`目录挂载到容器中。 -> 注意:示例中的1d94:53b7为第一步lspci -nn中打印出的值,请更新为自己设备的实际值。 + > 注意: 第4步安装驱动的过程中,如果询问是否更新rock、hyhal、vbios,一律选择'Y'或者'y'。 -```sh -$ sudo su root -$ sed -i 's!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 vfio-pci.ids=1d94:53b7"!' /etc/default/grub -``` +* (可选)上一步的设置,设备重启失效,想要永久生效,请做以下设置: + > 注意:示例中的1d94:53b7为第一步lspci -nn中打印出的值,请更新为自己设备的实际值。 + ```sh + $ sudo su root + $ sed -i 's!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 vfio-pci.ids=1d94:53b7"!' /etc/default/grub + ``` ### 启动DCU直通容器 机密容器的DCU的驱动版本为` rock-5.7.1-6.2.31-V1.6.3.aio.run`,对应的DTK版本为`24.04.3`。如果需要改变DCU驱动版本,需要手动编译Kata机密容器的kernel。 -- 启动容器 - - 我们将host上的`/opt/dtk-24.04.3`目录挂载到容器的`/opt/dtk`目录,将host上的`/opt/hyhal`目录挂载到容器的`opt/hyhal`目录,并给容器分配一个`dcu/k100`卡。 - - ``` - $ cat <<-EOF | kubectl apply -f - - apiVersion: v1 - kind: Pod - metadata: - name: ubuntu-dcu - spec: - runtimeClassName: kata-qemu-csv-dcu - containers: - - name: ubuntu-dcu - image: haigon.cn/test/ubuntu-dcu:latest - command: ["tail", "-f", "/dev/null"] - resources: - limits: - dcu/k100: "1" - volumeMounts: - - mountPath: /opt/dtk - name: dtk - - mountPath: /opt/hyhal - name: hyhal - - mountPath: /dev - name: dev - securityContext: - privileged: true - volumes: - - name: dtk - hostPath: - path: /opt/dtk-24.04.3 - - name: hyhal - hostPath: - path: /opt/hyhal - - name: dev - hostPath: - path: /dev - EOF - ``` - -- 进入容器中,使用DCU之前,需要启动hymgr和使能dtk环境变量 +* 启动容器 + + 我们将host上的`/opt/dtk-24.04.3`目录挂载到容器的`/opt/dtk`目录,将host上的`/opt/hyhal`目录挂载到容器的`opt/hyhal`目录,并给容器分配一个`dcu/k100`卡。 + + ```sh + $ cat <<-EOF | kubectl apply -f - + apiVersion: v1 + kind: Pod + metadata: + name: ubuntu-dcu + spec: + runtimeClassName: kata-qemu-csv-dcu + containers: + - name: ubuntu-dcu + image: haigon.cn/test/ubuntu-dcu:latest + command: ["tail", "-f", "/dev/null"] + resources: + limits: + dcu/k100: "1" + volumeMounts: + - mountPath: /opt/dtk + name: dtk + - mountPath: /opt/hyhal + name: hyhal + - mountPath: /dev + name: dev + securityContext: + privileged: true + volumes: + - name: dtk + hostPath: + path: /opt/dtk-24.04.3 + - name: hyhal + hostPath: + path: /opt/hyhal + - name: dev + hostPath: + path: /dev + EOF + ``` + +* 进入容器中,使用DCU之前,需要启动hymgr和使能dtk环境变量 ```sh $ kubectl exec -it ubuntu-dcu -- bash @@ -172,62 +172,146 @@ $ sed -i 's!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 vfio-pci.ids=1d94:53b7"!' /etc/defau root@ubuntu-dcu:/# rocminfo ``` -- 期望结果(能看到CPU和DCU两个设备): - - ```sh - hygon module is loaded - ===================== - HSA System Attributes - ===================== - Runtime Version: 1.1 - System Timestamp Freq.: 1000.000000MHz - Sig. Max Wait Duration: 18446744073709551615 (0xFFFFFFFFFFFFFFFF) (timestamp count) - Machine Model: LARGE - System Endianness: LITTLE - - ========== - HSA Agents - ========== - ******* - Agent 1 - ******* - Name: Hygon C86 3250 8-core Processor - Uuid: CPU-XX - Marketing Name: Hygon C86 3250 8-core Processor - Vendor Name: HygonGenuine - Feature: None specified - Profile: FULL_PROFILE - Float Round Mode: NEAR - . - . - . - ******* - Agent 2 - ******* - Name: ZIFANG - Uuid: GPU-XX - Marketing Name: C878186 - Vendor Name: HYGON - Feature: KERNEL_DISPATCH - Profile: BASE_PROFILE - Float Round Mode: NEAR - Max Queue Number: 128(0x80) - Queue Min Size: 4096(0x1000) - Queue Max Size: 131072(0x20000) - Queue Type: MULTI - Node: 1 - Device Type: DCU - . - . - . - *** Done *** - ``` +* 期望结果(能看到CPU和DCU两个设备): + + ```sh + hygon module is loaded + ===================== + HSA System Attributes + ===================== + Runtime Version: 1.1 + System Timestamp Freq.: 1000.000000MHz + Sig. Max Wait Duration: 18446744073709551615 (0xFFFFFFFFFFFFFFFF) (timestamp count) + Machine Model: LARGE + System Endianness: LITTLE + + ========== + HSA Agents + ========== + ******* + Agent 1 + ******* + Name: Hygon C86 3250 8-core Processor + Uuid: CPU-XX + Marketing Name: Hygon C86 3250 8-core Processor + Vendor Name: HygonGenuine + Feature: None specified + Profile: FULL_PROFILE + Float Round Mode: NEAR + . + . + . + ******* + Agent 2 + ******* + Name: ZIFANG + Uuid: GPU-XX + Marketing Name: C878186 + Vendor Name: HYGON + Feature: KERNEL_DISPATCH + Profile: BASE_PROFILE + Float Round Mode: NEAR + Max Queue Number: 128(0x80) + Queue Min Size: 4096(0x1000) + Queue Max Size: 131072(0x20000) + Queue Type: MULTI + Node: 1 + Device Type: DCU + . + . + . + *** Done *** + ``` * 测试完恢复内存设置 - ```sh - $ sudo sed -i "s/^default_memory *=.*\$/default_memory = 2048/g" /opt/confidential-containers/share/defaults/kata-containers/configuration-qemu-csv-dcu.toml - ``` + ```sh + $ sudo sed -i "s/^default_memory *=.*\$/default_memory = 2048/g" /opt/confidential-containers/share/defaults/kata-containers/configuration-qemu-csv-dcu.toml + ``` + +### 容器中运行Qwen +* 设置内存 + 运行推理容器所需内存较大,此处我们设置为40G + ```sh + $ sudo sed -i "s/^default_memory *=.*\$/default_memory = 40960/g" /opt/confidential-containers/share/defaults/kata-containers/configuration-qemu-csv-dcu.toml + ``` + +* 启动容器 + 我们在容器中安装了dtk,此处不再将host的dtk挂载到容器中。 + ```sh + $ cat <<-EOF | kubectl apply -f - + apiVersion: v1 + kind: Pod + metadata: + name: ubuntu-dcu-qwen + spec: + runtimeClassName: kata-qemu-csv-dcu + containers: + - name: ubuntu-dcu-qwen + image: haigon.cn/test/dtk24.04.3_qwen0.5b:latest + command: ["tail", "-f", "/dev/null"] + resources: + limits: + dcu/k100: "1" + volumeMounts: + - mountPath: /opt/hyhal + name: hyhal + - mountPath: /dev + name: dev + securityContext: + privileged: true + volumes: + - name: hyhal + hostPath: + path: /opt/hyhal + - name: dev + hostPath: + path: /dev + EOF + ``` + 这个容器镜像比较大,下载时间比较长,如果发生失败,多尝试几次。 + + +* 运行Qwen + + 首先启动hymgr, 然后使能dtk的环境变量,接着配置ollama的环境变量,最后启动ollama。 + + 开启一个容器终端 + ```sh + $ kubectl exec -it ubuntu-dcu-qwen -- bash + # ------------- below is container terminal output ------------- + root@ubuntu-dcu:/# HYCONTAINER_ENV=0 HYDISABLE_DRIVEROP=1 /opt/hyhal/bin/hymgr & + root@ubuntu-dcu:/# source /opt/dtk-24.04.3/env.sh + root@ubuntu-dcu:/# cd /root/ollama + root@ubuntu-dcu-qwen:~/ollama# export HSA_OVERRIDE_GFX_VERSION=9.0.6 + root@ubuntu-dcu-qwen:~/ollama# export ROCR_VISIBLE_DEVICES=0 + root@ubuntu-dcu-qwen:~/ollama# ./ollama serve + ``` + > 注意:HSA_OVERRIDE_GFX_VERSION:Z100为9.0.6, K100为9.2.6, K100-AI为9.2.8 + > ROCR_VISIBLE_DEVICES: 为参与推理的DCU卡号,如果有多个DCU卡,可以指定多个卡号,用逗号隔开。卡号可以通过`hy-smi`命令查询。 + + 再开启一个容器终端,执行推理 + ```sh + $ kubectl exec -it ubuntu-dcu-qwen -- bash + # ------------- below is container terminal output ------------- + root@ubuntu-dcu:/# cd /root/ollama + root@ubuntu-dcu-qwen:~/ollama# ./ollama run qwen:0.5b + ``` + 模型加载完成后,就可以开始输入问题,进行推理。 + + 可以再开一个容器终端,用来确认推理真实运行在DCU上 + ```sh + $ kubectl exec -it ubuntu-dcu-qwen -- bash + # ------------- below is container terminal output ------------- + root@ubuntu-dcu:/# source /opt/dtk-24.04.3/env.sh + root@ubuntu-dcu:/# hy-smi + ============================ System Management Interface ============================= + ====================================================================================== + DCU Temp AvgPwr Perf PwrCap VRAM% DCU% Mode + 0 47.0C 18.0W auto 450.0W 6% 0% N/A + ====================================================================================== + =================================== End of SMI Log =================================== + ``` ## 常见问题总结 @@ -243,50 +327,50 @@ $ sed -i 's!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 vfio-pci.ids=1d94:53b7"!' /etc/defau * DCU绑定到vfio驱动失败 - 正常情况下,经过执行`./setup_dcu_passthrough.sh -d 0000:bb:dd.ff`之后,在host上的/dev/vfio/目录,会存在设备的group id文件。如下图`/dev/vfio`目录下存在一个group id为16的文件: + 正常情况下,经过执行`./setup_dcu_passthrough.sh -d 0000:bb:dd.ff`之后,在host上的/dev/vfio/目录,会存在设备的group id文件。如下图`/dev/vfio`目录下存在一个group id为16的文件: - ```sh - $ ls /dev/vfio - 16 vfio - ``` + ```sh + $ ls /dev/vfio + 16 vfio + ``` - 如果该目录下不存在设备的group id文件,那么,需要在host上检查设备绑定vfio驱动是否成功。如过设备正常绑定到vfio驱动,那么lspci会显示设备当前的驱动为`Kernel driver in use: vfio-pci`。 + 如果该目录下不存在设备的group id文件,那么,需要在host上检查设备绑定vfio驱动是否成功。如过设备正常绑定到vfio驱动,那么lspci会显示设备当前的驱动为`Kernel driver in use: vfio-pci`。 - ```sh - $ lspci -s 0a:00.0 -v - 0a:00.0 Co-processor: Chengdu Haiguang IC Design Co., Ltd. KONGMING (rev 01) - Subsystem: Chengdu Haiguang IC Design Co., Ltd. K100 - Flags: fast devsel, IRQ 38 - Memory at 10000000000 (64-bit, prefetchable) [size=64G] - Memory at 11000000000 (64-bit, prefetchable) [size=2M] - Memory at a7700000 (32-bit, non-prefetchable) [size=512K] - Expansion ROM at a7780000 [disabled] [size=128K] - Capabilities: - Kernel driver in use: vfio-pci - ``` + ```sh + $ lspci -s 0a:00.0 -v + 0a:00.0 Co-processor: Chengdu Haiguang IC Design Co., Ltd. KONGMING (rev 01) + Subsystem: Chengdu Haiguang IC Design Co., Ltd. K100 + Flags: fast devsel, IRQ 38 + Memory at 10000000000 (64-bit, prefetchable) [size=64G] + Memory at 11000000000 (64-bit, prefetchable) [size=2M] + Memory at a7700000 (32-bit, non-prefetchable) [size=512K] + Expansion ROM at a7780000 [disabled] [size=128K] + Capabilities: + Kernel driver in use: vfio-pci + ``` * 直通到容器的设备数超过vfio device plugin资源池中的DCU设备数量,导致无法分配资源 - 检查直通到容器的DCU个数是否超过了绑定到vfio的DCU个数。 - + 检查直通到容器的DCU个数是否超过了绑定到vfio的DCU个数。 + * DCU卡状态异常。在host上通过`dmesg`命令查看 * 磁盘不足。当磁盘可用空间不足时,容器就会启动失败。通过kubectl describe pods查看,可以发现其中包含磁盘压力的日志。 * 容器中设备驱动加载失败 - 进入容器后,通过dmesg命令查看虚拟机kernel的日志,其中包含dcu驱动的错误日志信息。 + 进入容器后,通过dmesg命令查看虚拟机kernel的日志,其中包含dcu驱动的错误日志信息。 - 出现这种问题的原因可能是: + 出现这种问题的原因可能是: - * host没有开启IOMMU。可以检查Host的kernel日志,确认IOMMU是否开启 - * DCU的vbios没有更新。参考`使用一键部署脚本,部署DCU环境`一节。 + * host没有开启IOMMU。可以检查Host的kernel日志,确认IOMMU是否开启 + * DCU的vbios没有更新。参考`使用一键部署脚本,部署DCU环境`一节。 * 容器中执行rocminfo命令后,没有看到DCU设备信息 - * 确保在容器中开启了hymgr:`HYCONTAINER_ENV=0 /opt/hyhal/bin/hymgr &` - * 确保在容器中执行了`source /opt/dtk/env.sh` - * 确保容器中dmesg中包含dcu驱动加载正常,没有出现错误 + * 确保在容器中开启了hymgr:`HYCONTAINER_ENV=0 /opt/hyhal/bin/hymgr &` + * 确保在容器中执行了`source /opt/dtk/env.sh` + * 确保容器中dmesg中包含dcu驱动加载正常,没有出现错误 ## 附录