# Copyright 2026 The HuggingFace Inc. team. All rights reserved. from __future__ import annotations import os from pathlib import Path import pytest import torch from torch import nn from lerobot.configs.policies import PreTrainedConfig from lerobot.configs.types import FeatureType, PolicyFeature from lerobot.policies.factory import get_policy_class, make_policy_config, make_pre_post_processors from lerobot.policies.g05.configuration_g05 import G05_EMBODIMENT_MAPPINGS, G05Config from lerobot.policies.g05.modeling_g05 import G05Policy from lerobot.processor import PolicyProcessorPipeline from lerobot.utils.constants import ACTION, OBS_STATE, POLICY_PREPROCESSOR_DEFAULT_NAME class TinyG05Backend(nn.Module): def __init__(self): super().__init__() self.proj = nn.Linear(20, 20) self.last_samples = None def predict_action(self, batch): self.last_samples = batch["samples"] state = batch[OBS_STATE] if state.ndim == 2: state = state.unsqueeze(1) step = self.proj(state[:, -1]) return { ACTION: step.unsqueeze(1).expand(-1, 4, -1), "ar_action": (step + 1).unsqueeze(1).expand(-1, 4, -1), "cot_text": ["Subtask: move carefully"] * step.shape[0], } def forward(self, batch): prediction = self.proj(batch[OBS_STATE][:, -1]) target = batch[ACTION][:, 0] loss = torch.nn.functional.mse_loss(prediction, target) return loss, {"fm_loss": loss.detach()} def _features(): return { OBS_STATE: PolicyFeature(type=FeatureType.STATE, shape=(7,)), "observation.images.image": PolicyFeature(type=FeatureType.VISUAL, shape=(3, 8, 8)), "observation.images.wrist_image": PolicyFeature(type=FeatureType.VISUAL, shape=(3, 8, 8)), } def _config(**kwargs): normalization_mode = kwargs.pop("normalization_mode", "identity") return G05Config( checkpoint_profile="custom", normalization_mode=normalization_mode, input_features=_features(), output_features={ACTION: PolicyFeature(type=FeatureType.ACTION, shape=(7,))}, chunk_size=4, n_action_steps=kwargs.pop("n_action_steps", 4), device="cpu", **kwargs, ) def _policy_batch(task: str = " Pick café cup\nverbatim "): return { OBS_STATE: torch.zeros(1, 1, 20), ACTION: torch.zeros(1, 4, 20), "observation.images.image": torch.zeros(1, 3, 8, 8), "observation.images.wrist_image": torch.zeros(1, 3, 8, 8), "task": [task], "proprio_dim_is_pad": torch.zeros(20, dtype=torch.bool), } def test_factory_wiring_is_lazy(): assert make_policy_config("g05", checkpoint_profile="custom").type == "g05" assert get_policy_class("g05") is G05Policy def test_system2_fm_only_builder_uses_exact_cot_template_without_action_tokens(): config = _config( action_head="flow", runtime_system="system2", predict_cot=True, discrete_action=False, continuous_action=True, return_continuous_action=True, processor_metadata={ "samples_builder": { "_target_": ("g05.data_processor.processor.samples_builder.SubtaskCoTBuilderFMOnly") } }, ) assert "\n|Action: " in config.prompt_template assert " 0 and torch.isfinite(grad_norm) optimizer.step() assert metrics is not None and metrics["fm_loss"] >= 0 policy.save_pretrained(tmp_path) reloaded = G05Policy.from_pretrained( tmp_path, backend=TinyG05Backend(), local_files_only=True, strict=True ) expected = policy.predict_action_chunk(_policy_batch("save")) actual = reloaded.predict_action_chunk(_policy_batch("save")) torch.testing.assert_close(actual, expected) def test_save_pretrained_copies_required_gated_sidecars_portably(tmp_path: Path): source = tmp_path / "checkpoint" processor = source / "hf_processor" processor.mkdir(parents=True) (processor / "tokenizer.json").write_text("{}") tokenizer = source / "action_tokenizer.pt" torch.save({"codec": "ActionCodec"}, tokenizer) for name in ("LICENSE-G0.5", "NOTICE"): (source / name).write_text("{}") config = _config( author_model_config={ "hf_processor_path": str(processor), "AT_CONFIG": {"ckpt_dir": str(tokenizer)}, } ) output = tmp_path / "saved" G05Policy(config, backend=TinyG05Backend()).save_pretrained(output) assert (output / "hf_processor" / "tokenizer.json").is_file() assert (output / "action_tokenizer.pt").is_file() assert (output / "LICENSE-G0.5").is_file() loaded_config = PreTrainedConfig.from_pretrained(output) assert isinstance(loaded_config, G05Config) assert loaded_config.author_model_config["hf_processor_path"] == "hf_processor" assert loaded_config.author_model_config["AT_CONFIG"]["ckpt_dir"] == "action_tokenizer.pt" def test_tiny_fixed_batch_overfit_reduces_loss(): policy = G05Policy(_config(), backend=TinyG05Backend()) optimizer = torch.optim.AdamW(policy.get_optim_params()["params"], lr=5e-2) batch = _policy_batch("overfit") initial = policy(batch)[0].item() for _ in range(20): optimizer.zero_grad() loss, _ = policy(batch) loss.backward() optimizer.step() final = policy(batch)[0].item() assert final < initial * 0.25 @pytest.mark.skipif( not os.environ.get("LEROBOT_G05_CHECKPOINT"), reason="requires an accepted gated OpenGalaxea/G05 checkpoint and author CUDA environment", ) def test_gated_checkpoint_loads_strictly(): checkpoint = Path(os.environ["LEROBOT_G05_CHECKPOINT"]) policy = G05Policy.from_pretrained(checkpoint, local_files_only=True, strict=True) assert policy.config.source_checkpoint_revision