基于GPU实例的BEVFormer模型训练与优化

更新时间:
复制为 MD 格式

在自动驾驶(Autonomous Driving)模型训练场景中,BEVFormer 模型的训练通常需要消耗大量的计算和时间资源。您可以快速在阿里云ecs.ebmgn8v.48xlarge 实例上搭建训练环境,并利用阿里云专属的系统级(KeenTune)与框架级(算子与Python包)优化方案,提升 BEVFormer 模型(FP16/FP32精度)的单机训练吞吐量,从而降低计算成本并加速算法迭代。

方案概述

BEVFormer 是自动驾驶领域经典的鸟瞰图(BEV)感知模型,由于其复杂的注意力机制和算子,训练过程极其耗时。针对这一痛点,阿里云从操作系统到底层算子提供了全栈调优方案。

核心优化策略与预期收益:

  • OS 级优化 (KeenTune):利用阿里云全栈式智能优化产品 KeenTune(轻豚)调整 OS 性能 profile,在 FP16/FP32 下吞吐均有约12%的提升。

  • Python 基础包优化:优化 detectron2、mmdet3d 等基础依赖库,使 FP32 吞吐提升约 10%,FP16 提升约 15%。

  • 核心算子优化:优化了 mmdet3d 的关键 module(包括 ms_deformableAttention、SampleCams、gather_reference_points_cams、ConvergeCams、mFastLayerNorm、DCNv2),进一步提升 FP16 数据精度的训练性能,在原有BASE+Keentune优化+Python包优化的基础上,叠加算子优化后FP16下吞吐最大提升48%(mbs1时)。

叠加上述优化后,在 ecs.ebmgn8v.48xlarge 实例上,FP16 数据精度性能最大可提升 115%(mbs5时),FP32 数据精度性能最大可提升 47%(mbs2时)。

步骤一:创建 GPU 实例与基础环境准备

首先需购买阿里云 GPU 实例,并配置包含 GPU 驱动和容器引擎的基础运行环境。预装环境镜像免去了手动安装显卡驱动及容器 Toolkit 的繁琐步骤,推荐使用。

方式一:基于预装环境镜像(推荐)

  1. 创建GPU实例,实例配置如下:

    • 实例规格:ecs.ebmgn8v.48xlarge

    • 镜像:选择已预装环境的镜像,例如Alibaba Cloud Linux 3.2104 LTS 64位 预装NVIDIA GPU驱动和 CUDA(默认驱动版本 570.195.03)

      名称中带有 “预装NVIDIA GPU驱动和 CUDA” 后缀的镜像即为预装环境的镜像。
    • 系统盘:建议 200 GiB

    • 公网IP:开通公网 IP,以便后续下载依赖包和镜像

  2. 实例创建完成后,等待实例状态变为运行中。在实例列表中找到目标实例,单击远程连接,单击通过Workbench远程连接对应的立即登录

  3. 登录实例后执行以下命令验证底层硬件:

    nvidia-smi

    执行后,终端将输出表格,显示 ecs.ebmgn8v.48xlarge 实例的 8 张显卡的型号、显存状态及驱动版本信息。

方式二:基于纯净版 OS 手动安装

  1. 创建GPU实例,实例配置如下:

    • 实例规格:ecs.ebmgn8v.48xlarge

    • 镜像:选择纯净的 Ubuntu 或 Alibaba Cloud Linux 镜像,勾选安装GPU驱动选项并选择所需的驱动版本(如 570.195.03)

    • 系统盘:建议 200 GiB

    • 公网 IP:开通公网 IP,以便后续下载依赖包和镜像

  2. 等待实例状态变为运行中后,在实例列表中找到目标实例,单击远程连接,单击通过Workbench远程连接对应的立即登录

    首次登录时 GPU 驱动可能仍在安装,请耐心等待,完成后执行 nvidia-smi 确认驱动安装完成。执行后,终端将输出表格,显示 ecs.ebmgn8v.48xlarge 实例的 8 张显卡的型号、显存状态及驱动版本信息。
  3. 驱动就绪后,执行以下命令安装 Docker 和 nvidia-container-toolkit。

    1. 安装Docker。

      本文以以Alibaba Cloud Linux3为例,若使用其他系统,请参考安装并使用DockerDocker Compose完成安装。
      #添加Docker软件包源
      sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
      sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo
      #Alibaba Cloud Linux3专用的dnf源兼容插件
      sudo dnf -y install dnf-plugin-releasever-adapter --repo alinux3-plus
      #安装Docker社区版本,容器运行时containerd.io,以及Docker构建和Compose插件
      sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
      
      # 启动并设置 Docker 开机自启
      sudo systemctl start docker
      sudo systemctl enable docker
    2. 安装nvidia-container-toolkit并重启docker。

      dnf install -y anolis-epao-release
      dnf install -y nvidia-container-toolkit
      
      # 重启 Docker 服务使配置生效
      systemctl restart docker
      

