Download src/psi/models/psi0_rtc_test.py from Tinghong-Ye22/code-HFD: direct link, hf CLI and curl.
- Browser
- Download file 5.46 kB
-
https://huggingface.co/Tinghong-Ye22/code-HFD/resolve/main/src/psi/models/psi0_rtc_test.py
- Command line
-
hf download hf://Tinghong-Ye22/code-HFD/src/psi/models/psi0_rtc_test.py
-
curl -L -o psi0_rtc_test.py https://huggingface.co/Tinghong-Ye22/code-HFD/resolve/main/src/psi/models/psi0_rtc_test.py
5.46 kB
| from __future__ import annotations | |
| import importlib.util | |
| from contextlib import contextmanager | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| from psi.models.psi0 import ( | |
| ActionTransformerModel, | |
| Psi0Model, | |
| _validate_rtc_inference_contract, | |
| ) | |
| def _raises(error_type: type[Exception], message: str): | |
| try: | |
| yield | |
| except error_type as exc: | |
| assert message in str(exc), str(exc) | |
| else: | |
| raise AssertionError(f"Expected {error_type.__name__}: {message}") | |
| def _small_action_model(*, future_progress: bool = False) -> ActionTransformerModel: | |
| torch.manual_seed(7) | |
| model = ActionTransformerModel( | |
| attention_head_dim=4, | |
| num_attention_heads=2, | |
| caption_projection_dim=8, | |
| pooled_projection_dim=8, | |
| action_num_blocks=2, | |
| action_pred_horizon=6, | |
| action_dim=3, | |
| action_hidden_dim=8, | |
| action_nheads=2, | |
| n_conditions=0, | |
| odim=3, | |
| view_feature_dim=8, | |
| use_future_progress_embedding=future_progress, | |
| future_progress_dim=5, | |
| ) | |
| model.eval() | |
| return model | |
| def _forward_full_timeline( | |
| model: ActionTransformerModel, | |
| actions: torch.Tensor, | |
| *, | |
| future_progress: torch.Tensor | None = None, | |
| ) -> torch.Tensor: | |
| batch_size, horizon, _ = actions.shape | |
| return model( | |
| hidden_states=torch.empty(0), | |
| timestep=torch.full((batch_size, horizon), 500.0), | |
| joint_attention_kwargs={ | |
| "action_hidden_embeds": actions, | |
| "known_action_prefix": None, | |
| "views": torch.randn(batch_size, 1, 5, 8), | |
| "obs": torch.randn(batch_size, 1, 3), | |
| "traj2ds": None, | |
| "future_progress": future_progress, | |
| }, | |
| return_dict=True, | |
| ).action | |
| def test_v18_rtc_uses_one_full_timeline_action_forward() -> None: | |
| model = _small_action_model(future_progress=True) | |
| actions = torch.randn(2, 6, 3, requires_grad=True) | |
| output = _forward_full_timeline( | |
| model, | |
| actions, | |
| future_progress=torch.randn(2, 6, 5), | |
| ) | |
| assert output.shape == actions.shape | |
| # Stable initialization starts with an exact zero velocity field. | |
| torch.testing.assert_close(output, torch.zeros_like(output)) | |
| def test_stability_paths_are_zero_initialized_and_qk_normalized() -> None: | |
| model = _small_action_model(future_progress=True) | |
| for block in model.transformer_blocks: | |
| torch.testing.assert_close( | |
| block.norm1_act.linear.weight, | |
| torch.zeros_like(block.norm1_act.linear.weight), | |
| ) | |
| torch.testing.assert_close( | |
| block.norm1_act.linear.bias, | |
| torch.zeros_like(block.norm1_act.linear.bias), | |
| ) | |
| assert block.attn.norm_q is not None | |
| assert block.attn.norm_k is not None | |
| assert block.attn.norm_added_q is not None | |
| assert block.attn.norm_added_k is not None | |
| assert model.future_progress_mlp is not None | |
| torch.testing.assert_close( | |
| model.future_progress_mlp[-1].weight, | |
| torch.zeros_like(model.future_progress_mlp[-1].weight), | |
| ) | |
| torch.testing.assert_close( | |
| model.action_proj_out.linear.weight, | |
| torch.zeros_like(model.action_proj_out.linear.weight), | |
| ) | |
| def test_rtc_inference_delay_must_match_trained_overlap() -> None: | |
| assert _validate_rtc_inference_contract( | |
| action_horizon=30, | |
| action_exec_horizon=20, | |
| inference_delay=10, | |
| max_delay=11, | |
| ) == 10 | |
| with _raises(ValueError, "inference_delay=6, expected=10"): | |
| _validate_rtc_inference_contract( | |
| action_horizon=30, | |
| action_exec_horizon=20, | |
| inference_delay=6, | |
| max_delay=11, | |
| ) | |
| def test_rtc_model_entry_rejects_wrong_overlap_before_inference() -> None: | |
| model = Psi0Model.__new__(Psi0Model) | |
| torch.nn.Module.__init__(model) | |
| model.action_horizon = 30 | |
| model.action_dim = 3 | |
| model.action_exec_horizon = 20 | |
| model.rtc_max_delay = 11 | |
| model.device = torch.device("cpu") | |
| with _raises(ValueError, "inference_delay=6, expected=10"): | |
| model.predict_action_with_training_rtc_flow( | |
| observations=None, | |
| states=torch.zeros(1, 1, 3), | |
| instructions=["test"], | |
| num_inference_steps=1, | |
| traj2ds=None, | |
| prev_actions=torch.zeros(1, 30, 3), | |
| inference_delay=6, | |
| max_delay=11, | |
| ) | |
| def test_old_controller_shifts_exact_unexecuted_tail() -> None: | |
| deploy_path = ( | |
| Path(__file__).resolve().parents[1] | |
| / "deploy" | |
| / "psi_serve_rtc-trainingtimertc.py" | |
| ) | |
| spec = importlib.util.spec_from_file_location("psi_rtc_controller_test_module", deploy_path) | |
| assert spec is not None and spec.loader is not None | |
| module = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(module) | |
| old_chunk = np.arange(30 * 3, dtype=np.float32).reshape(30, 3) | |
| shifted = module._shift_unexecuted_tail_to_prefix(old_chunk, 20) | |
| np.testing.assert_array_equal(shifted[:10], old_chunk[20:]) | |
| np.testing.assert_array_equal(shifted[10:], np.zeros((20, 3), dtype=np.float32)) | |
| if __name__ == "__main__": | |
| tests = [ | |
| value | |
| for name, value in sorted(globals().items()) | |
| if name.startswith("test_") and callable(value) | |
| ] | |
| for test in tests: | |
| test() | |
| print(f"psi0_rtc_test: {len(tests)} passed") | |