# OpenVLA：LoRA 微调自己的数据集并部署 REST API 推理服务

> 原文链接：https://robotics.hellobike.com/tutorials/openvla-lora-finetune
> 频道：tutorials | 更新日期：2026-10-22 | 标签：OpenVLA, LoRA, RLDS, REST API, 机器人微调
> 核心摘要：本教程按 OpenVLA README 整理，从安装环境、准备 RLDS 数据集、注册自定义数据，到使用 vla-scripts/finetune.py 进行 LoRA 微调，并说明通过 vla-scripts/deploy.py 以 REST API 形式部署推理服务。

# OpenVLA：LoRA 微调自己的数据集并部署 REST API 推理服务

&gt; 本文依据以下官方文档整理：[raw.githubusercontent.com/openvla/openvla/main/README.md](https://raw.githubusercontent.com/openvla/openvla/main/README.md)。命令与版本以文档当前版本为准。

## 前置条件

- Python 3.10；仓库说明也提到应向后兼容任何 Python &gt;= 3.8
- PyTorch 2.2.*；文档测试版本为 PyTorch 2.2.0、torchvision 0.17.0
- transformers 4.40.1、tokenizers 0.19.1、timm 0.9.10、flash-attn 2.5.5
- LoRA 示例使用单张 A100 GPU、80 GB VRAM；较小 GPU 至少约 27 GB memory
- 数据集需为 RLDS format，或使用 custom PyTorch Dataset wrapper
- OpenVLA 预训练模型如 openvla/openvla-7b

## 1. 1. 创建 OpenVLA 环境并安装依赖

先按仓库安装 OpenVLA。文档说明完整安装用于 full-scale pretraining 和 distributed fine-tuning；仓库使用 Python 3.10，并要求 PyTorch 2.2.*。

```bash
## Create and activate conda environment
conda create -n openvla python=3.10 -y
conda activate openvla

## Install PyTorch. Below is a sample command to do this, but you should check the following link
## to find installation instructions that are specific to your compute platform:
## https://pytorch.org/get-started/locally/
conda install pytorch torchvision torchaudio pytorch-cuda=12.4 -c pytorch -c nvidia -y  # UPDATE ME!

## Clone and install the openvla repo
git clone https://github.com/openvla/openvla.git
cd openvla
pip install -e .

## Install Flash Attention 2 for training (https://github.com/Dao-AILab/flash-attention)
##   =>> If you run into difficulty, try `pip cache remove flash_attn` first
pip install packaging ninja
ninja --version; echo $?  # Verify Ninja --> should return exit code "0"
pip install "flash-attn==2.5.5" --no-build-isolation
```

## 2. 2. 准备 BridgeData V2 示例数据

README 的 LoRA 示例先下载 BridgeData V2，并把数据集改名为 `bridge_orig`。文档明确提示：省略改名步骤可能导致后续 runtime errors。

```bash
## Change directory to your base datasets folder
cd <PATH TO BASE DATASETS DIR>

## Download the full dataset (124 GB)
wget -r -nH --cut-dirs=4 --reject="index.html*" https://rail.eecs.berkeley.edu/datasets/bridge_release/data/tfds/bridge_dataset/

## Rename the dataset to `bridge_orig` (NOTE: Omitting this step may lead to runtime errors later)
mv bridge_dataset bridge_orig
```

## 3. 3. 接入自己的数据集

若使用其他数据，可从 Open X-Embodiment (OXE) mixture 下载；若是自定义数据集，文档给出两种方式：转换为兼容微调脚本的 RLDS format，或使用 custom PyTorch Dataset wrapper。推荐方式是 RLDS，因为相关 dataset 和 dataloader 经过更多测试。

## 4. 4. 注册 RLDS 数据集配置与变换

自定义数据转换为 RLDS 后，需要在数据加载器中注册。README 指定注册 dataset config 的位置是 `prismatic/vla/datasets/rlds/oxe/configs.py#L54`，注册 dataset transform function 的位置是 `prismatic/vla/datasets/rlds/oxe/transforms.py#L828`。

## 5. 5. 启动 LoRA 微调

集成新数据集后，使用同一个 `vla-scripts/finetune.py` 脚本启动 LoRA 微调。下面命令来自 README 示例，`--dataset_name` 对应已注册的数据集名。

```bash
torchrun --standalone --nnodes 1 --nproc-per-node 1 vla-scripts/finetune.py \
  --vla_path "openvla/openvla-7b" \
  --data_root_dir <PATH TO BASE DATASETS DIR> \
  --dataset_name bridge_orig \
  --run_root_dir <PATH TO LOG/CHECKPOINT DIR> \
  --adapter_tmp_dir <PATH TO TEMPORARY DIR TO SAVE ADAPTER WEIGHTS> \
  --lora_rank 32 \
  --batch_size 16 \
  --grad_accumulation_steps 1 \
  --learning_rate 5e-4 \
  --image_aug <True or False> \
  --wandb_project <PROJECT> \
  --wandb_entity <ENTITY> \
  --save_steps <NUMBER OF GRADIENT STEPS PER CHECKPOINT SAVE>
```

## 6. 6. 按显存与 GPU 数调整训练

README 说明 `--batch_size==16` 且 `--grad_accumulation_steps==1` 约需 72 GB GPU memory。较小 GPU 应降低 `--batch_size` 并提高 `--grad_accumulation_steps`。多 GPU 使用 PyTorch Distributed Data Parallel 时，将 `torchrun` 中 `--nproc-per-node` 设为可用 GPU 数。

## 7. 7. 部署推理服务

文档说明 OpenVLA 提供轻量脚本 `vla-scripts/deploy.py`，用于 serving OpenVLA models over a REST API。该方式便于把 OpenVLA models 集成进 existing robot control stacks，并移除对 powerful on-device compute 的要求。

## 验证是否跑通

安装阶段执行 `ninja --version; echo $?`，文档要求应返回 exit code `0`。微调阶段查看 training logs；若在 BridgeData V2 上设置 `--image_aug==False`，README 说明会观察到接近 100% `action_accuracy`。部署阶段确认使用 `vla-scripts/deploy.py` 以 REST API serving OpenVLA models。

## 文档里点名的注意事项

- README 明确固定依赖版本：PyTorch 2.2.0、torchvision 0.17.0、transformers 4.40.1、tokenizers 0.19.1、timm 0.9.10、flash-attn 2.5.5。
- BridgeData V2 下载后需要 `mv bridge_dataset bridge_orig`；省略该步骤可能导致后续 runtime errors。
- `--batch_size==16` 与 `--grad_accumulation_steps==1` 约需 72 GB GPU memory；较小 GPU 需降低 batch size 并提高 grad accumulation steps。
- 若 `--image_aug==False`，在 BridgeData V2 训练日志中会出现接近 100% `action_accuracy`，因为 `openvla-7b` 已在包含 BridgeData V2 的超集上预训练。
- `scripts/` mostly 是原始 `prismatic-vlms` 仓库遗留；用 `scripts/generate.py` 让现有 OpenVLA models 生成语言不会工作，因为当前 OpenVLA models 只训练生成 actions。
- 预训练模型派生自 Llama-2，因此受 Llama Community License 约束。

## 原始参考来源 (Sources)

- [raw.githubusercontent.com/openvla/openvla/main/README.md](https://raw.githubusercontent.com/openvla/openvla/main/README.md)