步骤二:拉取基础镜像运行基准测试

通过拉取阿里云预置的开源版基础镜像,快速运行BEVFormer单机训练,获取优化前的基准吞吐量。

镜像信息

镜像地址:egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/autonomous-driving:bevformer_base_ubuntu22.04_pytorch2.6_cu126_py310_20250923_140000

环境信息:

  • OS:Ubuntu 22.04.5 LTS amd64

  • Python:3.10.13

  • PyTorch:2.6.0.7

  • CUDA:12.6

查看镜像发布详情请移步制品发布中心:https://cr.console.aliyun.com/cn-hangzhou/instances/artifact,镜像位于 egs/autonomous-driving 仓库。

  1. 拉取 BEVFormer 基础镜像。

    docker pull egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/autonomous-driving:bevformer_base_ubuntu22.04_pytorch2.6_cu126_py310_20250923_140000
  2. 启动容器。

    docker run -dt --network=host --gpus all --privileged --ipc=host \
      --ulimit memlock=-1 --ulimit stack=67108864 \
      --name bevformer_base_test \
      egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/autonomous-driving:bevformer_base_ubuntu22.04_pytorch2.6_cu126_py310_20250923_140000

    参数解析:

    • --network=host:使用宿主机网络

    • --gpus all:挂载所有可用 GPU

    • --privileged:赋予容器扩展权限

    • --ipc=host:共享宿主机共享内存,避免 PyTorch DataLoader 报错

    • --ulimit memlock=-1 --ulimit stack=67108864:解除内存锁定与堆栈限制

  3. 进入容器交互式终端。

     docker exec -it bevformer_base_test /bin/bash
  4. 进入代码目录,发起训练测试:

    cd /root/BEVFormer
    
    # 测试 FP32 精度性能(末尾的 8 代表使用 8 张 GPU 并发训练)
    bash ./tools/dist_train.sh ./projects/configs/bevformer/bevformer_base.py 8
    
    # 或测试 FP16 精度性能
    bash ./tools/fp16/dist_train.sh ./projects/configs/bevformer_fp16/bevformer_base_fp16.py 8

观察并计算基准吞吐量 (Throughput)

在控制台打印的日志中,观察单轮迭代耗时(time字段,在日志的位置如图所示)。

image

基准吞吐量计算公式Throughput (samples/sec) = samples_per_gpu (mbs) * GPU数量 / 平均单论迭代耗时

例如,在FP32精度、samples_per_gpu=1 的配置下,若平均单轮迭代耗时为 1.397 秒,且使用 8 张 GPU,则基准吞吐量 = 1 * 8 / 1.397 ≈ 5.72

步骤三:应用全栈优化方案提升训练性能

依次开启宿主机操作系统级调优,并使用包含深度优化算子的专属镜像,提升硬件计算效率。

1. 利用 KeenTune 进行系统级性能调优

KeenTune(轻豚)是一款 AI 算法与专家知识库双轮驱动的操作系统全栈式智能优化产品。它能为主流的操作系统提供轻量化、跨平台的一键式性能调优,通过智能调整底层的内核参数,让 AI 训练任务在定制的运行环境中发挥出更好的硬件性能。

重要

