droboticflow-new/tests/backend/structured_model_operator_test.py
2026-07-02 12:54:48 +08:00

182 lines
7.2 KiB
Python

import os
import json
import pathlib
import types
import zipfile
import pytest
import backend.operators.builtin.structured_model_operator as module
from backend.operators.builtin.structured_model_operator import _prepare_transformers_cache
def helpers():
def hyperparams(node_data):
return {str(item["key"]): str(item.get("value", "")) for item in node_data.get("hyperparams", [])}
def write_json_artifact(run_id, node_id, output_dir, name, payload, output=None):
path = output_dir / name
path.write_text(json.dumps(payload), encoding="utf-8")
return {"runId": run_id, "nodeId": node_id, "name": name, "path": str(path), "type": "json"}
def build_artifact(run_id, node_id, path, artifact_type):
return {"runId": run_id, "nodeId": node_id, "name": path.name, "path": str(path), "type": artifact_type}
return {"hyperparams": hyperparams, "write_json_artifact": write_json_artifact, "build_artifact": build_artifact}
def test_prepare_transformers_cache_defaults_to_short_tmp_path(tmp_path):
cache_dir = _prepare_transformers_cache({}, tmp_path)
assert str(cache_dir).startswith("/tmp/droboticflow-hf-cache/")
assert cache_dir.is_dir()
assert os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] == "1"
assert os.environ["HF_HUB_DISABLE_XET"] == "1"
assert os.environ["HF_HOME"] == str(cache_dir)
assert os.environ["HUGGINGFACE_HUB_CACHE"] == str(cache_dir / "hub")
assert os.environ["TRANSFORMERS_CACHE"] == str(cache_dir / "transformers")
def test_prepare_transformers_cache_uses_explicit_hf_cache_dir(tmp_path):
explicit = tmp_path / "custom-cache"
cache_dir = _prepare_transformers_cache({"HF_CACHE_DIR": str(explicit)}, tmp_path)
assert cache_dir == explicit
assert explicit.is_dir()
def test_prepare_transformers_cache_rejects_unwritable_path(tmp_path, monkeypatch):
cache_dir = tmp_path / "cache"
cache_dir.mkdir()
monkeypatch.setattr(os, "access", lambda path, mode: False if path == cache_dir and mode == os.W_OK else True)
with pytest.raises(RuntimeError, match="not writable"):
_prepare_transformers_cache({"HF_CACHE_DIR": str(cache_dir)}, tmp_path)
def test_lerobot_act_training_builds_structured_framework_command(tmp_path, monkeypatch):
commands = []
def fake_run(command, **_kwargs):
commands.append(command)
output_dir = next(item.split("=", 1)[1] for item in command if item.startswith("--output_dir="))
checkpoint = pathlib.Path(output_dir) / "checkpoints" / "policy.safetensors"
checkpoint.parent.mkdir(parents=True, exist_ok=True)
checkpoint.write_bytes(b"checkpoint")
return types.SimpleNamespace(returncode=0, stdout="ok", stderr="")
monkeypatch.setattr(module.subprocess, "run", fake_run)
node_data = {
"hyperparams": [
{"key": "DATASET_REPO_ID", "value": "org/real_lerobot_dataset"},
{"key": "MAX_STEPS", "value": "10"},
{"key": "BATCH_SIZE", "value": "2"},
]
}
artifacts = module.run_lerobot_act_training("run-1", "act", tmp_path, node_data, [], helpers())
assert commands
assert commands[0][0] == "lerobot-train"
assert "--policy.type=act" in commands[0]
assert "--dataset.repo_id=org/real_lerobot_dataset" in commands[0]
assert any(item["name"] == "policy.safetensors" for item in artifacts)
def test_diffusion_policy_training_builds_hydra_command(tmp_path, monkeypatch):
commands = []
repo_root = tmp_path / "diffusion_policy"
repo_root.mkdir()
(repo_root / "train.py").write_text("print('train')", encoding="utf-8")
dataset = tmp_path / "dataset.zarr"
dataset.mkdir()
def fake_run(command, **_kwargs):
commands.append(command)
run_dir = next(item.split("=", 1)[1] for item in command if item.startswith("hydra.run.dir="))
checkpoint = pathlib.Path(run_dir) / "checkpoints" / "latest.ckpt"
checkpoint.parent.mkdir(parents=True, exist_ok=True)
checkpoint.write_bytes(b"checkpoint")
return types.SimpleNamespace(returncode=0, stdout="ok", stderr="")
monkeypatch.setattr(module.subprocess, "run", fake_run)
node_data = {
"hyperparams": [
{"key": "DIFFUSION_POLICY_ROOT", "value": str(repo_root)},
{"key": "DATASET_PATH", "value": str(dataset)},
{"key": "CONFIG_NAME", "value": "image_pusht_diffusion_policy_cnn.yaml"},
]
}
artifacts = module.run_diffusion_policy_training("run-1", "diffusion", tmp_path / "out", node_data, [], helpers())
assert commands
assert str(repo_root / "train.py") in commands[0]
assert f"task.dataset.zarr_path={dataset}" in commands[0]
assert any(item["name"] == "latest.ckpt" for item in artifacts)
def test_openvla_finetune_builds_lora_torchrun_command(tmp_path, monkeypatch):
commands = []
openvla_root = tmp_path / "openvla"
script = openvla_root / "vla-scripts" / "finetune.py"
script.parent.mkdir(parents=True)
script.write_text("print('finetune')", encoding="utf-8")
data_root = tmp_path / "rlds"
data_root.mkdir()
def fake_run(command, **_kwargs):
commands.append(command)
adapter_dir = tmp_path / "out" / "openvla-adapter"
adapter_dir.mkdir(parents=True, exist_ok=True)
(adapter_dir / "adapter_config.json").write_text("{}", encoding="utf-8")
return types.SimpleNamespace(returncode=0, stdout="ok", stderr="")
monkeypatch.setattr(module.subprocess, "run", fake_run)
node_data = {
"hyperparams": [
{"key": "OPENVLA_ROOT", "value": str(openvla_root)},
{"key": "DATA_ROOT_DIR", "value": str(data_root)},
{"key": "DATASET_NAME", "value": "droid_wipe"},
{"key": "MAX_STEPS", "value": "5"},
]
}
artifacts = module.run_openvla_finetune("run-1", "openvla", tmp_path / "out", node_data, [], helpers())
assert commands
assert commands[0][:5] == ["torchrun", "--standalone", "--nnodes", "1", "--nproc-per-node"]
assert str(script) in commands[0]
assert "--dataset_name" in commands[0]
assert "droid_wipe" in commands[0]
assert any(item["name"] == "openvla-adapter.zip" for item in artifacts)
def test_openvla_inference_consumes_upstream_model_zip(tmp_path, monkeypatch):
captured = {}
model_root = tmp_path / "model"
model_root.mkdir()
(model_root / "config.json").write_text("{}", encoding="utf-8")
archive_path = tmp_path / "model.zip"
with zipfile.ZipFile(archive_path, "w") as archive:
archive.write(model_root / "config.json", "openvla-adapter/config.json")
def fake_inference(run_id, node_id, output_dir, node_data, inputs, helper_map):
captured["params"] = helper_map["hyperparams"](node_data)
return [{"name": "inference-result.json"}]
monkeypatch.setattr(module, "run_transformers_text_inference", fake_inference)
artifacts = module.run_openvla_inference(
"run-1",
"infer",
tmp_path / "out",
{"hyperparams": [{"key": "PROMPT", "value": "pick object"}]},
[{"type": "model", "path": str(archive_path), "name": "openvla-adapter.zip"}],
helpers(),
)
assert artifacts == [{"name": "inference-result.json"}]
assert "upstream-model/openvla-adapter" in captured["params"]["MODEL_PATH"]