diff --git a/pi05/test_so101_rtc.py b/pi05/test_so101_rtc.py new file mode 100644 index 0000000..da14b7b --- /dev/null +++ b/pi05/test_so101_rtc.py @@ -0,0 +1,97 @@ +"""pi05/openpi_addon/so101_rtc.py のテスト (依頼 294). openpi・jax は使わない (重み・導きの重み・入口の確かめ・ +RTC の印がなければ包んだ Policy をそのまま呼ぶこと). RTC で答えを作る所は Pod で確かめる (check_294_replay_rtc.py). + + python -m unittest pi05/test_so101_rtc.py -v +""" +import math +import sys +import unittest +from pathlib import Path + +import numpy as np + +sys.path.insert(0, str(Path(__file__).resolve().parent / "openpi_addon")) +import so101_rtc as S # noqa: E402 + + +class TestWeights(unittest.TestCase): + def test_linear_doc_example(self): # 公式の get_prefix_weights の docstring の例 (start=2, end=6, total=10) + np.testing.assert_allclose(S.prefix_weights(2, 6, 10, "linear"), [1, 1, 0.8, 0.6, 0.4, 0.2, 0, 0, 0, 0], atol=1e-6) + + def test_exp_is_paper_eq5(self): # 論文の式 5: c_i = (H-s-i)/(H-s-d+1), W = c (e^c - 1)/(e - 1) + d, end, h = 14, 16, 30 + w = S.prefix_weights(d, end, h) + for i in range(h): + if i < d: + want = 1.0 + elif i < end: + c = (end - i) / (end - d + 1) + want = c * (math.exp(c) - 1) / (math.e - 1) + else: + want = 0.0 + self.assertAlmostEqual(float(w[i]), want, places=6, msg=i) + self.assertEqual(w.dtype, np.float32) + + def test_end_before_start(self): # 前の答えが d より短ければ, あるだけを 1 で固める + np.testing.assert_array_equal(S.prefix_weights(14, 10, 30), [1] * 10 + [0] * 20) + np.testing.assert_array_equal(S.prefix_weights(0, 10, 30)[10:], np.zeros(20)) + + def test_guidance_weight(self): # openpi の t=1 (雑音) は τ=0 で β. 公式の式 (c·inv_r2 を β で切る) + self.assertEqual(S.guidance_weight(1.0), 5.0) + for t in (0.9, 0.5, 0.1): + tau = 1 - t + r2 = (1 - tau) ** 2 / (tau ** 2 + (1 - tau) ** 2) + self.assertAlmostEqual(S.guidance_weight(t), min(5.0, (1 - tau) / (tau * r2))) + self.assertEqual(S.BETA, 5.0) + self.assertEqual(S.SCHEDULE, "exp") + + +class TestInputs(unittest.TestCase): + def test_check(self): + prev, d = S.check_rtc_inputs(np.zeros((16, 6)), np.int64(14), 30) + self.assertEqual((prev.shape, prev.dtype, d), ((16, 6), np.float32, 14)) + self.assertEqual(S.check_rtc_inputs(np.zeros((3, 6), np.float64), 3, 30)[1], 3) # python の int も受ける + for bad_prev, bad_d in ((np.zeros((0, 6)), 1), (np.zeros((31, 6)), 1), (np.zeros(6), 1), + (np.full((3, 6), np.nan), 1), (np.zeros((3, 6)), -1), (np.zeros((3, 6)), 31), + (np.zeros((3, 5)), 1), (np.zeros((3, 7)), 1), # codex R1 C3: 関節 6 つでない + (None, 1), (np.zeros((3, 6)), None), # どちらかが欠けている + (np.zeros((3, 6)), 2.5), (np.zeros((3, 6)), 2.0), # d が整数でない + (np.zeros((3, 6)), True), (np.zeros((3, 6)), "3"), (np.zeros((3, 6)), [3]), + (np.array([["a"] * 6] * 3), 1)): + with self.assertRaises(ValueError): + S.check_rtc_inputs(bad_prev, bad_d, 30) + + def test_extend(self): + p = np.arange(18, dtype=np.float32).reshape(3, 6) + e = S.extend_prefix(p, 5) + np.testing.assert_array_equal(e[:3], p) + np.testing.assert_array_equal(e[3:], [p[-1], p[-1]]) + np.testing.assert_array_equal(S.extend_prefix(p, 3), p) + + +class TestPassthrough(unittest.TestCase): + def test_no_rtc_calls_wrapped_policy(self): + class P: + _sample_kwargs = {} + metadata = {"m": 1} + + def __init__(self): + self.calls = [] + + def infer(self, obs, **kw): + self.calls.append((obs, kw)) + return {"actions": "same"} + p = P() + r = S.RtcPolicy(p) + obs = {"observation/state": np.zeros(6)} + out = r.infer(obs) + self.assertEqual(out, {"actions": "same"}) + self.assertIs(p.calls[0][0], obs) # 印がなければ同じ観測をそのまま渡す + self.assertEqual(r.metadata, {"m": 1}) + self.assertEqual(r._num_steps, 10) # openpi の既定と同じ回数 + self.assertFalse(S.has_rtc(obs)) + self.assertTrue(S.has_rtc({"rtc/delay": 3})) + + +if __name__ == "__main__": + unittest.main()