以下操作需在宿主机(Docker容器外)执行。若在Docker容器内,请先执行exit退出。

  1. 下载并安装 KeenTune。

    使用预装环境镜像创建的实例,已包含KeenTune,无需重新安装。
    wget https://alinux-ai-ext.oss-cn-hangzhou.aliyuncs.com/aiext-0.5/aiext-installer.sh
    sh aiext-installer.sh
    dnf makecache && dnf install -y keentuned keentune-target keentune-profiles-aiext.noarch
  2. 启用 KeenTune。

    systemctl stop tuned && systemctl disable tuned
    systemctl start keentune-target && systemctl enable keentune-target
    systemctl start keentuned && systemctl enable keentuned
    # 设置 AI 训练专属调优 Profile
    keentune profile set ai_train.profile
  3. 重启实例。

    KeenTune调优修改了底层内核参数,必须重启实例才能生效。

环境还原与卸载(可选)

如果需要对比优化前后的效果,或在测试结束后清理环境,请执行以下命令回滚配置:

# 1. 回滚 KeenTune 调优参数(需重启生效)
keentune profile rollback

# 2. 停用并卸载 KeenTune
systemctl stop keentuned keentune-target
systemctl disable keentuned keentune-target

# 根据 OS 类型卸载
if [ -f "/etc/os-release" ]; then
    # 读取 NAME 字段,判断是否包含 "Ubuntu"
    if grep -qi '^NAME="Ubuntu"' /etc/os-release; then
        apt purge keentuned -y
        apt purge keentune-target -y
        apt purge keentune-profiles-aiext.noarch -y
    else
        dnf remove -y keentuned keentune-target
        dnf remove -y keentune-profiles-aiext.noarch
    fi
fi

# 3. 恢复默认的 tuned 服务
systemctl start tuned && systemctl enable tuned
重要

执行完上述操作后,请务必重启 ECS 实例使回滚生效。

2. 使用专属镜像实现框架与算子加速

阿里云提供的专属优化镜像实现了训练环境的“开箱即用”。该镜像除了包含 BEVFormer 模型训练依赖的所有代码、数据和基础组件之外,还集成了框架级的 Python 基础包优化与核心算子深度优化。

镜像信息

镜像地址:egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/autonomous-driving:bevformer_optimized_ubuntu22.04_pytorch2.6_cu126_py310_20250923_140000

环境信息:

  • OS:Ubuntu 22.04.5 LTS amd64

  • Python:3.10.13

  • PyTorch:2.6.0.7

  • CUDA:12.6

优化的Python包:

  • apex-0.1+cu126.torch2.6

  • detectron2-0.6+cu126.torch2.6

  • loongkit_mde-0.0.1

  • mmcv_full-1.7.2+cu126.torch2.6

  • mmdet-2.28.2+ali

  • mmdet3d-1.0.0rc4+ali

算子优化: 优化了 mmdet3d 的关键 module(包括 ms_deformableAttention、SampleCams、gather_reference_points_cams、ConvergeCams、mFastLayerNorm、DCNv2),提升 FP16 数据精度的训练性能。

查看镜像发布详情请移步制品发布中心:https://cr.console.aliyun.com/cn-hangzhou/instances/artifact,镜像位于 egs/autonomous-driving 仓库。

实例重启后,请重新登录,并执行以下命令拉取并运行该专属优化镜像。

  1. 拉取 BEVFormer 优化镜像。

    docker pull egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/autonomous-driving:bevformer_optimized_ubuntu22.04_pytorch2.6_cu126_py310_20250923_140000
  2. 启动容器。

    docker run -dt --network=host --gpus all --privileged --ipc=host \
      --ulimit memlock=-1 --ulimit stack=67108864 \
      --name bevformer_optimized_test \
      egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/autonomous-driving:bevformer_optimized_ubuntu22.04_pytorch2.6_cu126_py310_20250923_140000
  3. 进入容器。

    docker exec -it bevformer_optimized_test /bin/bash
  4. 进入代码目录,发起训练测试。

    cd /root/BEVFormer
    
    # 测试 FP32 精度性能(末尾的 8 代表使用 8 张 GPU 并发训练)
    bash ./tools/dist_train.sh ./projects/configs/bevformer/bevformer_base.py 8
    
    # 或测试 FP16 精度性能
    bash ./tools/fp16/dist_train.sh ./projects/configs/bevformer_fp16/bevformer_base_fp16.py 8
    重要

    第一次使用优化镜像训练模型时会存在10分钟左右的预热过程,此后以相同精度重复训练该模型时,该预热过程不会重复出现。