Add portable cross-server pi0.5 ARX training package
Browse files- README.md +50 -0
- compute_pi05_norm_stats.py +112 -0
- python_compat/sitecustomize.py +22 -0
- setup_openpi.sh +24 -0
- train_from_hf.sh +79 -0
- train_pi05_lora.py +153 -0
README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Train pi0.5 on another server
|
| 2 |
+
|
| 3 |
+
This directory is the reproducible training entry point for the ARX X5 real-robot data. It is
|
| 4 |
+
separate from the ROS/CAN deployment tools. The default command downloads the complete 71-episode
|
| 5 |
+
two-camera dataset and starts LoRA fine-tuning; it does not connect to a robot.
|
| 6 |
+
|
| 7 |
+
## 1. Prepare OpenPI
|
| 8 |
+
|
| 9 |
+
Use the pinned RoboTwin/XPolicyLab checkout used by this project. The helper below clones the public
|
| 10 |
+
RoboTwin repository and its pinned XPolicyLab submodule, then installs OpenPI with `uv`:
|
| 11 |
+
|
| 12 |
+
```bash
|
| 13 |
+
./setup_openpi.sh /data/robotwin_arx
|
| 14 |
+
```
|
| 15 |
+
|
| 16 |
+
The resulting OpenPI path is `/data/robotwin_arx/RoboTwin/XPolicyLab/policy/Pi_05/openpi`.
|
| 17 |
+
The machine needs Python 3.11+, `uv`, NVIDIA drivers, and a CUDA 12.x runtime. For a 24 GB RTX 4090,
|
| 18 |
+
start with one GPU, batch size 1, and `XLA_PYTHON_CLIENT_ALLOCATOR=platform`.
|
| 19 |
+
|
| 20 |
+
## 2. Train from Hugging Face
|
| 21 |
+
|
| 22 |
+
Authenticate with a read token (do not put it in a repository file), then run:
|
| 23 |
+
|
| 24 |
+
```bash
|
| 25 |
+
./train_from_hf.sh \
|
| 26 |
+
--openpi-root /data/robotwin_arx/RoboTwin/XPolicyLab/policy/Pi_05/openpi \
|
| 27 |
+
--work-dir /data/arx_pi05 \
|
| 28 |
+
--dataset-id JackieMM/LQ-20260915-1-all71-2cam \
|
| 29 |
+
--gpu 0 \
|
| 30 |
+
--steps 30000 \
|
| 31 |
+
--save-interval 5000 \
|
| 32 |
+
--keep-period 5000
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
The script downloads the LeRobot dataset, copies its matching `norm_stats.json`, loads the pi0.5 DROID
|
| 36 |
+
parameters, and invokes `train_pi05_lora.py` with only `cam_high` and `cam_right_wrist`. It refuses to
|
| 37 |
+
overwrite an existing checkpoint directory. To resume an interrupted run, pass `--resume` and use a
|
| 38 |
+
new invocation that points at the existing checkpoint directory.
|
| 39 |
+
|
| 40 |
+
For two GPUs, pass `--gpu 0,1 --fsdp-devices 2`. FSDP can reduce per-GPU memory but may be slower than
|
| 41 |
+
one GPU because of inter-GPU communication; use one GPU unless memory requires sharding.
|
| 42 |
+
|
| 43 |
+
## Dataset choices
|
| 44 |
+
|
| 45 |
+
- Full data: `JackieMM/LQ-20260915-1-all71-2cam` (71 episodes, 25,920 frames).
|
| 46 |
+
- First 50: `JackieMM/LQ-20260915-1-first50-2cam` (50 episodes, 18,191 frames).
|
| 47 |
+
|
| 48 |
+
The action/state ABI is 14-dimensional. The left-wrist image is intentionally absent; OpenPI masks it
|
| 49 |
+
instead of duplicating another camera. Checkpoint files are local artifacts and are not included in the
|
| 50 |
+
source repository.
|
compute_pi05_norm_stats.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Compute openpi normalization stats for a local real-robot LeRobot dataset."""
|
| 3 |
+
|
| 4 |
+
import argparse
|
| 5 |
+
import dataclasses
|
| 6 |
+
import os
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
import pyarrow.parquet as pq
|
| 11 |
+
from openpi.shared import normalize
|
| 12 |
+
from openpi.training import config as training_config
|
| 13 |
+
from tqdm import tqdm
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def main() -> None:
|
| 17 |
+
parser = argparse.ArgumentParser()
|
| 18 |
+
parser.add_argument("--repo-id", required=True)
|
| 19 |
+
parser.add_argument(
|
| 20 |
+
"--config-name", default="pi05_base_aloha_full_sim_arx-x5_seed_0"
|
| 21 |
+
)
|
| 22 |
+
parser.add_argument("--assets-dir", type=Path, required=True)
|
| 23 |
+
parser.add_argument(
|
| 24 |
+
"--dataset-root",
|
| 25 |
+
type=Path,
|
| 26 |
+
default=None,
|
| 27 |
+
help="LeRobot dataset parent (defaults to YDMING_LEROBOT or the standard cache)",
|
| 28 |
+
)
|
| 29 |
+
parser.add_argument("--max-frames", type=int)
|
| 30 |
+
args = parser.parse_args()
|
| 31 |
+
|
| 32 |
+
config = training_config.get_config(args.config_name)
|
| 33 |
+
real_assets = dataclasses.replace(
|
| 34 |
+
config.data.assets,
|
| 35 |
+
assets_dir=str(args.assets_dir),
|
| 36 |
+
asset_id=args.repo_id,
|
| 37 |
+
)
|
| 38 |
+
real_data_factory = dataclasses.replace(
|
| 39 |
+
config.data,
|
| 40 |
+
repo_id=args.repo_id,
|
| 41 |
+
assets=real_assets,
|
| 42 |
+
)
|
| 43 |
+
config = dataclasses.replace(config, data=real_data_factory, num_workers=0)
|
| 44 |
+
if config.data.adapt_to_pi or not config.data.use_delta_joint_actions:
|
| 45 |
+
raise RuntimeError("Fast stats currently require adapt_to_pi=false and delta joint actions")
|
| 46 |
+
|
| 47 |
+
dataset_parent = args.dataset_root or Path(
|
| 48 |
+
os.environ.get("YDMING_LEROBOT", str(Path.home() / ".cache/huggingface/lerobot"))
|
| 49 |
+
)
|
| 50 |
+
dataset_root = dataset_parent / args.repo_id
|
| 51 |
+
parquet_files = sorted((dataset_root / "data").glob("**/*.parquet"))
|
| 52 |
+
if not parquet_files:
|
| 53 |
+
raise RuntimeError(f"No parquet files found under {dataset_root}")
|
| 54 |
+
tables = [
|
| 55 |
+
pq.read_table(path, columns=["observation.state", "action", "episode_index", "index"])
|
| 56 |
+
for path in parquet_files
|
| 57 |
+
]
|
| 58 |
+
state = np.asarray(
|
| 59 |
+
[value.as_py() for table in tables for value in table["observation.state"]],
|
| 60 |
+
dtype=np.float32,
|
| 61 |
+
)
|
| 62 |
+
action = np.asarray(
|
| 63 |
+
[value.as_py() for table in tables for value in table["action"]], dtype=np.float32
|
| 64 |
+
)
|
| 65 |
+
episode = np.asarray(
|
| 66 |
+
[value.as_py() for table in tables for value in table["episode_index"]], dtype=np.int64
|
| 67 |
+
)
|
| 68 |
+
absolute_index = np.asarray(
|
| 69 |
+
[value.as_py() for table in tables for value in table["index"]], dtype=np.int64
|
| 70 |
+
)
|
| 71 |
+
order = np.argsort(absolute_index)
|
| 72 |
+
state, action, episode = state[order], action[order], episode[order]
|
| 73 |
+
if state.shape != action.shape or state.shape[1] != 14:
|
| 74 |
+
raise RuntimeError(f"Unexpected numeric arrays: state={state.shape}, action={action.shape}")
|
| 75 |
+
|
| 76 |
+
frame_count = len(state)
|
| 77 |
+
if args.max_frames is not None:
|
| 78 |
+
frame_count = min(frame_count, args.max_frames)
|
| 79 |
+
if frame_count < 2:
|
| 80 |
+
raise RuntimeError("At least two frames are required for normalization stats")
|
| 81 |
+
|
| 82 |
+
stats = {
|
| 83 |
+
"state": normalize.RunningStats(),
|
| 84 |
+
"actions": normalize.RunningStats(),
|
| 85 |
+
}
|
| 86 |
+
horizon = config.model.action_horizon
|
| 87 |
+
delta_mask = np.asarray([True] * 6 + [False] + [True] * 6 + [False])
|
| 88 |
+
episode_ends = {
|
| 89 |
+
episode_id: np.flatnonzero(episode == episode_id)[-1] for episode_id in np.unique(episode)
|
| 90 |
+
}
|
| 91 |
+
for index in tqdm(range(frame_count), desc="Computing real-robot stats"):
|
| 92 |
+
end = episode_ends[episode[index]]
|
| 93 |
+
query = np.minimum(np.arange(index, index + horizon), end)
|
| 94 |
+
action_chunk = action[query].copy()
|
| 95 |
+
action_chunk[:, delta_mask] -= state[index, delta_mask]
|
| 96 |
+
if not np.isfinite(state[index]).all() or not np.isfinite(action_chunk).all():
|
| 97 |
+
raise RuntimeError(f"Non-finite numeric data at frame {index}")
|
| 98 |
+
stats["state"].update(state[index])
|
| 99 |
+
stats["actions"].update(action_chunk)
|
| 100 |
+
|
| 101 |
+
result = {key: accumulator.get_statistics() for key, accumulator in stats.items()}
|
| 102 |
+
output = args.assets_dir / args.repo_id
|
| 103 |
+
normalize.save(output, result)
|
| 104 |
+
print(f"frames={frame_count}")
|
| 105 |
+
print(f"output={output / 'norm_stats.json'}")
|
| 106 |
+
for key, value in result.items():
|
| 107 |
+
print(f"{key}.q01={np.asarray(value.q01).round(6).tolist()}")
|
| 108 |
+
print(f"{key}.q99={np.asarray(value.q99).round(6).tolist()}")
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
if __name__ == "__main__":
|
| 112 |
+
main()
|
python_compat/sitecustomize.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Compatibility aliases for XPolicyLab's pre-LeRobot-0.4 import paths."""
|
| 2 |
+
|
| 3 |
+
import sys
|
| 4 |
+
import types
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
try:
|
| 8 |
+
from lerobot.datasets import lerobot_dataset
|
| 9 |
+
except ImportError:
|
| 10 |
+
lerobot_dataset = None
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
if lerobot_dataset is not None and "lerobot.common.datasets.lerobot_dataset" not in sys.modules:
|
| 14 |
+
common = types.ModuleType("lerobot.common")
|
| 15 |
+
common.__path__ = []
|
| 16 |
+
datasets = types.ModuleType("lerobot.common.datasets")
|
| 17 |
+
datasets.__path__ = []
|
| 18 |
+
common.datasets = datasets
|
| 19 |
+
datasets.lerobot_dataset = lerobot_dataset
|
| 20 |
+
sys.modules["lerobot.common"] = common
|
| 21 |
+
sys.modules["lerobot.common.datasets"] = datasets
|
| 22 |
+
sys.modules["lerobot.common.datasets.lerobot_dataset"] = lerobot_dataset
|
setup_openpi.sh
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -euo pipefail
|
| 3 |
+
|
| 4 |
+
ROOT="${1:-${PWD}/robotwin_arx}"
|
| 5 |
+
ROBOTWIN_COMMIT="96c1feab536306b50c26af200044fcdf126e8904"
|
| 6 |
+
XPOLICYLAB_COMMIT="c37109c500be67d0dea6b36bf7337bbd26e763cd"
|
| 7 |
+
|
| 8 |
+
command -v git >/dev/null || { echo "git is required" >&2; exit 2; }
|
| 9 |
+
command -v uv >/dev/null || { echo "uv is required: https://docs.astral.sh/uv/" >&2; exit 2; }
|
| 10 |
+
|
| 11 |
+
mkdir -p "$(dirname "${ROOT}")"
|
| 12 |
+
if [[ ! -d "${ROOT}/RoboTwin/.git" ]]; then
|
| 13 |
+
git clone --recurse-submodules https://github.com/RoboTwin-Platform/RoboTwin.git "${ROOT}/RoboTwin"
|
| 14 |
+
fi
|
| 15 |
+
git -C "${ROOT}/RoboTwin" fetch --tags origin "${ROBOTWIN_COMMIT}"
|
| 16 |
+
git -C "${ROOT}/RoboTwin" checkout --detach "${ROBOTWIN_COMMIT}"
|
| 17 |
+
git -C "${ROOT}/RoboTwin" submodule update --init --recursive
|
| 18 |
+
git -C "${ROOT}/RoboTwin/XPolicyLab" fetch --tags origin "${XPOLICYLAB_COMMIT}"
|
| 19 |
+
git -C "${ROOT}/RoboTwin/XPolicyLab" checkout --detach "${XPOLICYLAB_COMMIT}"
|
| 20 |
+
|
| 21 |
+
OPENPI="${ROOT}/RoboTwin/XPolicyLab/policy/Pi_05/openpi"
|
| 22 |
+
[[ -f "${OPENPI}/pyproject.toml" ]] || { echo "OpenPI checkout not found: ${OPENPI}" >&2; exit 1; }
|
| 23 |
+
(cd "${OPENPI}" && GIT_LFS_SKIP_SMUDGE=1 uv sync --group lerobot)
|
| 24 |
+
printf 'OPENPI=%s\n' "${OPENPI}"
|
train_from_hf.sh
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -euo pipefail
|
| 3 |
+
|
| 4 |
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
| 5 |
+
OPENPI_ROOT=""
|
| 6 |
+
WORK_DIR="${PWD}/arx_pi05_work"
|
| 7 |
+
DATASET_ID="JackieMM/LQ-20260915-1-all71-2cam"
|
| 8 |
+
GPU="0"
|
| 9 |
+
STEPS=30000
|
| 10 |
+
BATCH_SIZE=1
|
| 11 |
+
FSDP_DEVICES=1
|
| 12 |
+
SAVE_INTERVAL=5000
|
| 13 |
+
KEEP_PERIOD=5000
|
| 14 |
+
RESUME=0
|
| 15 |
+
|
| 16 |
+
while [[ $# -gt 0 ]]; do
|
| 17 |
+
case "$1" in
|
| 18 |
+
--openpi-root) OPENPI_ROOT="$2"; shift 2 ;;
|
| 19 |
+
--work-dir) WORK_DIR="$2"; shift 2 ;;
|
| 20 |
+
--dataset-id) DATASET_ID="$2"; shift 2 ;;
|
| 21 |
+
--gpu) GPU="$2"; shift 2 ;;
|
| 22 |
+
--steps) STEPS="$2"; shift 2 ;;
|
| 23 |
+
--batch-size) BATCH_SIZE="$2"; shift 2 ;;
|
| 24 |
+
--fsdp-devices) FSDP_DEVICES="$2"; shift 2 ;;
|
| 25 |
+
--save-interval) SAVE_INTERVAL="$2"; shift 2 ;;
|
| 26 |
+
--keep-period) KEEP_PERIOD="$2"; shift 2 ;;
|
| 27 |
+
--resume) RESUME=1; shift ;;
|
| 28 |
+
*) echo "Unknown argument: $1" >&2; exit 2 ;;
|
| 29 |
+
esac
|
| 30 |
+
done
|
| 31 |
+
|
| 32 |
+
[[ -n "${OPENPI_ROOT}" && -f "${OPENPI_ROOT}/pyproject.toml" ]] || {
|
| 33 |
+
echo "Pass --openpi-root pointing to XPolicyLab/policy/Pi_05/openpi" >&2; exit 2;
|
| 34 |
+
}
|
| 35 |
+
if [[ -x "${OPENPI_ROOT}/.venv/bin/hf" ]]; then
|
| 36 |
+
HF_CMD="${OPENPI_ROOT}/.venv/bin/hf"
|
| 37 |
+
elif command -v hf >/dev/null; then
|
| 38 |
+
HF_CMD=hf
|
| 39 |
+
elif [[ -x "${OPENPI_ROOT}/.venv/bin/huggingface-cli" ]]; then
|
| 40 |
+
HF_CMD="${OPENPI_ROOT}/.venv/bin/huggingface-cli"
|
| 41 |
+
elif command -v huggingface-cli >/dev/null; then
|
| 42 |
+
HF_CMD=huggingface-cli
|
| 43 |
+
else
|
| 44 |
+
echo "Install huggingface_hub and run hf auth login first" >&2; exit 2
|
| 45 |
+
fi
|
| 46 |
+
|
| 47 |
+
DATASET_ROOT="${WORK_DIR}/lerobot/${DATASET_ID##*/}"
|
| 48 |
+
ASSETS_ROOT="${WORK_DIR}/pi05_assets"
|
| 49 |
+
CHECKPOINT_DIR="${WORK_DIR}/checkpoints/${DATASET_ID##*/}-pi05-lora-${STEPS}"
|
| 50 |
+
BASE_PARAMS="${WORK_DIR}/base_params/pi05_droid/params"
|
| 51 |
+
mkdir -p "${WORK_DIR}/lerobot" "${ASSETS_ROOT}" "${WORK_DIR}/base_params"
|
| 52 |
+
"${HF_CMD}" download "${DATASET_ID}" --repo-type dataset --local-dir "${DATASET_ROOT}"
|
| 53 |
+
mkdir -p "${ASSETS_ROOT}/${DATASET_ID##*/}"
|
| 54 |
+
cp "${DATASET_ROOT}/norm_stats.json" "${ASSETS_ROOT}/${DATASET_ID##*/}/norm_stats.json"
|
| 55 |
+
|
| 56 |
+
if [[ ! -d "${BASE_PARAMS}" ]]; then
|
| 57 |
+
"${OPENPI_ROOT}/.venv/bin/python" -c \
|
| 58 |
+
'from openpi.shared import download; import sys; print(download.maybe_download("gs://openpi-assets/checkpoints/pi05_droid/params"))' \
|
| 59 |
+
> "${WORK_DIR}/base_params_path.txt"
|
| 60 |
+
DOWNLOADED="$(tail -n 1 "${WORK_DIR}/base_params_path.txt")"
|
| 61 |
+
[[ -d "${DOWNLOADED}" ]] || { echo "Base checkpoint download failed" >&2; exit 1; }
|
| 62 |
+
BASE_PARAMS="${DOWNLOADED}"
|
| 63 |
+
fi
|
| 64 |
+
|
| 65 |
+
export HF_LEROBOT_HOME="${WORK_DIR}/lerobot"
|
| 66 |
+
export OPENPI_DATA_HOME="${WORK_DIR}/openpi_cache"
|
| 67 |
+
export XLA_PYTHON_CLIENT_ALLOCATOR=platform
|
| 68 |
+
export CUDA_VISIBLE_DEVICES="${GPU}"
|
| 69 |
+
export PYTHONPATH="${SCRIPT_DIR}/python_compat:${OPENPI_ROOT}/src:${OPENPI_ROOT}/..:${PYTHONPATH:-}"
|
| 70 |
+
|
| 71 |
+
ARGS=(
|
| 72 |
+
--repo-id "${DATASET_ID##*/}" --assets-dir "${ASSETS_ROOT}"
|
| 73 |
+
--base-params "${BASE_PARAMS}" --checkpoint-dir "${CHECKPOINT_DIR}" --openpi-root "${OPENPI_ROOT}"
|
| 74 |
+
--steps "${STEPS}" --batch-size "${BATCH_SIZE}" --fsdp-devices "${FSDP_DEVICES}"
|
| 75 |
+
--save-interval "${SAVE_INTERVAL}" --keep-period "${KEEP_PERIOD}"
|
| 76 |
+
--two-cameras --video-tolerance-s 0.04
|
| 77 |
+
)
|
| 78 |
+
[[ "${RESUME}" == 1 ]] && ARGS+=(--resume)
|
| 79 |
+
exec "${OPENPI_ROOT}/.venv/bin/python" "${SCRIPT_DIR}/train_pi05_lora.py" "${ARGS[@]}"
|
train_pi05_lora.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Run pi0.5 LoRA fine-tuning with a real ARX dataset on two 24 GB GPUs."""
|
| 3 |
+
|
| 4 |
+
import argparse
|
| 5 |
+
import dataclasses
|
| 6 |
+
import importlib.util
|
| 7 |
+
import os
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
from openpi import transforms
|
| 11 |
+
from openpi.training import config as training_config
|
| 12 |
+
from openpi.training import weight_loaders
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def load_train_module(openpi_root: Path):
|
| 16 |
+
path = openpi_root / "scripts/train.py"
|
| 17 |
+
spec = importlib.util.spec_from_file_location("openpi_train_script", path)
|
| 18 |
+
module = importlib.util.module_from_spec(spec)
|
| 19 |
+
assert spec.loader is not None
|
| 20 |
+
spec.loader.exec_module(module)
|
| 21 |
+
return module
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def main() -> None:
|
| 25 |
+
parser = argparse.ArgumentParser()
|
| 26 |
+
parser.add_argument("--repo-id", required=True)
|
| 27 |
+
parser.add_argument("--assets-dir", type=Path, required=True)
|
| 28 |
+
parser.add_argument("--checkpoint-dir", type=Path, required=True)
|
| 29 |
+
parser.add_argument("--base-params", type=Path, required=True)
|
| 30 |
+
parser.add_argument("--openpi-root", type=Path, default=None)
|
| 31 |
+
parser.add_argument("--steps", type=int, default=2)
|
| 32 |
+
parser.add_argument("--batch-size", type=int, default=2)
|
| 33 |
+
parser.add_argument("--fsdp-devices", type=int, default=2)
|
| 34 |
+
parser.add_argument(
|
| 35 |
+
"--save-interval",
|
| 36 |
+
type=int,
|
| 37 |
+
default=None,
|
| 38 |
+
help="Save every N steps (defaults to the total step count).",
|
| 39 |
+
)
|
| 40 |
+
parser.add_argument(
|
| 41 |
+
"--keep-period",
|
| 42 |
+
type=int,
|
| 43 |
+
default=None,
|
| 44 |
+
help="Preserve periodic checkpoints at this step interval.",
|
| 45 |
+
)
|
| 46 |
+
parser.add_argument(
|
| 47 |
+
"--config-name", default="pi05_base_aloha_full_sim_arx-x5_seed_0"
|
| 48 |
+
)
|
| 49 |
+
parser.add_argument(
|
| 50 |
+
"--two-cameras",
|
| 51 |
+
action="store_true",
|
| 52 |
+
help="Use only cam_high and cam_right_wrist; AlohaInputs masks the missing left wrist.",
|
| 53 |
+
)
|
| 54 |
+
parser.add_argument(
|
| 55 |
+
"--video-tolerance-s",
|
| 56 |
+
type=float,
|
| 57 |
+
default=0.04,
|
| 58 |
+
help="Timestamp tolerance for LeRobot video decoding (default: 40 ms).",
|
| 59 |
+
)
|
| 60 |
+
parser.add_argument("--resume", action="store_true")
|
| 61 |
+
args = parser.parse_args()
|
| 62 |
+
if args.steps < 1 or args.batch_size < 1:
|
| 63 |
+
raise SystemExit("steps and batch size must be positive")
|
| 64 |
+
if args.checkpoint_dir.exists() and not args.resume:
|
| 65 |
+
raise SystemExit(f"Refusing to overwrite existing checkpoint: {args.checkpoint_dir}")
|
| 66 |
+
|
| 67 |
+
# The exported ARX videos have a small container PTS offset relative to the
|
| 68 |
+
# nominal 30 Hz timestamps. OpenPI's default 100 us tolerance rejects those
|
| 69 |
+
# frames even though the neighboring frame is unambiguous.
|
| 70 |
+
if args.video_tolerance_s > 0:
|
| 71 |
+
from lerobot.datasets import lerobot_dataset
|
| 72 |
+
|
| 73 |
+
dataset_class = lerobot_dataset.LeRobotDataset
|
| 74 |
+
|
| 75 |
+
class TolerantLeRobotDataset(dataset_class):
|
| 76 |
+
def __init__(self, *dataset_args, **dataset_kwargs):
|
| 77 |
+
dataset_kwargs.setdefault("tolerance_s", args.video_tolerance_s)
|
| 78 |
+
super().__init__(*dataset_args, **dataset_kwargs)
|
| 79 |
+
|
| 80 |
+
lerobot_dataset.LeRobotDataset = TolerantLeRobotDataset
|
| 81 |
+
|
| 82 |
+
base = training_config.get_config(args.config_name)
|
| 83 |
+
model = dataclasses.replace(
|
| 84 |
+
base.model,
|
| 85 |
+
paligemma_variant="gemma_2b_lora",
|
| 86 |
+
action_expert_variant="gemma_300m_lora",
|
| 87 |
+
)
|
| 88 |
+
assets = dataclasses.replace(
|
| 89 |
+
base.data.assets,
|
| 90 |
+
assets_dir=str(args.assets_dir),
|
| 91 |
+
asset_id=args.repo_id,
|
| 92 |
+
)
|
| 93 |
+
data = dataclasses.replace(base.data, repo_id=args.repo_id, assets=assets)
|
| 94 |
+
if args.two_cameras:
|
| 95 |
+
data = dataclasses.replace(
|
| 96 |
+
data,
|
| 97 |
+
repack_transforms=transforms.Group(
|
| 98 |
+
inputs=[
|
| 99 |
+
transforms.RepackTransform(
|
| 100 |
+
{
|
| 101 |
+
"images": {
|
| 102 |
+
"cam_high": "observation.images.cam_high",
|
| 103 |
+
"cam_right_wrist": "observation.images.cam_right_wrist",
|
| 104 |
+
},
|
| 105 |
+
"state": "observation.state",
|
| 106 |
+
"actions": "action",
|
| 107 |
+
"prompt": "prompt",
|
| 108 |
+
}
|
| 109 |
+
)
|
| 110 |
+
]
|
| 111 |
+
),
|
| 112 |
+
)
|
| 113 |
+
config = dataclasses.replace(
|
| 114 |
+
base,
|
| 115 |
+
exp_name=args.checkpoint_dir.name,
|
| 116 |
+
model=model,
|
| 117 |
+
data=data,
|
| 118 |
+
weight_loader=weight_loaders.CheckpointWeightLoader(str(args.base_params)),
|
| 119 |
+
freeze_filter=model.get_freeze_filter(),
|
| 120 |
+
batch_size=args.batch_size,
|
| 121 |
+
fsdp_devices=args.fsdp_devices,
|
| 122 |
+
num_train_steps=args.steps,
|
| 123 |
+
num_workers=0,
|
| 124 |
+
log_interval=1,
|
| 125 |
+
# The checkpoint is multi-gigabyte; the training loop always saves the
|
| 126 |
+
# final step, so avoid spending most of a short smoke run on I/O.
|
| 127 |
+
save_interval=args.save_interval or args.steps,
|
| 128 |
+
keep_period=args.keep_period if args.keep_period is not None else base.keep_period,
|
| 129 |
+
checkpoint_dir_override=str(args.checkpoint_dir),
|
| 130 |
+
wandb_enabled=False,
|
| 131 |
+
overwrite=not args.resume,
|
| 132 |
+
resume=args.resume,
|
| 133 |
+
ema_decay=None,
|
| 134 |
+
)
|
| 135 |
+
# Prefer the co-located checkout, but keep the deployed server path usable
|
| 136 |
+
# when this script is launched from /tmp or another staging directory.
|
| 137 |
+
configured_root = args.openpi_root or os.environ.get("OPENPI_ROOT")
|
| 138 |
+
openpi_root = Path(configured_root) if configured_root else Path()
|
| 139 |
+
local_root = Path(__file__).resolve()
|
| 140 |
+
if not configured_root and len(local_root.parents) > 2:
|
| 141 |
+
candidate = local_root.parents[2] / "XPolicyLab/policy/Pi_05/openpi"
|
| 142 |
+
if candidate.exists():
|
| 143 |
+
openpi_root = candidate
|
| 144 |
+
if not (openpi_root / "scripts/train.py").exists():
|
| 145 |
+
raise FileNotFoundError(
|
| 146 |
+
"OpenPI checkout not found; pass --openpi-root or set OPENPI_ROOT"
|
| 147 |
+
)
|
| 148 |
+
train = load_train_module(openpi_root)
|
| 149 |
+
train.main(config)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
if __name__ == "__main__":
|
| 153 |
+
main()
|