File size: 2,745 Bytes
06f9561 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | from __future__ import annotations
import numpy as np
from psi.config.transform import SeedPosttrainRepackTransform
def _sample(mask: np.ndarray) -> dict[str, np.ndarray | str]:
actions = np.arange(4 * 36, dtype=np.float32).reshape(4, 36)
states = (actions + 0.25).astype(np.float32)
return {
"actions": actions,
"states": states,
"action_mask": mask,
"instruction": "synthetic",
}
def test_textop_preserves_frame_mask_from_1d_input() -> None:
transform = SeedPosttrainRepackTransform(textop_actions=True)
output = transform(_sample(np.asarray([1, 1, 0, 0], dtype=np.float32)))
mask = output["actions_mask"]
assert mask.shape == (4, 39)
np.testing.assert_array_equal(mask[:, 0], np.asarray([1, 1, 0, 0], dtype=np.float32))
np.testing.assert_array_equal(mask[0], np.ones(39, dtype=np.float32))
np.testing.assert_array_equal(mask[2], np.zeros(39, dtype=np.float32))
def test_textop_reduces_per_dimension_mask_to_frame_validity() -> None:
mask = np.ones((4, 36), dtype=np.float32)
mask[1, :] = 0.0
mask[3, 0] = 0.0 # at least one original action dimension remains valid
output = SeedPosttrainRepackTransform(textop_actions=True)(_sample(mask))
output_mask = output["actions_mask"]
np.testing.assert_array_equal(output_mask[0], np.ones(39, dtype=np.float32))
np.testing.assert_array_equal(output_mask[1], np.zeros(39, dtype=np.float32))
np.testing.assert_array_equal(output_mask[3], np.ones(39, dtype=np.float32))
def test_textop_rejects_mask_with_wrong_frame_count() -> None:
transform = SeedPosttrainRepackTransform(textop_actions=True)
try:
transform(_sample(np.ones((3, 36), dtype=np.float32)))
except ValueError as exc:
assert "does not match actions" in str(exc)
else:
raise AssertionError("TextOp must reject an action mask with wrong frame count")
def test_textop_repack_builds_two_temporal_context_tokens() -> None:
sample = _sample(np.ones(4, dtype=np.float32))
sample["smoothness_context_actions"] = np.asarray(
sample["actions"][:2],
dtype=np.float32,
)
sample["smoothness_context_reference_states"] = np.asarray(
sample["states"][:1],
dtype=np.float32,
)
output = SeedPosttrainRepackTransform(textop_actions=True)(sample)
assert output["smoothness_context_actions"].shape == (2, 39)
assert np.isfinite(output["smoothness_context_actions"]).all()
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"transform_seed_test: {len(tests)} passed")
|