Update README and adapter code
Browse files- README.md +22 -0
- code/hb_act/hb_act_args.py +95 -0
- code/hb_act/hb_act_config.py +99 -0
- code/hb_act/hb_act_patches.py +84 -0
- code/hb_act/hb_act_train.py +53 -0
- code/hb_act/verify_cpu.py +78 -0
README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
tags:
|
| 3 |
+
- robotics
|
| 4 |
+
- humanoid
|
| 5 |
+
- imitation-learning
|
| 6 |
+
---
|
| 7 |
+
# ACT — Humanoid Benchmark
|
| 8 |
+
|
| 9 |
+
ACT (Action Chunking Transformer, ResNet18, VAE; Psi0 baselines/act) checkpoints trained on Humanoid Benchmark tasks (100 demonstrations per task). Global batch 696 = 87 per GPU x 8 GPUs, one node per run, 20 epochs, saved at epoch 10 and 20.
|
| 10 |
+
|
| 11 |
+
| Folder | Task | Robot |
|
| 12 |
+
|---|---|---|
|
| 13 |
+
| `b6-pedal-bin/` | Pick up a chip barrel, step on the pedal bin, drop it in | Unitree G1 |
|
| 14 |
+
|
| 15 |
+
Each task folder has one folder per published checkpoint (`epoch-XXX/`). Every epoch folder is self-contained for
|
| 16 |
+
evaluation and deployment (weights, configuration, normalization statistics, `protocol.json`) and also carries the
|
| 17 |
+
simple training records kept for reproducibility (training plan, logs, metrics). Optimizer / scheduler / RNG /
|
| 18 |
+
DeepSpeed resume state is not included. `code/` holds the adapter code used for training and inference.
|
| 19 |
+
|
| 20 |
+
Model inputs are identical across all benchmark models: G1 state 44-D (29 body joint angles + 6 + 6 measured hand
|
| 21 |
+
values + 3 projected gravity), H2 state 78-D (75 joint angles + 3 projected gravity), no joint velocities.
|
| 22 |
+
Training used the Humanoid Benchmark online data augmentation spec v2 with the model's own image augmentation off.
|
code/hb_act/hb_act_args.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ACT args for the pinned Psi0 scripts/train.py (config hb_act_config), on the Psi0 views $G/psi0-data/<task>.
|
| 2 |
+
Same 20-epoch plan / probe decision as Psi-Zero (hb_psi0/hb_args.py: plan, decide, receipt); only the args differ.
|
| 3 |
+
|
| 4 |
+
hb_act_args.py plan <task> <batch>
|
| 5 |
+
hb_act_args.py args <task> (--decision D | --batch B --steps S --save V --warmup W) --out DIR --timestamp T
|
| 6 |
+
hb_act_args.py decide <task> <probe_dir> <decision.json> <cand ...>
|
| 7 |
+
"""
|
| 8 |
+
import argparse
|
| 9 |
+
import json
|
| 10 |
+
import os
|
| 11 |
+
import sys
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
H = Path(__file__).resolve().parent
|
| 15 |
+
sys.path.insert(0, str(H.parent / "hb_psi0"))
|
| 16 |
+
import hb_args as HA # noqa: E402 (TASKS, DATA, receipt, plan, decide; EPOCHS 20, save at 10, warmup 2.5 %)
|
| 17 |
+
|
| 18 |
+
CHUNK = 16 # native ACT G1 simulation recipe (train_act_g1_simple.sh)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def build_args(task, batch, steps, save, warmup, out, ts, workers, resume=True):
|
| 22 |
+
r = HA.receipt(task)
|
| 23 |
+
cams = r["image_keys"]
|
| 24 |
+
SK, AK, SD, AD, NB = r["state_key"], r["action_key"], int(r["state_dim"]), int(r["action_dim"]), int(r["body_dims"])
|
| 25 |
+
assert SD in (44, 78), f"raw state must be 44 / 78 (no joint velocity), got {SD}"
|
| 26 |
+
stats = str(HA.DATA / task / "meta" / "stats_psi0.json")
|
| 27 |
+
return [
|
| 28 |
+
"--seed=292285", f"--exp=hb-act-{task}", f"--timestamp={ts}",
|
| 29 |
+
*(["--train.resume_from_checkpoint=latest"] if resume else []),
|
| 30 |
+
"--train.name=act-g1", f"--train.output_dir={out}",
|
| 31 |
+
"--train.data_parallel=ddp", "--train.mixed_precision=bf16",
|
| 32 |
+
f"--train.train_batch_size={batch}", f"--train.val_batch_size={batch}",
|
| 33 |
+
"--train.max_checkpoints_to_keep=3", "--train.preserve_checkpoint_every=None",
|
| 34 |
+
"--train.gradient_accumulation_steps=1", "--train.learning_rate=1e-4",
|
| 35 |
+
f"--train.max_training_steps={steps}", "--train.warmup_ratio=None", f"--train.warmup_steps={warmup}",
|
| 36 |
+
f"--train.checkpointing_steps={save}", "--train.validation_steps=0",
|
| 37 |
+
"--train.max_grad_norm=1.0", "--train.lr_scheduler_type=cosine",
|
| 38 |
+
"--train.lr_scheduler_kwargs.weight_decay=1e-6", "--train.lr_scheduler_kwargs.betas", "0.95", "0.999",
|
| 39 |
+
f"--data.root_dir={HA.DATA}", "--data.train_repo_ids", task, "--data.val_repo_ids", task,
|
| 40 |
+
f"--data.num_workers={workers}", "--data.prefetch_factor=4",
|
| 41 |
+
"--data.transform.repack.image-keys", *cams,
|
| 42 |
+
"--data.transform.repack.state-keys", SK,
|
| 43 |
+
"--data.transform.repack.action-keys", AK,
|
| 44 |
+
f"--data.transform.repack.dataset-name=hb_{task}",
|
| 45 |
+
f"--data.transform.repack.pad-action-dim={AD}", f"--data.transform.repack.pad-state-dim={SD}",
|
| 46 |
+
"--data.transform.repack.instruction-key=task",
|
| 47 |
+
f"--data.transform.repack.action-chunk-size={CHUNK}",
|
| 48 |
+
"--data.transform.repack.state-temporal-jitter=0",
|
| 49 |
+
"--data.transform.repack.hb-delay-prob=0.15", "--data.transform.repack.hb-delay-max-frames=2",
|
| 50 |
+
f"--data.transform.repack.hb-body-dims={NB}", "--data.transform.repack.hb-bias-seed=292285",
|
| 51 |
+
f"--data.transform.field.stat-path={stats}", "--data.transform.field.state-noise-std=0.0",
|
| 52 |
+
"--data.transform.field.stat-action-keys", AK,
|
| 53 |
+
"--data.transform.field.stat-state-keys", SK,
|
| 54 |
+
"--data.transform.field.action_norm_type=bounds", "--data.transform.field.no-use-norm-mask",
|
| 55 |
+
"--data.transform.field.normalize-state",
|
| 56 |
+
f"--data.transform.field.pad-action-dim={AD}", f"--data.transform.field.pad-state-dim={SD}",
|
| 57 |
+
"--data.transform.model.no-img-aug", "--data.transform.model.hb-aug",
|
| 58 |
+
f"--model.chunk-size={CHUNK}", f"--model.n-action-steps={CHUNK}",
|
| 59 |
+
f"--model.action-dim={AD}", f"--model.state-dim={SD}",
|
| 60 |
+
"--model.use-vae", "--model.kl-weight=10.0",
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def main():
|
| 65 |
+
ap = argparse.ArgumentParser()
|
| 66 |
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
| 67 |
+
p = sub.add_parser("plan"); p.add_argument("task"); p.add_argument("batch", type=int)
|
| 68 |
+
p = sub.add_parser("args"); p.add_argument("task")
|
| 69 |
+
p.add_argument("--decision"); p.add_argument("--batch", type=int); p.add_argument("--steps", type=int)
|
| 70 |
+
p.add_argument("--save", type=int); p.add_argument("--warmup", type=int)
|
| 71 |
+
p.add_argument("--out", required=True); p.add_argument("--timestamp", required=True)
|
| 72 |
+
p.add_argument("--workers", type=int, default=int(os.environ.get("HB_NUM_WORKERS", "8")))
|
| 73 |
+
p.add_argument("--no-resume", action="store_true")
|
| 74 |
+
p = sub.add_parser("decide"); p.add_argument("task"); p.add_argument("probe_dir"); p.add_argument("decision")
|
| 75 |
+
p.add_argument("cands", nargs="+")
|
| 76 |
+
a = ap.parse_args()
|
| 77 |
+
if a.cmd != "decide" and a.task not in HA.TASKS:
|
| 78 |
+
sys.exit(f"unknown task {a.task}; known {HA.TASKS}")
|
| 79 |
+
if a.cmd == "plan":
|
| 80 |
+
print(json.dumps(HA.plan(HA.receipt(a.task)["frames"], a.batch), indent=2))
|
| 81 |
+
elif a.cmd == "args":
|
| 82 |
+
if a.decision:
|
| 83 |
+
d = json.loads(Path(a.decision).read_text())
|
| 84 |
+
assert d["task"] == a.task and d["frames"] == HA.receipt(a.task)["frames"], "stale decision file"
|
| 85 |
+
b, s, sv, w = d["per_gpu_batch"], d["steps"], d["save_steps"], d["warmup_steps"]
|
| 86 |
+
else:
|
| 87 |
+
b, s, sv, w = a.batch, a.steps, a.save, a.warmup
|
| 88 |
+
assert None not in (b, s, sv, w), "--batch/--steps/--save/--warmup or --decision"
|
| 89 |
+
print("\n".join(build_args(a.task, b, s, sv, w, a.out, a.timestamp, a.workers, not a.no_resume)))
|
| 90 |
+
else:
|
| 91 |
+
sys.exit(HA.decide(a.task, a.probe_dir, a.decision, a.cands))
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
main()
|
code/hb_act/hb_act_config.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ACT (Psi0 baselines/act = LeRobot ACT, ResNet18) launch config for the Humanoid Benchmark tasks.
|
| 2 |
+
|
| 3 |
+
Registered by hb_act_train.py as psi.config.train.hb_act_config; the pinned upstream scripts/train.py loads it
|
| 4 |
+
unchanged and Trainer.instantiate picks psi.trainers.act_g1.ActG1Trainer (--train.name=act-g1).
|
| 5 |
+
Native recipe = baselines/act/train_act_g1_simple.sh (G1 simulation): chunk 16, VAE (kl 10, latent 32), dim 512,
|
| 6 |
+
4 encoder / 1 decoder layers, ResNet18 ImageNet backbone, images resized to 256x480 (NEAREST) and center-cropped to
|
| 7 |
+
224x224, ImageNet normalisation, bounds-normalised actions and states, AdamW lr 1e-4 (betas 0.95/0.999, wd 1e-6),
|
| 8 |
+
cosine schedule, warmup 2.5 % (1000/40000), bf16 DDP.
|
| 9 |
+
HB changes (CLAUDE.md v2, identical inputs for every model):
|
| 10 |
+
* repack = hb_psi0_config.HbSonicRepackTransform (the same Psi0 view as Psi-Zero: G1 observation.hb44 /
|
| 11 |
+
action.hb76, H2 observation.hb78 / action.hb75, cameras cam0 + chest, raw state WITHOUT joint velocity),
|
| 12 |
+
with the HB v2 proprio augmentation (15 % delay 1..2 frames, +-2 deg per-trajectory bias, 0.25 deg noise).
|
| 13 |
+
* field = native SonicActionStateTransform (bounds), as Psi-Zero (same stats_psi0.json of the view).
|
| 14 |
+
* model = HbACTModelTransform: native geometry (resize 256x480 NEAREST, center crop 224x224) and normalisation;
|
| 15 |
+
native ColorJitter (img_aug) OFF, replaced by hb_aug_v2.augment on both cameras jointly at 224x224.
|
| 16 |
+
ACT natively encodes every camera with the shared ResNet18 and concatenates the feature maps (n_cameras = 2).
|
| 17 |
+
"""
|
| 18 |
+
import os
|
| 19 |
+
import sys
|
| 20 |
+
from typing import Any
|
| 21 |
+
|
| 22 |
+
import numpy as np
|
| 23 |
+
import torch
|
| 24 |
+
from pydantic import model_validator
|
| 25 |
+
from torchvision.transforms import v2
|
| 26 |
+
from typing_extensions import Self
|
| 27 |
+
|
| 28 |
+
_HERE = os.path.dirname(os.path.abspath(__file__))
|
| 29 |
+
_PSI0 = os.path.join(os.path.dirname(_HERE), "hb_psi0")
|
| 30 |
+
if _PSI0 not in sys.path:
|
| 31 |
+
sys.path.insert(0, _PSI0)
|
| 32 |
+
import hb_psi0_config as HP # noqa: E402 HbSonicRepackTransform, hb_rng, set_epoch, A = hb_aug_v2
|
| 33 |
+
|
| 34 |
+
from psi.config import transform as pt # noqa: E402
|
| 35 |
+
from psi.config import transform_psi0_sonic as ps # noqa: E402
|
| 36 |
+
from psi.config.config import LaunchConfig # noqa: E402
|
| 37 |
+
from psi.config.data_lerobot import LerobotDataConfig # noqa: E402
|
| 38 |
+
from psi.config.model_act import ACTModelConfig # noqa: E402
|
| 39 |
+
from psi.config.transform import DataTransform # noqa: E402
|
| 40 |
+
|
| 41 |
+
RAW_STATE_DIMS = (44, 78) # G1 / H2 benchmark state input (no joint velocity)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class HbACTModelTransform(pt.ACTModelTransform):
|
| 45 |
+
hb_aug: bool = True
|
| 46 |
+
|
| 47 |
+
def __call__(self, data: dict[str, Any], no_aug: bool = False, **kwargs) -> dict[str, Any]:
|
| 48 |
+
assert not self.img_aug, "ACT native ColorJitter must stay off (HB v2 replaces it)"
|
| 49 |
+
geom = v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True), self.resize(), self.center_crop()])
|
| 50 |
+
x = torch.stack([geom(img) for img in data["observations"]]) # (V, 3, 224, 224) in [0, 1], native geometry
|
| 51 |
+
if self.hb_aug and not no_aug:
|
| 52 |
+
y, p = HP.A.augment(x[None], HP.hb_rng()) # [T=1, V, 3, H, W]: colour shared, geometry/light per camera
|
| 53 |
+
x = y[0].clamp(0.0, 1.0)
|
| 54 |
+
HP._log_aug(p, y.shape)
|
| 55 |
+
images = self.normalize()(x)
|
| 56 |
+
mask = np.asarray(data["actions_mask"]).astype(bool)
|
| 57 |
+
if not mask.all():
|
| 58 |
+
raise ValueError("HB views never pad actions (actions_mask must be all True)")
|
| 59 |
+
return {
|
| 60 |
+
"observation.images": images, # (n_cameras, C, H, W)
|
| 61 |
+
"observation.state": data["states"], # (1, Ds)
|
| 62 |
+
"action": data["actions"], # (chunk, Da)
|
| 63 |
+
"action_is_pad": np.zeros(np.asarray(data["actions"]).shape[0], dtype=bool),
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class HbACTDataTransform(DataTransform):
|
| 68 |
+
repack: HP.HbSonicRepackTransform
|
| 69 |
+
field: ps.SonicActionStateTransform
|
| 70 |
+
model: HbACTModelTransform
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class HbACTDataConfig(LerobotDataConfig):
|
| 74 |
+
transform: HbACTDataTransform # must be re-annotated for tyro (see hb_psi0_config.HbDataConfig)
|
| 75 |
+
num_workers: int | None = None
|
| 76 |
+
prefetch_factor: int | None = None
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
class DynamicLaunchConfig(LaunchConfig):
|
| 80 |
+
data: HbACTDataConfig
|
| 81 |
+
model: ACTModelConfig
|
| 82 |
+
|
| 83 |
+
@model_validator(mode="after")
|
| 84 |
+
def check_hb_dims(self, __context: Any) -> Self:
|
| 85 |
+
repack, field, model = self.data.transform.repack, self.data.transform.field, self.model
|
| 86 |
+
assert repack.pad_action_dim == field.pad_action_dim == model.action_dim, \
|
| 87 |
+
(repack.pad_action_dim, field.pad_action_dim, model.action_dim)
|
| 88 |
+
assert repack.pad_state_dim == field.pad_state_dim == model.state_dim, \
|
| 89 |
+
(repack.pad_state_dim, field.pad_state_dim, model.state_dim)
|
| 90 |
+
assert model.state_dim in RAW_STATE_DIMS, f"ACT state must be the raw 44 / 78 dims (no velocity), got {model.state_dim}"
|
| 91 |
+
assert model.chunk_size == model.n_action_steps == repack.action_chunk_size, \
|
| 92 |
+
(model.chunk_size, model.n_action_steps, repack.action_chunk_size)
|
| 93 |
+
assert field.action_min is not None and len(field.action_min) == model.action_dim, "action stats width"
|
| 94 |
+
assert field.normalize_state and field.state_min is not None and len(field.state_min) == model.state_dim, \
|
| 95 |
+
"state stats width"
|
| 96 |
+
assert not self.data.transform.model.img_aug, "disable --data.transform.model.img-aug (HB v2 replaces it)"
|
| 97 |
+
assert field.state_noise_std == 0.0 and repack.state_temporal_jitter == 0
|
| 98 |
+
assert 0 <= repack.hb_body_dims <= model.state_dim
|
| 99 |
+
return self
|
code/hb_act/hb_act_patches.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Runtime patches on the pinned Psi0 ACT trainer (psi.trainers.act_g1.ActG1Trainer); no upstream file modified.
|
| 2 |
+
1. Trainer.next_epoch -> hb_psi0_config.set_epoch(epoch): per-trajectory joint bias in the DataLoader workers.
|
| 3 |
+
2. Trainer.log -> numeric metrics also to <run>/hb_metrics.jsonl.
|
| 4 |
+
3. ActG1Trainer.save_checkpoint: older ckpt_* keep the model weights, lose optimizer/scheduler/RNG/scaler state.
|
| 5 |
+
4. Probe mode (HB_PROBE_OUT): no checkpoints, step times recorded.
|
| 6 |
+
"""
|
| 7 |
+
import json
|
| 8 |
+
import math
|
| 9 |
+
import os
|
| 10 |
+
import time
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
|
| 14 |
+
PROBE: dict = {"times": []}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def apply() -> None:
|
| 18 |
+
import hb_psi0_config
|
| 19 |
+
from psi.trainers import act_g1 as A
|
| 20 |
+
from psi.trainers.trainer import Trainer
|
| 21 |
+
|
| 22 |
+
AT = A.ActG1Trainer
|
| 23 |
+
orig_next_epoch = Trainer.next_epoch
|
| 24 |
+
|
| 25 |
+
def hb_next_epoch(self, epoch):
|
| 26 |
+
hb_psi0_config.set_epoch(epoch)
|
| 27 |
+
return orig_next_epoch(self, epoch)
|
| 28 |
+
|
| 29 |
+
Trainer.next_epoch = hb_next_epoch
|
| 30 |
+
|
| 31 |
+
orig_log = Trainer.log
|
| 32 |
+
every = int(os.environ.get("HB_LOG_EVERY", "10"))
|
| 33 |
+
|
| 34 |
+
def hb_log(self, metrics, start_time=None):
|
| 35 |
+
orig_log(self, metrics, start_time)
|
| 36 |
+
if not self.accelerator.is_main_process:
|
| 37 |
+
return
|
| 38 |
+
step = int(getattr(self, "global_step", 0))
|
| 39 |
+
if step % every:
|
| 40 |
+
return
|
| 41 |
+
rec = {"step": step, "time": time.time()}
|
| 42 |
+
for k, v in metrics.items():
|
| 43 |
+
if isinstance(v, torch.Tensor) and v.numel() == 1:
|
| 44 |
+
v = v.item()
|
| 45 |
+
if isinstance(v, (int, float)) and math.isfinite(float(v)):
|
| 46 |
+
rec[k] = float(v)
|
| 47 |
+
with open(os.path.join(self.project_dir, "hb_metrics.jsonl"), "a") as f:
|
| 48 |
+
f.write(json.dumps(rec) + "\n")
|
| 49 |
+
|
| 50 |
+
Trainer.log = hb_log
|
| 51 |
+
|
| 52 |
+
orig_save = AT.save_checkpoint
|
| 53 |
+
|
| 54 |
+
def hb_save_checkpoint(self, global_step):
|
| 55 |
+
out = orig_save(self, global_step)
|
| 56 |
+
if self.accelerator.is_main_process:
|
| 57 |
+
import glob
|
| 58 |
+
import re
|
| 59 |
+
import shutil
|
| 60 |
+
root = os.path.join(self.project_dir, "checkpoints")
|
| 61 |
+
for d in glob.glob(os.path.join(root, "ckpt_*")):
|
| 62 |
+
if os.path.basename(d) == f"ckpt_{global_step}":
|
| 63 |
+
continue
|
| 64 |
+
for f in os.listdir(d):
|
| 65 |
+
if re.match(r"(optimizer|scheduler|random_states|scaler)", f):
|
| 66 |
+
p = os.path.join(d, f)
|
| 67 |
+
(shutil.rmtree if os.path.isdir(p) else os.remove)(p)
|
| 68 |
+
print(f"[hb] dropped resume state {p}", flush=True)
|
| 69 |
+
self.accelerator.wait_for_everyone()
|
| 70 |
+
return out
|
| 71 |
+
|
| 72 |
+
AT.save_checkpoint = hb_save_checkpoint
|
| 73 |
+
|
| 74 |
+
if os.environ.get("HB_PROBE_OUT"):
|
| 75 |
+
AT.save_checkpoint = lambda self, global_step: None
|
| 76 |
+
orig_step = AT.training_step
|
| 77 |
+
|
| 78 |
+
def timed_step(self, batch):
|
| 79 |
+
out = orig_step(self, batch)
|
| 80 |
+
torch.cuda.synchronize()
|
| 81 |
+
PROBE["times"].append(time.perf_counter())
|
| 82 |
+
return out
|
| 83 |
+
|
| 84 |
+
AT.training_step = timed_step
|
code/hb_act/hb_act_train.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""torchrun entry: register hb_act_config, apply hb_act_patches, run the pinned upstream Psi0 scripts/train.py.
|
| 2 |
+
Env: PSI_SRC = Psi0 checkout; HB_PROBE_OUT = dir -> probe mode (rank<R>.json with peak memory + step time).
|
| 3 |
+
"""
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
import runpy
|
| 7 |
+
import sys
|
| 8 |
+
import time
|
| 9 |
+
import traceback
|
| 10 |
+
|
| 11 |
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 12 |
+
sys.path.insert(0, HERE)
|
| 13 |
+
sys.path.insert(0, os.path.join(os.path.dirname(HERE), "hb_psi0"))
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def main() -> None:
|
| 17 |
+
psi = os.environ["PSI_SRC"]
|
| 18 |
+
import hb_act_config
|
| 19 |
+
sys.modules["psi.config.train.hb_act_config"] = hb_act_config
|
| 20 |
+
import hb_act_patches
|
| 21 |
+
hb_act_patches.apply()
|
| 22 |
+
entry = os.path.join(psi, "scripts", "train.py")
|
| 23 |
+
sys.argv = [entry, "hb_act_config"] + sys.argv[1:]
|
| 24 |
+
probe_out = os.environ.get("HB_PROBE_OUT")
|
| 25 |
+
t0 = time.time()
|
| 26 |
+
ok, err = True, None
|
| 27 |
+
try:
|
| 28 |
+
runpy.run_path(entry, run_name="__main__")
|
| 29 |
+
except BaseException as e: # noqa: BLE001
|
| 30 |
+
ok, err = False, f"{type(e).__name__}: {e}"[:4000]
|
| 31 |
+
if not probe_out:
|
| 32 |
+
raise
|
| 33 |
+
traceback.print_exc()
|
| 34 |
+
if probe_out:
|
| 35 |
+
import torch
|
| 36 |
+
rank = int(os.environ.get("RANK", "0"))
|
| 37 |
+
res = {"ok": ok, "error": err, "rank": rank, "wall_s": time.time() - t0}
|
| 38 |
+
if torch.cuda.is_available():
|
| 39 |
+
dev = torch.cuda.current_device()
|
| 40 |
+
res.update(peak_reserved=torch.cuda.max_memory_reserved(dev), total=torch.cuda.get_device_properties(dev).total_memory)
|
| 41 |
+
res["peak_frac"] = res["peak_reserved"] / res["total"]
|
| 42 |
+
t = hb_act_patches.PROBE["times"]
|
| 43 |
+
res["steps_done"] = len(t)
|
| 44 |
+
if len(t) >= 4:
|
| 45 |
+
res["sec_per_step"] = (t[-1] - t[1]) / (len(t) - 2)
|
| 46 |
+
os.makedirs(probe_out, exist_ok=True)
|
| 47 |
+
with open(os.path.join(probe_out, f"rank{rank}.json"), "w") as f:
|
| 48 |
+
json.dump(res, f, indent=2)
|
| 49 |
+
sys.exit(0 if ok else 1)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
main()
|
code/hb_act/verify_cpu.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""CPU checks of the HB ACT pipeline for one task (no GPU): args -> tyro -> DynamicLaunchConfig (same parse as
|
| 2 |
+
scripts/train.py), real Psi0 view samples (2 cameras 224x224, raw state 44/78 without velocity, chunk 16), HB v2
|
| 3 |
+
augmentation on / native ColorJitter off, and one ACTPolicy forward (ResNet18 ImageNet weights from TORCH_HOME).
|
| 4 |
+
Usage: <psi0 python> verify_cpu.py --task trolley [--samples 4]"""
|
| 5 |
+
import argparse
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
import tempfile
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
import torch
|
| 13 |
+
|
| 14 |
+
H = Path(__file__).resolve().parent
|
| 15 |
+
sys.path.insert(0, str(H))
|
| 16 |
+
sys.path.insert(0, str(H.parent / "hb_psi0"))
|
| 17 |
+
FAILS = []
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def check(c, m):
|
| 21 |
+
print((" ok " if c else " FAIL ") + m, flush=True)
|
| 22 |
+
if not c:
|
| 23 |
+
FAILS.append(m)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def main():
|
| 27 |
+
ap = argparse.ArgumentParser()
|
| 28 |
+
ap.add_argument("--task", default="trolley")
|
| 29 |
+
ap.add_argument("--samples", type=int, default=4)
|
| 30 |
+
a = ap.parse_args()
|
| 31 |
+
import tyro
|
| 32 |
+
import hb_act_args as AA
|
| 33 |
+
import hb_act_config as C
|
| 34 |
+
sys.modules["psi.config.train.hb_act_config"] = C
|
| 35 |
+
r = AA.HA.receipt(a.task)
|
| 36 |
+
SD, AD = int(r["state_dim"]), int(r["action_dim"])
|
| 37 |
+
args = AA.build_args(a.task, a.samples, 100, 50, 3, tempfile.mkdtemp(prefix="hb_act_verify_"), "verify", 2, resume=False)
|
| 38 |
+
cfg = tyro.cli(C.DynamicLaunchConfig, config=(tyro.conf.ConsolidateSubcommandArgs,), args=args)
|
| 39 |
+
m, mt = cfg.model, cfg.data.transform.model
|
| 40 |
+
print(f"[1] task {a.task}: robot {r.get('robot')} state {SD} action {AD} cams {r['image_keys']}")
|
| 41 |
+
check((m.action_dim, m.state_dim, m.chunk_size, m.n_action_steps) == (AD, SD, 16, 16), f"model A/S/chunk {m.action_dim}/{m.state_dim}/{m.chunk_size}")
|
| 42 |
+
check(SD in (44, 78), f"raw state {SD} = 44 (G1) / 78 (H2), no joint velocity")
|
| 43 |
+
check(not mt.img_aug and mt.hb_aug, "native ColorJitter off, HB v2 aug on")
|
| 44 |
+
check(tuple(mt.resize.size) == (256, 480) and tuple(mt.center_crop.size) == (224, 224), f"native geometry resize {mt.resize.size} crop {mt.center_crop.size}")
|
| 45 |
+
check(m.use_vae and m.kl_weight == 10.0 and cfg.train.learning_rate == 1e-4, "VAE kl 10, lr 1e-4 (native)")
|
| 46 |
+
ds = cfg.data(split="train")
|
| 47 |
+
check(len(ds) == r["frames"], f"len {len(ds)} == frames {r['frames']}")
|
| 48 |
+
items = [ds[int(i)] for i in np.linspace(0, len(ds) - 1, a.samples).astype(int)]
|
| 49 |
+
s = items[0]
|
| 50 |
+
print(" sample keys", sorted(s))
|
| 51 |
+
im = torch.as_tensor(s["observation.images"])
|
| 52 |
+
check(tuple(im.shape) == (len(r["image_keys"]), 3, 224, 224), f"images {tuple(im.shape)} = (n_cameras, 3, 224, 224)")
|
| 53 |
+
check(-2.2 < float(im.min()) and float(im.max()) < 2.7, f"ImageNet-normalised range [{float(im.min()):.2f}, {float(im.max()):.2f}]")
|
| 54 |
+
st, ac = np.asarray(s["observation.state"]), np.asarray(s["action"])
|
| 55 |
+
check(st.shape == (1, SD) and ac.shape == (16, AD), f"state {st.shape} action {ac.shape}")
|
| 56 |
+
check(np.abs(ac).max() <= 1.0 + 1e-4, f"actions bounds-normalised (max |a| {np.abs(ac).max():.3f})")
|
| 57 |
+
check(not np.asarray(s["action_is_pad"]).any(), "no action padding")
|
| 58 |
+
batch = torch.utils.data.default_collate(items)
|
| 59 |
+
from act.models.act import ACTConfig, ACTPolicy
|
| 60 |
+
ac_cfg = ACTConfig(chunk_size=m.chunk_size, n_action_steps=m.n_action_steps, action_dim=m.action_dim, state_dim=m.state_dim,
|
| 61 |
+
dim_model=m.dim_model, n_heads=m.n_heads, dim_feedforward=m.dim_feedforward,
|
| 62 |
+
feedforward_activation=m.feedforward_activation, n_encoder_layers=m.n_encoder_layers,
|
| 63 |
+
n_decoder_layers=m.n_decoder_layers, pre_norm=m.pre_norm, dropout=m.dropout, use_vae=m.use_vae,
|
| 64 |
+
latent_dim=m.latent_dim, n_vae_encoder_layers=m.n_vae_encoder_layers, kl_weight=m.kl_weight,
|
| 65 |
+
temporal_ensemble_coeff=m.temporal_ensemble_coeff)
|
| 66 |
+
pol = ACTPolicy(config=ac_cfg, dataset_stats=None)
|
| 67 |
+
if batch["observation.state"].dim() == 3:
|
| 68 |
+
batch["observation.state"] = batch["observation.state"].squeeze(1)
|
| 69 |
+
batch = {k: (v.float() if torch.is_tensor(v) and v.is_floating_point() else v) for k, v in batch.items()}
|
| 70 |
+
out = pol(batch)
|
| 71 |
+
check(all(torch.isfinite(torch.as_tensor(v)).all() for v in out.values() if torch.is_tensor(v)), f"ACT forward {{k: float(v) for k, v in out.items() if torch.is_tensor(v)}}".replace("{k", "").replace("}", "") + " " + str({k: round(float(v), 4) for k, v in out.items() if torch.is_tensor(v) or isinstance(v, float)}))
|
| 72 |
+
print(f" ACT params {sum(p.numel() for p in pol.parameters()) / 1e6:.1f} M")
|
| 73 |
+
print("VERIFY_" + ("OK" if not FAILS else f"FAILED ({len(FAILS)})"))
|
| 74 |
+
sys.exit(1 if FAILS else 0)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
main()
|